Write a Program That Declare Parallel Arrays Read From Text File C++ Student Grades

#1

  • New D.I.C Caput

Reputation: 0

  • View blog
  • Posts: four
  • Joined: eleven-October 11

Reading Text File Into Multiple Arrays

Posted eleven October 2011 - 08:28 PM

Problem:
In this assignment, I'm supposed to write a program in C (non C++ or C#) that tin 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 utilise more sophisticated data structures that I know exist, which may be more than efficient in doing this.

The data is in a file of the following form.

Quote

TFTFTFTFTF
0123 FFTFTFTFTF
0526 FFTTTFTFTF
7123 FFFFTFTFTF

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

Trouble Details:
The first line should become into one array.
The Student ID from the 2nd through upward to 50 lines should get into a second array.
The number of correct responses from the Pupil Response should go into a parallel assortment to the 2nd array. (The bodily student responses should not demand to be placed in an assortment). This is based on a comparison of Student Responses to the Respond Cardinal.
It is assumed that the information in the file is right as is and does not demand validation.

I've tried googling the discipline and the assignment for some hints on how to write the plan, but have had petty luck in finding information that might help me solve my problem on my own. My code is as follows correct at present

#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"))==Cypher)     {     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 two parts: one of getting the data into the program, and two, doing the comparison, but the method I'm doing the comparison is past 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 comparing is beneath. Not sure how to integrate this with the remainder of the code to a higher place.

#include <stdio.h> int main (void) {     char quizkey[x] = { '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:\northward",numright);     return 0; }            

There's more to the assignment, but I call back I can figure out the rest of it once I go this start function taken care of.

I would like to understand how to do the problem rather than only a lawmaking dump with niggling to no explanation (A code dump doesn't help me understand how and why the code works. Said understanding would let me exist able to apply the thought to other assignments in the time to come.) Thank you lot very much for any help you can offer.


Is This A Good Question/Topic? 0

  • +

#2 #define User is offline

Reputation: 1868

  • View blog
  • Posts: six,763
  • Joined: xix-February 09

Re: Reading Text File Into Multiple Arrays

Posted xi 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.            

Y'all want to read the first ten characters into an assortment, suggesting a for loop. Then read the student id and answers with a 2d loop.

#three Henry Lau User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: iv
  • Joined: 11-October eleven

Re: Reading Text File Into Multiple Arrays

Posted 11 Oct 2011 - 10:07 PM

#include <stdio.h>  int principal (void) {     char quizkey[10];     int studentid[50];     char answers[50];     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"))==Zippo)     {     printf("information.txt failed to open\n");     return 1;     }     else         for(i=0; i <ten;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\north",i,answers[i]);         } render 0; }            

Okay, I tried writing loops to practise 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 whatsoever other ID at all nor does it get the answers into the assortment. What am I doing wrong?

#iv #ascertain User is offline

Reputation: 1868

  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Reading Text File Into Multiple Arrays

Posted 11 October 2011 - 10:33 PM

You are just reading ane character before trying to read the id again.

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

In that location are a few ways of doing this.
Yous could utilise a second array (C cord) eg:

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

And so thats 50 rows of 10 characters. Or 50 rows of 10 alphabetic character strings - with an array infinite for a nix grapheme.
If y'all apply the cord method you lot could use fscanf with %s. Otherwise you lot would read the 10 characters into the array.

#5 Henry Lau User is offline

  • New D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 4
  • Joined: 11-October eleven

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 non processing the stuff in {} after that.

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

I amateur the assist you've given and then far, #define.

#half-dozen baavgai User is offline

Reputation: 7507

  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Reading Text File Into Multiple Arrays

Posted 12 Oct 2011 - 05:04 AM

You lot're confusing strings and arrays

Let's try:

#define KEY_SIZE 10 #ascertain MAX_STUDENTS fifty char quizkey[KEY_SIZE]; int studentid[MAX_STUDENTS]; char answers[MAX_STUDENTS][KEY_SIZE];            

Or, ameliorate, if you've covered structs:

#define KEY_SIZE 10 #ascertain MAX_STUDENTS l  typedef char Quiz[KEY_SIZE];  typedef struct { 	int id; 	Quiz answers; } Student;  Quiz quizkey; Student students[MAX_STUDENTS];            

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

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

Information technology's irritating, merely the odd matter is, the professor explicitly mentioned that we wouldn't need to utilise an array to store the actual responses

Ah! In this example, probably something more like:

#define KEY_SIZE 10  char correctAnswers[KEY_SIZE]; int studentId; char studentAnswers[KEY_SIZE];            

But grade every bit y'all read from the file.

#7 Henry Lau User is offline

  • New D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 4
  • Joined: eleven-October 11

Re: Reading Text File Into Multiple Arrays

Posted 12 October 2011 - eleven:37 PM

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

#include <stdio.h> #ascertain KEY_SIZE 10 #define MAX_STUDENTS l  int primary (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"))==Cypher)     {     printf("data.txt failed to open up\due north");     return 1;     }     else          for(i=0; i <10;i++)         {         fscanf(fp1,"%c",&quizkey[i]);         printf("quizkey[%d] is %c\north",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\due north",i,tempanswer[i]);         printf("numright is %d\n",numright);         } return 0; }            

That'due south what my current code looks similar right at present.

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 pupil is complete, it moves onto the 2d student through nth student (where n is at near 50).

This is what I want the entire plan to do at this point.
Footstep 1: Read the first line of the file into the answerkey array. (Done)
Stride 2: Read the first student ID into the studentid assortment (Done).
Footstep iii: Read the studentanswers into the tempanswer array and compare it to the answerkey array, outputting the number right into numright. (Washed)
Step iv: Store numright into numstudright (number of correct responses for the educatee, is parallel assortment to studentid assortment). (NOT Washed, tied to the loop further down)
Step v: Reset numright to 0, commencement with the the next studentid, post-obit steps ii through 5. (Stuck, pretty sure I need another for loop with a studentcounter variable for it to work).

#8 baavgai User is offline

Reputation: 7507

  • View blog
  • Posts: xv,558
  • Joined: 16-October 07

Re: Reading Text File Into Multiple Arrays

Posted 13 October 2011 - 06:09 AM

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

Tin can y'all use functions? Y'all should employ functions.

Reasonably, your code might look similar:

void printKey(char *primal); 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++; } 	} 	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("pupil %d got %d out of %d right\n",  			studentid,  			getRight(quizkey, tempanswer), 			KEY_SIZE); 		return 1; 	} 	render 0; }            

Write a Program That Declare Parallel Arrays Read From Text File C++ Student Grades

Source: https://www.dreamincode.net/forums/topic/250901-reading-text-file-into-multiple-arrays/

0 Response to "Write a Program That Declare Parallel Arrays Read From Text File C++ Student Grades"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel