113k views
1 vote
(Complete the Problem-Solving discussion in Word for Programming Challenge 2 on page 404. Your Problem-Solving discussion should include Problem Statement, Problem Analysis, Program Design, Program Code and Program Test in Words Document.) Create 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.

User Rakmoh
by
5.1k points

1 Answer

1 vote

Answer:

The approach to the question and appropriate comments are given below in C++

Step-by-step 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: \\";

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: \\";

for(int i=0;i<counter;i++)

cout<<"list["<<i<<"] --> "<<array[i]<<"\\";

cout<<"\\";

/*Find max element*/

max = array[0];

for(int i=0;i<counter;i++){

if(array[i] > max)

max = array[i];

}

cout<<"Max number = "<<max<<"\\";

delete array;

return 0;

}

User CinCout
by
5.1k points