Question

How to I solve this using modulo? Problem 1: Develop a C program that reads a...

How to I solve this using modulo?

Problem 1: Develop a C program that reads a file of integers called “numbers.txt” It then prints the occurrence count of each digit from [0-9]. Your code must utilize the following function:


void freqFun (int num, int counters[])


In this prototype, num represents the number read from the file and counters represents the set of counters that hold the occurrence frequency of each digit. Digits that do not occur in the file shall be omitted in the screen printout.


Sample execution
numbers.txt: 123 457 102 345 0
Output
Number Frequency
0 : 2
1 : 2
2 : 2
3 : 2
4 : 1
5 : 1
7 : 1Problem 1: Develop a C program that reads a file of integers called “numbers.txt” It then prints the occurrence count of each digit from [0-9]. Your code must utilize the following function:
void freqFun (int num, int counters[])


In this prototype, num represents the number read from the file and counters represents the set of counters that hold the occurrence frequency of each digit. Digits that do not occur in the file shall be omitted in the screen printout.
Sample execution


numbers.txt: 123 457 102 345 0


Output:


Number Frequency
0 : 2
1 : 2
2 : 2
3 : 2
4 : 1
5 : 1
7 : 1

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

Answer:

File: Frequency.c

#include <stdio.h>
#include <stdlib.h>
int main()
{
   FILE *filePointer;//declare file pointer
   char ch;
   int arr[]={0,0,0,0,0,0,0,0,0,0},i;//Initialize Array for counter
   filePointer = fopen("numbers.txt", "r");//Opening file
   if (filePointer == NULL)//if file not open
   {
       printf("File is not available \n");
   }
   else//if file open successfull.
   {
       while ((ch = fgetc(filePointer)) != EOF)//until reach to end of file
       {
           switch(ch)//using switch increase the frequency counter
           {
               case '0': arr[0]=arr[0]+1;break;                      
               case '1': arr[1]=arr[1]+1;break;
               case '2': arr[2]=arr[2]+1;break;
               case '3': arr[3]=arr[3]+1;break;
               case '4': arr[4]=arr[4]+1;break;
               case '5': arr[5]=arr[5]+1;break;
               case '6': arr[6]=arr[6]+1;break;
               case '7': arr[7]=arr[7]+1;break;
               case '8': arr[8]=arr[8]+1;break;
               case '9': arr[9]=arr[9]+1;break;
               default:break;
           }
       }
       for(i=0;i<10;i++)//to display each digit frequency
       {
           if(arr[i]!=0)//display only if have counter more than zero
               printf("\n%d : %d",i, arr[i]);
       }
   }
   fclose(filePointer);//closing file pointer
   return 0;
}

Screen:

Add a comment
Know the answer?
Add Answer to:
How to I solve this using modulo? Problem 1: Develop a C program that reads a...
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
  • For your second program, please read the data from the input file directly into an array....

    For your second program, please read the data from the input file directly into an array. (You may safely dimension your array to size 300.) Then close the input file. All subsequent processing will be done on the array. Your program should have two functions besides main(). The first function will print out the contents of the array in forward order, 10 numbers per line, each number right justified in a 5 byte field. The second function will print out...

  • How to write a Java file out that that reads from numbers.txt with these numbers 2...

    How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner {    public static void main(String[] args)    {        /* For Homework! Tokens*/        Scanner file = null;        PrintWriter fout= null;   ...

  • Problem 1: Write a program that reads a positive float number and displays the previous and...

    Problem 1: Write a program that reads a positive float number and displays the previous and next integers. Sample Run: Enter a float: 3.5 The previous and next integers are 3 and 4. Problem 2: Write a program that reads two integers and displays their sum, difference, product, and the result of their division. Sample Run: Enter two integers: 8 5 Sum: 8, Difference: 3, Product: 40, Division: 1.6 Problem 3: Write a program that reads a three-digit integer from...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • Write a c++ program that reads numbers from the user, and prints a table showing how...

    Write a c++ program that reads numbers from the user, and prints a table showing how many times each digit appears in each number. The user should be able to enter more than one number to be tested for repeated digits. The program should terminate when the user enters a number that is less than 0. Here is a sample output: Enter a number (-1 to end this loop): 41271092 Digit: 0 1 2 3 4 5 6 7 8...

  • c# csci312: character counter - vector design a program that reads in an ascii text file...

    c# csci312: character counter - vector design a program that reads in an ascii text file (provided) ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provide... C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provided) and creates an output file that contains each unique ASCII...

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

  • MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds...

    MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds them (if compatible) and prints their sum, 2) subtracts them (if compatible) and prints their difference, and 3) multiplies them (if compatible) and prints their product. Prompt the user for the names of two matrix input files (see format below) and place the output in a file of the user’s choosing. The format of the input files should contain the number of rows, followed...

  • Programming language is C++. 9. Write a program that reads digits and composes them into integers....

    Programming language is C++. 9. Write a program that reads digits and composes them into integers. For example, 123 is read as the characters 1, 2, and 3. The program should output 123 is 1 hundred and 2 tens and 3 ones. The number should be output as an int value Handle numbers with one, two, three, or four digits. Hint: To get the integer value 5 from the character '5' subtract '0' that is, '5'-'0'

  • Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to...

    Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to you: The main function creates 2 threads: consumer represents the consumer and executes the consume function, and producer represents the producer and executes the  produce function. You should declare and initialize 3 semaphores. Those semaphores should be used in the consume(..) and produce(...) functions. #include <stdio.h> #include <stdlib.h> #include <pthread.h> //compile and link with -pthread #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in, out; int num;...

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