Saturday, May 22, 2010

C++ Help! Printing array in reverse!?

Ok, I have to take an array of 10 numbers and output them in reverse. This is what I have and it isnt working? Can anyone help me fix it? Thanks.





#include %26lt;iostream%26gt;


using namespace std;





int main()





{


//--- Declare array and number of elements in it.


float a[10];


int n; // Number of values currenlty in array.


{


//--- Read numbers into an array


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


cout %26lt;%26lt; "Enter a number: " %26lt;%26lt; cin %26lt;%26lt; a[n];


}





//--- Print array in reverse order


for (int i=n-1; i%26gt;=0; i--)


{


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


}





return 0;


}

C++ Help! Printing array in reverse!?
The above is almost correct... the printingloop should startat MAX-1.
Reply:Might try an ascending sort algorithm then print it out.
Reply:#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#define MAX 10 /* Declare as Macro */





void main(void)


{


float a[10];


int i;


clrscr(); /* To clear the Screen */





for(i=0;i%26lt;MAX;i++)


{


cout %26lt;%26lt;"\n Enter"%26lt;%26lt;i+1%26lt;%26lt;" Number ";


cin %26gt;%26gt;a[i];


}





//DISPLAY THE ARRAY IN REVERSE ORDER





for(i=MAX;i%26gt;=0;i--)


{


cout %26lt;%26lt;"\n"%26lt;%26lt;a[i]%26lt;%26lt;"\n";


}





getch();


}
Reply:you not yet declare your i=int;


i


No comments:

Post a Comment