Hi,
We were given an assignment recently (not a graded assignment, just something to do if we wanted to study) and it was to write a program without using any arrays, that will take input of a max of 10 numbers, and then at the end, print out the largest and second largest number.
I have tried quite a few different ways of doing this and I just can't figure out what I'm doing wrong. It grabs and prints the largest successfully but no matter what I do, it always messes up on printing the second largest.
Code:
#include <stdio.h>
int main()
{
int counter = 1;
int largest = 0;
int number;
int slargest = 0;
while(counter <= 10){
printf("Please enter a positive integer: ");
scanf("%d", &number);
if(number >= 0){
if(number > largest){
largest = number;
}
if((number >= slargest)&& (number < largest)){
slargest = number;
}
}
counter++;
}
printf("The largest is: %d\n", largest);
printf("The second largest is: %d\n", slargest);
}
What am I doing wrong? Thanks in advance.