Reputation: 11
I need help, my program isn't working out, it keeps showing me 0 as the value! What is wrong with my code?
the program requires the user to enter the number of integers at the start.
#include <stdio.h>
int main(void)
{
int num;
int largest= 0;
int secondlargest= 0;
int thirdlargest=0;
printf("Please enter the number of integers:");
scanf("%d", &num);
if(num > 2)
{
scanf("%d",&largest);
while(--num > 0)
{
int tmp;
scanf("%d",&tmp);
if(tmp>largest)
largest=tmp;
if(tmp > secondlargest && tmp < largest)
secondlargest=tmp;
if(tmp > thirdlargest && tmp < secondlargest && tmp < largest)
thirdlargest=tmp;
}
printf("The third largest number is %d\n",thirdlargest);
}
else
{
printf("There is no third largest number.");
}
return 0;
}
here is my code, as written above! I need to submit it without using array! Any help would be deeply appreciated!
Upvotes: 0
Views: 2733
Reputation: 153498
Some pseudo code that simplifies and corrects your if()
structure.
int largest1 = INT_MIN;
int largest2 = INT_MIN;
int largest3 = INT_MIN;
int input = ...;
if (input > largest3) {
largest3 = input;
if (largest3 > largest2) {
swap(largest2, largest3);
if (largest2 > largest1) {
swap(largest1, largest2);
}
}
}
Upvotes: 0
Reputation: 777
When you found the first or second largest, then you need to "shift" the smaller ones (and you can replace your second and 3rd if by 'else if'). If you want to handle negatives input set the 3 largest to the smallest int. And use >= instead of > to handle duplicates.
int num;
int largest = INT_MIN;
int secondlargest = INT_MIN;
int thirdlargest = INT_MIN;
printf("Please enter the number of integers:");
scanf_s("%d", &num);
if (num > 2)
{
scanf_s("%d", &largest);
while (--num > 0)
{
int tmp;
scanf_s("%d", &tmp);
if (tmp >= largest){
thirdlargest = secondlargest;
secondlargest = largest;
largest = tmp;
}
else if (tmp >= secondlargest){
thirdlargest = secondlargest;
secondlargest = tmp;
}
else if (tmp > thirdlargest)
thirdlargest = tmp;
}
printf("The third largest number is %d\n", thirdlargest);
}
else
{
printf("There is no third largest number.");
}
return 0;
Upvotes: 2
Reputation: 41872
If you want to handle negatives too, set your three largest values to the minimum integer. Also use else if
instead of if
so you can use simple comparisons, instead of compound ones, followed by shifting your variables:
#include <stdio.h>
#include <limits.h>
int main(void)
{
int number;
printf("Please enter the number of integers: ");
(void) scanf("%d", &number);
if (number < 3)
{
printf("There is no third largest number.");
return 1;
}
int largest = INT_MIN;
int secondlargest = INT_MIN;
int thirdlargest = INT_MIN;
while (number-- > 0)
{
int temporary;
(void) scanf("%d", &temporary);
if (temporary > largest)
{
thirdlargest = secondlargest;
secondlargest = largest;
largest = temporary;
}
else if (temporary > secondlargest)
{
thirdlargest = secondlargest;
secondlargest = temporary;
}
else if (temporary > thirdlargest) {
thirdlargest = temporary;
}
}
printf("The third largest number is %d\n", thirdlargest);
return 0;
}
Upvotes: 1