Any C Gurus willing to help?? Need a little help with a program

geno

Lifer
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!
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
#include <iostream.h>

int main()
{
int counter=0; // Variables
int number=0;
int number2=0;
int largest=0
int secondlargest=0;


for(int x; x<10; x++)
cerr<<"Please enter a number ";
cin>>number;

if (number > largest )
{
secondlargest = largest;
largest = number;
else (number > secondlargest );
secondlargest = number2;
} // replace number


cout<<"The largest number listed is: <<number;
cout<< endl;
cout<<"The second largest number listed is: <<number2;
return 0;

} // main


This really could be done better with functions and a simple array with a sort, but I don't want to make all that since you used a simple method.
Good luck, I havent tried to even compile this, it is just a rough sketch


Edit: this is written in accordance with AP c++ so it may look messed up, sorry.
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Can you give an example of the inputs to the function? The way it is now if you have the same large value entered twice it will end up in both the #1 and #2 spots (the first time you enter the large number it won't pass the first test and will be assigned to #1, the second time it will pass the first test and fall down and not pass the second test)
Bill
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
some more hints:
- consider the input of all negative numbers. according to your program, your largest numbers will be zero. is that correct?
- what bsobel said: what if the two largest numbers are the same numbers?
- there are more to the relational operators than ==, <, and >. try to cross breed them ;)

-941-