Search code examples
cstdin

how can i verify that i got N^2 integers from standard input(Stdin)?


I'm having problem with a project in C that i need to submit. i was asked to write a program that gets N and N^2 integers and check whether they create a magic square.

however, i missed that the input should've been from stdin, and i don't know how to do that, as i used printf to request the user to give N and scanned it and then asked a user to enter N^2 integers and to check if it creates magic square. however, i don't know how to do it from stdin.

the input should be of the form: sizeofN,N1,N2,...,N^2. for instance: 3 8 1 6 3 5 7 4 9 2 - will mean that 3 is N and the other elements are what's after it.

so my questions are:

1)how do i obtain values from the standart input(stdin)in the mentioned way? is it from something like while ((c=getchar()) != EOF), or is there a smarter way?

2)how can i check whether i really got N^2 items? (the first number is N and i should get N^2 inputs after it, for instance if i got: 3 8 1 3 4 5 9 8 5 it means that N should be 3 and i should get 9 integers, but i only got 8, how can i write a function that checks that i really got N^2 numbers are getting N?

i already programmed a program to check whether a given matrix forms a magic square, so the question is about receiving information from stdin. please provide code so i can understand and learn.

thank you very much for your kind help.


Solution

  • First get the first number

    if( scanf("%d",&N) != 1){
       fprintf(stderr,"Error in input");
       exit(1);
    }
    

    Now you validate whether it is indeed a good number.

       if( N <= 0 ){
           fprintf(stderr,"Enter integral N >= 1");
           exit(1);
       }
    

    Now loop over,

       for(size_t i = 0; i < N*N; i++){
           if( scanf("%d",&a[i]) != 1){
               fprintf(stderr,"Enter integral N >= 1");
               exit(1);
           }
       }
    

    In this solution with scanf() you got N^2 numbers because in every iteration you correctly input an integer.

    Using getchar() is not a good choice. Because for every magic square of size>=4 will have 2 digits numbers atleast. You can't get them directly using getchar() unless you process them accordingly and make integer numbers out of them which is one option you have with getchar() but unnecessarily complicates thing.

    Also in case you want to validate that if there is more number required than entered or to have some more finer control over input taking then you can use fgets() and read a line at once and parse it to int.


    If those are provided as arguments to the program then you can easily do the validation part using argc and argv. Suppose you want to check whether the number of input given is alright or not.

    So you check,

    int main(){
        char *ptr;
        int N;
        errno = 0;
        if( argc > 2){
            long NN = strtol(argv[1], &ptr, 10);
            if (errno != 0 || *p != '\0' || NN > INT_MAX || NN < INT_MAX) {
                fprintf(stderr, "%s\n", "Error in parsing");
                exit(1);
            }
            else{
                N = (int)NN;
            }
        }
        int totalElmts = N*N;
    
        if( N && totalElmts / N != N){
            fprintf(stderr, "%s\n","Overflow");
        }
        if( argc != (totalElmts + 2) ){
            fprintf(stderr, "%s %s %s\n",argv[0],"N","NxN elements");
        }
        for(size_t i = 2; argv[i] != NULL; i++)
        {
            //convert argv[i] to `int`
        }
        ...
        return 0;
    }