Trying to study for exam, need help on two questions.

This is a discussion on Trying to study for exam, need help on two questions. within the C Programming forums, part of the General Programming Boards category; I have my final exam for intro to programming in c this sunday and upon studying found two questions that ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    16

    Trying to study for exam, need help on two questions.

    I have my final exam for intro to programming in c this sunday and upon studying found two questions that confuse me. I would appreciate if someone could help me solve them, I think my answers are wrong.

    Write a function called getGrades. The function that repeatedly prompts the user for positive integers until the user enters a negative value to stop. The function should return the average of these grades, the highest grade and the lowest grade.

    Code:
    double getGrades()
    {
    double average;
    double i;
    
    printf("Enter Grade");
    scanf("%lf", &i);
    }

    Write a function called Get_Info that takes a pointer to a student structure, (That has three fields: char array called name, and int id, and a double gpa) as its only argument. The function prompts the user for the required information to fill the structure and stores it in the appropriate fields.

    Code:
    typedef struct{
    	int id;
    	double gpa;
    	char name[SIZE];
    }student; 
    
    
     void Get_Info(student list[], int num)
     {
    	 int i;
    	 for(i=0;i<num;i++)
    	 {
    		 printf("\nName:%s", list[i].name);
    		 printf("\nGPA:%lf", list[i].gpa);
    		 printf("\nID: %d\n", list[i].id);
    	 }
     }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Rawr View Post
    I have my final exam for intro to programming in c this sunday and upon studying found two questions that confuse me. I would appreciate if someone could help me solve them, I think my answers are wrong.

    Write a function called getGrades. The function that repeatedly prompts the user for positive integers until the user enters a negative value to stop. The function should return the average of these grades, the highest grade and the lowest grade.

    Code:
    double getGrades()
    {
    double average;
    double i;
    
    printf("Enter Grade");
    scanf("%lf", &i);
    }

    Write a function called Get_Info that takes a pointer to a student structure, (That has three fields: char array called name, and int id, and a double gpa) as its only argument. The function prompts the user for the required information to fill the structure and stores it in the appropriate fields.

    Code:
    typedef struct{
    	int id;
    	double gpa;
    	char name[SIZE];
    }student; 
    
    
     void Get_Info(student list[], int num)
     {
    	 int i;
    	 for(i=0;i<num;i++)
    	 {
    		 printf("\nName:%s", list[i].name);
    		 printf("\nGPA:%lf", list[i].gpa);
    		 printf("\nID: %d\n", list[i].id);
    	 }
     }
    Regarding question 1) You are reading a double value for a grade when the questions actually asks for a positive integer.

    You are reading a single grade rather than looping until the grade you are reading is negative.

    question 2) You are passing an array and an int to a function that is required to only take a pointer.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 10:54 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  4. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 08:42 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM

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