Assignment:
A - Write a program that gets numbers from user until user enters "-1". Then program should write numbers into a file.
I have done this, but I can not do the B one:
B - Update your program and print a histogram into the file like shown below. Save your code in a new file.
Example
report.dat:
5 *****
8 ********
11 ***********
3 ***
Code from A:
#include <stdio.h>
int main() {
int num;
const int senitel = -1;
FILE*fileId;
printf("Please enter integer number (-1 to finish)");
scanf("%d", &num);
fileId = fopen("report.dat", "w");
while (num != senitel) {
fprintf(fileId, "%d \n", num);
scanf("%d", &num);
}
fclose(fileId);
return 0;
}
Instead of writing the user input directly to the file, you need to store it in a data structure temporarily. When the user enters the sentinel value, then output the contents of the data structure.
in pseudocode
ask user for input
while not sentinel
add to array[user value]++
get next input
for each element in array
if value > 0
fprintf value + " "
for (int i = 0; i < value; i++)
fprintf "*"
fprintf
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments