Student average program rookie in need of help

This is a discussion on Student average program rookie in need of help within the C Programming forums, part of the General Programming Boards category; Man look: Code: /*As I'm in GREAT MOOD I will code a bit for ya*/ #include <stdio.h> #include <stdlib.h> #include ...

  1. #16
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Man look:
    Code:
    /*As I'm in GREAT MOOD I will code a bit for ya*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void){
    	char *foo;
    	/*PAY ATTENTION*/
    	char bar[] = "bar";
    	char quux[20];	
    	char *zyz;
    	
    	foo = bar;
    	printf("Gimme input to quux :");
    	scanf("%s",quux);
    	
    	printf("%s\n",foo);
    	zyz = quux;
    
    	printf("%s\n",zyz);
    	strcpy(quux,foo);
    	printf("%s\n",quux);
    
    	return 0;
    }
    Then Ive compiled with:
    gcc -W -Wall -Wbad-function-cast -Wcast-align -Wcast-qual -Wdisabled-optimization -Wendif-labels -Wfloat-equal -Wformat-literal -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wno-unused-parameter -Wpointer-arith -Wshadow -Wstrict-prototypes -Wundef -Wwrite-strings -pedantic learn.c -o learn

    No warnings.

    I have your code doing the right thing but we want you to succed, you can't use an array as left value in C. So you need something like strcpy to read the names to the struct.
    Hope it helps. Also I humbly recommend you to read http://pw1.netcom.com/~tjensen/ptr/pointers.htm .

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    I need some serious help. This is an excercise in the book suggested to do to learn the structure stuff. I am completely lost. The book was very limited. Actually one of the worst course books i have ever seen.

    this excercise started off like this.

    Using the student record example in Section 12.2, "Accessing a Member," on page 394, write a function that prints the average for each student in a class. Let an A grade have Value 4, a B grade have value 3, and so forth.

    Here is all that page 394 has,, this whole chapter didnt explain very well. Ira pohl is a terrible c programing author.

    In file Class_info.h
    Code:
    #Define CLASS_SIZE 100
    
    struct student {
    char *last_name;
    int student_id;
    char grade;
    };
    in another file it has this:

    Code:
    #include "class_info.h"
    
    int main(void)
    {
    struct student temp, class[CLASS_SIZE];
    ..........
    then it says :

    assign values to memebers

    temp.grade = 'A'
    temp.last_name = "Bushker";
    temp.student_id = 590017;



    That is all there is on 394, not even a complete description on what the hell is going on. I am also taking C++ it is so much easier than this C programing class.


    I am working on my own with no instructor support. Can you please end the PAIN. Show me what to do and explain so i can understand structures.

    Flame me if you will. But if a person is learning sometimes it is best to show an example of how to use it , rather than cryptic messages on what small changes you would do. I know the homework policy. THIS IS NOT HOMEWORK < NOR AN ASSIGNMENT TO TURN IN. This is merely an excercise suggested to do to understand structures.


    Please help me.

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Your code, as posted recently. Watch the comments
    Code:
    #include "class_info.h"
    #include <stdio.h>
    
    /*!! added prototype */
    void average(struct student class[], int length);
    
    int main(void)
    {
      struct student class[CLASS_SIZE];
    
      class[0].grade = 'A';
      class[0].last_name = "Walker";
      class[0].student_id = 590017;
    
      class[1].grade = 'B';
      class[1].last_name = "Smith";
      class[1].student_id = 590118;
    
      class[2].grade = 'C';
      class[2].last_name = "jones";
      class[2].student_id = 590219;
    
      class[3].grade = 'A';
      class[3].last_name = "Bubba";
      class[3].student_id = 590320;
    
      class[4].grade = 'B';
      class[4].last_name = "johns";
      class[4].student_id = 590421;
    
      class[5].grade = 'C';
      class[5].last_name = "Walker";
      class[5].student_id = 590017;
    
      class[6].grade = 'D';
      class[6].last_name = "Smith";
      class[6].student_id = 590118;
    
      class[7].grade = 'B';
      class[7].last_name = "jones";
      class[7].student_id = 590219;
    
      class[8].grade = 'A';
      class[8].last_name = "Bubba";
      class[8].student_id = 590320;
    
      class[9].grade = 'C';
      class[9].last_name = "johns";
      class[9].student_id = 590421;
    
      /*!! changed into a function call */
      average( class, 10 );
    
      return 0;
    }
    
    
    void average(struct student class[], int length) /*!! deleted ; */
    {
      double sum=0.0,avg;
      int i;
      /*!! deleted - already passed as a parameter */
      /*!! int length = sizeof(class)/sizeof(student); */
    
      for( i=0;i<length;++i )
      {
        if( class[i].grade == 'A' )
          sum+=4;
        if( class[i].grade == 'B' )
          sum+=3;
        if( class[i].grade == 'C' )
          sum+=2;
        if( class[i].grade == 'D' )
          sum+=1;
        if( class[i].grade == 'F' )
          sum+=0;
      }
      avg = (sum/length);
      printf("The average for the class is %f.\n", avg);/*!! added ; */
    }
    /*!! deleted */
    /*!! return 0 */
    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.

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Salem thanks for the help to figure out class average. How would i go about figuring student averages?

  5. #20
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Quote Originally Posted by Mshock
    Salem thanks for the help to figure out class average. How would i go about figuring student averages?
    Use what you learnt here, and modify it to suit your needs.

  6. #21
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Well I still have no idea how to get each students average to printout. an A=4 , etc...

    thanks for trying to help me. I have no clue. Even when i follow what people are saying it still makes no since. I need to see this program work with this sturcture stuff to understand all the higher level talking in this post.

    Use the caveman stuff i started off with and keep it simple. I am still at a stand still in the structures chapter , and cant proceed to the next stuff without understanding this. People can type use strcpy etc... but i am serious the book is C by dissection by Ira pohl 4th edition. It is a horrible book for showing you anything in the excercises.

    I realize you guys get upset with the please do it for me. Please read the homework stuff. I just want to see this thing from start to finish to understand structures.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't know how to find the average of something? Do you know how to average in basic math? The concept is the same. Sum all of the numbers, and divide by the total number of numbers. Do that for each student individually to get each average.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    its the coding , not a matter of not understanding how to figure out averages of students grade.

  9. #24
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Well what is it that you don't get about structs? You said that you
    study C++ and C, so assuming that you've encountered classes,
    structs are just much simpler, less powerful versions of them.
    The idea is this: The basic data types are great and programmers
    come up with all sorts of logical representations of real world data
    using them, but its not always enough. Sometimes we want to
    represent a type of data with multiple attributes - in C we use
    structs to achieve representations of more complex "objects" (in
    a manner of speaking). We create these structs from more basic
    data types (int, char, float) and arrays of same as required (you
    have character strings in yours).

    The structs we write are essentially data types in their own right,
    in that they can be thought of as ints or floats and what not,
    except structs have separate parts (the attributes described
    above) called members. In our programs, we declare instances of
    ints, we can do the same with structs. We can even do arrays.

    Lets take a look at your program:

    Code:
    int main(void)
    {
      struct student class[CLASS_SIZE];
    
      class[0].grade = 'A';
      class[0].last_name = "Walker";
      class[0].student_id = 590017;
    The first line is declaring an array of your struct type called
    student, and calling it class. CLASS_SIZE is a constant you have
    defined as 100. You declared an array of 100 "students", just
    as you would do to declare 100 ints.

    The next three lines perform assignment on the 0th element
    of that array - setting the grade member to 'A', setting the
    last_name char pointer to the string "Walker", and setting
    the student_id to 590017. There you have a fully set up
    struct, 99 to go!

    It really doesn't get much clearer than this: the use of the '.'
    means that we are accessing a specific member of the struct.
    When a struct is passed by pointer to a function, the syntax for
    accessing members changes to '->', but that's not important for
    the current program.

    Lastly, just to simplify the code segment above, we are looking
    only at 1 element of the struct, so if we change the declaration
    to a single variable, the small section of code above behaves as
    follows:


    Code:
    int main(void)
    {
      struct student students;
    
      students.grade = 'A';
      students.last_name = "Walker";
      students.student_id = 590017;
    I'm only posting this to avoid any confusion with the use of an
    array. From what i've seen, this is a terrible task to introduce
    structs in a beggining C book - such books should take baby steps
    with the topic since the idea can confuse people. Why do you
    have to use that book if you recognise how bad it is?

    Hope this clarifies things a bit - post questions if you have any
    difficulty in understanding this post, feel free to come back and
    ask questions.

    Oh yeah, and about finding students average grade - you only
    have one grade per student - at the moment, that's the average
    grade - unless you implement features to add more grades to
    each individual students record.
    Last edited by Richie T; 05-17-2006 at 06:06 AM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  10. #25
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    void averageForStudent(struct student class[], int length, char *studentName);

    Coupled with
    if ( strcmp( class[i].last_name, studentName ) == 0 )

    Should be enough of a hint to at least try to write some kind of an answer.
    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 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 02:56 AM
  2. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  3. I have a function and I need multiple averages returned
    By tommy69 in forum C++ Programming
    Replies: 20
    Last Post: 04-13-2004, 11:45 AM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. C++ Program that Calculates average of three scores
    By dccog in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2002, 11:03 PM

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