7 Replies - 9318 Views - Last Post: 13 October 2011 - 06:09 AM Rate Topic: -----

#1 Henry Lau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 11

Reading Text File Into Multiple Arrays

Posted 11 October 2011 - 08:28 PM

Problem:
In this assignment, I'm supposed to write a program in C (not C++ or C#) that can grade a 10 question T/F quiz for up to 50 students. This course is an intermediate C course and I'm not supposed to use more sophisticated data structures that I know exist, which may be more efficient in doing this.

The data is in a file of the following form.

Quote

TFTFTFTFTF
0123 FFTFTFTFTF
0526 FFTTTFTFTF
7123 FFFFTFTFTF

The first line is the Answer Key.
The second through up to 50 lines are in the format, Student Id (a four digit number) and Student Responses.

Problem Details:
The first line should go into one array.
The Student ID from the 2nd through up to 50 lines should go into a second array.
The number of correct responses from the Student Response should go into a parallel array to the 2nd array. (The actual student responses should not need to be placed in an array). This is based on a comparison of Student Responses to the Answer Key.
It is assumed that the data in the file is correct as is and does not need validation.

I've tried googling the subject and the assignment for some hints on how to write the program, but have had little luck in finding information that might help me solve my problem on my own. My code is as follows right now

#include <stdio.h>

int main (void)
{
    char quizkey[10];
    int studentid[50];
    int numright[50];
    FILE *fp1;
    int i;
    for(i=0;i<10;i++)
    quizkey[i] = 0;
    i=0;
    if ((fp1=fopen("quiz.txt","r"))==NULL)
    {
    printf("quiz.txt failed to open\n");
    return 1;
    }
    else
       while((fscanf(fp1,"%c",&quizkey[i]))!=EOF)
        {
            fscanf(fp1,"%d",&studentid[i]); // I know I'm doing something wrong somewhere around here.
            printf("quizkey[%d] is %c\n",i,quizkey[i]); //Possibly here as well.
            i++;
        }
return 0;
}



I've tried separating the problem into 2 parts: one of getting the data into the program, and two, doing the comparison, but the method I'm doing the comparison is by storing the student response into an array and then doing the comparison, but I was told explicitly that this is not needed. The code for this comparison is below. Not sure how to integrate this with the rest of the code above.
#include <stdio.h>
int main (void)
{
    char quizkey[10] = { 'T', 'T', 'T', 'T', 'F', 'T', 'F', 'T', 'F', 'T'};
    char testcase[10] = { 'T', 'T', 'T', 'T', 'F', 'T', 'F', 'T', 'F', 'T'};
    int i;
    i = 0;
    int numright;
    numright = 0;
    for(i = 0; i < 10; i++)
    if(quizkey[i] == testcase[i])
    numright++;
    printf("numright is %d:\n",numright);
    return 0;
}


There's more to the assignment, but I think I can figure out the rest of it once I get this first part taken care of.

