Reading Data From a File and Storing Into Multiple Arrays
#1
Reading Text File Into Multiple Arrays
Posted xi October 2011 - 08:28 PM
Problem:
In this assignment, I'm supposed to write a programme in C (not C++ or C#) that can grade a ten question T/F quiz for upward to 50 students. This course is an intermediate C class and I'one thousand 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 post-obit class.
Quote
TFTFTFTFTF
0123 FFTFTFTFTF
0526 FFTTTFTFTF
7123 FFFFTFTFTF
The get-go line is the Answer Primal.
The second through up to l lines are in the format, Pupil Id (a iv digit number) and Student Responses.
Problem Details:
The start line should go into i 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 Educatee Response should go into a parallel array to the 2nd array. (The actual student responses should non need to be placed in an array). This is based on a comparing of Pupil Responses to the Answer Cardinal.
It is assumed that the data in the file is correct every bit is and does not need validation.
I've tried googling the subject and the assignment for some hints on how to write the plan, but have had little luck in finding data that might help me solve my problem on my own. My lawmaking is equally follows right now
#include <stdio.h> int master (void) { char quizkey[ten]; int studentid[fifty]; int numright[50]; FILE *fp1; int i; for(i=0;i<10;i++) quizkey[i] = 0; i=0; if ((fp1=fopen("quiz.txt","r"))==Aught) { printf("quiz.txt failed to open up\due north"); render 1; } else while((fscanf(fp1,"%c",&quizkey[i]))!=EOF) { fscanf(fp1,"%d",&studentid[i]); // I know I'k doing something wrong somewhere effectually here. printf("quizkey[%d] is %c\due north",i,quizkey[i]); //Possibly here every bit well. i++; } return 0; }
I've tried separating the trouble into 2 parts: 1 of getting the data into the program, and two, doing the comparing, but the method I'thousand doing the comparison is past storing the student response into an array and then doing the comparing, but I was told explicitly that this is not needed. The lawmaking for this comparison is below. Not certain how to integrate this with the residuum of the lawmaking 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 < x; i++) if(quizkey[i] == testcase[i]) numright++; printf("numright is %d:\n",numright); return 0; }
There'south more to the assignment, but I think I tin figure out the residue of it once I get this first part taken care of.
I would similar to understand how to do the trouble rather than merely a lawmaking dump with petty to no explanation (A code dump doesn't help me understand how and why the lawmaking works. Said agreement would permit me be able to apply the idea to other assignments in the future.) Thank you very much for any help y'all can offer.
#2
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'one thousand doing something wrong somewhere around here.
Yous want to read the get-go ten characters into an array, suggesting a for loop. Then read the student id and answers with a 2nd loop.
#3
Re: Reading Text File Into Multiple Arrays
Posted 11 October 2011 - 10:07 PM
#include <stdio.h> int master (void) { char quizkey[10]; int studentid[fifty]; char answers[fifty]; FILE *fp1; int i; for(i=0;i<x;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\due north",i,quizkey[i]); } i=0; for(i=0; i< 50; i++) { fscanf(fp1,"%d %c",&studentid[i],&answers[i]); //think the problem is hither. 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, just 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?
#4
Re: Reading Text File Into Multiple Arrays
Posted 11 October 2011 - 10:33 PM
Yous are just reading one character before trying to read the id again.
29 fscanf(fp1,"%d %c",&studentid[i],&answers[i]); //think the trouble is here.
There are a few ways of doing this.
You could utilize a 2D array (C string) eg:
char answers[fifty][10]; // or char answers[50][eleven];
And so thats 50 rows of ten characters. Or 50 rows of x letter strings - with an array space for a null character.
If you use the string method you could utilize fscanf with %s. Otherwise you lot would read the x characters into the array.
#5
Re: Reading Text File Into Multiple Arrays
Posted 11 October 2011 - 11:06 PM
?
I'g confused.
On line 27, I put a second for loop, but it'southward not processing the stuff in {} after that.
It's irritating, but the odd affair is, the professor explicitly mentioned that we wouldn't demand 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 shop the number of matches (ie an int between 0 and x) into the parallel array to the studentid array.
I amateur the help you've given so far, #define.
#6
Re: Reading Text File Into Multiple Arrays
Posted 12 October 2011 - 05:04 AM
You're confusing strings and arrays
Let'southward endeavour:
#define KEY_SIZE 10 #define MAX_STUDENTS fifty char quizkey[KEY_SIZE]; int studentid[MAX_STUDENTS]; char answers[MAX_STUDENTS][KEY_SIZE];
Or, better, if yous've covered structs:
#define KEY_SIZE 10 #ascertain MAX_STUDENTS 50 typedef char Quiz[KEY_SIZE]; typedef struct { int id; Quiz answers; } Student; Quiz quizkey; Pupil students[MAX_STUDENTS];
Read strings, non characters. First line, just a string into quizkey. Every line after that, an int and a string.
Henry Lau, on 12 October 2011 - 02:06 AM, said:
It's irritating, merely the odd affair is, the professor explicitly mentioned that we wouldn't need to use an array to store the bodily 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.
#seven
Re: Reading Text File Into Multiple Arrays
Posted 12 October 2011 - 11:37 PM
Can't use strucs, , only your communication is getting me 1 step closer to getting the data in and processed correctly, which is sweet.
#include <stdio.h> #define KEY_SIZE 10 #define MAX_STUDENTS fifty 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\northward",numright); } return 0; }
That's what my current lawmaking 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 consummate, information technology moves onto the second pupil through nth student (where n is at near 50).
This is what I want the entire program to do at this bespeak.
Footstep 1: Read the first line of the file into the answerkey assortment. (Done)
Step 2: Read the showtime student ID into the studentid array (Done).
Step 3: Read the studentanswers into the tempanswer array and compare it to the answerkey assortment, outputting the number correct into numright. (Done)
Footstep 4: Store numright into numstudright (number of correct responses for the student, is parallel array to studentid array). (Non Done, tied to the loop further down)
Step 5: Reset numright to 0, start with the the side by side studentid, post-obit steps 2 through 5. (Stuck, pretty sure I need some other for loop with a studentcounter variable for it to work).
#viii
Re: Reading Text File Into Multiple Arrays
Posted 13 Oct 2011 - 06:09 AM
I'm confused. You were told you wouldn't need arrays? So, why all the arrays?
Can you utilise functions? You should utilise functions.
Reasonably, your code might look like:
void printKey(char *key); int readKey(FILE *fp, char *due south); int getRight(char *quizkey, char *tempanswer) { int i, numright=0; for(i=0; i<KEY_SIZE;i++) { if(quizkey[i] == tempanswer[i]) { numright++; } } render 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("\due north"); numright = getRight(quizkey, tempanswer); printf("student %d got %d out of %d right\n", studentid, getRight(quizkey, tempanswer), KEY_SIZE); render 1; } return 0; }
Source: https://www.dreamincode.net/forums/topic/250901-reading-text-file-into-multiple-arrays/
0 Response to "Reading Data From a File and Storing Into Multiple Arrays"
ارسال یک نظر