Finding the largest of 15 numbers

Hello,
I need help with this assignment. I saw similar topics, but the codes were not right. This is my first assignment in C++:
Create a program to determine the largest number out of 15 numbers entered (numbers entered one at a time). This should be done in a function using this prototype:
double larger (double x, double y);
Make sure you use a for loop expression inside your function.

I don'y understand why I need the prototype. The program runs the first two lines and than stops running.
Thank you in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>
using namespace std;

double larger (double, double);

int main()
{
double max, num;
cout<<"Please, enter 15 numbers, one at a time, and the program will return the largest number.\n";
cout<<"Enter the first number: ";
cin>>"max";
for(int i=1; i<=14; i++)
{
cout<<"\nEnter another number: ";
cin>>"num";
max=larger(max,num);
}
cout<<"The largest number is: "<<larger;
return 0;
}

double larger (double x, double y)
{
if(x>y)
return x;
else return y;
}
Okay, I can tell you are new. First of all, you don't need quotation marks around your variables when you use cin.
cin>>"max"; is wrong.
it needs to be just
cin>>max;
fix cin>>"num"; too
Secondly, I don't think you ever defined larger. I'm not sure if you can put a function name as a variable at the same time. It should be
cout << "The largest number is: " << max;
You defined max as larger(max,num).
Which means, max is going to be equal to the returned value from your function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

double larger( double a, double b ) ; // declare the function
                                      // inform the compiler that there is a function named 'larger'
                                      // which accepts two parameters of type double and returns a double

int main()
{
    const int N = 15 ;

    std::cout << "Please, enter " << N << " numbers, one at a time,\n"
                 "and the program will return the largest number.\n";

    double number ;

    std::cout << "Enter the first number: ";
    std::cin >> number ;

    double largest_so_far = number ;

    for( int i = 1 ; i < N ; ++i ) // loops N-1 times i == 1, 2, 3 ... N-1
    {
        std::cout << "Enter another number: " ;
        std::cin >> number ;

        largest_so_far = larger( largest_so_far, number ) ; // fine: the compiler won't baulk saying that it has absolutely no idea
                                                            // what 'larger' is; it has seen the earlier declaration; it knows that
                                                            // the name 'larger' is the name of a function which accepts
                                                            // two parameters of type double and returns a double
    }

    std::cout << "The largest number is: " << largest_so_far << '\n' ;
}

double larger( double a, double b ) { return a<b ? b : a ; } // define the function:
                                                             // this is the function that was declared earlier 
JLBorges can you help me out? http://www.cplusplus.com/forum/general/182663/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

//Function Prototype.
double larger(double, double);

int main()
{
    const int SIZE{ 15 }; // Determine the size for the for loop.
    double largestNum{ 0.0 }, inputNum{ 0.0 }; 

    std::cout << "Please, enter 15 numbers.\n";
    largestNum = larger(inputNum, SIZE); //Call the function and return the largest number.

    std::cout << "The Largest Number is " << largestNum << "\n";

    return 0;
}

/*
Run the a for loop with two function parameters, inputNum
and SIZE. The inputNum is used for the user to input a number
inside the for loop. It uses an if statement to compare and
determine that the number inputted by the user is larger than
the variable maxNumber, which is set to the lowest value and
the user would not use to compare. The SIZE parameter is used
to set the max value for the for loop (i.e. 15).
*/
double larger(double inputNum, double SIZE)
{
    double maxNumber{ -9999 };

    for (int count = 0; count < SIZE; count++)
    {
	   std::cout << "Number #" << count + 1 << ": ";
	   std::cin >> inputNum;

	   if (inputNum > maxNumber)
		  maxNumber = inputNum;
    }

    return maxNumber;
}
Last edited on
The reason you need the function prototype is so that the compiler will recognize the function and the valid data types for its arguments when it is called. This allows you to define the function after main. Function prototypes are not 'necessary' as functions can be declare 'inline'. Main() is written as an inline function.
Thank you so much. It works just fine.
Topic archived. No new replies allowed.