array help please

This is a discussion on array help please within the C Programming forums, part of the General Programming Boards category; i'm trying to output a number of "*" for the number it corresponds to in an array e.g Code: array[3]={1,2,3}; ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    array help please

    i'm trying to output a number of "*" for the number it corresponds to in an array e.g
    Code:
    array[3]={1,2,3};
    output:
    1 |* 
    2 |**
    3 |***
    ii'm getting a weird output 'm not sure but i think the error is here
    Code:
    for(z_count=1; z_count<=nwords[y_count]; z_count++)
    here's my code:
    Code:
    #include <stdio.h>
    int main(void){
          int nwords[10]={1,2,3,4,5,0,0,0,0,0};
        int c, max, x, y_count, z_count;
        
        c = max = x = y_count = z_count = 0;
        
     
     for(x=1; x<=10; x++){
                 printf("%3d |",x);
                 
              //  for(y_count=0; y_count<10; y_count++){
                                for(z_count=1; z_count<=nwords[y_count]; z_count++){
                                               printf("*");
                                
                                }
               // }
                 printf("\n    |");
                 printf("\n");
        }        
         
        getch();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    The inner loop needs to to make use of variable x, rather than y_count.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    it works when i used x like this
    Code:
    for(z_count=1; z_count<=nwords[x-1]; z_count++)
    but now i don't understand why it works with x-1 and not y_count because there the same in value

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    The outer loop is iterating over x. So, x changes with each iteration of the outer loop. The value of y_count is always zero.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    ohhhhh i see thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

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