Thread: Who can spot what I did wrong?

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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,268
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10: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, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM