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

  1. #1
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24

    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.
    Attached Images Attached Images Exercise reverse array . I can't to find error .-untitled-jpg 

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    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.
    Last edited by Tibo-88; 12-15-2011 at 08:28 AM.

  3. #3
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by Tibo-88 View Post
    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. #4
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by ToNy_ View Post
    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. #5
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by joybanerjee39 View Post
    return type of main is not void it is int.
    Yes. I'm understood.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    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. #7
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by Tibo-88 View Post
    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. #8
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    I think i have to add return 0 at the end .

  9. #9
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2-dimensional array exercise!!!!!!!!!!!
    By kokujampo in forum C Programming
    Replies: 1
    Last Post: 04-02-2010, 04:17 PM
  2. another array exercise
    By alyeska in forum C++ Programming
    Replies: 4
    Last Post: 10-22-2007, 01:30 PM
  3. (!)Simple array exercise
    By Fizz in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2004, 12:45 PM
  4. sorting array efficiently...can't find the error
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2002, 02:32 PM
  5. reverse array logic error
    By rippascal in forum C++ Programming
    Replies: 5
    Last Post: 03-20-2002, 10:47 PM