Write a program that reads in n floating-point numbers and prints the largest of
ID: 3818291 • Letter: W
Question
Write a program that reads in n floating-point numbers and prints the largest of them. (Largest means most positive. So 5.1 is larger than -12.44.) The input and output should be exactly: How many numbers do you have? [USER ENTERS A POSITIVE INTEGER] Input number (count 1): [USER ENTERS A DECIMAL NUMBER] Input number (count 2): [USER ENTERS A DECIMAL NUMBER] Input number (count X): [USER ENTERS A DECIMAL NUMBER] The maximum is X. Assume the first input is indeed a positive integer. You may not use any libraries aside from iostream and string. Name your file my_max.cpp.Explanation / Answer
//Program to print maximum among n floating point numbers
#include <iostream>
using namespace std;
int main() {
int num;
float inputs;
float max=0;// Initialize it to 0 in the beginning
std::cout << "How many numbers do you have?" << std::endl;
std::cin >> num;
//using for loop;
for(int i=0;i<num;i++){
cin>>inputs;
if(inputs>max)
max=inputs;
}
cout<<"The maximum is "<<max;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.