Q BgQuestion:

Scholar
Karma Points: 205
Respect (70%):
posted by  Chrissy W on 7/23/2008 9:28:43 AM  |  status: Closed  

C++ Programming - for loop

Course Textbook Chapter Problem
Software Design C++: How to Program (5th) by Deitel, Deitel, Deitel N/A N/A
Question Details:
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
Bonus Point Alert! Earn +4 additional karma points for helping this annual member.

AAnswers:

Answer Question
Expert
Karma Points: 811
posted by sam_GT on 7/23/2008 12:43:43 PM  |  status: Live
Asker's Rating: Helpful   
Response Details:

#include<iostream.h>
#include<conio.h>
double GetMax(double x,double y)
{
  if ( x>y)
 return x;
 else
 return y;
}
int main()
{
double i;
double max=-9999,num;
clrscr();
cout<<"Enter 15 numbers:";
for(i=0;i<15;i++)
 {
 cin>>num;
 max=GetMax(num,max);
 }
 cout<<"The largest number is "<<max;
 getch();
return 0;
}

Mentor
Karma Points: 527
posted by Ashamalik on 7/23/2008 9:31:24 PM  |  status: Live
Asker's Rating: Helpful   
Response Details:
hiiiiiiii frnd,

#include<iostream.h>

#include<conio.h>

double GetMax(double x,double y)
{
  if ( x>y)
 return x;
 else
 return y;
}
int main()
{
double i;
double max=-9999,num;
clrscr();
cout<<"Enter 15 numbers:";
for(i=0;i<15;i++)
 {
 cin>>num;
 max=GetMax(num,max);
 }
 cout<<"The largest number is "<<max;
 getch();
return 0;
}



Answer Question
Ask New Question

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today »