elements revert to 0 after running functions - c++

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)
{
...
}

Related

in c++ expected primary expression before ']'

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);
}

collatz conjecture to print the number of objects and the sequence using class

#include<iostream>
using namespace std;
class ulam
{
int num;
double prod;
int cot;
public:
ulam(){cot=0;}
ulam(int x)
{
num=x;
}
void process()
{
for(int i=0;num==1;i++)
{
cout<<num<<endl;
if((num%2) == 0)
{
prod=num/2;
}
else
{
prod=(3*num)+1;
}
num=prod;
cot++;
}
}
void display()
{
cout<<"the number of steps required is: "<<cot;
}
};
int main()
{
int n;
cout<<"enter the number"<<endl;
cin>>n;
ulam obj(n);
obj.process();
obj.display();
}
when i write this code the cot value comes as a garbage value i think. i cant figure out where i went wrong. i used class because i feel it is more discriptive . but the main aim behind this program is to find the number of steps it is required for a number to reach one and to print the whole sequence of numbers. for thos who dont know about the collatz conjecture https://en.wikipedia.org/wiki/Collatz_conjecture
Your condition of the for loop inside process function is wrong. it should be num!=1. You need to initialize cot too. You don't need prod actually.
#include<iostream>
using namespace std;
class ulam
{
int num;
int cot;
public:
ulam()
{
cot=0;
}
ulam(int x)
{
cot=0;
num=x;
}
void process()
{
for(int i=0;num!=1;i++)
{
cout<<num<<endl;
if((num%2) == 0)
{
num=num/2;
}
else
{
num=(3*num)+1;
}
cot++;
}
}
void display()
{
cout<<"the number of steps required is: "<<cot;
}
};
int main()
{
int n;
cout<<"enter the number"<<endl;
cin>>n;
ulam obj(n);
obj.process();
obj.display();
return 0;
}
First Problem
In the constructor where you initialize when an integer is passed, you ALSO have to initialize cot like this:
ulam(int x)
{
cot = 0;
num = x;
}
Better yet, since cot is always going to be 0 at construction, just set cot to 0 as a private variable like this:
class ulam
{
int num;
double prod;
int cot = 0;
public:
//....
};
This means that you could still have a default constructor that will do nothing, and the one that takes an integer won't require cot to be set to 0.
Second Problem
Your second problem is that the loop condition is wrong. It should be num != 1, not num == 1. num == 1 would be the loop would never run unless 1 was passed in cin.

Swap 2 values of a stack does not work

#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);
}

bad alloc in QuickSort

I'm doing QuickSort but i'm getting bad_alloc() error.Sometimes code run perfectly but some time i'm getting error.The program run perfectly and do sort right but only when it's run but s out of 4 times it's give me bad _alloc error.
so what's the problem???????
#include <iostream>
using namespace std;
void quicksort(int *a,int,int);
int main()
{
int i,j,*a;
a = new int[j];
cout<<"Enter the total element:";
cin>>j;
for(i=0;i<j;i++){
cout<<"Enter element:";cin>>a[i];
}
quicksort(a,0,j-1);
cout<<"After sorting."<<endl;
for(i=0;i<j;i++){
cout<<a[i]<<endl;
}
return 0;
}
void quicksort(int *a,int u,int d){
int key = a[u];
int upper = u;
int lower = d;
while(key>a[u] && u<lower){
u++;
}
while(key<a[d] && d>upper){
d--;
}
if(u<d){
swap(a[u],a[d]);
quicksort(a,upper,lower);
}
if(u>=d){
swap(key,a[d]);
if(upper!=d)
{
quicksort(a,upper,d-1);
}
if(d!=lower)
{
quicksort(a,d+1,lower);
}
}
}
Move the statement
a = new int[j];
after
cin>>j;
You are allocating with "junk" as j takes the value whatever in the local stack memory location have!

Segmentation fault in this program

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.