![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 8
| The assignment reads: your program is to read from a file students.dat which may contain a list of student names that could contain as many as eleven characters. In addition to the list of names, your program should read four sets of numbers which represent two exams worth 20%, a final exam worth 30%, and homework worth 30%. The student's name and grades is on the first line, with no title lines. Find the weighted average grade for all students. The average grade is to berounded to the nearest integer, not to truncated to an integer. Then, sort the list of students by grade, and assign letter grades to each student. The sorting can be a selection or a bubble sort, and is to be done in function sub-program. Say also if the student passes or fails. Put the sorted list into a file called outstu.dat. Put a two line title above the output. The grade distribution is 85-100 A 70-84 B 55-69 C 40-54 D 0-39 F This is what i have so far: Code:
/* Homework 12 */
#include FILENAME "students.dat"
#include <stdio.h>
#include <math.h>
#include <string.h>
struct record
{
char name[12];
int ex1, ex2, final, hw, ave, grade passf1[5];
};
int main(void)
{
struct record s[50];
int nos, i=0;
FILE *students;
{
while (fscanf(students, "%s %i %i %i %i," s[i].name, s[i].ex1, s[i].ex2, s[i].final, s[i].hw)==5)
++i;
s[i].ave=s[i].ex1*0.2+s[i].ex2*0.2+s[i].final*0.3+s[i].hw*0.3;
if(s[i].ave>=85) s[i].grade='A';
if(s[i].ave>=70) s[i].grade='B';
if(s[i].ave>=55) s[i].grade='C';
if(s[i].ave>=40) s[i].grade='D';
if(s[i].ave>=0) s[i].grade='F';
if(s[i].ave>=40)
strcpy(s[i].passf1, "pass");
else strcpy(s[i].pass1, "fail");
sort(s, nos);
for(i=0, i<nos, ++i);
fprintf(outf, "%-11s %5i %5i %5i %5i %5i %c %4sh," s[i].name, s[i].ex1, s[i].ex2, s[i].final,
s[i].hw)
void sort(record s[], int n);
int k,l m;
record hold;
for(k=0, k<=2, ++k);
m=k;
for(i=k+1, j<=n);
if(s[j] < x[m], ave);
m=j;
hold=x[m];
x[m]=x[k];
x[k]=hold;
|
| lbillys is offline | |
| | #2 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #3 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| This code is so far from resembling something that might be called a C program it is not funny. Really, really, really bad. Horrific. It is a very serious mistake to think you can just start conceptualizing and typing and hope that when you are finished, that is the time to compile and test. You should be compiling and testing the code you are writing every 2 or 3 lines. That means breaking the task down into "sub tasks". You have a struct, and a datafile to read. Before you proceed to the more complicated needs of the program, you should write something that will read the file into the the struct array, and then print out all the data in the array, without performing any calculations or sorting. At least then you will have some foundation to build on, which right now you do not. If you cannot do that, drop the class now rather than failing later. You are a long way from completing this assignment. I'm sorry if this seems mean. I just want to make it very clear to you that, for whatever reason, you are screwing this up big time. AND YOU DEFINITELY NEED TO INDENT.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 05-16-2009 at 06:02 PM. |
| MK27 is online now | |
| | #4 |
| Registered User Join Date: May 2009
Posts: 8
| okay here is my retry Code: /* Homework 12 */
#define FILENAME "students.dat"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#define FILENAME2 "outstu.dat"
struct record
{char name[12]; int ex1, ex2, final, hw, ave, grade passfail[5];} record;
int main(void)
{
struct record s[50];
int nos=0, i=0;
struct record h[50]
FILE *grades
FILE *students;
/* Open file. */
void sort(record);
students=fopen(FILENAME, "r");
grades=fopen(FILENAME2, "w");
/*Read file. */
{
for(i=0; i<50; i++)
while (fscanf(students, "%s %i %i %i %i," s[i].name, s[i].ex1, s[i].ex2, s[i].final, s[i].hw)==5)
nos=1;
nos++
/*Determine Average for each Student */
s[i].ave=(s[i].ex1*0.2)+(s[i].ex2*0.2)+(s[i].final*0.3)+(s[i].hw*0.3)+0.5;
if(s[i].ave>=85) s[i].grade='A';
else
if(s[i].ave>=70) s[i].grade='B';
else
if(s[i].ave>=55) s[i].grade='C';
else
if(s[i].ave>=40) s[i].grade='D';
else
if(s[i].ave>=0) s[i].grade='F';
/*Determine if the student passed or failed*/
if(s[i].ave>=40)
strcpy(s[i].passfail, "pass");
else strcpy(s[i].passfail, "fail");
sort(s, nos);
for(i=0, i<nos, ++i);
fprintf(grades, "%11s %5i %5i %5i %c %4s \n", s[i].name, s[i].ex1, s[i].ex2, s[i].final, s[i].hw, s[i].grade, s[i].passfail);
/* Sorting Grades */
void sort(record s[], int nos);
{
int k, j, m, nos, s[50];
struct record hold;
for(k=0; k<=nos-2; k++);
m=k;
for(j=k+1; j<=nos-1; j++)
{
if(s[j] < s[m], ave);
m=j;
hold=s[m];
s[m]=s[k];
s[k]=hold;
}
}
return;
}
}
|
| lbillys is offline | |
| | #5 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| And when you compile that, what happens?
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #6 |
| Registered User Join Date: May 2009
Posts: 8
| i just got a lot of errors and warnings... hcp-129-8-135-3% gcc hw12.c -o hw12.out hw12.c:11: warning: no semicolon at end of struct or union hw12.c:11: error: syntax error before "passfail" hw12.c:11: warning: data definition has no type or storage class hw12.c: In function `main': hw12.c:15: error: storage size of 's' isn't known hw12.c:18: error: syntax error before "FILE" hw12.c:22: warning: parameter names (without types) in function declaration hw12.c:23: error: `students' undeclared (first use in this function) hw12.c:23: error: (Each undeclared identifier is reported only once hw12.c:23: error: for each function it appears in.) hw12.c:24: error: `grades' undeclared (first use in this function) hw12.c:29: error: syntax error before "s" hw12.c:35: error: syntax error before "s" hw12.c:53: error: syntax error before ')' token hw12.c:58: error: syntax error before "s" hw12.c:61: error: storage size of 'hold' isn't known hw12.c:68: error: `ave' undeclared (first use in this function) |
| lbillys is offline | |
| | #7 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #9 |
| Registered User Join Date: May 2009
Posts: 8
| alright i have gotten all the errors to leave except for two and five warnings. I am unsure how to fix these. hw12.c:20: warning: parameter names (without types) in function declaration hw12.c:27: warning: passing arg 1 of `fscanf' from incompatible pointer type hw12.c:27: warning: passing arg 2 of `fscanf' from incompatible pointer type hw12.c:47: warning: passing arg 1 of `strcpy' from incompatible pointer type hw12.c:48: warning: passing arg 1 of `strcpy' from incompatible pointer type hw12.c:68: error: incompatible types in assignment hw12.c:70: error: incompatible types in assignment here is my current code. Code: /* Homework 12 */
#define FILENAME "students.dat"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#define FILENAME2 "outstu.dat"
struct record {char name[12]; int ex1, ex2, final, hw, ave, grade, passfail[5];} record;
int main(void)
{
struct record s[50];
int nos=0, i=0;
FILE*grades;
FILE*students;
/* Open file. */
void sort(record);
students=fopen(FILENAME, "r");
grades=fopen(FILENAME2, "w");
/*Read file. */
{
for(i=0; i<50; i++)
while (fscanf("students, %s %i %i %i %i", &s[i].name, &s[i].ex1, &s[i].ex2, &s[i].final, &s[i].hw)==5)
nos=1;
nos++;
/*Determine Average for each Student */
s[i].ave=(s[i].ex1*0.2)+(s[i].ex2*0.2)+(s[i].final*0.3)+(s[i].hw*0.3)+0.5;
if(s[i].ave>=85) s[i].grade='A';
else
if(s[i].ave>=70) s[i].grade='B';
else
if(s[i].ave>=55) s[i].grade='C';
else
if(s[i].ave>=40) s[i].grade='D';
else
if(s[i].ave>=0) s[i].grade='F';
/*Determine if the student passed or failed*/
if(s[i].ave>=40)
strcpy(s[i].passfail, "pass");
else strcpy(s[i].passfail, "fail");
sort(s, nos);
for(i=0; i<nos; ++i);
fprintf(grades, "%11s %5i %5i %5i %c %4s \n", s[i].name, s[i].ex1, s[i].ex2, s[i].final, s[i].hw, s[i].grade, s[i].pas$
/* Sorting Grades */
void sort(int s[], int nos);
{
int k, j, m, ave, nos, s[50];
struct record hold;
for(k=0; k<=nos-2; k++);
m=k;
for(j=k+1; j<=nos-1; j++)
{
if(s[j] < s[m])
m=j;
hold=s[m];
s[m]=s[k];
s[k]=hold;
}
}
return;
}
}
|
| lbillys is offline | |
| | #10 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
An hour and a half ago I gave you some advice that you have not followed at all. Perhaps you believe this mess is somehow salvageable -- I don't blame you for sticking it out until you are convinced yourself. It is really just a question of how much longer that will take.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #11 |
| Registered User Join Date: May 2009
Posts: 8
| honestly, i have to finish this class out and i will this semester with the week left. if you want to help me, please do, but who sits on a computer programming message board at 6pm on a saturday to trash talk a student who is trying to get her assignment done? don't help me if you just want to belittle me. i know i am not good at this...that is precisely why i am on this site trying to learn from those who know far better than i do. |
| lbillys is offline | |
| | #12 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You've gotten about as far as you can with just blind typing. From here you need to (1) read and (2) think. The reading part comes in with the errors -- if you read both the error message, and the line 12 (or 20, etc.) that it refers to, then you will know what the error is. Also, you need to read the program you have written carefully -- because what you want it to do, and what it is going to do are two wildly different things. It would appear to me that you have the basic outline correct, but the implementation needs work. To be honest, I agree with MK here: the best thing you can do with this code (not the idea, but the code) is to burn it. Start again, and take more care with the implementation. |
| tabstop is offline | |
| | #13 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
If you want to "to learn from those who know far better", then do what I told you before: write a simple program to read the data into a struct array, and print the data in the array out to make sure you have done it correctly. Then proceed from there. This is not expressionist painting; there really is only two catagories here: do it right, or do it wrong. There are an infinite number of ways to do it wrong. There are probably a very limited number of ways to do it right. *it may be harder to take it from your prof, so consider yourself lucky. **I think that was tabstop's fault
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 05-16-2009 at 07:21 PM. | |
| MK27 is online now | |
| | #14 |
| Registered User Join Date: Apr 2009
Posts: 41
| Well to the original poster. I think your trying to think to big. You should be breaking your problems down to where any one thing you have to do is a small function. edit: I notice you've also posted your homework 14 in another thread. Also it should be mentioned that your professor/teacher/mentor told you specificaly to use a function sub-program for your sort. This mean I think you were sleeping in class to much. AND BTW: Giving up a saturday night is nothing if you want to learn programming. Sun, Mon, Tues, Wed, Thurs, Friday nights should all be set aside until you reach a decent level of programming profiecency or just give up now. Did you think you were special and could learn this material in your drooling-zombie-mode when you signed-up for the class? I would say it does get easier, But at the same time it gets easier it also requires even more time. Things only get bigger and require more time as they get more complex. You got into programming for what reason exactly? And on a honest note. I took a C programming class and before week two 25 % of the students dropped it. By the end of the class less than 33% were still there and even fewer actually passed it at all. Thats truely staggering. I guess it was to much for them to give up a saturday night. The professor said it was normal and to reason that they pack the class so full to start. At the beginning we had to get extra chairs to suit all the students. Anyone else here have stuff like this in there programming classes?(if you actually had to take them) Last edited by strickyc; 05-16-2009 at 08:11 PM. |
| strickyc is offline | |
| | #15 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Read this A development process Then start again with an empty file which you build up SLOWLY with lots of compiling and testing as you go.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Tags |
| beginning programming, external file, hecht, hw 12, strings |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| My program isn't inserting a new node in the beginning of the list... | RaDeuX | C Programming | 3 | 12-06-2008 07:54 PM |
| placing a clear or cls command at beginning of this program help needed | Mshock | C Programming | 15 | 05-13-2006 09:28 PM |
| writing to the beginning of a file | cloudy | C++ Programming | 7 | 04-12-2006 01:47 PM |
| writing to the beginning of the file | Jules | C++ Programming | 1 | 05-19-2004 01:31 AM |
| Please could someone help me with my 8-function calculator program? | Geeth Asokan | C Programming | 2 | 05-10-2002 04:16 PM |