no output,I have make a program to find all armstrong no.between 100 to 1000 - c++

#include<iostream>
using namespace std;
int main() {
int a,b,c,d;
c=0;
for(a=100;a<1000;a++) {
for(b=a;b>0;b=b/10) {
d=b%10;
c=c+d*d*d;
}
if(c==a) {
cout<<c<<endl;
}
}
return 0;
}

You have to initialize c before each check.
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
c=0;
for(a=100;a<1000;a++)
{
c=0; // add this
for(b=a;b>0;b=b/10)
{
d=b%10;
c=c+d*d*d;
}
if(c==a)
{
cout<<c<<endl;
}
}
return 0;
}

Related

how can i count digits of a number using recursion?

yes i know other ways to count digits and returning to main function from the recursion function, but i'd like to print it in the void function. im having difficulty with it. could somebody help?
#include <iostream>
using namespace std;
void recursive_function(int num)
{
int sum=0;
while(num!=0){
recursive_function(num/10);
sum++;
}
cout<<sum<<endl;
}
int main()
{
recursive_function(345289467);
return 0;
}
If you do not want to use the return-stack to count your digits, you will need to pass the current count throughout the call stack as function parameter:
void recursive_function(int num, int count=1)
{
if (num>=10) {
recursive_function(num/10, count+1);
} else {
cout<<count<<endl;
}
}
your recursive function should return integer.
#include <iostream>
using namespace std;
int recursive_function(int num)
{
if(num>9){
return 1+recursive_function(num/10);
}else
return 1;
}
int main()
{
cout << recursive_function(123456789);
return 0;
}

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

how to efficiently appraoch for SPOJ Square free numbers?

I tried my best to solve Spoj problem No Squares numbers but I'm getting (TLE). Please tell how to approach. I'm unable to find any proper approach. Here is my code:
#include <bits/stdc++.h>
using namespace std;
#define size 10000004
int mark[size+1];
void sieve()
{
for(int i=2,k;(k=i*i)<=size;++i)
{
for(int j=k;j<=size;j=j+k)
mark[j]=-1;
}
}
int fn(int a,int b,int c)
{
int i,j,k,cnt=0;
for(i=a;i<=b;++i)
{
j=i;
if(mark[j]!=-1)
{
while(j>0)
{
if(j%10==c)
{
cnt+=1;
break;
}
else
j=j/10;
}
}
}
return cnt;
}
int main()
{
sieve();
int t,a,b,c;
cin>>t;
for(int i=0;i<t;t++)
{
cin>>a>>b>>c;
cout<<fn(a,b,c)<<endl;
}
}

This code shows error "stu undeclared"?? what should i do

I know this error is because i have declared stu inside the for loop scope but its the necessity of the program.I want to declare an array for each test case (test case should all be input at once).Suggest me a way to achieve this.Is dynamic memory an alternative.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m =0;
for(int w=0;w<t;t++)
{
cin>>n[w]>>g[w];
int stu[n[w]];
for(int i=0;i<n[w];i++)
{
cin>>stu[i];
}
}
while(m<t)
{
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]=1)
{
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]=n[m])
{
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++)
{
for(int k=(j+1);k<n[m];k++)
{
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
You are declaring stu inside of a for loop, so it is limited to the scope of the loop. You then try to use it outside of the loop, where it is undeclared.
for(int w=0;w<t;t++)
{
...
int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
// OK to use stu here
...
}
// stu doesn't exist here
Also note that standard C++ does not support variable length arrays (VLAs), which is what you are attempting to use in the declaration of stu, as well as here:
int t;
cin>>t;
int n[t],g[t];
You can replace these arrays by std::vector<int>:
#include <iostream>
#include <vector>
int main()
{
int t=0;
cin>>t;
std::vector<int> n(t);
std::vector<int> g(t);
std::vector<int> stu ...;
}
The line
int stu[n[w]];
is inside a block and outside that block it won't be seen. You should move it out of the block, but doing so of course you can't use n[w], being w the looping var. You coudl put a limit to the max value n[w] can have, e.g.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
int t;
cin>>t;
int n[t],g[t]; // <- supported by some compiler, but not std
int m =0;
int stu[MAXV];
for(int w=0;w<t;t++) {
cin>>n[w]>>g[w];
for(int i=0;i<n[w] && i < MAXV;i++) {
cin>>stu[i];
}
}
while(m<t) {
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]==1) {
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]==n[m]) {
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++) {
for(int k=(j+1);k<n[m];k++) {
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
(I've fixed a couple of assignment in conditional when I suppose you meant == and not =, but I've not tested if the code does what you expect: it just compile, with g++ but not with other compiler likely, see comment in code)

Pass Vector by reference and Get Error

Following code is for test the pass the reference of a vector to a function. However, it will have some unknown fault. I got the error message from gdb as following:
Source is:
#include<iostream>
#include<vector>
using namespace std;
class ROLE
{
public:
ROLE(int);
int HP;
};
ROLE::ROLE (int input)
{
HP = input;
}
void checkVector(vector<ROLE>&input);
int main()
{
vector<ROLE> R;
ROLE K(102);
R.push_back(K);
K.HP = 111;
R.push_back(K);
checkVector(R);
return 1;
}
void checkVector(vector <ROLE> & input)
{
cout<<"size of vector "<<input.size()<<endl;
for(int i =0; i<input.size();i++)
{
cout<< input[i].HP<<endl;
}
}
I can't find the reason of the error. Any idea is appreciated!
I realized that error probably from the push_back, because I also get the error if I modified the code as following. Are there any idea about what happened?
#include<iostream>
#include<vector>
using namespace std;
class ROLE
{
public:
ROLE(int);
int HP;
};
ROLE::ROLE (int input)
{
HP = input;
}
int main()
{
vector<ROLE> R;
ROLE K(0);
K.HP=1;
R.push_back(K);
K.HP = 113;
R.push_back(K);
K.HP = 111;
R.push_back(K);
return 0;
}
Possible answer:
I get one way which can avoid the error.
Following is the no-error source.
#include<iostream>
#include<vector>
using namespace std;
class ROLE
{
public:
ROLE(int);
void constructAry(int inputHP);
int HP;
};
ROLE::ROLE (int input)
{
HP = input;
}
void ROLE::constructAry(int inputHP)
{
HP = inputHP;
}
void checkVector(vector<ROLE>&input);
int main()
{
vector<int> test;
vector<ROLE> R;
ROLE K(0);
K.constructAry(1);
R.push_back(K);
K.constructAry(2);
R.push_back(K);
K.constructAry(3);
R.push_back(K);
K.constructAry(4);
R.push_back(K);
checkVector(R);
return 1;
}
void checkVector(vector <ROLE> & input)
{
cout<<"size of vector "<<input.size()<<endl;
for(int i =0; i<input.size();i++)
{
cout<< input[i].HP<<endl;
}
}
So it seems that using a constructor-like function to modify the value of the same object and push_back the object in the vector can effectively avoid the bug.
The reason is ambiguous for me. Maybe the memory address's matter.