Thursday, July 9, 2009

C++ aRRay problem?

How would I make a program that prints out the odd positioned elements in an array of 10 integers.

C++ aRRay problem?
Its simple, see code below:





#include%26lt;iostream%26gt;





using namespace std;





int main()


{


int ar[10]; //populate the array yourself


int i = 1;


while(i%26lt;10)


{


cout%26lt;%26lt;ar[i]%26lt;%26lt;endl;


i = i+2;


}





return 0;


}





Hope this helps!
Reply:#include%26lt;iostream%26gt;





using namespace std;





int main()


{


int ar[10];





for(int i=0; i%26lt;10;i+=2)


cout %26lt;%26lt; ar[i] %26lt;%26lt; endl;





}





return 0;


}
Reply:int main()


{


int num[10];





for(int i=0; i%26lt;10;i++)


{


cin%26gt;%26gt; num[i] %26lt;%26lt; endl;


}





cout%26lt;%26lt;"Odd numbers in an array";





for(int a=0; a%26lt;10;a++)


{


if(num[a]%2==1)


{


cout%26lt;%26lt;num[a];


}


}





return 0;





}


No comments:

Post a Comment