Need to type a C program to input an unspecified number of integers and to determine and print the two smallest values. Im only allowed to use this statements: sentinel, while, if, if..else.
Need to type a C program to input an unspecified number of integers and to determine and print the two smallest values. Im only allowed to use this statements: sentinel, while, if, if..else.
So write some code and post back (and include the code) if you have problems.
To confirm: this thread is about C, not C++?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This is about C
Thats the problem with the code. I get really confused with the while and if..else and doesnt work at all
Thread moved to C programming forum.Originally Posted by edwinhndz17
Still, show what you tried. Perhaps we can help you to fix it.Originally Posted by edwinhndz17
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So *post* the code so that we can see where you are at on it.Thats the problem with the code. I get really confused with the while and if..else and doesnt work at all
Here it is. I know is wrong but i dont know how to fix it. Im studying in the university, Computer Engineering, im in second year and this is new to me. And my proffesor really stink explaining this to me.
Code:#include <stdio.h> #include <stdlib.h> /* header for function system */ int main( void ) { int counter, smallest, ssmallest; while( counter=!0){ printf( "Enter integer: "); scanf( "%d", &smallest); } if( counter<smallest){ scanf( "%d", &smallest); } else if(smallest<ssmallest){ scanf( "%d"); } else if(secondsmallest<smallest){ printf( "%d are the smallest numbers",x, y); scanf( "%d,%d", &x, &y); } system ("pause"); return 0; }
Im trying to use it with a sentinel, because i want to input an unspecific number of integers, but it doesnt work at all
First of all, it's imperative that you learn to indent you code properly. Here's an example:
So fix those errors first. Now then, the idea here is that most of the work will be done inside of the loop. The basic flow should be:Code:#include <stdio.h> #include <stdlib.h> /* header for function system */ int main( void ) { int counter, smallest, ssmallest; while( counter =! 0 ) // should be != { printf( "Enter integer: " ); scanf( "%d", &smallest ); } if( counter < smallest) { scanf( "%d", &smallest ); } else if( smallest < ssmallest ) { /* careful - every format must be matched with the address of some variable */ scanf( "%d" ); } else if( secondsmallest < smallest ) // secondsmallest not declared { printf( "%d are the smallest numbers",x, y ); // x, y not declared scanf( "%d,%d", &x, &y ); } system( "pause" ); return 0; }
1) declare 'smallest', set to INT_MIN
2) declare an input variable, say 'input'
3) loop
3b) read 'input'
3c) if 'input' equals some sentinal value (eg: 0, -1), break out of the loop
3d) if 'input' is less than 'smallest' then reassign 'smallest'
4) now outside of loop, print 'smallest'
EDIT: So I just realized that you really do need to find the *two* smallest numbers. Still, get it working with one number first before moving forward with the advanced case
Last edited by Sebastiani; 09-12-2009 at 11:40 AM.
Can u fix the code for me? That way i get confused. I will be very thankful if u fix the code and then reply it back. THANKS
Here's a hint: You don't actually do anything inside your loop with your numbers. You just read one, then read another, then another... until you enter zero, which has overwritten whatever it was you did right before that anyway.
Quzah.
Hope is the first step on the road to disappointment.