compiling error

N

nkhan

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;
}
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("grades.txt", "r");


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



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

score[NUMSTUDENTS] = 0;
ptr1 = &correct_answers[0];
ptr2 = &current_answers[0];


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


avg=AvgScore(score,i);

printf("%d\n",ID);
printf("%d\n",score);

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


for (score;score>avg;score++){
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.
 
W

Walter Roberson

:#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++;
: }

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(score,i);

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

nkhan

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.
 
E

E. Robert Tisdale

nkhan said:
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.
> cat main.c
#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.txt", "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_answer[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(students, 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;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
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
 
W

Walter Roberson

: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.
 
N

nkhan

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.
 
C

CBFalconer

nkhan said:
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.

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.
 
W

Walter Roberson

: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.
 
D

Dan P.

Walter Roberson said:
: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.


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
 
C

CBFalconer

Dan P. said:
.... snip ...

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.

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.
 
C

Christopher Benson-Manica

Dan P. said:
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.

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.
 
A

Alan Balmer

: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.
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?
 
A

Alan Balmer

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.

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

Walter Roberson

:On 22 Mar 2005 08:58:22 GMT, (e-mail address removed)-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:roberson+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.
 
W

Walter Roberson

: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.
 
A

Alan Balmer

:On 22 Mar 2005 08:58:22 GMT, (e-mail address removed)-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.

It fails.
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.

Counting is not necessary. As you say, humans are very good at
patterns, and in this case, the pattern is the consistent indentation
and character, visible at a glance. With varying characters, of
varying widths, that pattern recognition is useless, and analysis of
the levels requires just that, analysis. The width issue can be
resolved at the cost of using a fixed-width font for everything, but
that has other disadvantages, and still leaves the distraction of a
mixture of quote characters.
: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.
I see. So you're inflicting this on all of us just to spite CBF.
: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.

The short answer to my above question would seem to be yes.
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.
CBF is not the only reader here who has made this request. Others have
asked, and politely. It seems to me that you are the one interested in
exerting control.
 
C

Christopher Benson-Manica

Walter Roberson said:
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.

You and Mr. Falconer could apparently both benefit from better
newsreaders. As I noted elsewhere, my newsreader correctly identifies
your ':' delimiter as a quote character, but it also highlights up to
three levels of quoted text in different colors, rendering your point
of concern moot.

In any case, I would take a long look at any practice that would
prevent me from receiving advice from someone of Mr. Falconer's
extensive experience, but of course that's your perogative.
 
W

Walter Roberson

:On 22 Mar 2005 18:00:21 GMT, (e-mail address removed)-cnrc.gc.ca (Walter
:Roberson) wrote:

:>My quoting style uses a different character for
:>each indentation level, so that humans can tell which quote is which
:>at a glance.

:Counting is not necessary. As you say, humans are very good at
:patterns, and in this case, the pattern is the consistent indentation
:and character, visible at a glance.

If one mixes quotes of (say) level 6 and 7 in the text, using a
consistant '>' leader character, then unless said quotes are adjacent
to each other, humans cannot immediately tell which is which. Humans
are -not- good at detecting consistant indentation levels that only
differ by one character (but find it easier if the identation per level
is greater.)

Have you not, while coding, found yourself moving forward one line at a
time, staying in the same column, in order to verify that all your
indentation levels match up properly? Visual memory of indentations is
rapidly lost after a small number of lines, particularily once the part
to be matched is no longer visible on the screen. A unique character
per level is easier to recognize unless one can make the indentation
levels markedly different.


:With varying characters, of
:varying widths, that pattern recognition is useless, and analysis of
:the levels requires just that, analysis.

No, it just requires a glance at the character immediately proceeding
the quoted text to tell whether it is the same level or not.

:I see. So you're inflicting this on all of us just to spite CBF.

No, I have used the same quoting style for many years.
 
D

Dan P.

Walter Roberson said:
:On 22 Mar 2005 08:58:22 GMT, (e-mail address removed)-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 ...blah blah blah....


You don't actually work with customers in your job do you? If my customers
kept telling me over and over again that the user interface I designed was
difficult to read and understand, and I kept telling them "no it's not, it's
designed to be easier to read for humans", I'd be fired in a second!

Think about the logic of what you're saying. You have a bunch of readers,
who are all humans, telling you it is more *difficult* to read your quotes
and you keep saying it's *easier* for humans to read.

Well anyway, I hope you get a little enjoyment out of being a rebel in a
newsgroup. You gotta live for something in life I guess.


Dan
 
K

Keith Thompson

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.

Ok, here I am. Flattery will get you nowhere, but it's certainly
worth a try. :cool:}

Personally, I don't find your use of ':' for quotations to be a major
problem (for me; I don't speak for anyone else), but neither do I find
it at all helpful. I do find that the lack of a blank between the ':'
and the quoted text makes it more difficult to read; I have the same
problem with a '>' prefix not followed by a blank.

I sometimes use a ']' prefix to indicate that I'm quoting from
something other than a previous article in the thread. It's visually
similar enough to '>' that it looks like a quotation, but different
enough to serve as a marker that something interesting is going on.

For example, I might do something like (additionally meta-quoting by
indentation):

In another newsgroup a few months ago, so-and-so said something
that seems relevant to this discussion:
] Blah blah blah.
] Yadda yadda yadda.

The fact that a block of text is quoted from something other than the
current thread is interesting enough that it's worth calling attention
to it. The fact that a block of text is being quoted by Walter
Roberson isn't. (Nothing personal; the fact that a block of text is
being quoted by Keith Thompson isn't particularly interesting either.)

As for making it easier to read text that's quoted many levels deep,
I'm unconvinced. Very deep quotation is often a sign that somebody
didn't trim enough irrelevant text. And since there's no generally
accepted convention to use ':' in such cases, the only information it
conveys is that Walter Roberson is in the discussion (see above).

A number of people have said that they find the ':' prefix makes your
articles more difficult to read. Many have done so politely. I see
no reason to doubt them. I haven't seen anyone say that it makes them
easier to read. I take your word for it that your intent was greater
readability. Either you've been unsuccessful, or there's a silent
majority out there that disagrees with you.

I understand the temptation to dig in your heels when you think
someone is being rude. That creates a quandary when someone rudely
gives you good advice. (I'm not commenting on whether anyone actually
is being rude here.)

I'm reminded of the 8th of Henry Spencer's 10 Commandments for C
Programmers <http://www.plethora.net/~seebs/c/10com.html>:

Thou shalt make thy program's purpose and structure clear to thy
fellow man by using the One True Brace Style, even if thou likest
it not, for thy creativity is better used in solving problems than
in creating beautiful new impediments to understanding.

For the One True Brace Style, substitute the "> " quotation prefix.
Conformity in the little things is often a good thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top