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...
Java: create a program that reads in a piece of DNA sequence from a sequence file...
Java: create a program that reads in a piece of DNA sequence from a sequence file (dna.seq) (alternatively you can use the getRandomSeq(long) method of the RandomSeq class to generate a piece of DNA sequence), and then print out all the codons in three forward reading frames. Design a method called codon() that can be used to find all the codons from three reading frames. The method will take in an argument, the reading frame (1, 2, or 3), 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.
1. Design and implement a program that reads a series of integers from the user and...
1. Design and implement a program that reads a series of integers from the user and continually prints their average after every reading. The input stops when the user enters “stop” as the input value. Read each input value as a string, then check if it is “stop” or not. If the string is not “stop”, 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...
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, 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 integer), display 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.
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number. Enter 3 and 9 solution: 3456789 987654 45678 8765 567 76 6
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.
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT