开发者

Finding the largest and smallest integers in C

As I mentioned in another question I've been teaching myself C out of of K.N King's C Programming: A Modern Approach (2ndEdn开发者_开发技巧).

I'm enjoying it, but am hoping to post the odd question here for advice if appropriate because unfortunately I don't have a tutor and some bits raise more questions then they answer!

I'm up to a question that asks me to write a program that finds the largest and smallest of four integers entered by the user... I've come up with a way to find the largest, but for the life of me can't work out how to get the smallest out. The question says that four if statements should be sufficient. Math is not my forte, I'd appreciate any advice!

#include <stdio.h>

int main(int argc, const char *argv[])
{

    int one, two, three, four;

    printf("Enter four integers: ");

    scanf("%d %d %d %d", &one, &two, &three, &four);

    if (four > three && four > two && four > one)
            printf("Largest: %d", four);
    else if (three > four && three > two && three > one)
            printf("Largest: %d", three);
    else if (two > three && two > four && two > one)
            printf("Largest: %d", two);
    else
            printf("Largest: %d", one);

    return 0;

}

I'm trying to keep it simple, as I'm only up to chapter 5 of 27!

Cheers Andrew


if (first > second)
    swap(&first, &second);
if (third > fourth)
    swap(&third, &fourth);
if (first > third)
    swap(&first, &third);
if (second > fourth)
    swap(&second, &fourth);

printf("Smallest: %d\n", first);
printf("Largest: %d\n", fourth);

The implementation of the swap() function is left as exercise.


another way would be as such:

int one, two, three, four;  
//Assign values to the four variables;  
int largest, smallest;  
largest = max(max(max(one, two), three), four);  
smallest = min(min(min(one, two), three), four);  

Not a single if statement needed ;)


Within the context of the chapter:

  if (a > b) {
    max = a;
    min = b;
  } else {
    max = b;
    min = a;
  }
  if (c > d) {
    max2 = c;
    min2 = d;
  } else {
    max2 = d;
    min2 = c;
  }
  if (max < max2) {
    max = max2;
  }
  if (min > min2) {
    min = min2;
  }


I have that same book, and I'll admit it, that program gave me quite a headache. It's a little tricky for a beginner programmer.

First, you compare the first pair of integers (a and b in the code), and store local min and max somewhere. Do the same thing with the second pair. Then compare local minimums to get the global minimum, and do the same thing with maximums. No more than four if's.

#include <stdio.h>

int main (void)
{
   int a, b, c, d, min1, max1, min2, max2, min, max;
   scanf ("%d %d %d %d", &a, &b, &c, &d);
   if (a > b) 
   {
      max1 = a;
      min1 = b;
   }
   else 
   {
      max1 = b; 
      min1 = a;
   }
   if (c > d) 
   {
      max2 = c;
      min2 = d;
   }
   else 
   {
      max2 = d;
      min2 = c;
   }
   if (min1 < min2) min = min1;
   else min = min2;
   if (max1 > max2) max = max1;
   else max = max2;
   printf ("%d %d", max, min);
   return 0;
}

There are better ways to solve this, some of them are shown here, but the book covers them in the later chapters.


I'm going through K.N. King's "C Programming: A Modern Approach, Second Edition" too. Below is my solution for this thread's original question.

Note that I'm using only C concepts introduced up through Chapter 5 of the book. The original programming problem comes from Chapter 5, Programming Problem 7.

 21 #include <stdio.h>
 22 
 23 int main(void)
 24 {
 25     int i1, i2, i3, i4, large_1, small_1, large_2, small_2,
 26         largest, smallest;
 27 
 28     printf("\nEnter four integers: ");
 29     scanf("%d %d %d %d", &i1, &i2, &i3, &i4);
 30 
 31     if (i1 < i2) {
 32         small_1 = i1;
 33         large_1 = i2;
 34     } else {
 35         small_1 = i2;
 36         large_1 = i1;
 37     }
 38 
 39     if (i3 < i4) {
 40         small_2 = i3;
 41         large_2 = i4;
 42     } else {
 43         small_2 = i4;
 44         large_2 = i3;
 45     }
 46 
 47     if (large_1 < large_2)
 48         largest = large_2;
 49     else
 50         largest = large_1;
 51 
 52     if (small_1 < small_2)
 53         smallest = small_1;
 54     else
 55         smallest = small_2;
 56 
 57     printf("Largest: %d\n", largest);
 58     printf("Smallest: %d\n\n", smallest);
 59 
 60     return 0;
 61 }


#include <stdio.h>
/*
SOLUTION 1

int main(void) {
    int a1,a2,a3,a4,max,min,max1,min1,max2,min2;
    printf("Enter four integers : ");
    scanf("%d %d %d %d",&a1,&a2,&a3,&a4);
    if (a1 > a2) {
        max1 = a1;
        min1 = a2;
    } else {
        max1 = a2;
        min1 = a1;
    }
    if (a3 > a4) {
        max2 = a3;
        min2 = a4;
    } else {
        max2 = a4;
        min2 = a3;
    }
    if (max1 > max2)
        max = max1;
    else
        max = max2;
    if (min1 < min2)
        min = min1;
    else
        min = min2;
    printf("Largest : %d\n",max);
    printf("Smallest : %d\n",min);
}
*/

/*
SOLUTION 2
*/

int main(void) {
    int a1,a2,a3,a4;
    printf("Enter four integers : ");
    scanf("%d %d %d %d",&a1,&a2,&a3,&a4);
    if (a1 > a2) {
        int temp1 = a1; a1 = a2; a2 = temp1; // Swap the numbers (a1 to contain smallest number)
    }
    if (a3 > a4) {
        int temp2 = a3; a3 = a4; a4 = temp2; // Swap the numbers (a1 to contain smallest number)
    }
    if (a1 > a3) {
        int temp3 = a1; a1 = a3; a3 = temp3; // Swap the numbers (a1 to contain smallest number)
    }
    if (a2 > a4) {
        int temp4 = a2; a2 = a4; a4 = temp4; // Swap the numbers (a1 to contain smallest number)    
    }
    printf("Largest : %d\n",a4);
    printf("Smallest : %d\n",a1);
}


printf("Largest: %d\n",(one>two ? one:two)>(three>four ? three:four)
                ? (one>two ? one:two):(three>four ? three:four));
printf("Smallest: %d",(one<two ? one:two)<(three<four ? three:four)
                ? (one<two ? one:two):(three<four ? three:four));


I managed to solve this problem in even fewer than 4 if statements, here is my solution:

#include<stdio.h>

int main(void){
    int no1, no2, no3, no4;
    int max1, max2, max3, min1, min2, min3;

    printf("Enter four integers:");
    scanf_s("%d %d %d %d", &no1, &no2, &no3, &no4);

    if(no1 > no2 || no1 < no2 && no3 > no4 || no3 < no4){
        no1 > no2 ? (max1=no1) : (max1=no2);
        no1 > no2 ? (min1=no2) : (min1=no1);
        no3 > no4 ? (max2=no3) : (max2=no4);
        no3 > no4 ? (min2=no4) : (min2=no3);
    }
    if(max1 > max2 || max1 < max2 && min1 > min2 || min1 < min2){
        max1 > max2 ? (max3=max1) : (max3=max2);
        min1 > min2 ? (min3=min2) : (min3=min1);
    }

    printf("The largest number is %d \n", max3);
    printf("The smallest number is %d \n", min3);
}

However I don't know if I am doing the right things. At least I think it will help someone :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