C - Senitel controlled loop

sk4yb3n

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;
}
Geoffrey Sherritt

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ How to read from a file to do a count controlled loop?

From Dev

Having trouble with count controlled for loop

From Dev

when I try to end an event controlled loop in c it takes two inputs?

From Dev

Stepper motor controlled by C#

From Dev

Data controlled programs in c++

From Dev

Flag controlled while loop to search txt file

From Dev

Controlled infinite loop uses too much resources

From Dev

Getting infinite loop instead of expected controlled iteration

From Dev

Parot AR drone controlled by opencv in c++

From Dev

IP Address increased controlled way at C#

From Dev

Sentinel controlled while loops C#

From Dev

Parot AR drone controlled by opencv in c++

From Dev

Does C have enumeration-controlled loops?

From Dev

How to do a side by side form controlled by a button in C#

From Dev

USB Type-C, how is the direction of power delivery controlled?

From Dev

C - Read from keyboard (controlled by Ctrl+D)

From Dev

c# Controlled Thread Pooling. More threads are running simultaneously than expected

From Dev

C# If loop within a for loop within an if loop

From Dev

PHP for loop vs C for loop

From Dev

PHP for loop vs C for loop

From Dev

Controlled inputs and composite types

From Dev

Cancellation of CompletableFuture controlled by ExecutorService

From Dev

Jumping Height of character is not controlled

From Dev

python list controlled increment

From Dev

BLE: transmitWindowSize: can it be controlled?

From Dev

Controlled distribution through AppStore

From Dev

Jumping Height of character is not controlled

From Dev

BLE: transmitWindowSize: can it be controlled?

From Dev

Time controlled WLAN switching?

Related Related

HotTag

Archive