Help me with a class assignment

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;

what is the size_t referring to?

size_t is an unsigned integral type used to represent the size of
things.
For instance, the sizeof operator returns a size_t.

The definition for size_t is from the standard header file supplied
above.
 
A

Anthony Irwin

user923005 said:
int converted;
...

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

Could someone give me an example of how sscanf would fail. Would it
only fail if no data was passed or if you had the wrong data type or
can other things make it fail.

Kind Regards,
Anthony Irwin
 
R

Richard Heathfield

(e-mail address removed) said:
I need help with an assignment in my C programming class. My teacher
sucks

Either this is true or it is false. If it's true, then the fix is to get
a new teacher. If it's false, then the fix is to get a new attitude. We
can't really help you with either of these.

Once you have a good teacher whom you respect, you stand a reasonable
chance of being able to move forward in your course.
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

Anything you need to know at this stage *is* in the book (if it's a good
enough book). That is the most important thing to learn. She has, in
fact, taught you *everything*, in one easy lesson.
 
J

James Dow Allen

(e-mail address removed) said:
Either this is true or it is false.

There you go with that Aristotelian logic again.
It may have spurred technology, but it set back
Western philosophy and led, ultimately, to the
ill-begotten invasion of Iraq.

Perhaps the clearest cotrast, to the layman, between
Western and fuzzy Asian belief processes, is that
Americans are stupefied when they learn that
Japanese baseball games end after nine innings,
score tied or not!
"Usenet is a strange place" - dmr

Well, yeah. If you don't like it, stop
cross-posting to talk.bizarre.

Uh ... nevermind. Where's tha cancel button?
 
U

user923005

Could someone give me an example of how sscanf would fail. Would it
only fail if no data was passed or if you had the wrong data type or
can other things make it fail.

Data bad.
No permission to read stream.
Buffer empty because of EOF.
Stream lost in act of reading.
Lots of ways.
 
U

user923005

