Thread: Something is wrong with my program

  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
    4,183
    Your final for loop is different.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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, 02:18 PM
  2. What is wrong with this program??
    By cprog12 in forum C Programming
    Replies: 3
    Last Post: 11-24-2010, 12:48 PM
  3. What is wrong with my program
    By bijan311 in forum C++ Programming
    Replies: 18
    Last Post: 12-13-2009, 05:04 PM