Home > Software engineering >  Sorting array elements to find the largest and smallest number in C
Sorting array elements to find the largest and smallest number in C

Time:04-21

I'm solving this problem where I need to give some inputs, find the largest and smallest among them. Here is the problem statement

Ivan Vasilyevich came to the market and decided to buy two watermelons: one for himself and another for the wife's mother. It is clear to choose for himself the heaviest watermelon, and for mother-in-law the lightest. But there is one problem: there are many watermelons and he does not know how to choose the lightest and the heaviest one. Help him!

Input

The first line contains the number of watermelons n (n ≤ 30000). The second line contains n numbers, each number is a mass of corresponding watermelon. All weights of watermelons are positive integers and do not exceed 30000.

Output

Print two numbers: the weight of watermelon that Ivan Vasilyevich will buy for his mother-in-law and the weight of watermelon that he will buy himself, or print the message "Ooops!" (without quotes), if someone left without watermelon

Here's my code

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n, i, w[30000], gw, lw;

    scanf("%d", &n);
    
    n = abs(n);
    
    for (i = 0; i < n; i  )
    {
        scanf("%d", &w[i]);
    }

    if (n >= 2)
    {
    
        for (i = 0; i < n; i  )
        {
            if (w[0] < w[i])
                w[0] = w[i];
            gw = w[0];
        }
        for (i = 0; i < n; i  )
        {
            if (w[0] > w[i])
                w[0] = w[i];
            lw = w[0];
        }
    printf("%d %d", lw, gw);
    
    return 0;
    }
    else
    {
        printf("Ooops!");
        return 0;
    }
    
}

I'm getting wrong answer(96/100). What am I getting wrong?

CodePudding user response:

You do not need to allocate space for an array of 30k integers to find the min and max weights entered.

First, initialize min and max weights to the first integer entered and then update min and max accordingly as you read more weights. Use the variable cur (an integer) to store the last integer (i.e. weight) read.

That way, you do it all in one pass, rather than in multiple loops.

If you use scanf, it is good practice to check it's return value. For reference (from the C99 standard):

The scanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

In our case, when our scanf call is of the form scanf("%d", &a) where a is some int, we expect the call scanf("%d", &a) to return 1.

#include <stdio.h>
#include <stdlib.h>

#define MAX_WAT 30000    /* maximum number of watermelons */

int main(void) {
    int n, i, min, max, cur;

    /* prompt user for number of watermelons */
    printf("Enter number of watermelons: ");
    /* read integer, checking return value of scanf as expected */
    if (scanf("%d", &n) != 1) {
        printf("error in scanf\n");
        exit(EXIT_FAILURE);
    }

    if (n > MAX_WAT) {
        printf("Please enter less than %d watermelons.\n", MAX_WAT);
        return 0;
    }

    /* if zero or one watermelons, at least one person leaves without */
    if (n <= 1) {
        printf("Ooops!\n");
        return 0;
    }

    /* initialize min, max to first integer and update
       min, max accordingly as new weights are read    */
    printf("Enter weights of %d watermelons: ", n);
    scanf("%d", &cur);
    min = max = cur;
    for (i = 1; i < n; i  ) {
        if (scanf("%d", &cur) != 1) {
            printf("error in scanf\n");
            exit(EXIT_FAILURE);
        }

        if (cur < min)
            min = cur;
        if (cur > max)
            max = cur;
    }
    printf("Ivan Vasilyevich: %d\nMother: %d\n", max, min);

    return 0;
}

Example Session 1:

Enter number of watermelons: 5
Enter weights of 5 watermelons: 2 5 1 9 10
Ivan Vasilyevich: 10
Mother: 1

Example Session 2:

Enter number of watermelons: 1
Ooops!

Example Session 3:

Enter number of watermelons: 30001
Please enter less than 30000 watermelons.

CodePudding user response:

  • do not modify your original array
  • initialize your gw and lw
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n, i, w[30000], gw, lw;
    scanf("%d", &n);
  
    n = abs(n);
    
    for (i = 0; i < n; i  )
    {
        scanf("%d", &w[i]);
    }

    if (n >= 2)
    {
        
        gw = w[0];
        for (i = 0; i < n; i  )
        {
            if (gw < w[i]) gw = w[i];
        }
        lw = w[0];
        for (i = 0; i < n; i  )
        {
            if (lw > w[i]) lw = w[i];
        }
    printf("%d %d", lw, gw);
    
    return 0;
    }
    else
    {
        printf("Ooops!");
        return 0;
    }    
}
  • Related