Home > Net >  How to input the scanf first then print the if else in for loop
How to input the scanf first then print the if else in for loop

Time:10-14

whenever I input the scanf in this code

    scanf("%d\n",&t);
    for(i,x=1;i,x<=t;i,x  ){
        scanf("%d %d %d", &a, &b, &c);
        
        if(b c>=a){
            printf("Case #%d: yes\n",x);
        }
        else{
            printf("Case #%d: no\n",x);
        }
        
    }

it always become scanf -> printf -> scanf -> printf

and when i do this code

    scanf("%d\n",&t);
    for(i=1;i<=t;i  ){
        scanf("%d %d %d", &a, &b, &c);
    }
    
    for(x=1;x<=t;x  ){
        if(b c>=a){
            printf("Case #%d: yes\n",x);
        }
        else{
            printf("Case #%d: no\n",x);
        }
    }

it does not get the input from scanf. each a,b,c has different value. i want it to input all the scanf first then printf the result. how?

CodePudding user response:

To store the values for later, use an array.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    size_t t;
    if (scanf("%lu", &t) != 1) {
        fputs("Incorrect input.", stderr);
        return -1;
    }
    
    // Allocate a 2-d array with size t x 3
    int (*arr)[3] = malloc(sizeof(int[t][3]));
    if (!arr) {
        fputs("Could not allocate memory.", stderr);
        return -1;
    }
    
    // To use letters instead of numbers for the indexes
    enum {A, B, C};
    
    for (int i = 0; i < t; i  ) {
        if (scanf("%d %d %d", &arr[i][A], &arr[i][B], &arr[i][C]) != 3) {
            fputs("Incorrect input.", stderr);
            free(arr);
            return -1;
        }
    }
    
    for (int i = 0; i < t; i  ) {
        printf("Case #%d: ", i   1);
        if (arr[i][B]   arr[i][C] >= arr[i][A]) {
            puts("yes");
        }
        else{
            puts("no");
        }
    }

    free(arr);
    return 0;
}

This code uses dynamic memory allocation. If you haven't learned that yet, here's a simplified version with a fixed-size array:

#include <stdio.h>

int main()
{
    int arr[100][3];
    
    int t;
    if (scanf("%d", &t) != 1 || t <= 0 || t > 100) {
        puts("Incorrect input.");
        return -1;
    }
    
    for (int i = 0; i < t; i  ) {
        if (scanf("%d %d %d", &arr[i][0], &arr[i][1], &arr[i][2]) != 3) {
            puts("Incorrect input.");
            return -1;
        }
    }
    
    for (int i = 0; i < t; i  ) {
        printf("Case #%d: ", i   1);
        if (arr[i][1]   arr[i][2] >= arr[i][0]) {
            puts("yes");
        }
        else{
            puts("no");
        }
    }

    return 0;
}

CodePudding user response:

Something like this should work if you want to scanf -> printf -> scanf -> printf for all iterations:

#include <stdio.h>

int main(void)
{
    int a, b, c;
    int num_iterations;
    
    printf("Enter number of iterations: ");
    scanf("%d",&num_iterations);
    
    for(int i = 0; i < num_iterations;   i)
    {
        printf("Enter a, b, c: ");
        scanf("%d %d %d", &a, &b, &c);

        if (b c >= a) {
            printf("Case #%d: yes\n", i 1);
        }
        else {
            printf("Case #%d: no\n", i 1);
        }
    }
    
    return 0;
}

Something like this should work if you want to do all scanf's bfore all printf's:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int num_iterations;
    
    printf("Enter number of iterations: ");
    scanf("%d",&num_iterations);

    int *a = malloc(num_iterations * sizeof(int));
    int *b = malloc(num_iterations * sizeof(int));
    int *c = malloc(num_iterations * sizeof(int));

    for(int i = 0; i < num_iterations;   i)
    {
        printf("Enter a, b, c for iter %d: ", i);
        scanf("%d %d %d", &a[i], &b[i], &c[i]);
    }

    for(int i = 0; i < num_iterations;   i)
    {
        if (b[i] c[i] >= a[i]) {
            printf("Case #%d: yes\n", i 1);
        }
        else {
            printf("Case #%d: no\n", i 1);
        }
    }

    free(a);
    free(b);
    free(c);
    
    return 0;
}
  •  Tags:  
  • c
  • Related