1

I'm fairly new to coding and am currently learning C. In my C programming class, my instructor gave us the assignment of writing a program that uses a function which inputs five integers and prints the largest. The program is fairly simple even for me, but I'm facing some problems and was hoping to get some advice.

#include <stdio.h>

int largest(int x);

int main(void) {
    int integer1;

    largest(integer1);

    return 0;
}

int largest(int x) {
    int i;

    for (i = 0; i < 5; i++) {
        printf("Enter an integer: ");
        scanf_s("%d", &x);
    }

    return x;
}

This is the code that I have written. The main problem that I am having is that in my main method, the IDE tells me to initialize the value of integer1. However, I'm not really sure how to do that because I'm supposed to input the value within the largest() method via the scanf_s function. How may I solve this?

2
  • writing a program that uses a function which inputs five integers and prints the largest....umm, wher's that part? Commented Apr 10, 2017 at 11:06
  • What's the parameter supposed to do? You take an integer as parameter, then inside the function ask for user input. This doesn't make any sense, which is why you can't get it to compile. Commented Apr 10, 2017 at 11:07

2 Answers 2

2

The problem is here, the warning message is to warn you about the potential pitfall of using the value of an uninitialized automatic local variable. You made the call like

  largest(integer1);

but you ignore the return value, so the integer1 remains uninitialized.

Remember, in view of largest(), x is a local copy of the actual argument passed to that function, any changes made to x won't be reflecting to the caller.

That said, your code is nowhere near your requirement, sorry to say. A brief idea to get there would be

  • Create a function.
  • Create a variable (say, result) and initialize with minimum possible integer value, INT_MIN
  • Loop over 5 times, take user input, compare to the result value, if entered value found greater, store that into result, continue otherwise.
  • return result.
Sign up to request clarification or add additional context in comments.

Comments

1

I know that normally help for assignments shouldn't be given but I have to say that you might need to rethink what you want to do.

You are inputting an integer to the function named largest. But why are you only inputting a single integer to a function that should return the largest value. You can't do much with a single number in that case.

You should instead be inputting say an array of 5 values(as said in your assignment) to the function and let it return the largest.

The order would then be:

  1. Read 5 values and save to an array
  2. Call the function largest with the array as input
  3. Let the function do it's work and return the largest value
  4. Do what ever you want with the largest value

But if you only want to remove the warning simply type

int integer1 = 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.