Question

In: Computer Science

Do not use arrays and code a program that reads a sequence of positive integers from...

Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not use arrays and use C++

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main()
{
//variable declaration
int num, largest, smallest, largOcc=0, smallOcc=0;
  
//get user input
cout<<"Enter numbers: "<<endl;
cin>>num;
  
if(num==0)
{
return 0;
}
else if(num<0)
{
cout<<"Please enter only positive integer."<<endl;
}
  
smallest = num;
largest = num;
  
while(1)
{
cin>>num;
  
if(num==0)
break;
else if(num<0)
cout<<"Please enter only positive integer."<<endl;
  
if(num>largest)
{
largest = num;
largOcc = 0;
}
else if(num==largest)
{
largOcc++;
}
  
if(num<smallest)
{
smallest = num;
smallOcc = 0;
}
else if(num==smallest)
{
smallOcc++;
}
}
  
smallOcc++;
largOcc++;
  
//display result
cout<<"Largest = "<<largest<<endl;
cout<<"Smallest = "<<smallest<<endl;
cout<<"Largest occurrence = "<<largOcc<<endl;
cout<<"Smallest occurence = "<<smallOcc<<endl;

return 0;
}

OUTPUT:

Enter numbers:
3
2
6
2
2
6
6
6
5
6
0
Largest = 6
Smallest = 2
Largest occurrence = 5
Smallest occurence = 3



Related Solutions

Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
Convert the following text into a code format: The program reads an unspecified number of integers...
Convert the following text into a code format: The program reads an unspecified number of integers until a zero is entered. While the program reads each number it counts the number of positive numbers and the number of negative numbers that have been entered and sum up the values of the numbers entered. After the user enters zero, the program computes the average of the input values, not counting the zero. At the end, the program displays the average, and...
Design and implement a program that reads a series of 10 integers from the user and...
Design and implement a program that reads a series of 10 integers from the user and prints their average. Read each input value as a string, and then attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException (meaning that the input is not a valid number), print an appropriate error message and prompt for the number again. Continue reading values until 10 valid integers have been entered.
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Design, code, and test a program that accepts positive integers until the value 204 is entered....
Design, code, and test a program that accepts positive integers until the value 204 is entered. After input terminates, the program should report the total number of even integers (excluding the special number 204) , the average value of the even integers, the total number of odd integers, and the average value of the odd integers. The program should also report "Evens won!", "Odds won!", or "Evens and odds tied!" based on which total was greater. Test your program thoroughly...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose sum is 2n − 2. Prove that there exists a tree T on n vertices whose vertices have degrees a1, a2, . . . , an. Sketch of solution: Prove that there exist i and j such that ai = 1 and aj ≥ 2. Remove ai, subtract 1 from aj and induct on n.
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a loop that reads positive integers from standard input, printing out those values that are...
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integerthat is not positive. The values should be separated by single blank spaces. Declare any variables that are needed. PLEASE ANSWER IN C
Write a loop that reads positive integers from standard input, printing out those values that are...
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that terminates when it reads an integer that is not positive. The values should be separated by single blank spaces. Declare any variables that are needed. (In C language please).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT