help

hi friends ,
I have to put the output of a loop into an array.How do I put it???
Really we need to see what code you have so we can help you, but I'd say use a vector instead of an array for starters.

http://www.cplusplus.com/reference/vector/vector/push_back/
I am trying to find the largest even and odd numbers from the given numbers
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[20],eve[20],odd[20],b,a,c,d,lar=0,larg=0;
cout<<"enter the number of numbers";
cin>>a;
cout<<"enter the numbers";
for(int i=0;i<a;i++)
{
cin>>arr[i];
}
for(int j=0;j<i;j++)
{
b=arr[j]%2;
if(b==0)
{
eve[c]=arr[j];
cout<<"even numbers"<<eve[c]<<endl;
}
if(b==1)
{
odd[d]=arr[j];
cout<<"odd numbers"<<odd[d]<<endl;
}
}
for(int k=0;k<c;k++)
{
if(eve[k]>lar)

lar=eve[k];

}
cout<<"largest of even numbers is"<<lar<<endl;
for(int l=0;l<d;l++)
{
if(odd[l]>larg)

larg=odd[l];

}
cout<<"largest of odd numbers is"<<larg;
getch();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int j=0;j<i;j++)
{
  b=arr[j]%2;
  if(b==0)
  {
    eve[c]=arr[j];
    cout<<"even numbers"<<eve[c]<<endl;
  }
  if(b==1)
  {
    odd[d]=arr[j];
    cout<<"odd numbers"<<odd[d]<<endl;
  }
}


look at this code, what are the values of c and d every time the for loop is run? do they change?
I get even and odd numbers differentiated
for example
I put
1
8
7
4
5
6
then I get
odd number 1
even number8
odd number 7
even number 4
odd number 5
even number 6
Last edited on
but not in an array, what is the value of c and d for each itteration of the loop? Not having a go, just trying to guid you to see what's wrong.
Last edited on
How to find the value of c and d??
cout << c << "\n" << d << "\n";

or even better use the debugger of your programming IDE (F5 in visual studio) and set breakpoints.
I am getting strange results
c=1532
d=1556
so you need to set the values of c and d to 0 to start with and increment them whenever you put something into the odd and eve arrays.

1
2
3
4
for(int i=0;i<a;i++)
{
  cin>>arr[i];
}


Use the code where you input the numbers as a guide.
Last edited on
Hey u were good....u solved my problem
thank u so much.......
I only guided you... Hopefully you learned something and can see the other problems in your code {B¬)

Topic archived. No new replies allowed.