hi everyone,i dont understand the behaviour of my generate function using a nested loop that performs my insert function repeatedly.Inst ead of inserting the value of i,it seems to insert the maximum value of j
repeatedlyi.e 7 where j<=7 for example.I think the problem might be with my insert function,howeve r i see no problems.Can you help? -
#include <iostream>
-
#include <iostream>
-
#include <string>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <time.h>
-
-
using namespace std;
-
-
-
class SparseMat{
-
public:
-
struct link{
-
int num;
-
link *next;
-
int posx;
-
int posy;
-
};
-
SparseMat(int sizex1,int sizey1);
-
SparseMat();
-
SparseMat(const SparseMat &m);
-
link *first;
-
int sizex;
-
int sizey;
-
int state;
-
int exists(int x,int y);
-
void insert(int n,int posx,int posy);
-
int numAt(int posx,int posy);
-
SparseMat operator +(SparseMat b);
-
SparseMat operator -(SparseMat b);
-
SparseMat operator *(SparseMat b);
-
SparseMat& operator =(const SparseMat &m);
-
};
-
SparseMat::SparseMat(int sizex1,int sizey1){
-
state=0;
-
sizex=sizex1;
-
sizey=sizey1;
-
}
-
SparseMat::SparseMat(){
-
state=0;
-
}
-
void SparseMat::insert(int n,int posx,int posy){
-
link *current;
-
if(state==0){
-
state=1;
-
first=new link();
-
first->num=n;
-
first->next=NULL;
-
first->posx=posx;
-
first->posy=posy;
-
}
-
else{
-
current=new link();
-
current->num=n;
-
current->posx=posx;
-
current->posy=posy;
-
current->next=first;
-
first=current;
-
}
-
}
-
int SparseMat::numAt(int posx,int posy){
-
link *current=first;
-
for(int i=0;current->posx!=posx&¤t->posy!=posy;i++){
-
current= current->next;
-
if(current==NULL){
-
break;
-
}
-
}
-
return current->num;
-
}
-
int SparseMat::exists(int x,int y){
-
link *current=first;
-
int state=0;
-
for(int i=0;current!=NULL;i++){
-
if(current->posx==x&¤t->posy==y){
-
state=1;
-
break;
-
}
-
current=current->next;
-
}
-
return state;
-
}
-
SparseMat SparseMat::operator +(SparseMat b){
-
int n;
-
SparseMat res(sizex,sizey);
-
if(sizex==b.sizex&&sizey==b.sizey){
-
for(int i=0;i<=sizex;i++){
-
for(int j=0;j<=sizey;j++){
-
if(exists(i,j)==1&&b.exists(i,j)==1){
-
n=numAt(i,j)+b.numAt(i,j);
-
res.insert(n,i,j);
-
}
-
if(exists(i,j)==1&&b.exists(i,j)!=1){
-
n=numAt(i,j)+0;
-
res.insert(n,i,j);
-
}
-
if(exists(i,j)!=1&&b.exists(i,j)==1){
-
n=0+b.numAt(i,j);
-
res.insert(n,i,j);
-
}
-
}
-
}
-
}
-
else{
-
cout<<"error";
-
}
-
return res;
-
}
-
SparseMat SparseMat::operator -(SparseMat b){
-
int n;
-
SparseMat res(sizex,sizey);
-
if(sizex==b.sizex&&sizey==b.sizey){
-
for(int i=0;i<=sizex;i++){
-
for(int j=0;j<=sizey;j++){
-
if(exists(i,j)==1&&b.exists(i,j)==1){
-
n=numAt(i,j)-b.numAt(i,j);
-
res.insert(n,i,j);
-
}
-
if(exists(i,j)==1&&b.exists(i,j)!=1){
-
n=numAt(i,j)-0;
-
res.insert(n,i,j);
-
}
-
if(exists(i,j)!=1&&b.exists(i,j)==1){
-
n=0-b.numAt(i,j);
-
res.insert(n,i,j);
-
}
-
}
-
}
-
}
-
else{
-
cout<<"error";
-
}
-
return res;
-
}
-
SparseMat SparseMat::operator *(SparseMat b){
-
SparseMat res(sizex,sizey);
-
int n=0;
-
if(sizex==b.sizex&&sizey==b.sizey){
-
int rarr[sizex];
-
int carr[sizey];
-
for(int i=0;i<=sizey;i++){
-
for(int j=0;j<=sizex;j++){
-
if(exists(j,i)==1){
-
rarr[j]=numAt(j,i);
-
}
-
else{
-
rarr[j]=0;
-
}
-
}
-
for(int l=0;l<=sizex;l++){
-
for(int k=0;k<=sizey;k++){
-
if(b.exists(l,k)==1){
-
carr[k]=b.numAt(l,k);
-
}
-
else{
-
carr[k]=0;
-
}
-
}
-
for(int m=0;m<=sizex;m++){
-
n= rarr[m]*carr[m]+n;
-
}
-
res.insert(n,l,i);
-
}
-
}
-
}
-
else{
-
cout<<"error";
-
}
-
return res;
-
}
-
SparseMat::SparseMat(const SparseMat &m){
-
first=m.first;
-
sizex=m.sizex;
-
sizey=m.sizey;
-
state=m.state;
-
}
-
SparseMat& SparseMat::operator =(const SparseMat &m){
-
first=m.first;
-
sizex=m.sizex;
-
sizey=m.sizey;
-
state=m.state;
-
return *this;
-
}
-
-
-
class LinearEqns:public SparseMat{
-
public:
-
SparseMat xh;
-
LinearEqns(SparseMat x):SparseMat(){
-
xh=x;
-
}
-
void generate(int coef);
-
void solve();
-
};
-
void LinearEqns::generate(int coef){
-
-
this->SparseMat::sizex=coef+1;
-
this->SparseMat::sizey=xh.sizey;
-
-
for(int j=0;j<=7;j++){
-
-
for(int i=0;i<=7;i++){
-
this->SparseMat::insert(j,i,j);
-
-
}
-
}
-
}
-
int main(){
-
int x;
-
SparseMat h(2,3);
-
h.insert(1,0,0);
-
h.insert(2,0,1);
-
LinearEqns l(h);
-
l.generate(5);
-
cout<<l.numAt(1,2);
-
cin>>x;
-
}
-
1 1587 JosAH 11,448
Recognized Expert MVP
The indentation of your code stinks; I added those code tags for you but it doesn't help much; your code is unreadable. Please fix the indentation first before you want us to plough through all that code.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Chris Mantoulidis |
last post by:
I see some really weird output from this program (compiled with GCC
3.3.2 under Linux).
#include <iostream>
using namespace std;
int main() {
char *s;
s = "test1";
cout << "s = " << s << " and &s = " << &s << "\n";
|
by: s_m_b |
last post by:
Anyone ever seen this before?
A page that generates a calendar view, works just fine without 'option
explicit' .
Add this in, but have missed some dim statements out in error, so page
should error .
Instead of error, the un-dim-ed variables are ignored and their portions of
the code simply are not used - behavour as though entire loops etc are
commented out.
As soon as I add their dim statements, the code executes fine.
|
by: Deano |
last post by:
Here's the code;
Private Sub txtTeachName_LostFocus()
If IsNull(Me.txtName) Then 'line A
Forms!frmMainform!frmSubform.Locked = True
GoTo Exit_txtName
Else 'line B
Forms!frmMainform!frmSubform.Locked = False 'line C
|
by: Jeff |
last post by:
Hello everybody,
I was doing one of the exercises in the K&R book, and I got something
really strange. Here's the source code:
/*
* Exercise 2-2 from the K&R book, page 42
*/
#include <stdio.h>
|
by: Pankaj |
last post by:
Hi All,
I use a Hashtable in my program to keep unique items...at one instance I
need to repopulate this hashtable through a loop.
when starting repopulation hashtable.count() returns ZERO hence first item
gets added successfully but as soon as the control reaches top of foreach
loop
somehow a second item which indeed existed before repoulation started
appears in hashtable and count returns two hence this second item can not
add...
| |
by: Daniel Yelland |
last post by:
Hi,
I have developed a number of code libraries in Win32 DLLs and have written a
number of test suite executables that implicitly link to these libraries in
order to test them. In one of my test applications, which runs fine in Debug
mode, it is crashing in the destructor of a local object on the stack when
it is built in release mode.
An example of the C++ that causes the problem is as follows (apologies for
the contrived example): -
|
by: Charles Law |
last post by:
For some reason, when I click the X to close my MDI parent form, the action
appears to be re-directed to one of the MDI child forms, and the parent
remains open. I am then unable to close the application.
What should happen, is that the main MDI form should close, taking the child
forms with it. There is code to loop through the child forms, remove the
controls on each of them, and then close the form, but this code should
execute only...
|
by: Mick_fae_Glesga |
last post by:
OK, the solution to this is probably blindingly obvious to everyone,
but... surely it can't be right.
I am compiling with borland bcc32 free compiler
this piece of code is designed to identify the most significant bit in
a given element in an array of unsigned longs. Now I realise there may
be a more efficient way to do this, and if you know a better way please
let me know.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |