Answer:
The approach to the question and appropriate comments are given below in C++
Explanation:
Problem statement:
Write a program that will find and display the largest of a list of positive numbers entered by the user. The user should indicate that he/she has finished entering numbers by entering a 0.
Problem Analysis:
It can be completed in worst-case O(n) complexity, best case O(1) (if the first number is maxed element)
Program Design:
1. Start
2. Take the list of positive numbers for the user until he/she enter 0.
3. store the entered numbers in an array
4. find the max number from it.
5. Print the output
6. End
Program Code:
#include<iostream>
using namespace std;
int main(){
int num = 0, *array = NULL, i= 0, counter = 0, max = 0;
cout<<"Enter a list of positive numbers to find the maximum out of it and if you enter 0 that is the last number: \n";
array = new int;
/*Taking input from user until he/she enters 0*/
while(1){
cin>>num;
array[i] = num;
i++;counter++;
if(num == 0)
break;
}
cout<<"Print user input numbers: \n";
for(int i=0;i<counter;i++)
cout<<"list["<<i<<"] --> "<<array[i]<<"\n";
cout<<"\n";
/*Find max element*/
max = array[0];
for(int i=0;i<counter;i++){
if(array[i] > max)
max = array[i];
}
cout<<"Max number = "<<max<<"\n";
delete array;
return 0;
}