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

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    37

    Question C program with arrays, while loop, and sentinel value

    Hi, for this assignment I must allow the user to enter numbers into an array. When the array size exceeds 50 or when the user enters -999, the array will be displayed.

    The problem: The sentinel value is not being recognized, so I am not sure if the program works correctly at all. Here is my code.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 50
    
    int main()
    {
     int value = 0, arr[SIZE] = {0}, i;
     printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
     
     while (arr[i] != -999 || arr[SIZE] < 50){
      scanf_s("%i", &arr[i]);
      printf("Enter a new number.\n");
     }
     
     for (i = 0; i < SIZE; i++){
      printf("%i\n", arr[i]);
     }
     system("pause");
    }
    I would appreciate some help and advice. I've been working most of today to figure this out.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    Hi, for this assignment I must allow the user to enter numbers into an array. When the array size exceeds 50 or when the user enters -999, the array will be displayed.

    The problem: The sentinel value is not being recognized, so I am not sure if the program works correctly at all. Here is my code.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 50
    
    int main()
    {
     int value = 0, arr[SIZE] = {0}, i;
     printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
      
    //no incrementing of your i to keep track of amount.
    // NOT using your value variable 
    // to help control your while loop as well.  
    //while ( or ( or ) and) 
    //which should it really be?
    
     while (arr[i] != -999 || arr[SIZE] < 50){ // not a good idea either.
      scanf_s("%i", &arr[i]);
      printf("Enter a new number.\n");
     }
     
     for (i = 0; i < SIZE; i++){
      printf("%i\n", arr[i]);
     }
     system("pause");
    }
    I would appreciate some help and advice. I've been working most of today to figure this out.
    comments in your code.
    Last edited by userxbw; 11-26-2017 at 02:35 PM.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    There are many things wrong with this program, please endure while I explain everything:
    *) You never set the value of "i", you can't know what its value will be when the first loop starts.
    *) Arrays are indexed from 0 to SIZE-1, never attempt to read from or write to above element SIZE-1.
    *) You never increment "i" inside the first loop, therefore only a single element ever changes.
    *) Arrays don't keep track of their size, you should do that yourself with "value", a variable that you've declared but completely forgot about.
    *) Don't use the VS *_s functions, they're non-portable and just a placebo.
    *) Don't use system("pause"), your IDE should be able to pause the program by itself. If not, get a better IDE.

    You make it harder for yourself by choosing to solve it like that. I suggest an infinite loop with break statements:
    Code:
    for (;;) {
        // Read input
        // If input is -999, break
        // Save input and increment index
        // If index is equal to size, break
    }
    Last edited by GReaper; 11-26-2017 at 02:47 PM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by userxbw View Post
    comments in your code.
    I thought about your comments and came up with this:

    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: ");
     
     while (value != -999 && i < 50){
      scanf_s("%i", &value);
      i++;
      printf("Enter another number. Type -999 to stop the loop: ");
     }
     
     for (i = 0; i < SIZE; i++){
      printf("%i = %i \n", arr[i], value);
     }
     system("pause");
    }
    Am I getting any warmer?

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    I thought about your comments and came up with this:

    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: ");
     
     while (value != -999 && i < 50){
      scanf_s("%i", &value);
      i++;
      printf("Enter another number. Type -999 to stop the loop: ");
     }
     
     for (i = 0; i < SIZE; i++){
      printf("%i = %i \n", arr[i], value);
     }
     system("pause");
    }
    Am I getting any warmer?
    that is much better but how does the input get into the array now? you forgot an assignment.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    using #define SIZE 50 does not set 50 to each SIZE? And I'm sorry but how would another assignment exactly work? Thanks.

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

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by GReaper View Post
    There are many things wrong with this program, please endure while I explain everything:
    *) You never set the value of "i", you can't know what its value will be when the first loop starts.
    *) Arrays are indexed from 0 to SIZE-1, never attempt to read from or write to above element SIZE-1.
    *) You never increment "i" inside the first loop, therefore only a single element ever changes.
    *) Arrays don't keep track of their size, you should do that yourself with "value", a variable that you've declared but completely forgot about.
    *) Don't use the VS *_s functions, they're non-portable and just a placebo.
    *) Don't use system("pause"), your IDE should be able to pause the program by itself. If not, get a better IDE.

    You make it harder for yourself by choosing to solve it like that. I suggest an infinite loop with break statements:
    Code:
    for (;;) {
        // Read input
        // If input is -999, break
        // Save input and increment index
        // If index is equal to size, break
    }
    Thank for being honest. Unfortunetly I have to use a array for this. I (think) I have addressed some of your issues in my code in my last post.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    using #define SIZE 50 does not set 50 to each SIZE? And I'm sorry but how would another assignment exactly work? Thanks.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 50
    
    int main()
    {
     int value = 0, arr[SIZE] = {0}, i;
     printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
     
     while (value != -999 && i <= 49){
      scanf_s("%i", &value);
      printf("Enter another number. Type -999 to stop the loop: ");
     }
     
     for (i = 0; i < SIZE; i++){
      printf("%i = %i \n", arr[i], value);
     }
     return 0;
    }
    Code:
    #define SIZE 50
    
    int array[SIZE];
    gives you 50 elements to work with, 0 - 49. it is an int so the size of the int is whatever that is, some really big number. if it is a char then that you need to worry about mostly.
    Code:
    #define SIZE 50;
    char array[SIZE];
    is not the same as an int array declared the same way. that in just one "string" able to take up to a 49 letters including spaces the -1 is for '\0' (null).

    what you need to worry about is how to get arr[i] equal to value.
    Last edited by userxbw; 11-26-2017 at 03:27 PM.

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    .
    Quote Originally Posted by userxbw View Post
    Code:
    #define SIZE 50
    
    int array[SIZE];
    gives you 50 elements to work with, 0 - 49. it is an int so the size of the int is whatever that is, some really big number. if it is a char then that you need to worry about mostly.
    Code:
    #define SIZE 50;
    char array[SIZE];
    is not the same as an int array declared the same way. that in just one "string" able to take up to a 49 letters including spaces the -1 is for '\0' (null).

    what you need to worry about is how to get arr[i] equal to value.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 50
    
    int main()
    {
     int value, arr[SIZE] = {0}, i;
     printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
     
     while (value != -999 && i <= 49){
      scanf_s("%i", &value);
      printf("Enter another number. Type -999 to stop the loop: ");
     }
     
     while(value!=-999){
     i++;
     arr[i] = value;
     // I was thinking since i equals the number of the array
     // value would equal the array itself
    }
     // took out the for loop
     if(value == -999)
      printf("%i = %i \n", arr[i], value);
     
     // still prints line 12 with line 23 after entering -999
     // and numbers are not being stored into i or value :/
     return 0;
    }
    Last edited by Kayla Hoyte; 11-26-2017 at 04:11 PM. Reason: edit lines no

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Move lines #17 and #16 inside the first loop, preferably in that order, and then delete the second loop.
    Devoted my life to programming...

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    and Stop! using scanf_s please

    read post #7 on that issue.
    and what is the BIG difference between this
    Code:
     while (value != -999 && i <= 49)
    and this
    Code:
     while (value != -999 && i < 50)
    at what number does that second one stop at?
    Last edited by userxbw; 11-26-2017 at 08:32 PM.

  12. #12
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by userxbw View Post
    and Stop! using scanf_s please

    read post #7 on that issue.
    and what is the BIG difference between this
    Code:
     while (value != -999 && i <= 49)
    and this
    Code:
     while (value != -999 && i < 50)
    at what number does that second one stop at?
    I'm using Dev C++ so I use scanf, no?

    The first one stops when i is less than or equal to 49. The second one stops at 49 also?

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    I'm using Dev C++ so I use scanf, no?

    The first one stops when i is less than or equal to 49. The second one stops at 49 also?
    I do not know but scanf_s is not universal. if scanf works in DEV C++ then use it?
    and yes they both stop at 49. Just wanted to be sure you knew that.

  14. #14
    Registered User
    Join Date
    Sep 2017
    Posts
    37

    Angry

    Quote Originally Posted by userxbw View Post
    I do not know but scanf_s is not universal. if scanf works in DEV C++ then use it?
    and yes they both stop at 49. Just wanted to be sure you knew that.
    OH, I just realized the 'scanf_s' in my post. In my dev environment I have 'scanf'. My bad.

    I'm still stuck, unfortunately.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 50
    int main()
    {
     int value, arr[SIZE] = {0}, i;
     printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
     
     while (value != -999 && i < 50){
      scanf("%i", &value);
      printf("Enter another number. Type -999 to stop the loop: "); // keeps being printed with the other printf
      i++;
      arr[i] = value; // this must be wrong but idk how to fix
      if(value == -999){
       printf("%i = %i \n", arr[i], value); // not printing the array :/
     // also need it to print as many times the amount of number the user enters
     }
    }
     return 0;
    }
    Last edited by Kayla Hoyte; 11-27-2017 at 10:19 AM. Reason: comment

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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;
    }

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