Help me with a class assignment

J

jjmillertime

I need help with an assignment in my C programming class. My teacher
sucks and basically has told me that anything i need to know is in the
book, which is fine but she hasn't taught us much of anything and i've
been having a lot of problems with this program, so i scrapped it and
hopefully can get some help on here. any help would be great.

This is the assignment


Emphasis of this program is on arrays.

Write a program that will read in grades, calculates the average grade
and shows how much each score differs from the average.

Input: Prompt the user for input grades. Your program should read in
all the grades supplied by the user, but retain only those grades that
are in the range 0 to 100 (both values inclusive).

Your program should keep reading the grades from the keyboard until it
encounters EOF. You may store the grades as integers. You may assume
that no more than 80 grades will be supplied at any given time. Thus,
you may set the array size to 80. Use a macro for this purpose.


Output: Print the grades in a neatly formatted fashion as displayed
below (see 'Sample Runs' below)

Your program should terminate only when the user supplies EOF ( Ctrl D
- on UNIX)
Your program should be split into component functions.
Ensure that you have your Name, Class (Ex. cs211 - Day), Assignment
number, Description at the beginning of your program. Also, include
comments at appropriate positions in the program.
Your program should be indented neatly. DO NOT USE tabs (3 spaces
would be ideal)
Variable names should be descriptive.
Your program should be split up into several files as explained below:
o prog4.c : This will hold the main function
o prog4Fns.c: This file will consist of the component functions
o prog4.h: Header file for your program
You will need to #include your header file in both the source (.c)
files
DO NOT #include ONE SOURCE FILE INSIDE ANOTHER.
Compile your program with the -Wall option as follows:
o gcc -Wall prog4.c prog4Fns.c -o prog4
Hand in the files prog4.c prog4Fns.c electronically. Use the command:

~cs211d/bin/handin 4 prog4.c prog4Fns.c prog4.h

DO NOT e-mail the program to me. It will be deleted.



Some sample runs of the program:
Compile:
bugs> gcc -Wall prog4.c prog4Fns.c -o prog4
NOTE: In the following 'runs' blue font implies the lines are output
by the program, black font implies items typed in by the user.


In Run1, Run2, Run3 - we have no output displayed because the input
supplied does not consist of a single valid grade.

Further, note that the average is printed ONLY ONCE (ON THE FIRST
LINE)


Run1:
kira> prog4
Enter grades separated by spaces (min: 0, max: 100 ):
No grades entered. Exiting program
kira>



Run2:
kira> prog4
Enter grades separated by spaces (min: 0, max: 100 ): -24 -45 -93
-123
No valid grades entered. Exiting program
kira>



Run3:
kira> prog4
Enter grades separated by spaces (min: 0, max: 100 ): a
Invalid input; try again:
b
Invalid input; try again:
c
Invalid input; try again:
No grades entered. Exiting program
kira>




Run4:
kira> prog4
Enter grades separated by spaces (min: 0, max: 100 ): 23 -34 123 0
100
Discarding the 2 invalid grade(s) entered
Grade Average Deviation from Average
===================================================
23 41.00 -18.00
0 -41.00
100 59.00
kira>





Run5:
kira> prog4
Enter grades separated by spaces (min: 0, max: 100 ): 98 93 77
24 90



99

45

-123

12345

0

100

A
Invalid input; try again:

87

Discarding the 2 invalid grade(s) entered
Grade Average Deviation from Average
===================================================
98 71.30 26.70
93 21.70
77 5.70
24 -47.30
90 18.70
99 27.70
45 -26.30
0 -71.30
100 28.70
87 15.70
kira>

Run6:
kira> prog4
Enter grades separated by spaces (min: 0, max: 100 ): 98
99
98
97
98
Grade Average Deviation from Average
===================================================
98 98.00 0.00
99 1.00
98 0.00
97 -1.00
98 0.00
kira>
 
U

user923005

