+ 1
I want to put negative and positive numbers into two column, what`s wrong?
#include<iostream> using namespace std; int main() { int i,a[10]; int p[i],n[i]; for(i =1; i<=10 ; i++) { cout<<"enter number\n"; cin>>a[i]; if(a[i] > 0) p[i] = a[i]; else if(a[i] < 0 ) n[i] = a[i]; } for(int j=0,k=0 ; j<= 10 &&k <=10 ; j++,k++) { cout<<"p"<<" = "<<p[i]<<'\t'<<"N"<<" = "<<n[k]<<endl; } return 0; }
7 Respostas
+ 2
alireza mohammadian try this:
https://code.sololearn.com/cuJP62CLeTqQ/?ref=app
however, I didn't handle case for length of n greater than length of p to have output column aligned ^^
+ 1
You are declaring p[],n[] arrays again in loop. Remove that and try.. those not exist outside loop and overhidden previous declarations in loop.
if you enter 5 -ve ,and +ve numbers then p,n arrays have only 5 numbers so no need to print 10 numbers of p,n both arrays in last loop ,that may give you garbage values.... so you need find how many positive and how many negative..
+ 1
there are several mistakes:
first declaration of p and n array use i as length, wich is not yet initialized ^^
second declaration of p and n overide global declaration and create new arrays at each loop iteration
when outputing result, you expect same size for both arrays, so ten negative and ten positive
+ 1
thank your for your helping
0
#include<iostream>
using namespace std;
int main()
{
int countera = 0,counterb = 0;
int i,a;
int p[i],n[i];
for(i =0; i< 10 ; i++)
{
cout<<"enter number\n";
cin>>a;
if(a >= 0)
{
p[countera] = a;
countera++;
}
if(a < 0 )
{
n[counterb] = a;
counterb++;
}
}
for(int j=0,k=0 ; j<=countera &&k <=counterb ; j++,k++)
{
cout<<"p"<<" = "<<p[j]<<'\t';
cout<<"N"<<" = "<<n[k]<<endl;
}
return 0;
}
0
Thank you.
I chang my program in this way that you say but it is not work. what are your opinion .