0

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?

4
  • 4
    Now would be a good time to learn to debug your own code. Run your program in a debugger and step thru it to trace the execution and variable values. How to debug small programs Commented Apr 21, 2022 at 10:51
  • 3
    You know that there is no need to sort anything, just have a maximum and minimum number stored somewhere. There is also no need to store the whole array. Commented Apr 21, 2022 at 10:57
  • 2
    Your algorithm will make you loose the first value. The logic is also reversed, with lw being the greatest value and gw being the lowest. Commented Apr 21, 2022 at 10:57
  • 1
    Always check the return value of scanf() before you use the input. Commented Apr 21, 2022 at 10:57

2 Answers 2

1

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.

While it is good practice to check the return value, it is not absolutely necessary. If this is a program for one of your classes and you have never worked with the return value of scanf, you could remove all the checks for the return value below and the program should function the same. That said, it would show great initiative if you do check for the return value and reference the C standard in your justification for checking it (as the return value provides very useful information).

#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.
Sign up to request clarification or add additional context in comments.

Comments

0
  • 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;
    }    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.