Show us the code for what you have already tried and we will show you
where you went off.
 
J

jjmillertime

Show us the code for what you have already tried and we will show you
where you went off.

actually i don't have any code for it left, i deleted it because i was
frustrated. Could you help me start it out?
 
O

osmium

I need help with an assignment in my C programming class. My teacher
sucks and basically has told me that anything i need to know is in the
book, which is fine but she hasn't taught us much of anything and i've
been having a lot of problems with this program, so i scrapped it and
hopefully can get some help on here. any help would be great.

<snip>

It's hard to know where you need help. There is a lot of bulk there
describing a pretty simple problem. As I see it you read in up to 80 grades
and put them in an array, compute the mean grade, and compute and output the
difference between each grade and the mean. You are to cast out invalid
grades. Can you write a single monolithic program, ignoring the rest of the
instructions, that does that? If you can't, which part can't you do? If
you get this fundamental program working, you can then putter with it and
change it to meet the exquisitely detailed instructions you were given.

If you don't post *something* in the way of code you are unlikely to get any
help here. If nothing else, post your scrapped attempt.
 
U

user923005

actually i don't have any code for it left, i deleted it because i was
frustrated. Could you help me start it out?

int main(int argc, char **argv)
{
/* Put your work here. */
return 0;
}

man fgets might be a good place to start.
You might also be interested in strtok() or sscanf().

The description you have given is plenty to complete the assignment.
Just put in C code that does each of the described steps.
Make the adjustments in baby steps and write simple functions to do
simple jobs.
After you are sure that your simple function works, then go on to the
next step.
 
J

jjmillertime

<snip>

It's hard to know where you need help. There is a lot of bulk there
describing a pretty simple problem. As I see it you read in up to 80 grades
and put them in an array, compute the mean grade, and compute and output the
difference between each grade and the mean. You are to cast out invalid
grades. Can you write a single monolithic program, ignoring the rest of the
instructions, that does that? If you can't, which part can't you do? If
you get this fundamental program working, you can then putter with it and
change it to meet the exquisitely detailed instructions you were given.

If you don't post *something* in the way of code you are unlikely to get any
help here. If nothing else, post your scrapped attempt.

honestly anything would help, i would really like an example that i
could work off of, and an idea of where to go from there. i don't know
if anyone would be willing to do that. basically i've figured out that
my mind doesn't really work the way computer programmer minds work and
it would be nice to have someone write out at least a starting point
and maybe explain how i would finish it
 
U

user923005

honestly anything would help, i would really like an example that i
could work off of, and an idea of where to go from there. i don't know
if anyone would be willing to do that. basically i've figured out that
my mind doesn't really work the way computer programmer minds work and
it would be nice to have someone write out at least a starting point
and maybe explain how i would finish it

Start by reading in a list of integers until EOF.
Store them in an array of dimension at least 80.
You can assume standard input and output (stdin, stdout).
All the steps to solve this problem are easy.
If you put your best foot forward, we'll be done in two hours.
 
J

jjmillertime

Start by reading in a list of integers until EOF.
Store them in an array of dimension at least 80.
You can assume standard input and output (stdin, stdout).
All the steps to solve this problem are easy.
If you put your best foot forward, we'll be done in two hours.

ok, how do i get the program to put the numbers in the array when they
are typed?
would i use a for statement?
 
E

Eric Sosman

actually i don't have any code for it left, i deleted it because i was
frustrated. Could you help me start it out?

First things first: TURN OFF YOUR COMPUTER, RIGHT NOW!!!

Most "undelete" programs are effective only if the disk
has not been used, or used only very lightly. Every moment
you leave the computer running is another moment that it
might choose to re-use the disk sectors that formerly held
your work and may still hold its remains, so SHUT IT DOWN!!!
Don't wait; the computer is so much faster than you are that
it beggars imagination.

