what is the output of this code fragment?

This is a discussion on what is the output of this code fragment? within the C Programming forums, part of the General Programming Boards category; Code: #define N_ELEMS 5 int main(void) { int array[N_ELEMS]; int i; for(i=0;i<N_ELEMS;i++) array[i]=i; for(i=0;i<N_ELEMS-1;) array[++i]++; } Hi, how do i ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    what is the output of this code fragment?

    Code:
    #define N_ELEMS 5
    
    int main(void)
    {
      int array[N_ELEMS];
      int i;
    
      for(i=0;i<N_ELEMS;i++)
        array[i]=i;
    
      for(i=0;i<N_ELEMS-1;)
        array[++i]++;
    
    }
    Hi, how do i know the output of this code??
    i tried using this way, but the results seems dogey..any ideas why??

    Code:
    #define N_ELEMS 5
    #include <stdio.h>
    
    int main(void)
    {
      int array[N_ELEMS];
      int i;
    
      for(i=0;i<N_ELEMS;i++)
      {
        array[i]=i;
      }
       printf("%i\n",array);
    
      for(i=0;i<N_ELEMS-1;)
      {
        array[++i]++;
      }  
        printf("%i\n",array);
    
    return 0;
    }
    Pls advise me, thank u, is the printf which i have done a incorrect way??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    You need another for loop and
    Code:
    printf( "%d ", array[i] );

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    hi

    Sorry salem,

    where should this loop be then?? and shd the printf statement be array[i] or just array?? because i am printing the results of the array after the loop so i dont need the [i] right?? or am i wrong??

    regards ryan

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    what you are doing is printing every array element in a loop, so you need a [i]
    :wq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out the output of code ...
    By Newworld in forum C Programming
    Replies: 5
    Last Post: 09-19-2004, 11:32 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 02:29 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 02:27 PM
  4. code fragment pls help
    By Damo in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 10:57 AM

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