help beginning programming

This is a discussion on help beginning programming within the C Programming forums, part of the General Programming Boards category; I have a homework assignment that I have no idea where to go or what to do next. Could you ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    8

    Angry help beginning programming

    I have a homework assignment that I have no idea where to go or what to do next. Could you please help? Thanks!
    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;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lbillys View Post
    Code:
    /* Homework 12 */  /*Does this mean this is your twelfth homework? */
    
    #include FILENAME "students.dat"  /*You know by now, surely, how to open a file; and this isn't it*/
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    struct record
    {
            char name[12];
            int ex1, ex2, final, hw, ave, grade passf1[5];  /*What type do you want passf1 to be?*/
    };
    
    int main(void)
    {
    struct record s[50];
    int nos, i=0;
    FILE *students;  /*Remember opening a file?  Maybe you should do that now.*/
    {
    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;  /*Now you're not working on the data you just read in, but the one after that*/
    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';  /*Hooray!  Everybody gets an 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)  /*You have to finish this off before you can start doing another function*/
    void sort(record s[], int n);
    int k,l m;
    record hold;
    for(k=0, k<=2, ++k);  /*Only going to sort two records? */
    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;
    And you definitely need to indent.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    Last edited by MK27; 05-16-2009 at 07:02 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #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;
    }
    }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    And when you compile that, what happens?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #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)

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lbillys View Post
    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)
    And do you intend the fix the errors? Start at the top: no semicolon at end of struct is pretty straightforward, as is syntax error before passfail (I even mentioned it in my comments the first time). Fixing these will help with some of the later ones.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lbillys View Post
    i just got a lot of errors and warnings...
    Imagine that. I would say you should read this post. Carefully.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #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;
    }
    }

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lbillys View Post
    alright i have gotten all the errors to leave except for two and five warnings. I am unsure how to fix these.
    Just delete those two lines, which will at least allow you to compile and witness what a monstrosity this is. It won't matter at all, since the flow of execution leading up to lines 68 & 70 is nonsensical.

    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #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.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lbillys View Post
    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.
    I have felt this way before**. I know it is hard to take criticism like that*. But I am not going to lie to you, either. It is also hard to admit to yourself when you have wasted your time and have to scrap something and start again. I've been there too. Programming is fun when it goes well -- when it doesn't, it's just frustration and staring at a stupid monitor. And no one will feel sorry for you in your failed attempts to be a programmer (there are much worse tragedies in the world). It is very difficult at first.

    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
    Last edited by MK27; 05-16-2009 at 08:21 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #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 09:11 PM.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  2. Replies: 15
    Last Post: 05-13-2006, 10:28 PM
  3. writing to the beginning of a file
    By cloudy in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2006, 02:47 PM
  4. writing to the beginning of the file
    By Jules in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 02:31 AM
  5. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21