Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Pleae help me i am a beginner to computer science

  1. Jan 19, 2009 #1
    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.

    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");

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

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

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

    int readAnswerArray(FILE *infile)
    int numElems;

    if (numElems>1 && numElems <100)
    return numElems;
    else if(!(numElems>1 && numElems<100)
    return 0;
    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++)
    correctStudentAnswers(int answerArray, int stuAnswerArray,int frequencyArray);
    fprintf(outfile,"%d %d"Student ID stuAnswerArray);
    return 0;

    /*void readArray()

    int correctStudentAnswers()
    if (answerArray==stuAnswerArray)
    return ;


    /*void printAnswerData(outfile, numElems(int), int answerArray,int frequencyArray)

    Thank you so much.
    Take care.
  2. jcsd
  3. Jan 19, 2009 #2


    User Avatar

    Staff: Mentor

    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.
  4. Jan 19, 2009 #3
    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
  5. Jan 19, 2009 #4
    i am not even being able to read the file as it says in function 1,can you help me fix function 1 atleast?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook