Hello,
I am new this is my first post and was hoping someone may be able to direct me to my error, what I am asked to do,
Write a C program that takes multiple integers from the user and finds the minimum value. The numbers
entered by the user are positive integers. When the user is done entering all the numbers, the user enters
the sentinel value of -1.
This is a sample output. Notice how the program prints a counter (1, 2, 3, ) to help the user keep track of
how many numbers were entered so far. Include this feature in your program.
I have tried different ways to get the result from while loops, do while, and if-else statements. We have not done any max/min problem in class and the book was kind of vague. We have not learned arrays yet in class and I can't figure a way to compare the integers the user inputs as it is an infinite(from what I am reading) amount aloud. The problem I am seeing is the loop works and stops with the sentinel value but the INT_MIN is using the sentinel value. I also have not addressed the input of negative numbers yet with the first program I wrote, maybe one of my problems.
Thank You for any help,
I have also tried this way as well, min value still comes back as -1 the sentinel value, and the else statement is being ignored. I am sure it is something silly but I am lost and confused at this point.Code:/*File:HW2Q3.c, A C program that takes multiple integers from the user and finds the minimum value. The numbers entered by the user are positive integers. When the user is done entering all the numbers, the user enters the sentinel value of -1.*/ /*header files*/ #include<stdio.h> #include<limits.h> /*****start program*****/ int main() { /*****declare variables*****/ int a;//user input int counter=0;//counts entries INT_MIN;//for minimum value int sentinel;//program stops asking for input a=4;//initialize start value so while loop starts sentinel=-1;//initialize sentinel value INT_MIN != sentinel;/*****tried to use this statement to not include -1 in minimum value (didn't work)*****/ printf("Finding the minimum...\n"); printf("Enter integers (-1 to finish)\n"); while(a!=-1)//start while loop to take user input and keep track of input { printf("%d integers: ",counter+1);//prints to screen scanf("%d",&a);//user input counter=counter + 1;//counts the entries from user /******I have tried this outside loop and same result as inside loop******/ if(a==-1)//start of if statement,(a) is compared to -1 print message printf("INT_MIN=%d\n",a);//prints minimum value entered }//end while loop system("pause");//pause program for user return 0;//terminate program } //end main
Code:/*File:HW2Q3.c, A C program that takes multiple integers from the user and finds the minimum value. The numbers entered by the user are positive integers. When the user is done entering all the numbers, the user enters the sentinel value of -1.*/ /*header files*/ #include<stdio.h> #include<limits.h> /*****start program*****/ int main() { /*****declare variables*****/ int a;//user input int counter=0;//counts entries INT_MIN;//for minimum value a=4; if(a>=0) { while(a!=-1)//start while loop to take user input and keep track of input { printf("%d Enter numbers: ",counter+1);//prints to screen scanf("%d",&a);//user input counter=counter + 1;//counts the entries from user if(a==-1) printf("INT_MIN=%d\n",a);//prints minimum value entered }//end while loop } else { printf("Use only positive numbers or enter -1 to finish input\n"); } system("pause");//pause program for user return 0;//terminate program } //end main