Something is wrong with my program

This is a discussion on Something is wrong with my program within the C Programming forums, part of the General Programming Boards category; I wrote two programs to switch the order of an array. I wrote them in the same way, but only ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    61

    Something is wrong with my program

    I wrote two programs to switch the order of an array. I wrote them in the same way, but only one of them works correctly. Could someone tell me what is wrong?

    Oh I just figured out the problem
    This is the one that works:
    http://i49.tinypic.com/2ljqp2c.png
    Code:
    #include <stdio.h>
    #define SIZE 10
    
    void main(void)
    {
        int x[] = {-3,2,27,0,-10,31,-99,4,-47,33};
        int c=0,temp=0,last_index=0;
        
        last_index=SIZE-1;
        for( c=0; c < last_index/2; c++){
            temp= x[c];
            x[c] = x[last_index-c];
            x[last_index-c]=temp;
        }
            for(c= 0; c < SIZE; c++) {
                
                printf("\n x[%d] = %d ",c,x[c]);}
    }
    This is the one that doesn't work:
    http://i45.tinypic.com/8vq6c0.png
    Code:
    #include <stdio.h>
    #define SIZE 10
    
    void main(void)
    {
    
        int x[] = {-3,2,27,0,-10,31,-39,4,-47,33};
        int i=0,last=0,temp=0;
        
        last = SIZE-1;
        for(i = 0; i < last/2 ;i++){
            temp = x[i];
            x[i] = x[last-i];
            x[last-i] = temp;
        
        }
        for(i = 0; i < last/2 ;i++){
            printf("x[%d] is %d\n",i,x[i]);
        }
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    Your final for loop is different.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my program?
    By Farnaz in forum C++ Programming
    Replies: 29
    Last Post: 02-28-2011, 01:18 PM
  2. What is wrong with this program??
    By cprog12 in forum C Programming
    Replies: 3
    Last Post: 11-24-2010, 11:48 AM
  3. What is wrong with my program
    By bijan311 in forum C++ Programming
    Replies: 18
    Last Post: 12-13-2009, 04:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21