compiling error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nkhan

    compiling error

    Need some help on this please:


    EECE1207 Spring 2005

    Computer Assignment # 1
    Due Wednesday, March 23rd

    The file ‘grades.txt’, as shown below, contains the results of a
    true-false exam given to a group of students. You may assume that the
    number of students will always be less than 20. The first line contains
    the key answer representing the 10 correct answers. Starting from the
    second line, each line of the data file contains the student
    identification number and the student’s answer to 10 true-false
    questions.
    Write a program that reads as input the file ‘grades.txt’. The program
    should start by readng the correct answer from the first line into a 1-D
    character array named correct_answers , then do the following tasks:
    Read the students ID numbers into a 1-D integer array named ID, and read
    each student’s answers into a 1-D character array containing the answers
    for the current student, named current_answers . The number of students,
    num_students, should be determined in the process.
    While reading the data in (1), compute and store the number of correct
    answers for each student in a 1-D integer array named scores.
    After reading all the data, determine the average score, AvgScore.
    Finally, print a three-column table on the screen that displays the ID
    number, the score, and the grade (Pass or Fail) for each student.

    The grades should be determined as follows: If the score is larger than
    AvgScore then the grade is Pass; otherwise the grade is Fail.

    Contents of the file ‘grades.txt’:

    FTFFTFFTFT
    1080 FTTFTFTTFT
    1340 FTFTFTTTFF
    1341 FTTFTTTTTT
    1401 TTFFTFFTTT
    1462 TTFTTTFFTF
    1464 FTFFTFFTFF
    1465 TTTTTTTTTT
    1466 FFFFFFFFFF
    1467 FTFTFFTFTF
    1468 FFTTFFTTFF
    1469 TTTTTFFFFF
    1470 FFFFFTTTTT

    Note:
    Your program must be written so that it could process a file with any
    number of students.
    Adequate comments should be placed throughout your program to briefly
    describe it statements.





    This is what I have so far:




    #include<stdio. h>


    AvgScore(int score[],int i){
    int total;
    int avg;
    int j;
    for(total = 0, j = 0; j < i; i++)
    {
    total += score[i];
    }
    avg = total/i;
    return avg;
    }


    int main (void)
    {
    #define NUMSTUDENTS 20
    #define NUMANSWERS 10
    int ID[NUMSTUDENTS];
    char correct_answers[NUMANSWERS+2];
    char current_answers[NUMANSWERS+2];
    int i=0;
    int score[NUMSTUDENTS];
    char *ptr1, *ptr2;
    int avg;
    FILE* fpi=fopen("grad es.txt", "r");


    //read correct answers
    fscanf(fpi,"%s" ,&correct_answe rs);
    //check for valid file
    if(!fpi) printf("Did not open file");



    while( !feof(fpi) ){
    // read student-id
    fscanf(fpi,"%d" , &ID[i]);
    // read answers
    fscanf(fpi,"%s" ,current_answer s);
    i++;
    // process data not shown
    //i= num of students
    }
    fclose(fpi);

    score[NUMSTUDENTS] = 0;
    ptr1 = &correct_answer s[0];
    ptr2 = &current_answer s[0];


    for (i = 0; i < 10; i++){
    if (*ptr1++ == *ptr2++)
    score[i]++;
    }


    avg=AvgScore(sc ore,i);

    printf("%d\n",I D[i]);
    printf("%d\n",s core[i]);

    for (score[i];score[i]<avg;score[i]++){
    printf("Fail"); }


    for (score[i];score[i]>avg;score[i]++){
    printf("Pass"); }

    return 0;

    }


    I keep getting a steam!=null error upon trying to execute the file, the
    compiler shows no errors or warnings however. Any help would be greatly
    appreciated. Thank you.

  • Walter Roberson

    #2
    Re: compiling error

    In article <b724dde7ec958b 96dfbf5c9e58260 ee0@localhost.t alkaboutprogram ming.com>,
    nkhan <nkhan@memphis. edu> wrote:
    :Need some help on this please:

    :#define NUMSTUDENTS 20

    : int score[NUMSTUDENTS];

    : score[NUMSTUDENTS] = 0;

    Indices go from 0 to the (array size minus 1); 0 to NUMSTUDENTS-1
    in this case. Thus score[NUMSTUDENTS] does not exist.

    : for (i = 0; i < 10; i++){

    Where did that 10 come from? Is that the same 10 as NUMANSWERS ?

    : if (*ptr1++ == *ptr2++)
    : score[i]++;
    : }

    But if the 10 is NUMANSWERS then you have the problem that you
    are indexing score, which is the number of students, with the
    answer number. Suppose there were only 5 students and 10 answers?

    : avg=AvgScore(sc ore,i);

    What is this calculating the average score of? Students? Answers?

    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest

    Comment

    • nkhan

      #3
      Re: compiling error

      score[NUMSTUDENTS] = 9; better?

      I was using the 10 to loop 10 times b/c it was defined in the problem
      there were 10 answers...

      The function is calculating the average score of students.


      Comment

      • Walter Roberson

        #4
        Re: compiling error

        In article <be0595b868d6b4 1102bd6b6e1530c 991@localhost.t alkaboutprogram ming.com>,
        nkhan <nkhan@memphis. edu> wrote:
        :score[NUMSTUDENTS] = 9; better?

        No, you appear to have missed the point by a long shot.

        Take a piece of paper and write on it one box for every array entry in the
        array 'score'. Now simulate your code score[NUMSTUDENTS] = 9; and see
        what you get.


        :I was using the 10 to loop 10 times b/c it was defined in the problem
        :there were 10 answers...

        If 10 is a fixed number, then why did you bother #define'ing the number
        of answers before?


        :The function is calculating the average score of students.

        Hint: it isn't. Take that piece of paper mentioned above and
        hand-simulate a couple of iterations of your 'for' loop.
        --
        History is a pile of debris -- Laurie Anderson

        Comment

        • nkhan

          #5
          Re: compiling error

          score[NUMSTUDENTS] = 9; better?

          I was using the 10 to loop 10 times b/c it was defined in the problem
          there were 10 answers...

          The function is calculating the average score of students.


          Comment

          • E. Robert Tisdale

            #6
            Re: compiling error

            nkhan wrote:[color=blue]
            > Need some help on this please:
            >
            >
            > EECE1207 Spring 2005
            >
            > Computer Assignment # 1
            > Due Wednesday, March 23rd
            >
            > The file ‘grades.txt’, as shown below, contains the results of a
            > true-false exam given to a group of students. You may assume that the
            > number of students will always be less than 20. The first line contains
            > the key answer representing the 10 correct answers. Starting from the
            > second line, each line of the data file contains the student
            > identification number and the student’s answer to 10 true-false
            > questions.
            > Write a program that reads as input the file ‘grades.txt’. The program
            > should start by readng the correct answer from the first line into a 1-D
            > character array named correct_answers , then do the following tasks:
            > Read the students ID numbers into a 1-D integer array named ID, and read
            > each student’s answers into a 1-D character array containing the answers
            > for the current student, named current_answers . The number of students,
            > num_students, should be determined in the process.
            > While reading the data in (1), compute and store the number of correct
            > answers for each student in a 1-D integer array named scores.
            > After reading all the data, determine the average score, AvgScore.
            > Finally, print a three-column table on the screen that displays the ID
            > number, the score, and the grade (Pass or Fail) for each student.
            >
            > The grades should be determined as follows: If the score is larger than
            > AvgScore then the grade is Pass; otherwise the grade is Fail.
            >
            > Contents of the file ‘grades.txt’:
            >
            > FTFFTFFTFT
            > 1080 FTTFTFTTFT
            > 1340 FTFTFTTTFF
            > 1341 FTTFTTTTTT
            > 1401 TTFFTFFTTT
            > 1462 TTFTTTFFTF
            > 1464 FTFFTFFTFF
            > 1465 TTTTTTTTTT
            > 1466 FFFFFFFFFF
            > 1467 FTFTFFTFTF
            > 1468 FFTTFFTTFF
            > 1469 TTTTTFFFFF
            > 1470 FFFFFTTTTT
            >
            > Note:
            > Your program must be written so that
            > it could process a file with any number of students.
            > Adequate comments should be placed throughout your program
            > to briefly describe its statements.[/color]
            [color=blue]
            > cat main.c[/color]
            #include <stdio.h>

            double average(const size_t n, int score[n]) {
            int total = 0;
            for (size_t j = 0; j < n; ++j) {
            total += score[j];
            }
            return (double)total/n;
            }

            int main(int argc, char* argv[]) {
            FILE* fpi = fopen("grades.t xt", "r");
            if (NULL == fpi) {
            printf("Could not open file grades.txt!");
            }
            else { // (NULL != fpi)
            const
            size_t MAXANSWERS = 10;
            char correct_answer[MAXANSWERS+1];
            char current_answer[MAXANSWERS+1];
            const
            size_t MAXSTUDENTS = 20;
            int ID[MAXSTUDENTS];
            int score[MAXSTUDENTS];

            size_t students = 0; // number of students
            //Read correct answers.
            fscanf(fpi, "%s", correct_answer) ;
            while (!feof(fpi)) { //Check for valid file.
            ID[students] = 0;
            // Read student-id.
            if (fscanf(fpi, "%d", &ID[students]) < 1)
            break;
            // Read answers.
            if (fscanf(fpi, "%s", &current_ans wer[students]) < 1)
            break;
            score[students] = 0;
            const
            size_t answers = MAXANSWERS;
            for (size_t answer = 0; answer < answers; ++answer) {
            if (correct_answer[answer] == current_answer[answer])
            ++score[students];
            }
            ++students;
            // Process data not shown.
            }
            fclose(fpi);

            if (0 < students) {
            const
            double averageScore = average(student s, score);

            for (size_t student = 0; student < students; ++student) {
            printf("%d\n", ID[student]);
            printf("%d\n", score[student]);
            if (score[student] < averageScore) {
            printf("Fail\n\ n");
            }
            else {
            printf("Pass\n\ n");
            }
            }
            }
            }
            return 0;
            }
            [color=blue]
            > gcc -Wall -std=c99 -pedantic -o main main.c
            > ./main[/color]
            1080
            8
            Pass

            1340
            5
            Fail

            1341
            6
            Pass

            1401
            7
            Pass

            1462
            6
            Pass

            1464
            6
            Pass

            1465
            6
            Pass

            1466
            5
            Fail

            1467
            6
            Pass

            1468
            5
            Fail

            1469
            5
            Fail

            1470
            5
            Fail

            Comment

            • CBFalconer

              #7
              Re: compiling error

              nkhan wrote:[color=blue]
              >
              > score[NUMSTUDENTS] = 9; better?
              >
              > I was using the 10 to loop 10 times b/c it was defined in the problem
              > there were 10 answers...
              >
              > The function is calculating the average score of students.[/color]

              Better than what? What function?

              You need to learn to quote. Other messages are not necessarily
              visible. In this case Robersons are never visible, because he
              refuses to mark quotes properly, and is PLONKED (meaning I am never
              annoyed by seeing his offerings - they go straight into the
              garbage). Each article should stand on its own.

              The only other answer I see is from our resident Troll, and while
              it may work for him it probably won't for you, because you probably
              don't have a C99 compiler. Besides, his answer is too baroque, and
              your instructor is going to know you pirated it. It doesn't help
              you learn anything.

              Break your program up into usable small pieces. One is "read the
              correct answers". Another is "fill the id / studentanswers
              array". Another is "compute the average" (you did that). Probably
              the last is "scan and emit grades". Each should be a function, and
              receive (or deposit) its data through parameters. Each function
              should be simple enough that you can trivially check its
              correctness.

              --
              "I conclude that there are two ways of constructing a software
              design: One way is to make it so simple that there are obviously
              no deficiencies and the other way is to make it so complicated
              that there are no obvious deficiencies." -- C. A. R. Hoare


              Comment

              • Walter Roberson

                #8
                Re: compiling error

                In article <423FCF4B.C8F17 800@yahoo.com>,
                CBFalconer <cbfalconer@wor ldnet.att.net> wrote:
                :You need to learn to quote.

                Yes, the OP should have given context.

                : Other messages are not necessarily
                :visible. In this case Robersons are never visible,

                Never visible to -you- is different than "never visible".

                :because he
                :refuses to mark quotes properly,

                Nonsense. My posts meet all RFC requirements. If your
                newsreader messes them up then that's your problem.

                :and is PLONKED (meaning I am never
                :annoyed by seeing his offerings - they go straight into the
                :garbage).

                Did you manage to get the PLONKing right the third time?
                The first two times you were back responding to me again
                within days.
                --
                "This was a Golden Age, a time of high adventure, rich living and
                hard dying... but nobody thought so." -- Alfred Bester, TSMD

                Comment

                • Dan P.

                  #9
                  Re: compiling error


                  "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.ca> wrote in message
                  news:d1omne$leh $1@canopus.cc.u manitoba.ca...[color=blue]
                  > In article <423FCF4B.C8F17 800@yahoo.com>,
                  > CBFalconer <cbfalconer@wor ldnet.att.net> wrote:
                  > :You need to learn to quote.
                  >
                  > Yes, the OP should have given context.
                  >
                  > : Other messages are not necessarily
                  > :visible. In this case Robersons are never visible,
                  >
                  > Never visible to -you- is different than "never visible".
                  >
                  > :because he
                  > :refuses to mark quotes properly,
                  >
                  > Nonsense. My posts meet all RFC requirements. If your
                  > newsreader messes them up then that's your problem.
                  >
                  > :and is PLONKED (meaning I am never
                  > :annoyed by seeing his offerings - they go straight into the
                  > :garbage).
                  >
                  > Did you manage to get the PLONKing right the third time?
                  > The first two times you were back responding to me again
                  > within days.[/color]


                  I think you'd make everyone on newsgroups alot happier if you used the ">"
                  character to denote a quote instead of ":". Using the colon makes the
                  quote, at a quick glance, undetectable. Using the standard ">" makes
                  viewing quotes very easy.



                  Dan


                  Comment

                  • CBFalconer

                    #10
                    Re: compiling error

                    "Dan P." wrote:[color=blue]
                    > "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.ca> wrote[color=green]
                    >> CBFalconer <cbfalconer@wor ldnet.att.net> wrote:[/color]
                    >[/color]
                    .... snip ...[color=blue][color=green]
                    >>
                    >> :and is PLONKED (meaning I am never
                    >> :annoyed by seeing his offerings - they go straight into the
                    >> :garbage).
                    >>
                    >> Did you manage to get the PLONKing right the third time?
                    >> The first two times you were back responding to me again
                    >> within days.[/color]
                    >
                    > I think you'd make everyone on newsgroups alot happier if you
                    > used the ">" character to denote a quote instead of ":". Using
                    > the colon makes the quote, at a quick glance, undetectable.
                    > Using the standard ">" makes viewing quotes very easy.[/color]

                    As you can see he is too full of his own self-importance. He
                    doesn't even appreciate being given multiple chances and
                    explanations. Luckily most Canadians are not so anti-social. So
                    the only cure is to persuade everybody to PLONK him, and then maybe
                    he will go away.

                    --
                    "I conclude that there are two ways of constructing a software
                    design: One way is to make it so simple that there are obviously
                    no deficiencies and the other way is to make it so complicated
                    that there are no obvious deficiencies." -- C. A. R. Hoare

                    Comment

                    • Christopher Benson-Manica

                      #11
                      Re: compiling error

                      Dan P. <dperlbergerN0S PAM@ec.rr.com> spoke thus:
                      [color=blue]
                      > I think you'd make everyone on newsgroups alot happier if you used the ">"
                      > character to denote a quote instead of ":". Using the colon makes the
                      > quote, at a quick glance, undetectable. Using the standard ">" makes
                      > viewing quotes very easy.[/color]

                      It makes no difference at all to me - my newsreader recognizes ':' as
                      a valid quote character. Now S.M. Ryan's quote character, a '#', is
                      another story altogether.

                      --
                      Christopher Benson-Manica | I *should* know what I'm talking about - if I
                      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                      Comment

                      • Alan Balmer

                        #12
                        Re: compiling error

                        On 22 Mar 2005 08:58:22 GMT, roberson@ibd.nr c-cnrc.gc.ca (Walter
                        Roberson) wrote:
                        [color=blue]
                        >In article <423FCF4B.C8F17 800@yahoo.com>,
                        >CBFalconer <cbfalconer@wor ldnet.att.net> wrote:
                        >:You need to learn to quote.
                        >
                        >Yes, the OP should have given context.
                        >
                        >: Other messages are not necessarily
                        >:visible. In this case Robersons are never visible,
                        >
                        >Never visible to -you- is different than "never visible".
                        >
                        >:because he
                        >:refuses to mark quotes properly,
                        >
                        >Nonsense. My posts meet all RFC requirements. If your
                        >newsreader messes them up then that's your problem.
                        >[/color]
                        It's not nonsense - it makes your replies hard to read, especially
                        when mixing quotes from others who use the conventional quote marking.
                        Why do you take such delight in causing a problem? What's the gain?
                        Some weird kind of ego trip?
                        [color=blue]
                        >:and is PLONKED (meaning I am never
                        >:annoyed by seeing his offerings - they go straight into the
                        >:garbage).
                        >
                        >Did you manage to get the PLONKing right the third time?
                        >The first two times you were back responding to me again
                        >within days.[/color]

                        --
                        Al Balmer
                        Balmer Consulting
                        removebalmercon sultingthis@att .net

                        Comment

                        • Alan Balmer

                          #13
                          Re: compiling error

                          On Tue, 22 Mar 2005 14:16:02 +0000 (UTC), Christopher Benson-Manica
                          <ataru@nospam.c yberspace.org> wrote:
                          [color=blue]
                          >Dan P. <dperlbergerN0S PAM@ec.rr.com> spoke thus:
                          >[color=green]
                          >> I think you'd make everyone on newsgroups alot happier if you used the ">"
                          >> character to denote a quote instead of ":". Using the colon makes the
                          >> quote, at a quick glance, undetectable. Using the standard ">" makes
                          >> viewing quotes very easy.[/color]
                          >
                          >It makes no difference at all to me - my newsreader recognizes ':' as
                          >a valid quote character. Now S.M. Ryan's quote character, a '#', is
                          >another story altogether.[/color]

                          It's not the newsreader, it's the human reader that's affected.

                          --
                          Al Balmer
                          Balmer Consulting
                          removebalmercon sultingthis@att .net

                          Comment

                          • Walter Roberson

                            #14
                            Re: compiling error

                            In article <8gg041h44aclgp d1fhtqd6qv7gm06 2u17m@4ax.com>,
                            Alan Balmer <albalmer@spamc op.net> wrote:
                            :On 22 Mar 2005 08:58:22 GMT, roberson@ibd.nr c-cnrc.gc.ca (Walter
                            :Roberson) wrote:
                            :>Nonsense. My posts meet all RFC requirements. If your
                            :>newsreader messes them up then that's your problem.

                            :It's not nonsense - it makes your replies hard to read, especially
                            :when mixing quotes from others who use the conventional quote marking.

                            Alan, my quoting style is there to make quotes -easier- to read
                            for humans. My quoting style uses a different character for
                            each indentation level, so that humans can tell which quote is which
                            at a glance. Humans are very good at pattern matching with distinct
                            characters, but not nearly as good at mentally counting rows of
                            repetitions of the same character once the count gets beyond
                            about 4.


                            :Why do you take such delight in causing a problem? What's the gain?

                            CBF fairly consistantly uses strong pressure tactics to attempt to
                            get people to confirm to his vision of what this newsgroup should
                            be like. I have so far seen no reason to meet any of his
                            demands, and good reasons not to in most cases.

                            :Some weird kind of ego trip?

                            When someone says the newsgroup equivilent of, "I'm going to hold my
                            breath until I turn blue unless you do things my way!" then one can
                            fuss around and conciliate them, or one can say "Go ahead and turn blue
                            then, we are not impressed by you!"

                            Someone has to take a stand, and though it is certainly not my usual
                            position to do so, it seems to have fallen onto me in this situation.

                            If it were Chris or Keith making the request, then I would give their
                            points serious consideration, as it appears to me that both of those
                            gentlemen are more interested in helping people than they are in
                            controlling others.


                            If anyone happens to be curious as to whether provocation is my usual
                            style, then you are invited to skim through the google groups archives
                            of my postings, http://groups.google.ca/groups?q=author%3Aroberson+ibd
                            (Yeah, that count that shows up is a pretty good approximation of
                            the number of posts I've made.) I think you will find that I am
                            seldom provocative without strong reason. Except perhaps in some
                            messages about politics, posted through my personal account @mts.net
                            on local wpg.* (Winnipeg) newsgroups.
                            --
                            Beware of bugs in the above code; I have only proved it correct,
                            not tried it. -- Donald Knuth

                            Comment

                            • Walter Roberson

                              #15
                              Re: compiling error

                              In article <42401DA1.9F7B6 A44@yahoo.com>,
                              CBFalconer <cbfalconer@wor ldnet.att.net> wrote:
                              :As you can see he is too full of his own self-importance.

                              No, but I am too empty of the self-importance of certain other people.

                              :So
                              :the only cure is to persuade everybody to PLONK him, and then maybe
                              :he will go away.

                              A major premise by which certain people attempt to control the
                              newsgroup is that there are too many newcomers who don't know what
                              the current clique rules are about what is acceptable and what
                              is not, and that if the newcomers are not brushed off right at
                              the beginning then there will not be enough time to deal with
                              everyone.

                              If the barbarian hordes really are waiting at the gates, then
                              there is no shortage of people who will not have PLONKed me
                              and whom I can be of some assistance to.

                              The circumstances under which mass PLONKing would have
                              a noticable effect is if there is the horde has reduced to
                              a trickle -- in which case there would no longer be a need
                              for the big "You have visited c.l.c. Now go home!" flag that
                              some tend to wave around.
                              --
                              Usenet is like a slice of lemon, wrapped around a large gold brick.

                              Comment

                              Working...