Menu

Sentinel for end of input data.

2009-05-20
2012-09-26
  • Holly Benton

    Holly Benton - 2009-05-20

    Ok, I have tried to look online, and through my books just cant seem to find it.

    A small portion of my code.

    What iam trying to do is accept up to 10 numbers save it to an array, but if they input a -1 or less end the list of numbers.

    I have tried a 'while' loop, rewriting the 'for' loop, iam just not getting it. Any help would be apprecited. I am also new to programing in C I have not learned about pointers yet ( saw some reference that went with array and pointers did not understand them). So a basic understanding of how to do it would be most helpful for me.

    using dev C++ 4.9.9.2
    Windows XP Professional

    include <stdio.h>

    include <stdlib.h>

    define NUM 10

    void bubbleSort(int pos[], int array_size);

    int main()
    {
    int pos[NUM], i, list, nine, sum = 0, mean = 0, median = 0;
    int numbers, small = 0, largest = 0, cnt;

    printf(&quot;CS133SP09: Lab6 Track 2 - Statistics Package\n&quot;);
    printf(&quot;Enter a series of up to 10 positive numbers.\n&quot;);
    printf(&quot;Terminate the list with a -1.\n&quot;);
    
    for(i = 0; i &lt; NUM; i++) 
    scanf(&quot;%d&quot;, &amp;pos[i]); //saves the array
    
    bubbleSort(pos, NUM);   //perform bubble sort on array
       printf(&quot;The values in ascending order:\n&quot;);
       for(i = 0; i &lt; NUM; i++)
       printf(&quot;%i\n&quot;, pos[i]);
    

    system("PAUSE");
    return 0;
    }
    //definition
    void bubbleSort(int pos[], int array_size) //list 1
    {
    int i, j, temp;

    for (i = (array_size - 1); i >= 0; i--)
    {
    for (j = 1; j <= i; j++)
    {
    if (pos[j-1] > pos[j])
    {
    temp = pos[j-1];
    pos[j-1] = pos[j];
    pos[j] = temp;
    }
    }
    }
    }


    Thank you once again.
    Akumanight

     
    • cpns

      cpns - 2009-05-21

      Your loop gets exactly NUM values. You have no code to check for -1.

      Show us your attempt at that. It would be better for someone to show you where you are going wrong than to simply 'do it for you'. Besides, not posting your attempt just makes it look like you are hoping for a free ride on your homework assignment.

      Consider that the middle expression in the for-loop statement can be any expression; you can test both i and the input value. I suggest that you do not assign the input directly to your array, but to a separate variable, test the variable and if valid put it in the array. The loop test is then i < NUM and input valid.

      Clifford

       
    • Holly Benton

      Holly Benton - 2009-05-21

      Sorry Clifford,

      Just a very crazy time yesturday with that. Been trying off and on for serveral days. Did not want you to do my homework, just lost. Thank you for the time for posting.

      This is what I tried after reading your post.

      changing the for loop like this,
      for(i = 0; i < 10 && i > -1; i++)

      still only does the 10 variables.

      So I wrote a while loop,
      while(i <= 10 && i > -1)
      {
      for(i = 0; i < 10; i++) //saves the array
      scanf("%d", &pos[i]); //saves the array
      }

      went straight into my next list of stuff that was up.
      printf("1. Display the values in ascending order: \n");
      scanf("%d", &list);
      **

      without waiting for a list of numbers to be inputed.

      Looked over all my notes again, plus what you said finally came together.

      for(i = 0; i < NUM; ++i) //saves the array
      {
      scanf("%d", &temp); //saves the array
      if(temp <= -1) break;
      sum += temp;
      pos[i] = temp;
      }
      count = i; //count is not my array size

      Thank you for your time to post Clifford.

       
    • cpns

      cpns - 2009-05-22

      > for(i = 0; i < 10 && i > -1; i++)

      You are testing the index, not the input.

      > without waiting for a list of numbers to be inputed.

      When you use %d with scanf, it only extracts decimal digits from the stream, that leaves the <newline> in the buffer, which then satisfies all the following scanf() calls, but without and data that satisfies the %d so the <newline> is never extracted. See http://www.it.usyd.edu.au/~dasymond/mirror/c-faq/stdio/scanfprobs.html for more information.

      Your solution works, but breaking out of loops is often considered bad practice - I do for one it is a code maintenance nightmare. I would probably code it thus:

      int input ;
      for(i = 0; i < NUM && input != -1; i++ )
      {
      scanf("%d", &input);
      if( input != -1)
      {
      sum += temp;
      pos[i] = temp;
      }
      }

      count = i - 1 ;
      while( getchar() != '\n' ){ / do nothing/ } // flush line buffer

      Note the necessary adjustment for count because now i is incremented before the sentinel test. Also note the method of clearing the line buffer so that any subsequent input is not aborted early.

      Of course another solution is to remove the if() and write the sentinel to the array, that way you may not need to retain count at all, but your array will need to be NUM+1 long.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.