Once it's turned off, take the whole thing to a commercial
data recovery firm. (It's probably enough to take them just
the disk drive, but why risk damaging it with a clumsy removal?
Let the experts do it.) The data recovery people will tell
you whether the deleted files can be retrieved, and will quote
you a price for retrieving it. My understanding is that their
prices are very steep, but given the enormous value of the code
you lost, I'm sure you will count it cheap.

When (if) they recover your code for you, and when (if) you
have worked off the loan you took to pay them, come back here
and we'll help you debug it. Good luck!
 
A

Anthony Irwin

user923005 said:
Show us the code for what you have already tried and we will show you
where you went off.

Hi,

I am teaching myself C and thought I would use this guys assignment
for my learning purposes also.

I have written some sample code but I don't know how to trap EOF what
am I doing wrong.


#include <stdio.h>
#include <stdlib.h>

int main(void) {
int nbr_grade;
char chr_grade[20];

while(1) {
printf("enter a grade number or ctrl + d to quit: ");

fgets(chr_grade, sizeof(chr_grade), stdin);
sscanf(chr_grade, "%d", &nbr_grade);

if (nbr_grade == EOF)
break;

printf("\nthe number entered is %d\n", nbr_grade);
}

return 0;
}
 
U

user923005

user923005 said:
Show us the code for what you have already tried and we will show you
where you went off.

Hi,

I am teaching myself C and thought I would use this guys assignment
for my learning purposes also.

I have written some sample code but I don't know how to trap EOF what
am I doing wrong.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int nbr_grade;
char chr_grade[20];

