Create a C-program that counts how many times each of the numbers 0-4 have been typed.

Question

Exercise 6

Create a C-program that counts how many times each of the numbers 0-4 have been typed. Use a switch-case construction. Use default to count the number of other characters. The input will be halted with ^Z (EOF). EOF means End-of-File and is defined in <stdio.h>. Thus, the constant EOF can be used in a condition (test if EOF has been typed).

Print the amount of times a certain number has been typed.

Name the program freq.c

Exercise 7

Extend the program of exercise 6 in such a way that the frequency of number 3 is shown in words.

E.g.: Number three appears two times.

Only print the frequency when it is smaller than three. If the frequency is three or larger, then print: “The number three appears more than two times.”

 

 

Summary

1. While the first one, asks the user have to create a code in C, so count 0-4 how many times has been repeated using only in switch cases. The input is only at the end of the file (EOF). Use default to count the number of other characters.

2. While in the second one, we have to create the frequency of 3 is inside the code and count how many times it has repeated.

Explanation

1. The first one, the code says that count the number 0-4, and the user has to enter the values the EOF says that -1 if the numbers are not in the 0-4. After, getting that the switch case is initiated in the code for the occurrence of getting output. If we do not get the values in the system the default values give the output of the system.

2. The second one, the code is checking the frequency 3 in which the whitespaces which are getting errors in the code given according to the string by the user with the specific characters to read it and written in words. Now, the frequency of the code is one or two, so the code is printed as same as user, or else the output will be printed as a message which is given by prompt.

Code

#include <stdio.h>
int main()
{
int p[6];
for(int q=0;q<6;q++)
p[q]=0;
while(1){
int d;
scanf("%d",&d);
int eof=-1;
if (d==eof)
break;
switch(d){
case 0:
p[0]++;
break;
case 1:
p[1]++;
break;
case 2:
p[2]++;
break;
case 3:
p[3]++;
break;
case 4:
p[4]++;
break;
default:
p[5]++;
}
}
for(int q=0;q<5;q++)
printf("Number of %d occurred = %d\n",q,p[i]);
printf("Number of times other characters occurred = %d",p[5];
}

Output

numbers 0-4 have been typed

 

Code

#include<stdio.h>
int main() 
{
char c[100];
scanf("%[^\n]%*c",c);
if(c[21]=='o'&&c[22] =='n'&&c[23]=='e')
printf("Frequency of 3: 1\n");
else if(c[21]=='t'&&c[22]=='w'&&c[23]=='o')
printf("Frequency of 3: 2\n");
else
printf("the number 3 appears more than 2 times:");
}

Output

numbers 0-4 have been typed

 

Also, the read another blog which is to write a relational schema for the diagram.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *