Who can spot what I did wrong?

This is a discussion on Who can spot what I did wrong? within the C Programming forums, part of the General Programming Boards category; Write a function qualityPoints that inputs a student's average and returns 4 if a student's average is 90-100, 3 if ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Who can spot what I did wrong?

    Write a function qualityPoints that inputs a student's average and returns 4 if a student's average is 90-100, 3 if the average is 80-89, 2 if the average is 70-79, 1 if the average is 60-69, and 0 if the average is lower than 60.

    Code:
    #include <stdio.h>
    
    int qualityPoints(int a, int b, int c, int d, int f);
    int main() {
    
    	int x;
    	int q;
    	int a;
    	int b;
    	int c;
    	int d;
    	int f;
    
    
    	printf("Enter student's average grade:", x);
    	scanf("%d", &x);
    
    	q = qualityPoints(a, b, c, d, f);
    
    	printf("Student's average is between %d", q );
    
    	return 0;
    
    }
    
    int qualityPoints(int a,int b, int c, int d, int f) {
    	
    
    	for(a = 100; a >= 90; a--){
    		if(a >= 90)
    			return 4;
    	}
    	for(b = 89; b >= 80; b--){
    		if(b >= 80)
    			return 3;
    	}
    	for(c = 79; c >= 70; c--){
    		if(c >= 70)
    			return 2;
    	}
    	for(d = 69; d >= 60; d--){
    		if(d >= 60)
    			return 1;
    	}
    	for(f = 59; f >= 0; f--){
    		if(f < 60)
    			return 0;
    	}
    }
    What do I have wrong?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    remove all loops

    and write code like

    Code:
    if (cond1)
       return 4;
    else if(cond2)
       return 3;
    else if(cond3)
       return 2;
    else if (cond4)
       return 1;
    else
       return 0;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I did it but i have 1 error. It says that I missing a semi-colon before return 0 which makes no sense but when i put it in in changes to two different errors.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by jturner38 View Post
    I did it but i have 1 error. It says that I missing a semi-colon before return 0 which makes no sense but when i put it in in changes to two different errors.
    show your latest code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Code:
    #include <stdio.h>
    
    int qualityPoints(int a, int b, int c, int d, int f);
    int main() {
    
    	int x;
    	int q;
    	int a;
    	int b;
    	int c;
    	int d;
    	int f;
    
    
    	printf("Enter student's average grade:", x);
    	scanf("%d", &x);
    
    	q = qualityPoints(a, b, c, d, f);
    
    	printf("Student's average is between %d", q );
    
    	return 0;
    
    }
    
    int qualityPoints(int a,int b, int c, int d, int f) {
    
    	if(a >= 90)
    		return 4;
    	else if(b >= 80)
    		return 3;
    	else if(c >= 70)
    		return 2;
    	else if(d >= 60)
    		return 1;
    	else(f < 60)
    		return 0;
    	
    }

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    else(f < 60)
    You don't need the (f < 60) part, just the else.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Now it says f is not being initialized when I debug it!

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by jturner38 View Post
    Now it says f is not being initialized when I debug it!
    Look at your code. You are using the variable 'f' without ever assigning a value to it.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by bithub View Post
    Look at your code. You are using the variable 'f' without ever assigning a value to it.
    Well, the entire code doesn't make any sense to me anyway. Why do we need these variables in the first place? Just check if the score is in some range and return some value...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Well how do u do a range problem? Unless I go back to my originall part where I had for statements. Did u see what I had before? Ok im getting confused by everybody's input. In order to check a value to see if its in a certain range you have to do what?

  11. #11
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    replace a, b, c, d, & f with x

    Code:
    .
    .
    .
    q = qualityPoints(x);
    .
    .
    .
    int qualityPoints(int x) 
    {
      if(x >= 90)
        return 4;
      else if(x >= 80)
        return 3;
                .
                .
                .
    }
    Last edited by ಠ_ಠ; 03-23-2009 at 06:51 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  12. #12
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Oh duh! I gotcha. I had thought that originally but i dont y i changed it to multiple variables!

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Please refer following code:
    to meet your requirement.

    Code:
    #include "stdio.h"
    int qualityPoints(int);
    int main() {
    
    	int x = 0,q = 0;
    	
    	printf("Enter student's average grade:", x);
    	scanf("%d", &x);
    
    	q = qualityPoints(x);
    
    	printf("Student's average is between %d", q );
    
    	return 0;
    
    }
    
    int qualityPoints(int x) {
    	
      int i;
    	for(i = 100; i >= 90; i--){
    		if(x >= 90)
    			return 4;
    	}
    	for(i = 89; i >= 80; i--){
    		if(x >= 80)
    			return 3;
    	}
    	for(i = 79; i >= 70; i--){
    		if(x >= 70)
    			return 2;
    	}
    	for(i = 69; i >= 60; i--){
    		if(x >= 60)
    			return 1;
    	}
    	for(i = 59; i>= 0; i--){
    		if(x < 60)
    			return 0;
    	}
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by shwetha_siddu
    Please refer following code:
    to meet your requirement.
    You might want to read the thread before making a suggestion. The problem has already been solved with a solution far better than what you suggested.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  2. Agh, what's wrong with this function?
    By Grins2Pain in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2003, 02:59 AM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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