C014 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.

[codesyntax lang=”c”]

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

[/codesyntax]

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.

[codesyntax lang=”c”]

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

[/codesyntax]

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

C program code :

[codesyntax lang=”c” lines=”normal”]

#include<stdio.h>
void main()
{
    int NUM[20],TEMP[20],I,J;
    clrscr();
    printf("ENTER TWENTY NUMBERS TO BE REVERSED\n");
    for(I=0;I<20;I++)
    {
        scanf("%d",&NUM[I]);
    }
    J=19;
    for(I=0;I<20;I++)
    {
        TEMP[J]=NUM[I];
        J--;
    }
    printf("\nORIGINAL NUMBERS ARE :- \n");
    for(I=0;I<20;I++)
    {
        printf(" %d\t",NUM[I]);
    }
    printf("\nREVERSE NUMBERS ARE :- \n");
    for(I=0;I<20;I++)
    {
        printf("\t %d",TEMP[I]);
    }
    getch();
}

[/codesyntax]

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

 

Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply