hi everyone,i dont understand the behaviour of my generate function using a nested loop that performs my insert function repeatedly.Instead 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,however 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 1557
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 << "...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |