Question

(Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and...

(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 one time, the plural word “times” is used in the output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Consider the following C program written in Dev C++ :

#include <stdio.h>
#include <stdlib.h> //Libraries used.

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
     
int arr[100],length,i=0,j=0 ; //declaring array and length variable.
  
printf("Enter the size of the array : ");
scanf("%d",&length);
  
printf("\nEnter integers between 1 and 100 : ");
for(i=0;i<length;i++)
scanf("%d",&arr[i]); //getting the array as an input.
  
//Array frequency will store frequencies of element in array.
int frequency[length];
int visited = -1;
  
for(i = 0; i < length; i++){
int count = 1; //for counting purpose.
for(j = i+1; j < length; j++){
if(arr[i] == arr[j]){
count++; //incrementing the count variable.
//To avoid counting same element again
frequency[j] = visited;
}
}
if(frequency[i] != visited)
frequency[i] = count;
}
  
//Displays the frequency of each element present in array

for(i = 0; i < length; i++){
if(frequency[i] != visited){
if(frequency[i]>1)
printf("\n %d occurs %d times.",arr[i],frequency[i]);
else
printf("\n %d occurs %d time.",arr[i],frequency[i]);
}
}
  
   return 0;
}

The output of the above code is :

Add a comment
Know the answer?
Add Answer to:
(Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Counting Occurrence of Numbers

    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...

  • HW Help? I cannot seem to figure this one out.. --Write a program that reads the...

    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...

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    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...

    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...

  • C++counting occurrence of numbers

    write a program that read the integers between 1 and 100 and count the occurrence of each number. Assume the input end with 0. here is samole of the program:Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0 Enter2 occurs 2 times3 occurs 1 time4 occurs 1 time5 occurs 2 times6 occurs 1 time23 occurs 1 time43 occurs 1 time

  • [[c+]] . {{Counting occurrence of numbers}}. Write a program that reads 10 integers between 0 and...

    [[c+]] . {{Counting occurrence of numbers}}. Write a program that reads 10 integers between 0 and 99 and counts the occurrences of each.

  • Write a program that reads the integers between 1 and 100 and counts the occurences of each

    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...

  • Write a program that reads integers, finds the smallest of them, and counts its occurrences. Assume...

    Write a program that reads integers, finds the smallest of them, and counts its occurrences. Assume that the input ends with number -1. Suppose that you entered 4 2 9 2 2 -1; the program finds that the smallest is 2 and the occurrence count for 2 is 3. (Hint: Maintain two variables, min and count. min stores the current min number, and count stores its occurrences. Initially, assign the first number to min and 1 to count. Compare each...

  • *IN PYTHON* (Count single digits) Write a program that generates 1,000 random integers between 0 and...

    *IN PYTHON* (Count single digits) Write a program that generates 1,000 random integers between 0 and 9 and displays the count for each number. (Hint: Use a list of ten integers, say counts, to store the counts for the number of 0s, 1s, ..., 9s.) Rubric 5 pts: Proper use of list to count occurrences of numbers 5 pts: Proper calculations of counts 5 pts: Proper output Note: No software dev plan or UML required

  • Using Python, Lists: Write a program that randomly generates 1000 numbers between 0 and 9 and...

    Using Python, Lists: Write a program that randomly generates 1000 numbers between 0 and 9 and counts the count of each number. The output should have this form: 3 occurs 5 times 5 occurs 1 time Notice that if there is more than 1. The word times is used and if there is only 1, the word times is used.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT