An interactive C program that will take as input a set of 20 integers and store them in an array and using a temporary array of equal length, reverse the order of the integers & display the values.

By | August 29, 2013

Let’s identify variables needed for this program.

In this program, we need several variables to store and compare the values to find the largest and the smallest out of them. Its not recommended to take number of variables declared of the same data type with different names and remembering it. It becomes worst when the variable numbers goes on increasing to large number. Hence we have a savior called ARRAY.

Array is a nothing different than any variable but the only difference is that it allows us to use more than one variables with a single name to it. The size of array is decided by us. We can take 2 to n numbers according to our requirement. It has to be accessed with the help of index number ranging from 0 to n-1. (e.g. num[0],num[1],num[2],num[3]) 

First variable will be the one which will save the values entered by the user and it will be array NUM[20]. Here we are taking twenty different values hence taking number 20. Second variable will be array TEMP[20] for saving temporary reversed values which will be displayed later. other variables will be I which will be for FOR Loop and J will help us in the reversing logic index so in all Four variables.
The identified variables are NUM[20],TEMP[20],I,J.

int NUM[20],TEMP[20],I,J;

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

In this program there is a requirement of accessing the array NUM[0],NUM[1]….NUM[9]. Here we have the index values ranging from zero to the given number minus one (i.e. n-1) . Best Loop for such kind of condition is FOR Loop the value of loop will start with 0 to 19. We will need four FOR loops, First one for scanning required array values. Second one for reversing all the numbers found in original array. and third & fourth ones for printing original array values and reversed array values.

In this program, The main thing to do is reverse the array elements and save in another array. As we know, the array index of NUM[20] starts with 0 – 19 and same with TEMP[20] that is 0 – 19, so if we see it in reverse order it will be 19 – 0 main logic of the program is that initialize the variable with 19 and decrement J by J– with the loop which executing for 20 times. No need to worry about when to stop.

J=19;
    for(I=0;I<20;I++)
    {
        TEMP[J]=NUM[I];
        J--;
    }

Finally print the Numbers of Original Array and Reverse Array at the end of the program.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4. int NUM[20],TEMP[20],I,J;
  5. clrscr();
  6. printf("ENTER TWENTY NUMBERS TO BE REVERSED\n");
  7. for(I=0;I<20;I++)
  8. {
  9. scanf("%d",&NUM[I]);
  10. }
  11. J=19;
  12. for(I=0;I<20;I++)
  13. {
  14. TEMP[J]=NUM[I];
  15. J--;
  16. }
  17. printf("\nORIGINAL NUMBERS ARE :- \n");
  18. for(I=0;I<20;I++)
  19. {
  20. printf(" %d\t",NUM[I]);
  21. }
  22. printf("\nREVERSE NUMBERS ARE :- \n");
  23. for(I=0;I<20;I++)
  24. {
  25. printf("\t %d",TEMP[I]);
  26. }
  27. getch();
  28. }

Note:- whenever you have large number of inputs to be entered, we can use space instead of enter button all the time. You can see have its done in Output screen.

SCREEN SHOTS:-

C_program_Array_Reverse

C_program_Array_Reverse_Output

Leave a Reply