# Thread: Exercise reverse array . I can't to find error .

1. ## Exercise reverse array . I can't to find error .

I have a code :
Code:
```#include<stdio.h>#include<conio.h>

void main()
{
int A[100];
int B[100];
int i,n;
// Input total values
printf("How many values do you want to input :  ");
scanf("%d",&n);
// Input each of value
for(i=0;i<n;i++)
{
printf("Number %d : ",i+1);
scanf("%d",&A[i]);
}
// Coppies A[i] to B[i]
for(i=n;i>0;i--)
{
A[i] = B[i];
}
// Display
for(i =0;i<n;i++)
{
printf("\n Array after reverse : %d ", B[i]);
}
}```
I don't know why after reverse array , it show like that. Plz help me.

2. You are inputing the values into the array A, and then assigning the value of the array B into the array A . You should do the opposite, assigning the value of the array A into the array B! (instead of "A[i] = B[i] it should be "B[i]=A[i]"). And since B is not initialized, you just print some garbage values. Also, not as important, you should check that the user does not input a a number above 100 otherwise you will have an overflow.

Oh and also important, your second for loop should start from "n-1". In fact, you input "n" values that you store in A from 0 to "n-1". So when you reverse you should start at "n-1" and finish at 0, not at 1 (so i > -1 for the exit condition).

And finally, the most important, your algorithm is not reversing the array, just copying it. In fact, doing B[i] = A[i] will give the same array at the end. So you should think about a way to copy the last value of A into the first square of B.

3. Originally Posted by Tibo-88
You are inputing the values into the array A, and then assigning the value of the array B into the array A . You should do the opposite, assigning the value of the array A into the array B! (instead of "A[i] = B[i] it should be "B[i]=A[i]"). And since B is not initialized, you just print some garbage values. Also, not as important, you should check that the user does not input a a number above 100 otherwise you will have an overflow.

Oh and also important, your second for loop should start from "n-1". In fact, you input "n" values that you store in A from 0 to "n-1". So when you reverse you should start at "n-1" and finish at 0, not at 1 (so i > -1 for the exit condition).

And finally, the most important, your algorithm is not reversing the array, just copying it. In fact, doing B[i] = A[i] will give the same array at the end. So you should think about a way to copy the last value of A into the first square of B.

Tks a lots . I can be edit it . It is good. But how can I input n again if n >100 .
Code:
```#include<stdio.h>#include<conio.h>

void main()
{
int A[100];
int B[100];
int i,n;
// Input total values
printf("How many values do you want to input :  ");
scanf("%d",&n);
if(n>100)
{
printf("Please input again, n not above 100");
}
else
{
// Input each of value
for(i=0;i<n;i++)
{
printf("Number %d : ",i+1);
scanf("%d",&A[i]);
}

// Coppies A[i] to B[i]
for(i =0;i<n;i++)
{
B[i] = A[i];
}
// Display
for(i=n-1;i>-1;i--)
{
printf("\n Array after reverse : %d ", B[i]);
}
}
}```

4. Originally Posted by ToNy_
Tks a lots . I can be edit it . It is good. But how can I input n again if n >100 .
Code:
```#include<stdio.h>#include<conio.h>

void main()
{
int A[100];
int B[100];
int i,n;
// Input total values
printf("How many values do you want to input :  ");
scanf("%d",&n);
if(n>100)
{
printf("Please input again, n not above 100");
}
else
{
// Input each of value
for(i=0;i<n;i++)
{
printf("Number %d : ",i+1);
scanf("%d",&A[i]);
}

// Coppies A[i] to B[i]
for(i =0;i<n;i++)
{
B[i] = A[i];
}
// Display
for(i=n-1;i>-1;i--)
{
printf("\n Array after reverse : %d ", B[i]);
}
}
}```
return type of main is not void it is int.

5. Originally Posted by joybanerjee39
return type of main is not void it is int.
Yes. I'm understood.

6. Yeah true for the main, I did not notice it! Take a look at : FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.

For coming back to the input, the usual way is to use a while loop ( "while( n < 0 || n > 100 )" ).

And also, if your only purpose is to print the array in the reverse order, do you really need to copy it?

7. Originally Posted by Tibo-88
Yeah true for the main, I did not notice it! Take a look at : FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.

For coming back to the input, the usual way is to use a while loop ( "while( n < 0 || n > 100 )" ).

And also, if your only purpose is to print the array in the reverse order, do you really need to copy it?
While(n>0||n<100) . I don't know why . Exercise give me some hints like that.

8. I think i have to add return 0 at the end .

9. Yes you do! And actually now that I think about it, it is better to use do-while loop, because you always want to ask for input at least once (the condition remains the same).