Thread: C program with arrays, while loop, and sentinel value

  1. #16
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by jimblumberg View Post
    You need to insure that you properly initialize all of your variables.

    Also your while loop is a little messed up. Remember you're checking for the sentinel value at the start of the loop, so you should be getting your user entries right before the loop ends. Something like:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 50
    
    int main()
    {
        int value = 0, arr[SIZE] = {0}, i = 0;  // Don't forget to initialize all of your variables.
        printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
    
        // You need to get the first value from the user prior to the while loop.
        scanf("%d", &value);
    
        while (i < 50 && value != -999) // Notice the change here.  
        {
            arr[i] = value;
            printf("%i = %i \n", arr[i], value);
    
            printf("Enter another number. Type -999 to stop the loop: "); // keeps being printed with the other printf
            scanf("%i", &value);
    
            i++;
        }
        return 0;
    }
    Thank you, that solved most of the issues.
    Now the only problem is that 'i' isn't incrementing. I need it to start at zero and end at 49. Is 'i' in the incorrect place?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 50
    int main()
    {
        int value = 0, arr[SIZE] = {0}, i = 0;  
        
        printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
        scanf("%d", &value);
        while (i < 50 && value != -999){
         i++;
            printf("arr[%i] = %i \n", arr[i], value);
            printf("Enter another number. Type -999 to stop the loop: ");
            scanf("%i", &value);
        }
        
        return 0;
    }

  2. #17
    Banned
    Join Date
    Aug 2017
    Posts
    861
    place your i++ on the last line in your loop, the way you have it it is incrementing before you add it to your arr so you're going to end up one short.


    a good way to trouble shoot your issues is using printf to print out the values you're working with so you can see what is going on with them. then you should be able to discern them better.
    Last edited by userxbw; 11-27-2017 at 01:31 PM.

  3. #18
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by userxbw View Post
    place your i++ on the last line in your loop, the way you have it it is incrementing before you add it to your arr so you're going to end up one short.


    a good way to trouble shoot your issues is using printf to print out the values you're working with so you can see what is going on with them. then you should be able to discern them better.
    That is a brilliant idea. I did so and I see that 'i' does not add one to itself. Do I need to add 'i += 1'?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 50
    int main()
    {
        int value = 0, arr[SIZE] = {0}, i = 0;  
        
        printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
        scanf("%d", &value);
     i+=1;
     // printf("%i \n", i); i is 1 but turns to 0?
     // need i to start at 0 and add one
     
        while (i < 50 && value != -999){
            printf("arr[%i] = %i \n", arr[i], value);
            printf("Enter another number. Type -999 to stop the loop: ");
            scanf("%i", &value);
            i++;
            // printf("%i \n", i); 
            // i staying as 0l
        }
        
        return 0;
    }

  4. #19
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    That is a brilliant idea. I did so and I see that 'i' does not add one to itself. Do I need to add 'i += 1'?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 50
    int main()
    {
        int value = 0, arr[SIZE] = {0}, i = 0;  
        
        printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
        scanf("%d", &value);
     i+=1; // get rid of that it does not belong 
     // printf("%i \n", i); i is 1 but turns to 0?
     // need i to start at 0 and add one
     
        while (i < 50 && value != -999){
            printf("arr[%i] = %i \n", arr[i], value);
            printf("Enter another number. Type -999 to stop the loop: ");
            scanf("%i", &value);
            i++; / / this belongs
            // printf("%i \n", i); 
            // i staying as 0l
        }
        
        return 0;
    }
    with your i on top it sets i = 1 so your array [ 0 ] never gets a value inside of it.
    whence you got that corrected leave it alone .
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define SIZE 50
     
    int main()
    {
        int value = 0, arr[SIZE] = {0}, i = 0;  // Don't forget to initialize all of your variables.
        printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
     
        // You need to get the first value from the user prior to the while loop.
        scanf("%d", &value);
     
       while (i < 50 && value != -999) // Notice the change here.  
        {
            printf("loop i set to 0 at start\non top of loop i = %d\n", i);
            arr[i] = value;
            printf("arr [ %i ] = value = %i i = %d\n", arr[i], value, i);
            i++; 
            printf("on bottom of of loop i = %d\n", i);
            printf("Enter another number. Type -999 to stop the loop: "); // keeps being printed with the other printf
            scanf("%i", &value);        
        }
        
        
        int j = 0;
    for ( ; j < 50; j++)
        printf("element number %d\n count = %d \n arr = %d\n", j,j+1, arr[j]);
        
        return 0;
    }
    Last edited by userxbw; 11-27-2017 at 08:17 PM.

  5. #20
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by userxbw View Post
    with your i on top it sets i = 1 so your array [ 0 ] never gets a value inside of it.
    whence you got that corrected leave it alone .
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define SIZE 50
     
    int main()
    {
        int value = 0, arr[SIZE] = {0}, i = 0;  // Don't forget to initialize all of your variables.
        printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
     
        // You need to get the first value from the user prior to the while loop.
        scanf("%d", &value);
     
       while (i < 50 && value != -999) // Notice the change here.  
        {
            printf("loop i set to 0 at start\non top of loop i = %d\n", i);
            arr[i] = value;
            printf("arr [ %i ] = value = %i i = %d\n", arr[i], value, i);
            i++; 
            printf("on bottom of of loop i = %d\n", i);
            printf("Enter another number. Type -999 to stop the loop: "); // keeps being printed with the other printf
            scanf("%i", &value);        
        }
        
        
        
        int j = 0;
    for ( ; j < 50; j++)
        printf("%d arr\n element number %d\n", arr[j], j);
        
        return 0;
    }
    I think I got it! thank you sir

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861
    thanks goes to jimblumberg as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2017, 12:20 PM
  2. Replies: 6
    Last Post: 03-09-2016, 01:30 PM
  3. sentinel controlled do-while loop
    By theCanuck in forum C++ Programming
    Replies: 10
    Last Post: 03-22-2011, 11:07 PM
  4. Sentinel while loop and errors.
    By lollypop in forum C Programming
    Replies: 12
    Last Post: 10-19-2003, 12:40 PM
  5. Sentinel Loop?
    By TerryBogard in forum C Programming
    Replies: 1
    Last Post: 12-17-2002, 01:11 AM

Tags for this Thread