- Dec 26, 1999
- 25,074
- 4
- 0
Yes...this is for a homework assignemnt, but I just need a little push in the right direction, this is the only way I learn this stuff, is to see how it's done right (I'm getting tutoring soon) But could you guys look at this real quick and see where this is going wrong? What the prog is supposed to do is this: Takes 10 digits and prints the two largest digits in the list. I'm not even a novice at this stuff, but I'm trying to get a grasp on what's what... Any help is GREATLY appreciated since I'm still trying to learn the mechanics of all of this. Anyways, here's the prog
/* Problem 3.24 - Eric Simpson */
/* Listing largest number in a given list */
#include <stdio.h>
int main()
{
int counter, number, largest, secondlargest; /* declaring
variables */
largest = 0 ; /* initializing variables to 0 */
secondlargest = 0;
counter = 0 ;
while ( counter < 10 ) { /*generates prompt*/
printf( "Enter number\n" ) ;
scanf ( "%d", &number ) ;
counter++; /* incrememnts counter by 1 */
if (number > largest ){ /* overwrites largest only if */
secondlargest = largest;
largest = number;} /* number is greater than
*/
else (number > secondlargest );
secondlargest = number;
}
printf( "The largest number listed is: %d\n", largest ) ; /*lists largest
number*/
printf( "The second largest number listed is: %d\n",
secondlargest);
return 0; /*terminates normally*/
}
And what happens in the end is it prints out the largest number twice (the last two printf's show the same thing), I think number is getting written to both of my largest variables somehow, but I can't see why... could someone give me some insight here? Thanks so much!
/* Problem 3.24 - Eric Simpson */
/* Listing largest number in a given list */
#include <stdio.h>
int main()
{
int counter, number, largest, secondlargest; /* declaring
variables */
largest = 0 ; /* initializing variables to 0 */
secondlargest = 0;
counter = 0 ;
while ( counter < 10 ) { /*generates prompt*/
printf( "Enter number\n" ) ;
scanf ( "%d", &number ) ;
counter++; /* incrememnts counter by 1 */
if (number > largest ){ /* overwrites largest only if */
secondlargest = largest;
largest = number;} /* number is greater than
*/
else (number > secondlargest );
secondlargest = number;
}
printf( "The largest number listed is: %d\n", largest ) ; /*lists largest
number*/
printf( "The second largest number listed is: %d\n",
secondlargest);
return 0; /*terminates normally*/
}
And what happens in the end is it prints out the largest number twice (the last two printf's show the same thing), I think number is getting written to both of my largest variables somehow, but I can't see why... could someone give me some insight here? Thanks so much!
