Question

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 number is not in the file.

Hints: An array of size 101 is good enough. A number in the file plays the role of an index.

For example:

Suppose the content of the file: a.txt is as follows:

99 2 99

3

-12 80 12 33

3 99 100 1234 84

The display output is:

2 has occurred: 1 times

3 has occurred: 2 times

12 has occurred: 1 times

33 has occurred: 1 times

80 has occurred: 1 times

84 has occurred: 1 times

99 has occurred: 3 times

100 has occurred: 1 times

Note that this is just a sample dialog. Your program should work for any number of data values.

Answer:

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

Code :

Text_file:

Output :

Raw_code :

#include<stdio.h>
int main()
{
   int arr[101]={0};/*Initialization all Array Elements with '0's*/
   int i,num;
   char ch;
   FILE *f1;   /*pointer to a file*/
   f1=fopen("a.txt","r");/*Opening file in Read mode*/
   do
   {
       fscanf(f1,"%d",&num);   /*scaning integer from file*/
       if(num>=0 && num<=100)/*checking condition*/
       {
           arr[num]=arr[num]+1;/*counting integer occurance*/
       }
   }while((ch=getc(f1))!=EOF);/*Reading Upto Ending of file*/
   for(i=0;i<101;i++)/*Printing The Occurances*/
   {
       if(arr[i]!=0)/*Eliminating Numbers which occured zero times*/
       {
           printf("%d has occured: %d times\n",i,arr[i]);
       }
   }
}

Text_file :

99 2 99

3

-12 80 12 33

3 99 100 1234 84

Add a comment
Know the answer?
Add Answer to:
C Program Question: Write a program that reads all integers that are in the range of...
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 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...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • LANGUAGE = JAVA (IDE Eclipse preferred) Lab 21 AEIOU Counter Objective: Write a program that reads...

    LANGUAGE = JAVA (IDE Eclipse preferred) Lab 21 AEIOU Counter Objective: Write a program that reads a file and counts the number of times the vowels ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ occurs exactly in that order. It ignores consonants It ignores any type of space It ignores case The only thing it cannot ignore is if another vowel occurs out of order These count AEIOU aeiou hahehihohu Take it out These do not AEIuO Taco is good Take it over...

  • This needs to be linux compatible in C++ code. Write a program that reads a file...

    This needs to be linux compatible in C++ code. Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167,...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

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

  • c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detec...

    c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detected. (An example of such a file is shown below). Also, the program should copy from values[ 1 any value which has even sum of digits and its index (location) to two new arrays evenArr 1 and idxArrl I, respectively. In addition, the program must...

  • I need to write a C++ program that reads two integers from a data filed called...

    I need to write a C++ program that reads two integers from a data filed called "integers.dat" The first integer should be low and the other one high. The program should loop through all integers between the low and high values and include the low and high integers and then display Display the integers in the file Displays the number of integers divisible by 5 OR 6, but not both. Displays the sum of the integers from condition 2 as...

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