Question

C language program Write a program to do the following tasks. Generate x random integers, the...

C language program

Write a program to do the following tasks.

Generate x random integers, the values of which are in the range [2, y]. The values of x and y are decided by the user, and both no larger than 100.

Calculate and print the number of times each integer in [2, y] occurred in these x random integers. For example, suppose x=9, y=6 and the generated random integers are

4, 4, 3, 6, 4, 4, 3, 2, 6

then the result is  

2: 1

3: 2

4: 4

5: 0

6: 2

where “2: 1” means 2 occurs once, “3: 2” means 3 occurs twice and so on.

 Print the values in [2, y] in decreasing order of the number of times they occurred. If two values in [2, y] occurred the same number of times, they should be decreasingly ordered by their own values. With the above example, the result is

4, 6, 3, 2, 5

where 6 is ordered before 3 since 6 is larger than 3, although they both occurred twice.

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

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,j, track[101] = {0}, value_x, value_y, trackedNumbers[101] = {0};
  
printf("Enter values\nx: ");
scanf("%d", &value_x);

printf("y: ");
scanf("%d", &value_y);

srand(time(NULL));
  
for(i=0;i<value_x;i++)
{
int n = (rand() % (value_y-2+1) + 2);
track[n]++;
trackedNumbers[n] = n;
}

printf("\nFrequencies\n");
for(i= 2; i<= value_x; i++)
if(track[i] >= 1)
printf("%d: %d\n", i, track[i]);
  
for(i=2; i<=value_x; i++)
for(j=i; j<=value_x; j++)
if(track[i] > 0 && track[i] <= track[j])
{
int temp = track[i];
track[i] = track[j];
track[j] = temp;

temp = trackedNumbers[i];
trackedNumbers[i] = trackedNumbers[j];
trackedNumbers[j] = temp;
}

printf("\nOrder (decreasing): ");
for(i= 2; i<= value_x; i++)
if(track[i] != 0)
printf("%d ", trackedNumbers[i]);
return 0;
}

-- Please up vote or comment if you have any doubts. Happy Learning!

Add a comment
Know the answer?
Add Answer to:
C language program Write a program to do the following tasks. Generate x random integers, the...
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
  • Please do in C and only use stdio.h. Write a program that reads n integers into...

    Please do in C and only use stdio.h. Write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. The values should be printed in ascending order. Turn in your code and input/result together. Suppose, for example, that you input the values -7 3 3 -7 5 5 3 as the elements of your array. Then your program should print -7...

  • C Program Question: Write a program that reads all integers that are in the range of...

    C Program Question: Write a program that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file. Note: The program ignores any number less than 0 or greater than 100. Note: Do not display zero if a...

  • C# prograaming language 1. write a program that generates 10,000 random integers in the range of ...

    c# prograaming language 1. write a program that generates 10,000 random integers in the range of 0-9 and them in binary search tree. 2. use any sort algorithm (insertion, bubble, quick...) to display a list of each of the integers and how many times they appear. 3. at last, add ruction to the BinarySearchTree class that count the number of edges in a tree. Write a program that generates 10,000 random integers in the range of 0-9 and store them...

  • 1. Create List 2. Create Random integers 3. Search List Write a program that gets random...

    1. Create List 2. Create Random integers 3. Search List Write a program that gets random integers and populates a list, then searches the list 1. Write a program that creates an empty list 2. Populate the list with 100 random integers 3. After creating the list, print the length of elements in the list 4. Select a range of numbers as 10 – 20 5. Find out the number of times ‘15’ appears in the list and print it...

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

  • The language is C++ Write a program that uses the Rand_Int function to store 20 random...

    The language is C++ Write a program that uses the Rand_Int function to store 20 random die rolls for a 6-sided dice. The program should print out the values horizontally then the program should call a user-defined function that will put () around any duplicate values beside each other. For example, if you rolled this: 3 1 1 3 6 1 5 3 2 6 2 2 2 4 6 1 3 6 6 3 Then you will have the...

  • Write a program that prompt the user to enter 3 integers: x,y and z. Your program...

    Write a program that prompt the user to enter 3 integers: x,y and z. Your program should output the answer to the user based on the divisibility of x and y by z as follows: Input If both x and y are divisible by z Result X and y are both divisible by 2. X is divisible by z. Y is divisible by z. Both X and Y are not divisible by If x is only divisible by z If...

  • Write a program in python that generates X random integers Num. Num is a random number...

    Write a program in python that generates X random integers Num. Num is a random number between 20 to 50. X is a random number between 10 to 15. Calculate and show the Smallest, Largest, Sum, and Average of those numbers. You are not allowed to use Python functions sample(), min(), max(), average(), sort(), sorted()!! HINTs: to find Smallest.... 1) X is a random number between 10 to 15... for example 11...this determines how many times the loop will happen...

  • Write MARIE assembly language programs that do the following: I. Write a program that inputs thre...

    Write MARIE assembly language programs that do the following: I. Write a program that inputs three integers, a, b, and c, in that order. It computes the following ia-bi-fc+ c The result should be written to output 2. Write a program that inputs integers, s. y, and z. It outputs the difference of the langest and first element entered. You may assume x. y, and z all have different values. So if 8, 12, and 9 are input, the output...

  • Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.r...

    Please write a JAVA program according to following requirement. Thanks Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.random () generates a random integer greater than or equal to 0 and less than 1. The expression Math. random) 6generates random numbers between 0 and 5, simulating the throw of a die. In this lab assignment, you will use an array to test whether the random generator is fair; that is, whether each possible...

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