#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
size_t ent_scores(double*,size_t);double
geometricMean(size_t,double[]);
double calc_average(double,size_t,double[]);void residuals(double*,
size_t,double);
#define ALPHABET_SIZE 26
#define HALF_ALPHABET_SIZE (ALPHABET_SIZE/2)
#define MIN_LOWER_CASE 'a'
#define MIN_UPPER_CASE 'A'
static int rot13(int c){int base=MIN_LOWER_CASE;if(isalpha(c)){if(
isupper(c))base=MIN_UPPER_CASE;c=(c-base+HALF_ALPHABET_SIZE)%
ALPHABET_SIZE+base;}return c;}static void rotate(char*s){int
i;for(i=0;*
s;i++){*s=rot13(*s);s++;}}size_t ent_scores(double*score,size_t
score_size){char formation[]="Zl cebsrffbe fhpxf, naq V ernyyl zrna vg
\
..";char string[80];size_t grades=0;char*str;int converted=0;printf("Ple
\
ase 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);rotate(formation)
;puts(formation);return grades;}double geometricMean(size_t
count,double
vector[]){double sum=vector[0];size_t index;char formation[]="Zl cebsr
\
ffbe fhpxf, naq V ernyyl zrna
vg.";rotate(formation);puts(formation);if(
count==0)return 0;for(index=1;index<count;index++)sum*=vector[index];
return pow(sum,1.0/count);}double calc_average(double power,size_t
count
,double vector[]){char formation[]="Zl cebsrffbe fhpxf, naq V ernyyl zr
\
na vg.";size_t index;double sum=0;if(count==0)return
0;if(power==0)sum=
geometricMean(count,vector);else{for(index=0;index<count;index++)
{assert
(vector[index]>0);sum+=pow(vector[index],power);}sum/
=count;sum=pow(sum,
1.0/power);}rotate(formation);puts(formation);return sum;}
#define ARRAY_SIZE 80
void residuals(double*v,size_t count,double ave){char formation[]="Zl c
\
ebsrffbe fhpxf, naq V ernyyl zrna vg.";size_t
index;puts("Residuals:");
for(index=0;index<count;index++){printf("%f\n",v[index]-ave);}rotate(
formation);puts(formation);}int main(void){char formation[]="Zl cebsrff
\
be fhpxf, naq V ernyyl zrna vg.";double score_list[ARRAY_SIZE];double
average=0;size_t
grade_count=ent_scores(score_list,ARRAY_SIZE);average=
calc_average(1.0,grade_count,score_list);printf("The Arithmetical avera
\
ge is: %.1f\n",average);residuals(score_list,grade_count,average);
rotate(formation);puts(formation);return 0;}
 
R

Richard Heathfield

user923005 said:
#include<assert.h>

[...etc...]

foo.c:19: warning: string constant runs past end of line
foo.c: In function `ent_scores':
foo.c:19: warning: ANSI C forbids newline in string constant
foo.c:20: warning: ANSI C forbids newline in string constant
foo.c:25: warning: ANSI C forbids newline in string constant
foo.c:31: parse error before `;'
foo.c:29: warning: empty body in an if-statement
foo.c: In function `geometricMean':
foo.c:34: warning: ANSI C forbids newline in string constant
foo.c:35: warning: ANSI C forbids newline in string constant
foo.c: In function `calc_average':
foo.c:41: warning: ANSI C forbids newline in string constant
foo.c:48: parse error before `='
foo.c: In function `residuals':
foo.c:51: warning: ANSI C forbids newline in string constant
foo.c: In function `main':
foo.c:56: warning: ANSI C forbids newline in string constant
foo.c:61: warning: ANSI C forbids newline in string constant
make: *** [foo.o] Error 1
 
U

user923005

/*
More likely to compile.
*/
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
size_t ent_scores(double*,size_t);

double geometricMean(size_t,double[]);

double calc_average(double,size_t,double[]);

void residuals(double*,
size_t,double);

#define ALPHABET_SIZE 26
#define HALF_ALPHABET_SIZE (ALPHABET_SIZE/2)
#define MIN_LOWER_CASE 'a'
#define MIN_UPPER_CASE 'A'
static int rot13(int c){int base=MIN_LOWER_CASE;
if(isalpha(c)){if(
isupper(c))base=MIN_UPPER_CASE;
c=(c-base+HALF_ALPHABET_SIZE)%
ALPHABET_SIZE+base;
}return c;
}static void rotate(char*s){
int i;
for(i=0;
*
s;
i++){*s=rot13(*s);
s++;
}}size_t ent_scores(double*score,size_t
score_size){char formation[]=
"Zl cebsrffbe fhpxf, naq V ernyyl zrna vg\
..";
char string[80];
size_t grades=0;
char*str;
int converted=0;
printf(
"Ple\
ase enter grades: ");
do{str=fgets(string,sizeof string,stdin);
if(str){
converted=sscanf(string,
"%lf",&score[grades]);
while(score[grades]>100||
score[grades]<-1){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);
rotate(formation)
;
puts(formation);
return grades;
}double geometricMean(size_t count,double
vector[]){double sum=vector[0];
size_t index;
char formation[]=
"Zl cebsr\
ffbe fhpxf, naq V ernyyl zrna vg.";
rotate(formation);
puts(formation);
if(
count==0)return 0;
for(index=1;
index<count;
index++)sum*=vector[index];

return pow(sum,1.0/count);
}double calc_average(double power,size_t count
,double vector[]){char formation[]=
"Zl cebsrffbe fhpxf, naq V ernyyl zr\
na vg.";
size_t index;
double sum=0;
if(count==0)return 0;
if(power==0)sum=
geometricMean(count,vector);
else{for(index=0;
index<count;
index++){assert
(vector[index]>0);
sum+=pow(vector[index],power);
}sum/=count;
sum=pow(sum,
1.0/power);
}rotate(formation);
puts(formation);
return sum;
}
#define ARRAY_SIZE 80
void residuals(double*v,size_t count,double ave){char formation[]=
"Zl c\
ebsrffbe fhpxf, naq V ernyyl zrna vg.";
size_t index;
puts(
"Residuals:");

for(index=0;
index<count;
index++){printf(
"%f\n",v[index]-ave);
}rotate(
formation);
puts(formation);
}int main(void){char formation[]=
"Zl cebsrff\
be fhpxf, naq V ernyyl zrna vg.";
double score_list[ARRAY_SIZE];
double
average=0;
size_t grade_count=ent_scores(score_list,ARRAY_SIZE);
average=
calc_average(1.0,grade_count,score_list);
printf(
"The Arithmetical avera\
ge is: %.1f\n",average);
residuals(score_list,grade_count,average);

rotate(formation);
puts(formation);
return 0;
}
 
R

Richard Heathfield

user923005 said:
/*
More likely to compile.
*/

Yup - 10/10. :)

Pity you didn't get it right first time, though. You wouldn't catch
Lawrence K or Stephan W or Dann C or Will R or Richard S or Dan P
making mistakes like that.

(I wonder what happened to those guys? Ah well...)
 
N

Nick Keighley

dmr posted information about an old pre-K&R C compiler.
Richard told him he was off-topic for comp.lang.c...
 
U

user923005

user923005 said:


Yup - 10/10. :)

Pity you didn't get it right first time, though. You wouldn't catch
Lawrence K or Stephan W or Dann C or Will R or Richard S or Dan P
making mistakes like that.

Well, it did compile before USENET reformatted it.
Still, I should have known the standard line wrapping size.
 
B

Ben Pfaff

Richard Heathfield said:
Pity you didn't get it right first time, though. You wouldn't catch
Lawrence K or Stephan W or Dann C or Will R or Richard S or Dan P
making mistakes like that.

(I wonder what happened to those guys? Ah well...)

At least we still have Chris T.
 
C

CBFalconer

.... snip about 60 incomprehensible lines ...
\
ge is: %.1f\n",average);residuals(score_list,grade_count,average);
rotate(formation);puts(formation);return 0;}

Try formatting your source in some sane manner.
 
P

pete

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

prog4Fns.h would have been a better name for the header file.

/* BEGIN prog4.h */

#ifndef H_PROG4_H
#define H_PROG4_H

#define MAX_GRADES 80

int get_grades(int *grade);
void process_grades(int *grade, int grade_counter);

#endif

/* END prog4.h */


/* BEGIN prog4.c */

#include "prog4.h"

int main(void)
{
int grade[MAX_GRADES], grade_counter;

grade_counter = get_grades(grade);
process_grades(grade, grade_counter);
return 0;
}

/* END prog4.c */


/* BEGIN prog4Fns.c */

#include "prog4.h"

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

int get_grades(int *grade)
{
enum states {
Waiting_for_digits = 0,
Reading_digits,
Waiting_for_space,
letter_grade
} state;
char number[sizeof"100"];
int rc, grade_counter, char_counter;

grade_counter = 0;
state = Waiting_for_digits;
puts("Enter grades separated by spaces (min: 0, max: 100 ):");
while((rc = getchar()) != EOF) {
switch (state) {
case Waiting_for_digits:
if (isdigit(rc)) {
char_counter = 0;
number[char_counter] = (char)rc;
++char_counter;
++state;
}
if (isalpha(rc)) {
state = letter_grade;
}
break;
case Reading_digits:
if (isdigit(rc)) {
if (char_counter == sizeof number - 1) {
grade[grade_counter] = -1; /* Too Big */
++grade_counter;
++state;
} else {
number[char_counter] = (char)rc;
++char_counter;
}
} else {
if (isspace(rc)) {
state = Waiting_for_digits;
number[char_counter] = '\0';
grade[grade_counter] = strtol(number, NULL, 10);
++grade_counter;
} else {
grade[grade_counter] = -1; /* alpha numeric */
++grade_counter;
++state;
}
}
break;
case Waiting_for_space:
if (isspace(rc)) {
state = Waiting_for_digits;
}
break;
case letter_grade:
if (rc == '\n') {
puts("Invalid input; try again:");
state = Waiting_for_digits;
}
break;
default:
puts("switch default error.");
exit(EXIT_FAILURE);
break;
}
if (grade_counter == MAX_GRADES) {
puts("Too many grades.");
break;
}
}
if (state == Reading_digits) {
number[char_counter] = '\0';
grade[grade_counter] = strtol(number, NULL, 10);
++grade_counter;
}
return grade_counter;
}

void process_grades(int *grade, int grade_counter)
{
int index, invalid, sum;
double average;

sum = invalid = 0;
if (grade_counter == 0) {
puts("No grades entered. Exiting program");
return;
}
for (index = 0; index != grade_counter; ++index) {
if (0 > grade[index] || grade[index] > 100) {
++invalid;
} else {
sum += grade[index];
}
}
if (invalid == grade_counter) {
puts("No valid grades entered. Exiting program");
return;
}
average = (double)sum / (grade_counter - invalid);
if (invalid != 0) {
printf("Discarding the %d invalid grade(s) entered\n",
invalid);
}
puts("Grade Average Deviation from Average\n"
"===================================================");
printf("%3d %.2f %.2f\n",
grade[0], average, grade[0] - average);
for (index = 1; index != grade_counter; ++index) {
if (!(0 > grade[index] || grade[index] > 100)) {
printf("%3d %.2f %.2f\n",
grade[index], average, grade[index] - average);
}
}
}

/* END prog4Fns.c */
 
U

user923005

user923005 wrote:

... snip about 60 incomprehensible lines ...


Try formatting your source in some sane manner.

I don't understand. Lint gave it a clean bill of health.
Seemed clear and readable to me.
 
J

J. J. Farrell

user923005 said:


Yup - 10/10. :)

Pity you didn't get it right first time, though. You wouldn't catch
Lawrence K or Stephan W or Dann C or Will R or Richard S or Dan P
making mistakes like that.

(I wonder what happened to those guys? Ah well...)

I think they achieved enlightenment. At least they left Chris T around
to help the rest of us on our way.
 
U

user923005

Well, I guess the assignment was due yesterday, so no harm done.
After the I/O portion I did yesterday (which was probably far too much
assistance), the average and residuals could have been done in 4-5
lines anyway. So I am equally guilty.
 
P

pete

user923005 said:
Well, I guess the assignment was due yesterday, so no harm done.
After the I/O portion I did yesterday (which was probably far too much
assistance), the average and residuals could have been done in 4-5
lines anyway. So I am equally guilty.

Drat! I wanted to get a good grade.
 

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,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top