I would like to understand how to do the problem rather than just a code dump with little to no explanation (A code dump doesn't help me understand how and why the code works. Said understanding would let me be able to apply the idea to other assignments in the future.) Thank you very much for any help you can offer.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Text File Into Multiple Arrays

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Reading Text File Into Multiple Arrays

Posted 11 October 2011 - 09:21 PM

Here the problem is: you are reading a character then an integer until the end of the file.

19	       while((fscanf(fp1,"%c",&quizkey[i]))!=EOF)
20	        {
21	            fscanf(fp1,"%d",&studentid[i]); // I know I'm doing something wrong somewhere around here.




You want to read the first ten characters into an array, suggesting a for loop. Then read the student id and answers with a second loop.
Was This Post Helpful? 1
  • +
  • -

#3 Henry Lau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 11

Re: Reading Text File Into Multiple Arrays

Posted 11 October 2011 - 10:07 PM

#include <stdio.h>

int main (void)
{
    char quizkey[10];
    int studentid[50];
    char answers[50];
    FILE *fp1;
    int i;
    for(i=0;i<10;i++)
    quizkey[i] = 0;
    studentid[i] = 0;
    i=0;
    if ((fp1=fopen("C:\\Users\\Henry\\Desktop\\BG Assignments\\quiz.txt","r"))==NULL)
    {
    printf("data.txt failed to open\n");
    return 1;
    }
    else
        for(i=0; i <10;i++)
        {
        fscanf(fp1,"%c",&quizkey[i]);
        printf("quizkey[%d] is %c\n",i,quizkey[i]);
        }
        i=0;

		for(i=0; i< 50; i++)
        {
        fscanf(fp1,"%d %c",&studentid[i],&answers[i]); //think the problem is here.
        printf("studentid[%d] is %d\n",i,studentid[i]);
        printf("answers[%d] is %c\n",i,answers[i]);
        }
return 0;
}


Okay, I tried writing loops to do the job, but when I wrote the second loop, the results weren't what I expected.

It gets the first ID correctly, but does not get any other ID at all nor does it get the answers into the array. What am I doing wrong?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Reading Text File Into Multiple Arrays

Posted 11 October 2011 - 10:33 PM

You are only reading one character before trying to read the id again.

29	        fscanf(fp1,"%d %c",&studentid[i],&answers[i]); //think the problem is here.



There are a few ways of doing this.
You could use a 2D array (C string) eg:

char answers[50][10];
// or
char answers[50][11];



So thats 50 rows of 10 characters. Or 50 rows of 10 letter strings - with an array space for a null character.
If you use the string method you could use fscanf with %s. Otherwise you would read the ten characters into the array.
Was This Post Helpful? 0
  • +
  • -

#5 Henry Lau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 11

Re: Reading Text File Into Multiple Arrays

Posted 11 October 2011 - 11:06 PM

?

I'm confused.

On line 27, I put a second for loop, but it's not processing the stuff in {} after that.

It's irritating, but the odd thing is, the professor explicitly mentioned that we wouldn't need to use an array to store the actual responses (ie, a string of TTTFTFTTFT). Just somehow need to compare the string with the answerkey array and store the number of matches (ie an int between 0 and 10) into the parallel array to the studentid array.

I apprentice the help you've given so far, #define.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,678
  • Joined: 16-October 07

Re: Reading Text File Into Multiple Arrays

Posted 12 October 2011 - 05:04 AM

You're confusing strings and arrays

Let's try:
#define KEY_SIZE 10
#define MAX_STUDENTS 50
char quizkey[KEY_SIZE];
int studentid[MAX_STUDENTS];
char answers[MAX_STUDENTS][KEY_SIZE];



Or, better, if you've covered structs:
#define KEY_SIZE 10
#define MAX_STUDENTS 50

typedef char Quiz[KEY_SIZE];

typedef struct {
	int id;
	Quiz answers;
} Student;

Quiz quizkey;
Student students[MAX_STUDENTS];



Read strings, not characters. First line, just a string into quizkey. Every line after that, an int and a string.

View PostHenry Lau, on 12 October 2011 - 02:06 AM, said:

It's irritating, but the odd thing is, the professor explicitly mentioned that we wouldn't need to use an array to store the actual responses


Ah! In this case, probably something more like:
#define KEY_SIZE 10

char correctAnswers[KEY_SIZE];
int studentId;
char studentAnswers[KEY_SIZE];



Just grade as you read from the file.
Was This Post Helpful? 0
  • +
  • -

#7 Henry Lau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 11

Re: Reading Text File Into Multiple Arrays

Posted 12 October 2011 - 11:37 PM

Can't use strucs, :(, but your advice is getting me one step closer to getting the data in and processed correctly, which is sweet.

#include <stdio.h>
#define KEY_SIZE 10
#define MAX_STUDENTS 50

int main (void)
{
    char quizkey[KEY_SIZE];
    int studentid[MAX_STUDENTS];
    char tempanswer[KEY_SIZE];
    int studcounter;
    int numright;
    int numstudright[MAX_STUDENTS];
    numright = 0;
    FILE *fp1;
    int i;
    for(i=0;i<10;i++)
    quizkey[i] = 0;
    studentid[i] = 0;
    i=0;
    if ((fp1=fopen("C:\\Users\\Henry\\Desktop\\BG Assignments\\quiz.txt","r"))==NULL)
    {
    printf("data.txt failed to open\n");
    return 1;
    }
    else

        for(i=0; i <10;i++)
        {
        fscanf(fp1,"%c",&quizkey[i]);
        printf("quizkey[%d] is %c\n",i,quizkey[i]);
        }
        i=0;
        for(i=0; i< 10; i++)
        {
        fscanf(fp1,"%d",&studentid[i]);
        fscanf(fp1," %c",&tempanswer[i]);
        if(quizkey[i] == tempanswer[i])
        {
        numright++;
        }//if


        printf("studentid[%d] is %d\n",i,studentid[i]);
        printf("tempanswers[%d] is %c\n",i,tempanswer[i]);
        printf("numright is %d\n",numright);
        }
return 0;
}



That's what my current code looks like right now.

Figured out the getting the studentanswers into the array for testing. The part I'm stuck on now is how to properly loop it, so after the first student is complete, it moves onto the second student through nth student (where n is at most 50).

This is what I want the entire program to do at this point.
Step 1: Read the first line of the file into the answerkey array. (Done)
Step 2: Read the first student ID into the studentid array (Done).
Step 3: Read the studentanswers into the tempanswer array and compare it to the answerkey array, outputting the number correct into numright. (Done)
Step 4: Store numright into numstudright (number of correct responses for the student, is parallel array to studentid array). (NOT DONE, tied to the loop further down)
Step 5: Reset numright to 0, start with the the next studentid, following steps 2 through 5. (Stuck, pretty sure I need another for loop with a studentcounter variable for it to work).
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,678
  • Joined: 16-October 07

Re: Reading Text File Into Multiple Arrays

Posted 13 October 2011 - 06:09 AM

I'm confused. You were told you wouldn't need arrays? So, why all the arrays?

Can you use functions? You should use functions.

Reasonably, your code might look like:
void printKey(char *key);
int readKey(FILE *fp, char *s);

int getRight(char *quizkey, char *tempanswer) {
	int i, numright=0;
	for(i=0; i<KEY_SIZE;i++) {
		if(quizkey[i] == tempanswer[i]) { numright++; }
	}
	return numright;
}

int processStudent(FILE *fp, char *quizkey) {
	char tempanswer[KEY_SIZE];
	int studentid;
	
	if (fscanf(fp,"%d",&studentid) && readKey(fp, tempanswer)) {
		printf("studentanswer is"); printKey(tempanswer); printf("\n");
		numright = getRight(quizkey, tempanswer);
		printf("student %d got %d out of %d correct\n", 
			studentid, 
			getRight(quizkey, tempanswer),
			KEY_SIZE);
		return 1;
	}
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1