Program plan:
• In the main() function,
o Read the number of integers between 1 and 100.
o Use for loop to initialize the value of each array element.
o Use the while loop to read the numbers and stop reading the number until the user enters the number 0.
o Use for loop to read the number of integers.
• Next, compare whether the entered element is greater than 0.
• If yes, at sequence loop execution check whether the current element is repeated again. If condition is true, then update the count of current element.
• Finally, return and display the count of occurrences of a given number.
Program:
/**********************************************************
* Program demonstrates count the occurrence of numbers *
**********************************************************/
This program demonstrates to calculate the count of occurrences of numbers.
// Header file section
#include
using namespace std;
The function main() is a predefined function. It uses loop statement to get the number of integers from the user, and to count the occurrence of the numbers.
// Main function definition
int main()
{
//Declare the variables
int array[100];
int numbers;
cout<<"\nCount the occurrence of numbers:\n";
Read the input values for ‘numbers’.
//Get the input from user
cout<<"\nEnter the integers between 1 and 100 (Enter '0' to stop):\n";
Execute the for loop until it exceeds 100 numbers and then it initializes the value of each array element.
/* Loop used to initialize values for all array elements */
for (int i = 0; i < 100; i++)
{
// Initialize the value of array
array[i] = 0;
}
Use the while loop to read the numbers and exit the loop until the user enters the value 0.
// Read the numbers
cin >> numbers;
/* Execute the while loop until the number is not
equal to 0*/
while (numbers != 0)
{
array[numbers - 1]++;
cin >> numbers;
}
Execute for loop until the j variable is less than 100.
/* Execute the for loop until the condition leads to false.*/
for (int j = 1; j < 100; j++)
{
Ensure whether the array element is greater than 0.
/* Check whether the array variable contains an element greater than 0 */
if (array[j] > 0)
{
Calculate and display the count of occurrences of a given number.
/*Count and display the occurences of a
number*/
cout << (j + 1) << " occurs " << array[j] <<
((array[j] == 1) ? " time" : " times") << endl;
}
}
//Pause the input screen
system ("pause");
//Return the value
return 0;
}
Sample Output:
Count the occurrence of numbers:
Enter the integers between 1 and 100 (Enter '0' to stop):
78
23
45
65
56
23
0
23 occurs 2 times
45 occurs 1 time
56 occurs 1 time
65 occurs 1 time
78 occurs 1 time
Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 02 occurs 2 times3 occurs 1 time4 occurs 1 time5 occurs 2 times6 occurs 1 time23 occurs 1 time43 occurs 1 timeNote that if a number occurs more than one time, the plural word...
(Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and counts the occurrences of each. Here is a sample run of the program: Enter integers between 1 and 100: 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time 2 5 6 5 4 3 23 43 2 Note that if a number occurs more than...
Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...
Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header: public static String convertMillis(long millis) For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1...
HW Help? I cannot seem to figure this one out.. --Write a program that reads the integers between 1 and 100 and counts the occurrences of each. assume the input ends with 0. Example: 2 5 6 5 4 3 23 43 2 0 ENTER 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time Note that if a number occurs more...
Could someone please help me with the following;Write a program that reads the integers between 1 and 100 and counts the occurences of each. Assume the input ends with 0. You should have two additional methodsbesides main. Their signature is:public static void displayResults(int[] numList) - This method displays the results as shown below.public static int getValidInput() - This method gets the input from the keyboard, makes sure it falls between 1 and 100 and if so returns the number to...
[[c+]] . {{Counting occurrence of numbers}}. Write a program that reads 10 integers between 0 and 99 and counts the occurrences of each.
In JAVA, write a program that generates 10 random numbers between 0 and 9. Use count arrays for each number. A sample output would be: The 10 random numbers are: 5 7 1 9 0 0 1 2 3 4 0 occurs 2 times 1 occurs 2 times 2 occurs 1 time 3 occurs 1 time 4 occurs 1 time 5 occurs 1 time 6 occurs 0 times 7 occurs 1 time 8 occurs 0 times 9 occurs 1 time
I'm in my first java class at school and really need some help. I've looked online for different programs for the same assignment I'm doing now, and none of them seemto work. Here is the assignment:Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 3 5 2 55 5 0; the program finds that the largest is 5 and the occurrence count...
Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a program that reads unspecified number of integers , determines how many negative and positive values have been read. Also calculate total and average. Your program will end with input 0. • Output Enter an integer, the input ends if it is 0: 25 34 -89 72 -35 -67 21 48 0 The number of positives is 5 The number of negatives is 3 The...
> doesn't work
josh pathaway Thu, Oct 28, 2021 10:03 PM