cobwebs in head

This is a discussion on cobwebs in head within the C Programming forums, part of the General Programming Boards category; hi, i have just come back to school after 18 years i dont know what possessed me to think i ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    cobwebs in head

    hi, i have just come back to school after 18 years i dont know what
    possessed me to think i could learn how to program. i am so
    handicapped when it comes to technology. i have tried to figure out
    how to post my problem on your message board but i cant figure it out.
    (i think maybe i just did)
    here is my program it will not "compile" yyyyyyyyyyyyy????????
    Code:
    #include <stdio.h>
    #define students 3
    #define exams 4
    
    /* function prototypes */
    void minimum( const int[][ exams ], int, int );
    void maximum( const int[][ exams ], int, int );
    void average( const int[][ exams ], int, int );
    void printArray( const int[][ exams ], int, int );
    
    int main()
    {
    
       const int studentGrades[ students ][ exams ] =  
          { { 77, 68, 86, 73 },
            { 96, 87, 89, 78 },
            { 70, 90, 86, 81 } };
    
       void (*f[4])(int [][ exams ], int, int) = 
    {printArray,minimum,maximum,average};
    
       int choice;
    
       printf( "Enter a choice:\n");
    printf( "0 Print the array of grades\n");
    printf( "1 Find the minimum grade\n");
    printf( "2 Find the maximum grade\n");
    printf( "3 Print the average on all test for each student\n");
    printf( "4 End Program\n");
               scanf( "%d", choice);
    
       while( choice >= 0 && choice < 4 ){
    
               (*f[choice])(studentGrades,students, exams);
    
               printf( "Enter a number between 0 and 3, 4 to end: ");
               scanf( "%d", &choice);
       }
    
       printf( "Program execution completed.");
    
       
       return 0;
    
    } 
    void printArray( const int studentGrades[students][ exams ], int 
    students, int exams )
    {
       int i;
       int j;
    
       printf( "                 [0]  [1]  [2]  [3]" );
    
       for ( i = 0; i < students; i++ ) {
    
          printf( "\nstudentGrades[%d] ", i );
    
          for ( j = 0; j < exams; j++ ) {
    
             printf( "%-5d", studentGrades[ i ][ j ] );
    
          } 
    
       } 
    
    } 
    /* Find the minimum grade */
    void minimum( const int studentGrades[students][ exams ], int 
    students, int exams )
    {
       int i;
       int j;
       int lowGrade = 100;
    
       for ( i = 0; i < students; i++ ) {
    
          for ( j = 0; j < exams; j++ ) {
    
             if ( studentGrades[ i ][ j ] < lowGrade ) {
    
                lowGrade = studentGrades[ i ][ j ];
    
    printf( "Lowest Grade is %d",lowGrade);
             } 
    
          } 
    
       }
    
    } 
    /* Find the maximum grade */
    void maximum( const int studentGrades[students][ exams ], int 
    students, int exams )
    {
       int i;
       int j;
       int highGrade = 0; 
    
       for ( i = 0; i < students; i++ ) {
    
          for ( j = 0; j < exams; j++ ) {
    
             if ( studentGrades[ i ][ j ] > highGrade ) {
    
                highGrade = studentGrades[ i ][ j ];
    
    printf( "Highest Grade is %d", highGrade);
             }
    
          } 
    
       } 
    
    } 
    /* Determine the average grade for a particular student */
    void average( const int studentGrades[students][exams], int students, 
    int exams)
    {
       int i; 
    
       while(i < students){
    
               int sum = 0;
               double avg = 0.00;
    
       for ( j = 0; j < exams; j++ )
     
          sum += studentsGrades[ i ][ j ];
    
              avg = static_cast<double>(sum)/exams;
    
              printf( "The average grade for student %d is %.2f", i, avg);
    
              i++;
       } 
    }
    what was i thinking please end my misery
    Last edited by Salem; 07-13-2007 at 09:59 AM. Reason: Fix the code tags - next time, press preview before posting

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Using the code tags, and indenting your code helps greatly in debugging not to mention reading it.
    It's actually C++ code (not C), if you intended it to be C++ try compiling it with a C++ compiler (ie not a C compiler) or vice versa.

    ie:
    Code:
    avg = static_cast<double>(sum)/exams;
    is a C++ cast.

    What errors and warnings does your compiler tell you of?
    Also consider reading A development process

    Welcome to the boards by the way
    Last edited by zacs7; 07-13-2007 at 07:49 AM. Reason: Lying is bad

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    i have an example in c++, i guess thats my problem im trying to translate it into c.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    So your trying to translate an example written in C++ to C?
    Usually people scoff and ask why, but since your back at school... Anyway be procedural, as for casts read http://cboard.cprogramming.com/showt...213#post621213 - CornedBee wrote that fantastically.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    ok im not sure translate is the right word, but yes i need it compile with a c compiler

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well then you would need to translate it, C++ and C are different languages although C is a subset of C++ (meaning C++ is a superset of C). ie C compiles as C++ (usually) but C++ does not compile as C.

    Try indent the code ie go into notepad (or any texteditor), make all the braces aligned ({ and }), well it's hard to explain - make it look like:

    * Useless code babble to demonstrate indenting follows
    Code:
    #include <stdio.h>
    
    void foo(void * dummy)
    {
        printf("this is a dummy function");
        return;
    }
    
    int main(void)
    {
        int a = 160, b = 80;
    
        foo(NULL);
    
        printf("This a bla blah");
        if(a == 170)
        {
            if(b == 80)
            {
                 printf("a = &#37;d, b = %d", a, b);
            }
        }
        return 0;
    }
    Then repost your new, nice looking code in codetags - then we can move on (I'd do it but I'm tired :P).

    Code:
    #define exams 4
    #define students 6
    
    void average( const int studentGrades[students][exams], int students,
    int exams)
    Usually defines and macros are written so they're easily distinguishable from other code. You also can't reuse variable names (in the same scope) again,
    after the preprocessor passes, this would become
    Code:
    void average( const int studentGrades[students][exams], int 6,
    int 4)
    How much of C have you learnt?
    Last edited by zacs7; 07-13-2007 at 08:13 AM.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Here's your code in code tags:

    Code:
    #include <stdio.h>
    #define students 3
    #define exams 4
    
    /* function prototypes */
    void minimum( const int[][ exams ], int, int );
    void maximum( const int[][ exams ], int, int );
    void average( const int[][ exams ], int, int );
    void printArray( const int[][ exams ], int, int );
    
    int main()
    {
    
       const int studentGrades[ students ][ exams ] =  
          { { 77, 68, 86, 73 },
            { 96, 87, 89, 78 },
            { 70, 90, 86, 81 } };
    
       void (*f[4])(int [][ exams ], int, int) = 
    {printArray,minimum,maximum,average};
    
       int choice;
    
       printf( "Enter a choice:\n");
    printf( "0 Print the array of grades\n");
    printf( "1 Find the minimum grade\n");
    printf( "2 Find the maximum grade\n");
    printf( "3 Print the average on all test for each student\n");
    printf( "4 End Program\n");
               scanf( "%d", choice);
    
       while( choice >= 0 && choice < 4 ){
    
               (*f[choice])(studentGrades,students, exams);
    
               printf( "Enter a number between 0 and 3, 4 to end: ");
               scanf( "%d", &choice);
       }
    
       printf( "Program execution completed.");
    
       
       return 0;
    
    } 
    void printArray( const int studentGrades[students][ exams ], int 
    students, int exams )
    {
       int i;
       int j;
    
       printf( "                 [0]  [1]  [2]  [3]" );
    
       for ( i = 0; i < students; i++ ) {
    
          printf( "\nstudentGrades[%d] ", i );
    
          for ( j = 0; j < exams; j++ ) {
    
             printf( "%-5d", studentGrades[ i ][ j ] );
    
          } 
    
       } 
    
    } 
    /* Find the minimum grade */
    void minimum( const int studentGrades[students][ exams ], int 
    students, int exams )
    {
       int i;
       int j;
       int lowGrade = 100;
    
       for ( i = 0; i < students; i++ ) {
    
          for ( j = 0; j < exams; j++ ) {
    
             if ( studentGrades[ i ][ j ] < lowGrade ) {
    
                lowGrade = studentGrades[ i ][ j ];
    
    printf( "Lowest Grade is %d",lowGrade);
             } 
    
          } 
    
       }
    
    } 
    /* Find the maximum grade */
    void maximum( const int studentGrades[students][ exams ], int 
    students, int exams )
    {
       int i;
       int j;
       int highGrade = 0; 
    
       for ( i = 0; i < students; i++ ) {
    
          for ( j = 0; j < exams; j++ ) {
    
             if ( studentGrades[ i ][ j ] > highGrade ) {
    
                highGrade = studentGrades[ i ][ j ];
    
    printf( "Highest Grade is %d", highGrade);
             }
    
          } 
    
       } 
    
    } 
    /* Determine the average grade for a particular student */
    void average( const int studentGrades[students][exams], int students, 
    int exams)
    {
       int i; 
    
       while(i < students){
    
               int sum = 0;
               double avg = 0.00;
    
       for ( j = 0; j < exams; j++ )
     
          sum += studentsGrades[ i ][ j ];
    
              avg = static_cast<double>(sum)/exams;
    
              printf( "The average grade for student %d is %.2f", i, avg);
    
              i++;
       } 
    }
    Here are some of the problems you have:

    • You pointer table should be defined to take const arrays, just like the functions they point to.
    • When you use scanf() to read a value into choice, you need the &.
    • You have #defines for exams and students. You then use variable names with the same name! Can't do that. #defines in this example are used by the C preprocessor to go through your code and replace every instance of that "variable" with the constatnt number that you give it. Don't use variable names that match a #define'd value.
    • In your function definitions, don't declare studentGrades with a first parameter for size. In other words, it should be an array something like this: const int studentGrades[][exams]
    • Change the static_cast<double> to (double).

      Code:
      avg = static_cast<double>(sum)/exams;
      Becomes:

      Code:
      avg = (double)(sum)/exams;
      Although you still have to fix exams being a #define'd value and a variable name.


    Think that's most of them.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    MacGyver I'm curious, did you indent that by hand?

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If I did, it would have looked prettier.

    It was already indented (or whatever you would like to call that form ) in his post, but without code tags so the formatting was lost. I just put the tags around it.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    I'm wondering how you managed to post without code tags.

    Edit:
    Now I see - all you did was put [CODE][/CODE] at the start of the post and thus missed the whole point of insisting that you use them.

    In future, code at the start of your code, and /code at the end of your code, repeated as necessary for each code block.
    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.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Disable JS?

    It was already indented (or whatever you would like to call that form ) in his post, but without code tags so the formatting was lost. I just put the tags around it.
    Wow your a smart ducky! I never thought of that

    Salem you seem to never be on for more than a second? Do you use the RSS feeds or something? It's like *bang* he's gone, *bang* he's here!?!
    Last edited by zacs7; 07-13-2007 at 08:21 AM.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Salem View Post
    I'm wondering how you managed to post without code tags.
    I would respectfully suggest someone "fix" that if it can be done without much effort. For whatever reason people can get away with empty code tags, which is what happened in this case. A requirement that code tags be around some block of text would fix that.... or at least make it slightly harder for this to happen.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why not put the hole post in code tags? Ie a space is kept as a space etc? Or is that just silly?

    Quote Originally Posted by MacGyver
    I would respectfully suggest someone "fix" that if it can be done without much effort.
    Regex FTW

    Anyway what compiler are you using jmonster?
    Last edited by zacs7; 07-13-2007 at 08:26 AM.

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    thnx to everybody. i just got back from class/lab, played around with it and got it to work(crazy). after this class if i live to be a thousand, this is all the c language i think i will ever need. you people rock if u understand this stuff:-)

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There's not much that is really hard to understand about your program.

    You make an array and put some grades in it.

    You then go through the array to find the minimum grade.
    Then you go through the same array to find the maximum grade.

    Then you go through the array again to find the average grade.

    These are "standard" operations that are done in many programs, especially going through an array to calculate or find, some piece of data/info.

    Pretty soon, it's like tying your shoelaces, ez. C is more difficult than many higher level languages, because it works so closely to the model of the computers architecture, and was designed originally for highly trained professional programmers. The syntax and features of the language reflect that, clearly.

    I'm sure looking at your program, you've come to the conclusion that everything you needed to do, could have been done in one simple search through your array; the minimum, maximum and average don't need a separate function or search through the array.

    I liked your breaking the algorithm up, however. Excellent start for beginners to practice and learn the importance of doing that, when needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  2. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  3. Linked List Help
    By Perverse in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2005, 07:33 AM
  4. Linked list, is this correct?
    By scrappy in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 11:06 PM
  5. change stack to a queue
    By sballew in forum C Programming
    Replies: 12
    Last Post: 12-03-2001, 10:16 PM

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