Shortly: I have two linear linked lists which represent a polynomial. I have to multiply them. I've wrote everything down here. The only problem is that I get a segmentation fault on one line (if(n.grad<r->a.grad) - also marked in the code below).
I've tried this program in Borland and it works!
In CodeBlocks or MinGW it simply crashes.
#include <iostream>
using namespace std;
struct poli
{
int grad;
float coe;
};
struct Nod
{
poli a;
Nod *adr;
};
Nod *v,*sf,*v1,*vs,*vp;
void add_first(Nod *&v, poli n)
{
if(v)
{
Nod *p;
p=new Nod;
p->a=n;
p->adr=v;
v=p;
}
else
{
v=new Nod;
sf=v;
v->a=n;
v->adr=0;
}
}
void add_last(Nod *&v, poli n)
{
if(!v)
{
v=new Nod;
v->a=n;
v->adr=0;
}
else
{
Nod *p,*sf;
sf=v;
while(sf->adr)
sf=sf->adr;
p=new Nod;
p->a=n;
p->adr=0;
sf->adr=p;
sf=p;
}
}
void add_before(Nod *v, int val, poli val_add)
{
Nod *p, *q;
if(v->a.grad==val)
{
p=new Nod;
p->a=val_add;
p->adr=v;
v=p;
}
else
{
p=v;
while(p->adr->a.grad!=val&&p->adr->adr)
p=p->adr;
if(p->adr->a.grad==val)
{
q=new Nod;
q->a=val_add;
q->adr=p->adr;
p->adr=q;
}
}
}
void produs(Nod *v, Nod *v1, Nod *&vp)
{
Nod *p,*q,*r;
int gasit;
poli n;
p=v;
while(p)
{
q=v1;
while(q)
{
n.grad=p->a.grad+q->a.grad;
n.coe=p->a.coe*q->a.coe;
r=vp;
gasit=0;
while(r)
{
if(n.grad==r->a.grad)
{
r->a.coe+=n.coe;
gasit=1;
}
r=r->adr;
}
if(!gasit)
{
r=vp;
if(n.grad<r->a.grad) /////////////// HERE I get the call stack
add_first(vp,n);
else
{
while(r->adr&&n.grad>r->adr->a.grad)
r=r->adr;
if(r->adr&&n.grad<r->adr->a.grad)
add_before(vp,r->adr->a.grad,n);
else
add_last(vp,n);
}
}
q=q->adr;
}
p=p->adr;
}
}
void tipar(Nod *v)
{
Nod *p;
p=v;
while(p)
{
cout<<"+"<<p->a.coe<<"x^"<<p->a.grad;
p=p->adr;
}
}
int main()
{
int n,m,i;
poli a;
cout<<"Cate elemente are polinomu' 1?";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Baga gradu'";
cin>>a.grad;
cout<<"Introdu-mi coe";
cin>>a.coe;
add_last(v,a);
add_last(vs,a);
}
cout<<"Cate elemente are polinomu' 2?";
cin>>m;
for(i=0;i<m;i++)
{
cout<<"Baga gradu'";
cin>>a.grad;
cout<<"Introdu-mi coe";
cin>>a.coe;
add_last(v1,a);
}
produs(v,v1,vp);
tipar(vp);
return 0;
}
Here is the call stack window contents:
#0 004016C4 produs (v=0x4d25a0, v1=0x4d26b8, vp=#0x474018) at F:\Programe\ma ballz(23.02) (F:\Programe\suma polinom\main.cpp:142)
#1 004019F2 main () at F:\Programe\ma ballz(23.02) (F:\Programe\suma polinom\main.cpp:195)
looks like vp is NULL, or garbage, and then you do r = vp...
Probably NULL because it didn't fall in the loop.
It has a garbage value because you never initialize it with a value. You just declare it in the beginning.
Anyway you should learn to give meaningful names to your variables so it will be more readable and therefor maintainable.
Related
I was writing this task https://codeforces.com/contest/459/problem/D on codeforces and my solution uses trees, but after running update function every element goes back to 0.
#include<bits/stdc++.h>
using namespace std;
int n,k,a[1000009],ans1[1000009],ans2[1000009],fans,w;
map<int,int> pref;
struct Node{
Node *L,*R;
int Sum;
Node()
{
Sum=0;
L=NULL;
R=NULL;
}
}*root = new Node();
void update(Node *it,int l,int r,int q)
{
if(it==NULL) it = new Node();
if(q<l || q>r) return;
it->Sum ++;
cout<<l<<" "<<r<<" "<<q<<" "<<it->Sum<<endl;
if(l==r) return;
update(it->L,l,(l+r)/2,q);
update(it->R,(l+r)/2+1,r,q);
}
void findans(Node *it,int l,int r,int L,int R)
{
if(it==NULL)
{
it = new Node();
cout<<"1 ";
}
cout<<l<<" "<<r<<" "<<L<<" "<<R<<" "<<it->Sum<<endl;
if(l>=L && r<=R) w+=it->Sum;
if(l!=r) if(l<=R && r>=L)
{
findans(it->L,l,(l+r)/2,L,R);
findans(it->R,(l+r)/2+1,r,L,R);
}
}
int main()
{
cin>>n;
for(k=1;k<=n;k++)
{
cin>>a[k];
pref[a[k]]++;
ans1[k]=pref[a[k]];
}
pref.clear();
for(k=n;k>=1;k--)
{
pref[a[k]]++;
ans2[k]=pref[a[k]];
}
for(k=n;k>=1;k--)
{
w=0;
findans(root,1,n,1,ans1[k]-1);
fans+=w;
update(root,1,n,ans2[k]);
cout<<endl;
}
cout<<fans;
}
this is the code(I have extra couts so I could find out where was the problem). also root gets updated normally but every other nodes -> sum goes back to 0 ( sorry for my bad english )
if(it==NULL) it = new Node();
it itself is a local variable, so any changes to it will only be scoped to the function. In other words, calling findans(x, ...) will leave x in the same state it was prior to calling the function.
To fix this, change findans() to accept a reference to a pointer:
void update(Node *&it,int l,int r,int q)
{
...
}
Here is my code for quick sort. I am a beginner kindly please help.
#include<iostream>
using namespace std;
class quick
{
private:
int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(a[],left,right);
obj.putdata();
return (0);
}
It is giving me error in int main() function:
a is not declared in this scope.
expected primary expression before ']'.
As the answer is given by #Shubham Khatri. Here is the corrected code.
#include<iostream>
using namespace std;
class quick
{
public: int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<n;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}
while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
As it mentions you have not declared a as a variable inside the int main(). Rather it is an object of quick. In a function you don't pass array like a[] rather as a only .since a, left, right are a private variable of a class you can't access it from the object directly. Declare it as public and use it as obj.a, obj.left, obj.right inside sort function.
Complete code:
#include<iostream>
using namespace std;
class quick
{
public:
int n,left,right,i,j;
float a[55];
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
#include <iostream>
#include <string.h>
using namespace std;
#define NMAX 10 // pre-processing directive
template<typename T>
class Stack {
public:
T Smain, Saux;
T stackArray[NMAX];
int topLevel;
int getTopLevel()
{
return this->topLevel;
}
void push(T x)
{
if (topLevel >= NMAX - 1)
{
cout<<"The stack is full: we have already NMAX elements!\n";
return;
}
stackArray[++topLevel] = x;
}
int isEmpty()
{
return (topLevel < 0);
}
T pop()
{
if (isEmpty()) {
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel--];
}
T peek()
{
if (isEmpty())
{
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel];
}
void afficher() {
cout<<"On affiche la pile:";
for ( int i=0; i<=topLevel; i++ )
cout<<stackArray[i]<<" ";
cout<<endl;
}
Stack()
{
topLevel = -1;
}
~Stack() {
}
void change(int i)
{
while (Smain.topLevel>i){
Saux.push(Smain.pop());}
T aux1=Smain.pop();
T aux2=Smain.pop();
Smain.push(aux1);
Smain.push(aux2);
while (Saux.topLevel>=0){
Smain.push(Saux.pop());}
}
};
int main()
{
Stack<int> stack1;
Stack<int> stack2;
stack1.push(1);
stack1.push(2);
stack1.push(3);
stack1.push(4);
change(3);
stack1.afficher();
return 0;
}
This program has to swap the i and i-1 position of a stack, but i get the error: 'change' was not declared in this scope and i don't know how to make it work. please help me and thanks to you in advance.
change is declared inside the class Stack, so it becomes a method on a Stack instance.
You probably want to call it as stack1.change(3) instead.
You cannot call change() function which is inside a class directly.instead use an class object to call it.
class stack{
}objectname;
int main(){
objectname.change(3);
}
m working on ns2...did some changes in aodv.cc and added some functions of my own
void nb_traffic_update(int id,struct nb_traffic_stat **nblist,int nid,int flag,int hop_count)
..to detect sinkhole attack..when m running the code with small no of nodes i get results but when i increase the number of nodes i get segmentation fault.
This is my nb_traffic.h file
struct nb_traffic_stat
{
int id;
int recvrequest;
int routereply;
int no_of_hops;
//int no_of_updation;
struct nb_traffic_stat *next;
};
struct traffic_stat
{
int id;
struct nb_traffic_stat **list;
struct traffic_stat *next;
};
struct ftraffic_stat
{
int sendrequest;
int routereply;
};
modification to aodv.cc
struct traffic_stat *tlist=NULL,*ttail=NULL;
void
AODV::recvReply(Packet *p) {
...
if (ih->daddr() == index) { // If I am the original source
.....
nb_traffic_update(index,&nblist,ih->saddr(),1,rp->rp_hop_count);//1 is for receiving the route reply
}
}
void
AODV::recvRequest(Packet *p) {
....
/*after ensuring this is the new routerequest*/
struct hdr_cmn *ch = HDR_CMN(p);
if(ch->num_forwards()==1)
{
nb_traffic_update(index,&nblist,rq->rq_src,0,0);//0 is for receiving the request
}
}
my neighbour traffic update function
void nb_traffic_update(int id,struct nb_traffic_stat **nblist,int nid,int flag,int hop_count)
{
int n;
//printf("inside nb_traffic_update:%d\n",id);
if(*nblist==NULL)
{
struct nb_traffic_stat *ptr;
ptr=(struct nb_traffic_stat*)malloc(sizeof(struct nb_traffic_stat));
ptr->id=nid;
ptr->next=NULL;
if(flag==0)
{
ptr->recvrequest=1;
ptr->routereply=0;
ptr->no_of_hops=0;
//ptr->no_of_updation=0;
}
else
{
ptr->recvrequest=0;
ptr->routereply=1;
ptr->no_of_hops=hop_count;
//ptr->no_of_updation=1;
}
*nblist=ptr;
struct traffic_stat *sptr;
sptr=tlist;
while(sptr!=NULL&&sptr->id!=id)
{
sptr=sptr->next;
}
assert(sptr!=NULL);
sptr->list=nblist;
}
else
{
int found=0;
struct nb_traffic_stat *tptr,*prevtptr;
tptr=*nblist;
while(tptr!=NULL&&tptr->id<=nid)
{
if(tptr->id==nid)
{
found=1;
break;
}
prevtptr=tptr;
tptr=tptr->next;
}
if(found)
{
if(flag==0)
{
tptr->recvrequest++;
}
else
{
tptr->routereply++;
tptr->no_of_hops=hop_count;
//tptr->no_of_updation++;
}
}
else
{
struct nb_traffic_stat *ptr;
ptr=(struct nb_traffic_stat*)malloc(sizeof(struct nb_traffic_stat));
ptr->id=nid;
if(flag==0)
{
ptr->recvrequest=1;
ptr->routereply=0;
ptr->no_of_hops=0;
//ptr->no_of_updation=0;
}
else
{
ptr->recvrequest=0;
ptr->routereply=1;
ptr->no_of_hops=hop_count;
//ptr->no_of_updation=1;
}
ptr->next=prevtptr->next;
prevtptr->next=ptr;
}
}
}
You are not checking for nblist to be NULL inside the nb_traffic_update(int, nb_traffic_stat**, int, int, int) function, which leads to the segfault.
Also inside the conditional statement if (*nblist==NULL) you are doing: *nblist=ptr;. Which means *NULL = ptr; which may cause the segfault.
Run your tcl with gdb it will show the function which cause the segment fault...
for ex, gdb -args ns path/to/tcl/script.tcl
HI,
I got this Error in my code i can't understand how to pass a command line argument while the exe of my programe is not created how i write the name of that .exe file.
C:\Program Files\Microsoft Visual Studio\MyProjects\filehandling\file.cpp(205) : error C2451: conditional expression of type 'class std::basic_fstream >' is illegal
Ambiguous user-defined-conversion
#include "iostream"
#include "cstdlib"
#include "cstdio"
#include "ctime"
#include "fstream"
#include "istream"
using namespace std;
class shapes
{
public:
virtual void draw()=0;
virtual void save(fstream &out)=0;
virtual void open(fstream &in)=0;
};
class myline : public shapes
{
private:
int sx,sy,ex,ey,color;
public:
myline()
{}
myline(int x1, int y1, int x2, int y2, int clr)
{
sx=x1;
sy=y1;
ex=x2;
ey=y2;
color=clr;
}
void draw()
{
cout<<"Line-draw()"<<endl;
}
void save(fstream &out)
{
out<<"L"<<"\n";
out<<sx<<""<<sy<<""<<ex<<""<<ey<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>ex>>ey>>color;
}
};
class myrectangle: public shapes
{
private:
int sx,sy,ex,ey,color;
public:
myrectangle()
{}
myrectangle(int x1, int y1,int x2, int y2,int clr)
{
sx=x1;
sy=y1;
ex=x2;
ey=y2;
color=clr;
}
void draw()
{
cout<<"Rectangle-draw()"<<endl;
}
void save(fstream &out)
{
out<<"R"<<"\n";
out<<sx<<""<<sy<<""<<ex<<""<<ey<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>ex>>ey>>color;
}
};
class mycircle: public shapes
{
private:
int sx, sy, radius, color;
public:
mycircle()
{
}
mycircle(int x1, int y1, int r, int clr)
{
sx=x1;
sy=y1;
radius=r;
color=clr;
}
void draw()
{
cout<<"Circle-draw()"<<endl;
}
void save(fstream &out)
{
out<<"C"<<"\n";
out<<sx<<""<<sy<<""<<radius<<""<<color<<"\n";
}
void open(fstream &in)
{
in>>sx>>sy>>radius>>color;
}
};
struct node
{
void*obj;
node*link;
};
class objarray
{
private:
node*head;
public:
objarray()
{
head= NULL;
}
void add(void*o)
{
node*temp = new node;
temp->obj=o;
temp->link=NULL;
if(head==NULL)
head=temp;
else
{
node*q;
q=head;
while(q->link != NULL)
q=q->link;
q->link=temp;
}
}
void*getobj(int i)
{
node*q;
q=head;
int n;
for (n=1; n<i; n++)
{
q=q->link;
}
return(q->link);
}
int getcount()
{
int n=0;
node*q;
q=head;
while(q != NULL)
{
q=q->link;
n++;
}
return n;
}
~objarray()
{
node *q;
q=head;
while(q != NULL)
{
head = head->link;
delete q;
q=head;
}
}
};
int main(int argc ,char*argv[])
{
fstream file;
char choice;
int clmum,sx,sy,ex,ey,rad;
shapes*ptr;
objarray arr;
char a[2];
int i;
if(argc==2)
file.open(argv[1], ios::in|ios::out);
while(file)
{
file>>a;
if(strcmp(a,"L")==0)
{
myline*l = new myline();
l->open(file);
arr.add(l);
}
if(strcmp(a,"R")==0)
{
myrectangle *a=new myrectangle();
a->open(file);
arr.add(a);
}
if(strcmp(a,"C")==0)
{
mycircle*c=new mycircle();
c->open(file);
arr.add(c);
}
}
int count = arr.getcount();
for(i=1; i<=count; i++)
{
ptr=(shapes*)arr.getobj(i);
ptr->draw();
}
srand((unsigned ) time(NULL));
while(1)
{
cout<<endl<<"1.Line 2. Rectanle 3.Circle 4.Exit"<<endl;
cout<<"Your Choice:";
fflush(stdin);
cin.get(choice);;
clmum=rand()%16;
sx=rand()%638;
sy=rand()%478;
ex=rand()%638;
ey=rand()%478;
rad=rand()%200;
myline*l;
myrectangle*a;
mycircle*c;
switch(choice)
{
case '1':
l = new myline(sx, sy, ex,ey,clmum);
if(l=NULL)
exit(1);
arr.add(l);
cout<<"Following Line added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"ex ="<<ex<<"ey ="<<ey<<"color ="<<clmum<<endl;
break;
case '2':
a = new myrectangle(sx,sy,ex,ey,clmum);
if(a==NULL)
exit(1);
arr.add(a);
cout<<"Following Rectangle added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"ex ="<<ex<<"ey ="<<ey<<"color ="<<clmum<<endl;
break;
case '3':
c=new mycircle(sx,sy,rad,clmum);
if(c==NULL);
exit(1);
arr.add(c);
cout<<"Following Circle added to array"<<endl;
cout<<"sx="<<sx<<"sy="<<sy<<"rad ="<<rad<<"color"<<clmum<<endl;
break;
case '4':
if(argc==1)
{
cout<<"Enter File name:";
char name[67];
cin>>name;
file.open(name,ios::out);
}
count=arr.getcount();
file.seekp(0L,ios::beg);
file.clear();
for(i=1; i<=count;i++)
{
ptr=(shapes*) arr.getobj(i);
ptr->save(file);
}
file.close();
cout<<"Array save to file......exiting"<<endl;
exit(1);
}
}
return 0;
}
Here's your problem area (at least the area of the problem you've identified):
while(file)
{
file>>a;
What you're getting should be a warning, not an error -- there's one conversion that should be used here. Even though what it's told you is technically wrong, it's still done you a favor by identifying buggy code. The problem is that you're testing whether the read succeeded before you actually do the read. Therefore, when/if the read fails, you'll execute another iteration of your loop before the loop exits.
You want to combine the reading and testing so you'll detect a failed read immediately after it happens. You can do this by replacing both of the lines above with: while (file >> a) {
Your problem is the line 205, while (file). It should be while (!file.eof())
As pointed out by CariElf, your file won't turn to NULL or 0 or something like that during that while loop. On the other hand, the file's "end of file" marker will be true at some point.
About your... well, other question I guess ("i can't understand how to pass a command line argument while the exe of my programe is not created"):
You can tell VS you want to pass some command line arguments by changing "Command Arguments" in Project->Properties, on the Configuration Properties->Debugging tab.