# Finding smallest and second smallest integer and largest and second largest integer

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-29-2009
youngvito
Finding smallest and second smallest integer and largest and second largest integer
I am writing a progam that finds the smallest and second smallest integer and also the largest and second to largest integer in a list of 20 integer. The program is to read them from a file. I figured out how to find the first smallest and first largest number but i have no idea how to find the second smallest and second largest number. Can someone please help me.

Code:

```#include <stdio.h> int main (void) {         int counter;         int num1;         int num2;         int largest = 0;         int smallest;                 for (counter = 0; counter < 20; counter++);         {                 scanf("%d", &num1);                 if(num1 < smallest)                         smallest = num1;                         }         for (counter = 0; counter < 20; counter++)         {                 scanf("%d", &num2);                 if(num2 > largest)                         largest = num2;         }         printf("\nSmallest: %d", smallest);         printf("\nLargest: %d", largest);          return 0; }```
• 05-29-2009
tabstop
Why should it be different than before? Keep track of the current two smallest numbers as you go through, and update as necessary.
• 05-29-2009
youngvito
I still cant get it to work. I dont know whats wrong with it.

Code:

```#include <stdio.h> int main (void) {         int counter;         int num1;         int num2;         int num3;         int num4;         int largest = 0;         int slargest = 0;         int ssmallest;         int smallest;                 for (counter = 0; counter < 20; counter++);         {                 scanf("%d", &num1);                 if(num1 < smallest)                         smallest = num1;                         }         for (counter = 0; counter < 20; counter++)         {                 scanf("%d", &num2);                 if(num2 > largest)                         largest = num2;         }         for (counter = 0; counter < 20; counter++)         {                 scanf("%d", &num3);                 if (num3 < ssmallest && ssmallest > smallest)                         ssmallest = num3;         }         for (counter = 0; counter < 20; counter++)         {                 scanf("%d", &num4);                 if (num4 > slargest && slargest < largest)                         slargest = num4;         }         printf("\nSmallest: %d", smallest);         printf("\nSecond Smallest: %d", ssmallest);         printf("\nLargest: %d", largest);         printf("\nSecond largest: %d", slargest);          return 0; }```
• 05-29-2009
tabstop
You have to do it at the same time.
• 05-29-2009
slingerland3g
Think about what you are setting num1 and smallest to, in your first for loop. You are not using the counter variable at all which you should be. Also this can all be done within one for loop as well and not 4 separate loops. Keep track of your smallest and largest as tabstop mentioned and make use of temp variable to hold your values.

Write out some pseudocode first on paper and map out the logic of what should be occuring.
• 05-29-2009
youngvito
i wrote the algorithme and put it into one loop but i still cant get it to work. If you could give me some more advice i would greatly appreciate it.

Code:

```#include <stdio.h> int main (void) {         int counter;         int num1;         int num2;         int num3;         int num4;         int largest;         int slargest;         int ssmallest;         int smallest;                 for (counter = 0; counter < 20; counter++);         {                 scanf("%d", &num1);                 if(num1 < smallest)                         smallest = num1;                 else if (largest > num1)                         largest = num1;                                         if (ssmallest < num1 && ssmallest > smallest)                         ssmallest = num1;                 else if (slargest > num1 && slargest < largest)                         slargest = num1;         }                 printf("\nSmallest: %d", smallest);         printf("\nSecond Smallest: %d", ssmallest);         printf("\nLargest: %d", largest);         printf("\nSecond largest: %d", slargest);          return 0; }```
• 05-29-2009
slingerland3g
At a quick glance, you should be setting

Code:

``` largest < num1  then   largest = num1```
If the prior largest value was in a temporary variable, say largest_temp, then set

Code:

``` slargest = largest_temp;```

This is a bit if rough psedocode, but I hope you get the idea. Only replace your ssmallest or slargest only if the smallest or largest values were changed.
• 05-29-2009
youngvito
im sorry can you give a little bit more advice on how to code it. I have very little idea of what you are talking about.
• 05-29-2009
slingerland3g
I would encourage you to set all your variables to 0 as well. Perhaps a while loop would be more preferred over a for loop for your user inputs.

Code:

``` if(num1 < smallest) {       smallest = num1;       ssmallest = smallest; } else if (largest < num1) {       largest = num1;       slargest = largest; }```
• 05-29-2009
youngvito
for some reason my program stil doesnt work, it wont execute, it shows a problem. can you please tell me how to fix the problem.
Code:

```#include <stdio.h> int main (void) {         int counter = 0;         int num1;         int largest = 0;         int slargest = 0;         int ssmallest = 0;         int smallest = 0;                 while (counter < 20);         {                 scanf("%d", &num1);                 if (num1 < smallest)                 {                         smallest = num1;                         ssmallest = smallest;             }                 else if (largest > num1)                 {                         largest = num1;                         slargest = largest;                 }                 counter++;         }                 printf("\nSmallest: %d", smallest);         printf("\nSecond Smallest: %d", ssmallest);         printf("\nLargest: %d", largest);         printf("\nSecond largest: %d", slargest);          return 0; }```
• 05-29-2009
quzah
You should set 'smallest' to something large. That way when you enter a number, it has a chance to store it. Otherwise, if you enter numbers 1,2,3,4, it's just going to still have your initial zero there. Also, remove the 'else'. Also, you should be setting up your seconds before you set your ests. Otherwise:

est = 5
nd = est

Means that nd = 5, so you're just copying the same number twice every time, instead of keeping track what used to be there. PLUS, consider this:

est = 5
nd = 0
num = 3

Num is not greater than the biggest, but is greater than your second, but you'll never actually store it, because the way your check was done.

Quzah.
• 05-29-2009
slingerland3g
quzah, totally true, in some tests I ran into that very issue.
• 05-29-2009
youngvito
Sorry i still cant get the program to work. Can you tell me where the problem is please.

Code:

```#include <stdio.h> int main (void) {         int counter = 0;         int num1;         int largest = 0;         int slargest = 0;         int ssmallest = 0;         int smallest = 100;                 while (counter < 20);         {                 scanf("%d", &num1);                 if (num1 < smallest)                 {                         smallest = num1;                         ssmallest = smallest;             }                 if (num1 > largest)                 {                         largest = num1;                         slargest = largest;                 }                 counter++;         }                 printf("\nSmallest: %d", smallest);         printf("\nSecond Smallest: %d", ssmallest);         printf("\nLargest: %d", largest);         printf("\nSecond largest: %d", slargest);          return 0; }```
• 05-29-2009
slingerland3g
Well remove that oddly placed semicolon after the while statement. You may want to still work out the logic of your variable assignments, as what quzah has stated,as your smallest may still to always being 100.
• 05-29-2009
ursula
what if you swap these lines around:
Code:

```ssmallest = smallest; smallest = num1;```
Code:

```slargest = largest; largest = num1;```
you want slargest and ssmallest to save the values before you change largest and smallest respectively.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last