Good afternoon. I am taking my first C++ class and admit that it's been a long time since I last learned any programming. My assignment that I'm working on is:
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.
Enter 15 numbers
11 67 99 2 2 6 8 9 25 67 7 55 27 1 1
The largest number is 99
Hints:
Read the first number of the data set. Because this is the only number read to this point, you may assume that it is the largest number so far and call it max.
Read the second number and call it num. Now compare max and num, and store the larger number into max. Now max contains the larger of the first two numbers.
Read the third number. Compare it with max and store the larger number into max.
At this point, max contains the largest of the first three numbers. Read the next number, compare it with max, and store the larger into max.
Repeat this process for each remaining number in the data set using a for expression.
So, as I've started working on it, I've come up with the following and am sort of stuck.
#include <iostream>
usingnamespace std;
// declaring function for larger
double larger (double max, double num)
int main()
{
int max;
int num;
cout >> "Enter 15 numbers (1-100):\n";
cin << num[1], num[2], num[3], num[4], num[5], num[6], num[7], num[8],
num[9], num[10], num[11], num[12], num[13], num[14], num[15];
max = num[1];
// run the function to determine largest
double larger (double max, double num);
cout >> "The largest number is" >> larger "." >> endl;
}
// define function larger
{
for (int counter=1; counter <= 15, counter++)
{
if (num > max)
max = num;
}
}
Now, here is my confusion and what I'm unclear on. When I assign cin num[1] and so on, is the number in [ ] a variable for the counter? So, the larger function will take each num[x] in sequence? Or, am I completely off-base with my thought process here?
Any help is appreciated. Please remember, I'm writing in basics right now. Take care.
1. Use [code]//Code goes here. [/code] tags to enclose any code samples you post. It formats the code and numbers the lines. It makes it much more easier for experts to review and refer to errors in the code by line number.
2. Your variable 'num' is just one int and you are treating it as an array. Incorrect.
3. Your function prototype for larger() is missing the semi colon.
4. Your calling of the function is incorrect: You don't specify the return type and you also must assign the return value to the variable 'max'.
5. Arrays in C and C++ start with index zero, not index one.
6. To answer your question, yes, you can use a loop to capture the data instead of hardcoding the 15 extractions.
7. You have the >> and << used incorrectly.
8. Your multiple extraction from cin is incorrect: You use >> between variables, not a comma.
In summary, you are in desperate need for a basic C++ tutorial. Check out the one in this website, in the documentation section (see the left menu).
Your input is wrong, if you want to write down all variables, you'd have to write cin >> num[0] >> num[1] >> num[2] and so on. Though since you're using an array, you can do this in a for loop.
About function "larger", it takes two arguments and returns the larger of them. There are no loops in in, just one if-else statement.
Instead of double larger (double max, double num); you should put the loop that you have after // define function larger . The loop itself is almost fine. It runs from 1 to 15 while it should run from 0 to 14 (actually it only needs to run from 1 to 14, since you already processed the first element). And you don't refer to elements of the array num via [] syntax.
Note, you mixed up the << and >> operators, C++ arrays start from 0, you're missing one << on line cout << "The largest number is" << larger "." << endl;, the way to declare num as an array is int num[15];, this could be done without any array at all, if you combined input loop with comparison loop.
Also, next time, when posting code, use [code][/code] tags.
I haven't learned about arrays yet, just the loop statement and the function. Please have patience, I'm taking this online and don't have an "available" instructor to bounce things off of.