0

I am trying to figure out how I can put this code in a function, so I can call it back in my main, but nothing is working. I am quite new to programming, thanks in advance! (So the goal is to let the user input numbers and the highest one gets picked out).

for (z = 0; z < 10; z++) {
    cin >> array[z];


    for (int i = 0; i < 10; i++)
    {
        if (array[i] > temp)
            temp = array[i];
    }

}
4
  • 3
    "Nothing is working" is not a useful problem description. Besides, the shown code in this question fails to meet the requirements for a minimal reproducible example; as such no answer will be possible. Commented Oct 7, 2016 at 2:36
  • You've barely described your problem. You need to explicitly state what your problem is. Commented Oct 7, 2016 at 2:39
  • Dear Sam, I tried putting it in a function but I get errors with either the function notation or it wont calculate the highest number. And for your last criteria, I dont believe there is a need to expand my code by declaring the variables. The problem is that this piece of code will not succesfully execute once you put it in a function. Sorry for the bad problem description my English sucks. Thanks for the fast replies. Commented Oct 7, 2016 at 2:48
  • temp starts out as what ?? (see why you provide MCVE's? to avoid having to ask questions like that). And do you really want to march a comparison loop through elements you haven't even loaded yet ? Commented Oct 7, 2016 at 3:00

1 Answer 1

3

it looks like this should be two different "for" loops

you forgot to declare z, temp and array[]

EDIT: don't forget if you initialize array[] within the function you need to include the aggregate

try this:

int z, i;
int temp = 0;
int array[10] = {0,0,0,0,0,0,0,0,0,0};

for (z = 0; z < 10; z++) {
    cin >> array[z];
}


for (i = 0; i < 10; i++)
{
    if (array[i] > temp)
        temp = array[i];
}

this function would take no arguments, so you would declare it and call it as "function()"

SECOND EDIT: i was actually able to get a function like this to work, and it would look like this:

#include <iostream>

using namespace std;

void function();

int main() {
    cout << "enter 10 numbers: " << endl;

    function();

    return 0;
}

void function () {
    int z, i;
    int temp = 0;
    int array[10] = {0,0,0,0,0,0,0,0,0,0};

    for (z = 0; z < 10; z++) {
    cin >> array[z];
    }


    for (i = 0; i < 10; i++) {
        if (array[i] > temp)
            temp = array[i];
    }

    cout << "your largest number is: " << temp;
}

good luck out there man

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god! Thank you so much for this! My mistake was that I tried to put the array in the function parameters. Thanks once again.

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.