Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use the following in C, The producer program will give an output of 10 random nu

ID: 3738511 • Letter: U

Question

use the following in C,

The producer program will give an output of 10 random numbers that range from 3 to 7.

For this program, create a program that will generate 10 random numbers from the producer program and sends it to the receiver which is the consumer program that creates a histogram with a bargraph that will display the frequency of the numbers.

Here is the sample that gives example of how the program should run that you can run in linux(executable file) in order to run it you will need to put in your directory and type in ./filename.out

producer program -> https://ufile.io/yn7go

consumer program ->https://ufile.io/6hw6e

Sample Output:

Values Received: 4, 4, 6, 6, 6, 7, 7, 3, 6, 5
----- HISTOGRAM -----
3: *
4: **
5: *
6: ****
7: **
END OF PROGRAM RUN

Explanation / Answer

/*************** Producer file *********************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <ctype.h>
//#include <assert.h>

int main() {
  FILE *fptr;
  fptr = fopen("file1.txt", "w"); // generate the file to be passed to consumer.c file
  int randnum1, maxnum = 7, minnum = 3, i;
  for(i = 0; i < 10 ; i++) {
   randnum1 = (rand() % (maxnum - minnum + 1)) + minnum;
   if (i == 9)
   fprintf(fptr, "%d", randnum1);
   else
   fprintf(fptr, "%d, ",randnum1);
  }
  fprintf(fptr, " ");
  fclose(fptr);
  return 0;
}


/*************** Consumer file *********************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
   char c;
   char num1[10];
   int ii, count[5] ={0, 0, 0, 0, 0}, n = 0;
   FILE *fptr1;
   fptr1 = fopen("file1.txt", "r");
   if (fptr1 == NULL) {
   printf("Cannot open the file for reading ");
   exit(1);
   }

   while((c = fgetc(fptr1)) != EOF) {
   if (isdigit(c))
   num1[n++] = c;
   }
   fclose(fptr1);
   printf("Input Recieved :: ")
   for (ii = 0; ii < 10 ; ii++) {
   if (ii == 9)
   printf("%c", num1[ii]);
   else
   printf("%c, ", num1[ii]);
   }

   for (ii = 0; ii < 10 ; ii++) {
   if (num1[ii] == '3')
   count[0]++;
   else if (num1[ii] == '4')
   count[1]++;
   else if (num1[ii] == '5')
   count[2]++;
   else if (num1[ii] == '6')
   count[3]++;
   else if (num1[ii] == '7')
   count[4]++;
   }
   printf(" 3 : ");
   while (count[0] != 0) {
   printf("*");
   count[0]--;
   }
   printf(" 4 : ");
   while (count[1] != 0) {
   printf("*");
   count[1]--;
   }
   printf(" 5 : ");
   while (count[2] != 0) {
   printf("*");
   count[2]--;
   }
   printf(" 6 : ");
   while (count[3] != 0) {
   printf("*");
   count[3]--;
   }
   printf(" 7 : ");
   while (count[4] != 0) {
   printf("*");
   count[4]--;
   }
   printf(" ");
   remove("file1.txt"); // remove the file generated by producer

   return 0;
}

/*********************** Output of the code ********************
>> gcc producer.c -o producer.o
>> ./producer.o
>> ls
   file1.txt producer.o producer.c
>> gcc consumer.c -o consumer.o
>> ./consumer.o
   Input Recieved :: 6, 4, 5, 3, 6, 3, 4, 5, 7, 4
   3 : **
   4 : ***
   5 : **
   6 : **
   7 : *
****************************************************************/