简体   繁体   中英

How do I print out a sequence of answers using a for loop in C?

The question might be a little vague still so allow me to elaborate. My goal is to calculate download speeds based on user input. So a sample output is shown here .

So I've gone ahead and asked for user input using the following code (this part totally works but just to give you the bigger picture)

#include <stdio.h>

int main(void) {

int minFileSize, maxFileSize, maxDLSpeed, minDLSpeed;
int time;


//first we get user input for minimum size and download speeds
printf("Enter a minimum file size: ");
scanf("%d", &minFileSize);
//after this condition is satisfied..
while (minFileSize <= 0 ) {
    printf("Invalid input. Try again: \n");
    scanf("%d", &minFileSize);

    while(getchar() != '\n' );                        //clears caches
}
//program continues
printf("Enter a minimum download speed (MB/s): ");
scanf("%d", &minDLSpeed);
while (minDLSpeed <= 0) {
    printf("Invalid input. Try again: ");
    scanf("%d", &minDLSpeed);

    while ( getchar() != '\n' );
}
printf("Enter a maximum file size (MB): ");
scanf("%d", &maxFileSize);
while (maxFileSize < minFileSize) {                   //maxFileSize can't be lower than minFileSize
    printf("Invalid input. Try again: ");
    scanf("%d", &maxFileSize);

    while (getchar() != '\n');
}
printf("Enter maximum download speed (MB/s): ");
scanf("%d", &maxDLSpeed);
while (maxDLSpeed < minDLSpeed) {
    printf("Invalid input. Try again: ");
    scanf("%d", &maxDLSpeed);

    while (getchar() != '\n');
}

where the while (getchar() != '\\n') just clears the cache so we can proceed to the next input.

After getting the user input, I then use a nested for-loop to loop through the minimum file size all the way to the maximum size (so it stops when it reaches the maximum size, which is based on what the user enters) and then do the same thing for the minimum download speed and maximum download speed. Then each time the loop iterates, its supposed to output the download speed, which is (file size / download speed) So i/j represents this

Here's the loop I used for that

for (int i = minFileSize; i <= maxFileSize; i++)
{
    for (int j = minDLSpeed; j<= maxDLSpeed; j++) {

        time = i / j;     
    };
}

Phew If you've read that far, thank you! Now my question is how do I print the output in the format that I showed above, so that each time the loop runs, it prints out the correct download speed based on the file size and download speed.

Your loop seems to be the wrong way around. The outer for loop should be for the download speeds, and the inner loop for the file sizes:

for (int dl_speed=minDLSpeed; dl_speed<=maxDLSpeed; dl_speed++) {
    printf("%6.2fMB/s |", (float)dl_speed);
    for (int file_size=minFileSize; file_size<=maxFileSize; file_size++)
        printf("%6.2fs", (float)file_size / dl_speed);
    printf("\n");
}

That should give you the body of the table. You will need to add the header lines too and you should now be able to work out how to do that given the code shown above. For your input values you will get a table that looks like this:

Enter a minimum file size: 8
Enter a minimum download speed (MB/s): 9
Enter a maximum file size (MB): 13
Enter maximum download speed (MB/s): 12
  9.00MB/s |  0.89s  1.00s  1.11s  1.22s  1.33s  1.44s
 10.00MB/s |  0.80s  0.90s  1.00s  1.10s  1.20s  1.30s
 11.00MB/s |  0.73s  0.82s  0.91s  1.00s  1.09s  1.18s
 12.00MB/s |  0.67s  0.75s  0.83s  0.92s  1.00s  1.08s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM