Question

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 occurs 2 times

3 occurs 3 times

5 occurs 2 times

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

Here is code:

#include <stdio.h>

#include <stdlib.h>

int *addElement(int *array, int num, int size)

{

// I need the function please

int *new_arr = (int *)malloc(sizeof(int) * size);

int i;

for (i = 0; i < size - 1; i++)

{

new_arr[i] = array[i];

}

new_arr[i] = num;

return new_arr;

}

// Driver function

int main()

{

int size = 0, num;

int *array;

printf("Enter numbers (0 to quit) : ");

while (1)

{

scanf("%d", &num);

if (num == 0)

break;

array = addElement(array, num, ++size);

}

int dupArray[size], freq[size];

int dupSize = 0;

for (int i = 0; i < size; i++)

{

int found = 0;

for (int j = 0; j < dupSize; j++)

{

if (dupArray[j] == array[i])

{

freq[j] = freq[j] + 1;

found = 1;

break;

}

}

if (found == 0)

{

dupArray[dupSize] = array[i];

freq[dupSize] = 1;

dupSize++;

}

}

for (int i = 0; i < dupSize; i++)

{

printf("%d occurs %d times\n", dupArray[i], freq[i]);

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
Please do in C and only use stdio.h. Write a program that reads n integers into...
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
  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • in C language Write a program to find the element of an array, which occurs maximum...

    in C language Write a program to find the element of an array, which occurs maximum number of times and its count. The program should accept the array elements as input from the user and print out the most occurring element along with its count. Hint: array size is 5. For example: Enter array elements (array size 5): 22321 Output: Max occurring element: 2. count: 3

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

  • C++ Problems: 1. Write a program that reads in an array of user given size n and then it reads se...

    C++ Problems: 1. Write a program that reads in an array of user given size n and then it reads several increment values terminated by 0. The program calls each time a function incarray() that accepts an array A, its size S and an increment value inc and updates yje array by incrementing all its values by the passed value of inc. The program prints out the array after each update. 2. write a program that reads in a n...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • (a) Write a C program to print a list of all integers between 1 and 1,000...

    (a) Write a C program to print a list of all integers between 1 and 1,000 (inclusive) which are divisible by 7 but not by 13. The list should be printed to a file called "output.dat". Remember to close the file after use. (35%) (b) Explain what is meant by the C preprocessor. Explain the effect of the following line of code: #define SQUARE (x) (x) * (x) (25%) (c) Explain the concept of an array in C. Write down...

  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • Write a program that reads a list of integers in the range (0 – 49) and prints out the numbers that does not repeat twice

    IN PYTHONWrite a program that reads a list of integers in the range (0 – 49) and prints out the numbers that does not repeat twice.The first input of the program is the number of integers contained in this list, followed by the integers contained in the list. Print the numbers in in ascending order.Case 1:INPUT: 5 2 2 3 3 1OUTPUT:1Case 2:INPUT:6 1 2 1 2 2 2OUTPUT:2Case 3:INPUT:4 3 2 1 2OUTPUT: 1 3343 2 1 213

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

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
ADVERTISEMENT