Pleae help me i am a beginner to computer science

Click For Summary

Discussion Overview

The discussion revolves around a beginner's challenges with a C programming assignment related to correcting a multiple-choice test using file input and output. Participants are addressing specific coding errors and seeking guidance on implementing various functions required by the assignment.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • The original poster (Ayesha) describes the assignment requirements and shares her incomplete code, requesting help to fix errors and complete missing functions.
  • Ayesha expresses difficulty with arrays and file handling, indicating a lack of experience with these concepts.
  • One participant asks for clarification on the specific errors encountered during compilation, suggesting that identifying these issues could help in providing targeted assistance.
  • Ayesha inquires if her implementations of certain functions are correct and requests hints for writing additional functions.
  • Ayesha mentions an inability to read the input file as required in her first function, seeking help specifically for that function.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the correctness of Ayesha's code, and multiple views on how to assist her remain. The discussion is ongoing and unresolved.

Contextual Notes

There are indications of missing assumptions in Ayesha's code, such as the proper handling of function parameters and array declarations. The discussion highlights unresolved syntax and logic errors without providing specific corrections.

Who May Find This Useful

Beginners in computer science, particularly those learning C programming and dealing with file I/O and arrays, may find this discussion relevant.

ayesha
Messages
3
Reaction score
0
Hey i am new to computer science have just started taking C classes,i got an assignment its really hard below is the question,and below that i have posted my answer what ever i have done up till now,can somebody help me fix the error and i haven't done function 3 and 5 if somebody could help me with them,and help me fix the errors in my code.any help will be appreciated,please help me as i haven't done much of arrays and files in class and this assignment is really tough for a beginner like me,i am trying my best but.please help me thank you.


Problem:
The program will be correcting a multiple-choice test using an input (text) file. The first number in the file will be how many test questions. The next line of the input file will contain several integers (possible values: 1, 2, 3, 4, or 5) for the answer key. The rest of the file will contain a student ID (4-digits) and the same number of ints as the answer key for the student's answers. You will be writing to another text file each student ID, and the student's score (number correct). After the last student is processed, you will be displaying how many students answered each test question correctly (you'll need another array of this, I'm calling it frequencyArray. In main do the following:
• Open the input file and output file (you may hard-code the filenames here). If the files don't open, print an error message and quit the program.
• Call a function (see 1. below) to read test answer "key" answerArray. This function will also give you the number of elements in the array, so save in a local variable (like numElems).
• If the answer key was read in properly (0<numElems <=100), do the following:
o Call a function (see 2. below) to process the rest of the input file (student data), using the answerArray, and writing each student ID and student's score to the output file, and filling in the frequencyArray . (continued===>)
o Call a function (see 5.below) to print numElems elements of the answerArray and frequencyArray to the output text file.
You must have the following in your program:
1. function (call from main, I'm calling readAnswerArray here) to do the following:
• Read the number of elements (numElems) for the test (once only). If numElems is valid (1 to 100) read that many numbers from the input file, assigning to the answerArray , using another function (see 3. below).
• If numElems is valid, return it in a return statement, but if numElems is not valid, return 0.
2. function (call from main, I'm calling processStudentData here) to do the following:
• Initialize all elements in the frequencyArray to 0
• print headers in the output file for "Student ID", and "Test Score"
• For each line of student data in the file (stop when you try to read a student ID, but it's the end of file):
o Read the student ID
o Read the student's test answers (I'm calling it stuAnswerArray) (use function 3. below)
o Call another function (see 4.below) to "correct" the student's answers (and update the frequencyArray)
o Print the student ID and the return value of function 3 to the output file
3. function (I'm calling readArray) that will read numElems (parameter) numbers from the text file into the array parameter.
4. function (call from processStudentData, I'm calling correctStudentAnswers here) that will compare each answerArray element to its the corresponding stuAnswerArray element, and increment the corresponding element in the frequencyArray if they match. This function will also keep a count of how many student answers are correct and return that count (in a return statement). (continued===>)
5. function (call from main, I'm calling printAnswerData) that will print to the output file, a header for each test answer, then on the next line (below each header), an answer, then below that (on the next line), each corresponding element in the frequencyArray.

DO NOT USE ANY EXTERNAL VARIABLES! You'll have to pass the arrays (declared in main) to other functions. Also, main should be mostly function and "subroutine" calls.

Include in your program:
- Arrays of ints (size of 100 for each array)
- Functions as specified above (may have more)
- Comments for program, variables and functions (use textbook's style of function comments)

Below is my code,whatever i have done uptill now,please help me fix the errors and help me with the rest of the program.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
// for system function
#include <ctype.h>
#include <math.h>
#include <conio.h>
# define MAX_ELEMS 100;

int readAnswerArray(FILE *infile);
int processStudentData(FILE *infile,FILE *outfile,int numElems,int answerArray);
void readArray(int numElems, int answerArray);
int correctStudentAnswers(int answerArray, int stuAnswerArray,int frequencyArray);
void printAnswerData(outfile, int numElems, int answerArray,int frequencyArray);


int main(void)
{
FILE *infile;
FILE *outfile;
int numElems;
infile=fopen("inlab1.txt", "r");
outfile=fopen("outlab1.txt","w");

if(infile==NULL || outfile ==NULL )
{
printf("Cannot open file(s)--quitting program\n");
exit(1);
}

numElems=readanswerArray(infile);
processStudentData(infile,outfile,int numElems,int answerArray);
printAnswerData(outfile,int numElems, int answerArray,int frequencyArray);

system("pause");//in stdlib.h to pause run
getch();
return 0;
}

int readAnswerArray(FILE *infile)
{
int numElems;
fscanf(infile,"%d",&numElems);

if (numElems>1 && numElems <100)
readArray=answerArray[numElems];
return numElems;
else if(!(numElems>1 && numElems<100)
{
return 0;
}
else
return 1;
}

int processStudentData(FILE *infile,FILE *outfile,int numElems,int answerArray)
{
int frequencyArray[]={0};
fprintf(outfile,"%s%s",Student ID Test Score);
for(int i=0;i<MAX_ELEM &&fscanf(outfile,"%d",Student ID);i++)
readArray(stuAnswerArray);
correctStudentAnswers(int answerArray, int stuAnswerArray,int frequencyArray);
fprintf(outfile,"%d %d"Student ID stuAnswerArray);
return 0;
}

/*void readArray()
{
}*/

int correctStudentAnswers()
{
for(i=0;i<numElems;i++)
if (answerArray==stuAnswerArray)
frequencyArray++;
return ;

}

/*void printAnswerData(outfile, numElems(int), int answerArray,int frequencyArray)
{
fprintf(outfile,"%s",Problem#
*/

Thank you so much.
Take care.
Regards,
Ayesha.
 
Physics news on Phys.org
Fix what error? What did the compiler say about the build?

It's really hard to provide help on such a large problem. If you can try your best until you get stuck on a particular error, we can try to help you find syntax or logic errors in your code.
 
i mean do you think that my code for function 1,2 and 4 is ok?
can you give me some hint as to how to write function 3 and 5?
i am trying my best.
pleaseee help me i am a beginner.
thank you
 
i am not even being able to read the file as it says in function 1,can you help me fix function 1 atleast?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
8
Views
4K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K