Stuck in Arrays
I have created an array. There are two arrays, I want to print larger than 50 values in the s[j] array.
It should be like this
But I don't know why my array prints like this.
0 70 92 68 68 90 99 99 99 99 |
There should be a small mistake. Here is the code. Kindly someone help me to correct this. Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
using namespace std;
int t[10]={40, 70, 92, 68, 43,90, 99, 23, 45, 1};
int s[10];
int main()
{
for (int j=0,i=0; i < 10; i++)
{
if (t[i]>=50)
s[j]=t[i];
cout << s[j] << "\t";
}
cout << endl;
return 0;
}
|
Your code with proper formatting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
using namespace std;
int t[10]={40, 70, 92, 68, 43,90, 99, 23, 45, 1};
int s[10];
int main()
{
for (int j=0,i=0; i < 10; i++)
{
if (t[i]>=50)
s[j]=t[i];
cout << s[j] << "\t";
}
cout << endl;
return 0;
}
|
Note that j never changes in your program. It is always 0.
I have changed it like this
1 2 3
|
s[j]=t[i];
cout << s[j] << "\t";
s[j++];
|
Now the out put is like
I want to rearrange it as zeros to end and others from s[0].
After doing following changes it showed,
1 2 3 4 5 6 7
|
if (t[i]>=50){
s[j]=t[i];
cout << s[j] << "\t";
s[j++];
}
}
cout << endl;
|
I forgot to add 2 curl brackets between if condition.
Now its
Thank you for the help.
Last edited on
Topic archived. No new replies allowed.