0

This is my assignment Question for a first semester C programming course.

You have been hired under a student working scheme program. Your first task is to write a C program to assist in one of the professors in the faculty to grade the final exam of his/her students. The exam consists of 20 multiple-choice questions. Each question has one of four possible answers: A, B, C, or D. The program will read in the students’ answers and the correct answers from files and prints out the result onto the screen and also the file output. There are at least 15 students in class.

What I'm thinking to do is to read the file of the students answers and store the values in an array using a function and the same thing with the correct answers file, then return both arrays to the main function and send them to another function to compare the answers. However I am having trouble in returning the string arrays to the main function.

This is my code:

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


//Modules
char** readstudent()
{
    FILE*s_ans;
    static char arrs[20][20];
    int i,j;
    s_ans=fopen("trial2.txt","r");

    if (s_ans == NULL)//check if file can be opened
    {
    printf("error student");
    }

    while(!feof(s_ans))
    {
        for(j=0;j<20;j++)
        {
            fscanf(s_ans,"%s",arrs[j]);
        }
    }
    printf("ReadStudent\n");
    for(i=0;i<20;i++)
    {
        printf("%d\t %s\n",i+1,arrs[i]);
    }

    return arrs;
}

char** readcorrect()
{
    FILE*c_ans;
    static char arrc[20][20];
    int x,i;;   
    c_ans=fopen("CorrectAnswers.txt","r");

    if (c_ans == NULL)//check if file can be opened
    {
    printf("error correct");
    }

    while(!feof(c_ans))
    {
        for(x=0;x<20;x++)
        {
            fscanf(c_ans,"%s",arrc[x]);
        }
    }
    printf("ReadCorrect\n");
    for(i=0;i<20;i++)
    {
        printf("%d\t %s\n",i+1,arrc[i]);
    }

    return arrc;
}

//Main
int main()
{
    int i,j,n,x;
    char **as_ans;
    char **ac_ans;

    as_ans=readstudent();
    ac_ans=readcorrect();

    printf("Main");
    for(i=0;i<20;i++)
    {
        printf("%s",as_ans[i]);
    }

    return 0;
}
}

and I'm getting "[Warning] return from incompatible pointer type" twice after compiling it.

Anyone could help me to successfully return the arrays back to the main function please? Note that this is my first time dealing with C Thank you!

  • You may also want to read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – n. m. could be an AI Dec 09 '19 at 07:25

1 Answers1

0

You can do it by creating your two dimensional array dynamically, so that it doesn't have the scope of function and then return from function as shown below:

char** func(){
    char **arrs = (char **)malloc(20 * sizeof(char *));
    ...
        // your logic
    ...
    return arrs;
}

After the usage you can free the space of dynamically allocated 2-D array.

Himanshu Singh
  • 2,117
  • 1
  • 5
  • 15
  • Well after applying what you said my function compiles with no errors nor warnings but the execution is empty. – Marwan Deabes Dec 09 '19 at 08:27
  • @MarwanDeabes how you have inserted and read element from this type of 2-D array. That can be the issue but i hope you are clear on your original question i.e. `how to return 2-D string from array` – Himanshu Singh Dec 09 '19 at 08:31