Thread: C programming newb needs help

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    36

    C programming newb needs help

    Hello all:
    I am new to C programming and looking for some help and support. I'm currently taking a freshman level CS course in programming and its really new to me. I'm not asking anyone to do my homework, I'm just looking for some guidance in programming.

    Here is the problem that I am currently dealing with User Defined Functions. Some of this may seem elementary to you but its very new to me so please be nice. :-)

    In the main function user should be asked to enter 6 grades ( 0 - 100), first function called gradeAverage should take two arguments totalgrade and numberofGrades each which is an integer and return a float which is the average.
    The second function should take one argument as an float which is the average and return a character ( A, B, C, F) indicating the grade in character notation.

    Here is the code I have wrote along with output results and complier errors. Can someone please offer this programming newb some guidance? Thanks!

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    int x, y, z;
    //x is the Grade Average
    //y is the total grade
    //z is the number of grades
    
    float gradeAverage(int, int);
    char printgrade(float);
    
    
         printf("Enter the number of grades: %d\n", z);
         scanf("%d", z);
         for(z=0;z<5;z++)
         {
    	printf("The ", z);
    	printf("grade is \n");
    	scanf("Please enter the grade %d\n", &z);
         }
    
    return 0;
    }
    
    
    float x(int y, int z)
    {
    int total;
    
        float avg;
        avg = total/y;
        return avg;
        printf("The grade average is: %d\n");
    }
    
    //char printgrade(float y);
    //{
    
        // if(y>=90)
        // return 'A';
        // printf("You got A for the cource\n");
    
    	//else
    
         //if(y>=80)
         //return 'B';
         //printf("You got a B for the cource\n");
    
    	//else
    
         //if(y>=70)
         //return 'B';
         //printf("You got a C for the cource\n");
    
    	//else
    
         //if(y>=60)
         //return 'C';
         //printf("You got a D for the cource\n");
    
    	//else
    
         //if(y>=50)
         //return 'F';
         //printf("You got an F for the cource\n");
    
    //}
    Here is a sample of the output:

    Enter the number of grades: 1075120320
    12
    The grade is
    The grade is
    The grade is
    The grade is
    The grade is

    And here are the compliler errors I get when I remove the rest of the remarks:

    gcc assign.c
    assign.c:43: parse error before `{'
    assign.c:47: parse error before string constant
    assign.c:47: warning: data definition has no type or storage class
    assign.c:53: parse error before string constant
    assign.c:53: warning: data definition has no type or storage class
    assign.c:59: parse error before string constant
    assign.c:59: warning: data definition has no type or storage class
    assign.c:65: parse error before string constant
    assign.c:65: warning: data definition has no type or storage class
    assign.c:71: parse error before string constant
    assign.c:71: warning: data definition has no type or storage class

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right away, break out from that "x, y, and z" kind of variable naming. Yes, we use i, j, and k, for standard idiom loop counters, but any other variables PLEASE give them short and descriptive names: avgGrade, numGrades, etc. (something like that).

    You can't have a grade the user enters, also serve as a loop counter. They have to be separate variables:

    Use another variable for the grade - not the loop counter.

    Code:
         for(z=0;z<5;z++)
         {
    	printf("The ", z);
    	printf("grade is \n");
    	scanf("Please enter the grade %d\n", &z);
         }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Can you give me an example of what sort of variable to use instead of using the loop counter?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by davidjg View Post
    Here is the code I have wrote along with output results and complier errors. Can someone please offer this programming newb some guidance? Thanks!
    You'll probably get better suggestions from some of the others but I've noted a couple of things in red that may help you along....


    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    int x, y, z;                          
    //x is the Grade Average     <--- use descriptive names eg: int NumGrades
    
    //y is the total grade
    //z is the number of grades
    
    float gradeAverage(int, int);  <<--- these should be above main, not in main
    char printgrade(float);
    
    
         printf("Enter the number of grades: %d\n", z);
         scanf("%d", z);       <---- should be  scanf("%d",&z);
         for(z=0;z<5;z++)   <---- should be: for (int i = 0; i < z; i++); 
        {                                                 
                    printf("The ", z);    <--- s/b  printf("The %d", i);
    	printf("grade is \n");
    	scanf("Please enter the grade %d\n", &z);  <--- should be at the top of the loop
         }
    
    return 0;
    }
    
    
    float x(int y, int z)
    {
    int total;
    
        float avg;
        avg = total/y;
        return avg;                 <---- exit before printing result
        printf("The grade average is: %d\n");
    }
    
    //char printgrade(float y);
    //{
    
        // if(y>=90)
        // return 'A';
        // printf("You got A for the cource\n");
    
    	//else
    
         //if(y>=80)
         //return 'B';
         //printf("You got a B for the cource\n");
    
    	//else
    
         //if(y>=70)
         //return 'B';
         //printf("You got a C for the cource\n");
    
    	//else
    
         //if(y>=60)
         //return 'C';
         //printf("You got a D for the cource\n");
    
    	//else
    
         //if(y>=50)
         //return 'F';
         //printf("You got an F for the cource\n");
    
    //}
    Think about the order in which things have to be done. Think about doing it in the minimum number of steps.

    Also as a matter of personal style... I use function declarations (the ones with the semicolons) very sparingly. I prefer to put my main() at the bottom of the page and place functions above. I find it very helpful in finding problems with undeclared or unintialized variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  2. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  3. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  4. Detective 1: Who do you think killed him? Detective 2: google it newb.
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 10-09-2004, 03:35 AM
  5. Newb With Small Pointer Issue
    By G-Prime in forum C Programming
    Replies: 7
    Last Post: 09-06-2004, 04:09 PM