while(1) {
printf("enter a grade number or ctrl + d to quit: ");

fgets(chr_grade, sizeof(chr_grade), stdin);

Check the return of fgets() because it will be NULL at EOF.
sscanf(chr_grade, "%d", &nbr_grade);

Check to see how many entries got converted. If it isn't exactly 1,
then there is a problem.
if (nbr_grade == EOF)
break;

nbr_grade will never be EOF, except by chance (some bad input with a
certain negative grade).
 
J

jjmillertime

Start by reading in a list of integers until EOF.
Store them in an array of dimension at least 80.
You can assume standard input and output (stdin, stdout).
All the steps to solve this problem are easy.
If you put your best foot forward, we'll be done in two hours.

ok this is what i have so far, i know it's probably not right, so some
help would be great

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int ent_scores (double, int);
int calc_average (double, int, double);
int calc_diff (double, int, double, double);

int main (void)
{
int SIZE = 80;
double avg_score[SIZE];
double avg_diff = 0;

int ent_score (double score, int SIZE);
int calc_avg (double score, int SIZE, double avg_score);
int calc_difference (double score, int SIZE, double avg_score,
double diff);
int print_grade (double avg_score);

printf ("The average score is: %d", avg_score);
printf ("The difference is : %d", avg_diff);

}

int ent_scores(double score, int SIZE, grade)
{
int grade[80]
for (int grade = 0; grade < SIZE; ++grade)
{
printf ("Please enter grades: ");
scanf (%d, &score[grade]);
while( score[grade] > 100 || score[grade] < 0 )
{
printf (" Invalid grade - please Re-enter grades/n");
printf (" between 0 and 100: ");
scanf (%d, &score[ grade ];
}
}

}

int calc_average(double score, int SIZE, double avg_score, int i)
{
double sum = 0;
for(int i = 0; i < SIZE; ++i)
for(int i = 0; i < SIZE; ++i)
sum += score;

avg_score = sum/SIZE;
return (avg_score);
}


void calc_diff (double score, int SIZE, double avg_score , double
diff)

{
double sum_of_squares = 0;

for (int grade = 0; grade < SIZE; ++grade)
{
double diff = (score[grade] - avg_score);
sum_of_squares +=(diff * diff);
}
diff = (sum_of_squares / (SIZE - 1));
}
 
J

jjmillertime

Start by reading in a list of integers until EOF.
Store them in an array of dimension at least 80.
You can assume standard input and output (stdin, stdout).
All the steps to solve this problem are easy.
If you put your best foot forward, we'll be done in two hours.

ok this is what i have so far, i know it's probably not right, so some
help would be great

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int ent_scores (double, int);
int calc_average (double, int, double);
int calc_diff (double, int, double, double);

int main (void)
{
int SIZE = 80;
double avg_score[SIZE];
double avg_diff = 0;

int ent_score (double score, int SIZE);
int calc_avg (double score, int SIZE, double avg_score);
int calc_difference (double score, int SIZE, double avg_score,
double diff);
int print_grade (double avg_score);

printf ("The average score is: %d", avg_score);
printf ("The difference is : %d", avg_diff);

}

int ent_scores(double score, int SIZE, grade)
{
int grade[80]
for (int grade = 0; grade < SIZE; ++grade)
{
printf ("Please enter grades: ");
scanf (%d, &score[grade]);
while( score[grade] > 100 || score[grade] < 0 )
{
printf (" Invalid grade - please Re-enter grades/n");
printf (" between 0 and 100: ");
scanf (%d, &score[ grade ];
}
}

}

int calc_average(double score, int SIZE, double avg_score, int i)
{
double sum = 0;
for(int i = 0; i < SIZE; ++i)
for(int i = 0; i < SIZE; ++i)
sum += score;

avg_score = sum/SIZE;
return (avg_score);
}


void calc_diff (double score, int SIZE, double avg_score , double
diff)

{
double sum_of_squares = 0;

for (int grade = 0; grade < SIZE; ++grade)
{
double diff = (score[grade] - avg_score);
sum_of_squares +=(diff * diff);
}
diff = (sum_of_squares / (SIZE - 1));
}
 
A

Anthony Irwin

user923005 said:
Check the return of fgets() because it will be NULL at EOF.

Thanks for that got it working great now.
Check to see how many entries got converted. If it isn't exactly 1,
then there is a problem.

I don't understand what you mean here.


Kind Regards,
Anthony Irwin
 
U

user923005

On Mar 19, 6:05 pm, (e-mail address removed) wrote:
Start by reading in a list of integers until EOF.
Store them in an array of dimension at least 80.
You can assume standard input and output (stdin, stdout).
All the steps to solve this problem are easy.
If you put your best foot forward, we'll be done in two hours.

ok this is what i have so far, i know it's probably not right, so some
help would be great

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int ent_scores (double, int);
int calc_average (double, int, double);
int calc_diff (double, int, double, double);

int main (void)
{
int SIZE = 80;
double avg_score[SIZE];
double avg_diff = 0;

int ent_score (double score, int SIZE);
int calc_avg (double score, int SIZE, double avg_score);
int calc_difference (double score, int SIZE, double avg_score,
double diff);
int print_grade (double avg_score);

printf ("The average score is: %d", avg_score);
printf ("The difference is : %d", avg_diff);

}

If you try compiling it before you send it here, it will help you to
have less passes.

The places where you call the function, it is not supposed to look
like a prototype.
When you actually use it, instead of:
int ent_score (double score, int SIZE);

It should look like:

value = ent_score (score, SIZE);

When a parameter is an array, declare it like this:
int ent_score (double *score_list, int SIZE);

or like this:
int ent_score (double score_list[], int SIZE);

which means more or less the same thing.
 
J

jjmillertime

ok this is what i have so far, i know it's probably not right, so some
help would be great
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int ent_scores (double, int);
int calc_average (double, int, double);
int calc_diff (double, int, double, double);
int main (void)
{
int SIZE = 80;
double avg_score[SIZE];
double avg_diff = 0;
int ent_score (double score, int SIZE);
int calc_avg (double score, int SIZE, double avg_score);
int calc_difference (double score, int SIZE, double avg_score,
double diff);
int print_grade (double avg_score);
printf ("The average score is: %d", avg_score);
printf ("The difference is : %d", avg_diff);

If you try compiling it before you send it here, it will help you to
have less passes.

The places where you call the function, it is not supposed to look
like a prototype.
When you actually use it, instead of:
int ent_score (double score, int SIZE);

It should look like:

value = ent_score (score, SIZE);

When a parameter is an array, declare it like this:
int ent_score (double *score_list, int SIZE);

or like this:
int ent_score (double score_list[], int SIZE);

which means more or less the same thing.

like this?


int ent_score (double score_list[], int SIZE);
{
int score_list[80]
for (int score_list = 0; score_list < SIZE; ++score_list)
{
printf ("Please enter grades: ");
scanf (%d, &score_list[]);
while( score_list > 100 || score_list < 0 )
{
printf (" Invalid grade - please Re-enter grades/n");
printf (" between 0 and 100: ");
scanf (%d, score_list;
}
}
 
U

user923005

You are biting off too big of a piece at once. Get the first things
to work first, and then move on to the second things.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


size_t ent_scores(double *, size_t);
double calc_average(double *, size_t);
void calc_diffs(double *, size_t);


#define ARRAY_SIZE 80

int main(void)
{
double score_list[ARRAY_SIZE];
double average = 0;

int grade_count = ent_scores(score_list, ARRAY_SIZE);
/*
average = calc_avg(score_list, ARRAY_SIZE);
calc_diffs(score_list, ARRAY_SIZE);


printf("The average score is: %.2f", avg_score);
*/
return 0;

}

size_t ent_scores(double *score, size_t score_size)
{
char string[80];
size_t grades = 0;
char *str;
int converted = 0;
printf("Please enter grades: ");
do {
str = fgets(string, sizeof string, stdin);
if (str) {
converted = sscanf(string, "%lf", &score[grades]);
while (score[grades] > 100 || score[grades] < 0) {
puts("Invalid grade - grades must be between 0 and
100.");
puts("Please try again:");
str = fgets(string, sizeof string, stdin);
if (str)
converted = sscanf(string, "%lf", &score[grades]);
}
if (converted == 1)
grades++;
}
} while (grades < score_size && str && converted == 1);
return grades;
}
 
U

user923005

Thanks for that got it working great now.



I don't understand what you mean here.

int converted;
....

converted = sscanf(chr_grade, "%d", &nbr_grade);
if (!(converted == 1)) handle_error();
 
J

jjmillertime

You are biting off too big of a piece at once. Get the first things
to work first, and then move on to the second things.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

size_t ent_scores(double *, size_t);
double calc_average(double *, size_t);
void calc_diffs(double *, size_t);

#define ARRAY_SIZE 80

int main(void)
{
double score_list[ARRAY_SIZE];
double average = 0;

int grade_count = ent_scores(score_list, ARRAY_SIZE);
/*
average = calc_avg(score_list, ARRAY_SIZE);
calc_diffs(score_list, ARRAY_SIZE);

printf("The average score is: %.2f", avg_score);
*/
return 0;

}

size_t ent_scores(double *score, size_t score_size)
{
char string[80];
size_t grades = 0;
char *str;
int converted = 0;
printf("Please enter grades: ");
do {
str = fgets(string, sizeof string, stdin);
if (str) {
converted = sscanf(string, "%lf", &score[grades]);
while (score[grades] > 100 || score[grades] < 0) {
puts("Invalid grade - grades must be between 0 and
100.");
puts("Please try again:");
str = fgets(string, sizeof string, stdin);
if (str)
converted = sscanf(string, "%lf", &score[grades]);
}
if (converted == 1)
grades++;
}
} while (grades < score_size && str && converted == 1);
return grades;

}

what is the size_t referring to?
 
J

jjmillertime

int converted;
...

converted = sscanf(chr_grade, "%d", &nbr_grade);
if (!(converted == 1)) handle_error();

what does handle_error do? i've never seen it used
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top