Problem with Functions

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 928 Views - Last Post: 17 September 2011 - 03:48 PM Rate Topic: -----

#1 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Problem with Functions

Posted 15 September 2011 - 12:17 PM

Hello !! I have been trying to solve this problem since many days but I could not figure it out. I have also edited it many times ...but it still doesn't work. Please guide me through. Thanks appreciate the help.

The Question and the code below:

Question
--------


A talent competition has 5 judges, each of whom awards a score between 0 and 10 to each performer. Fractional scores, such as 8.3 are allowed. A performer's final score is determined by dropping the highest and lowest score received, then averaging the 3 remaining scores. Write a program that uses this method to calculate a contestant's score. It should include the following functions:

void getJudgeData() -- This should ask the user for a judge's score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the 5 judges.

double calcScore() -- This should calculate and display the average of the 3 scores that remain after dropping the highest and lowest scores the performer received. This function should be called just once by main, and should be passed the 5 scores.

The last two functions, described below, should be called by calcScore, which uses the returned information to determine which of the scores to drop.

int findLowest() -- This should find and return the lowest of the 5 scores passed to it.

int findHighest() -- This should find and return the highest of the 5 scores passed to it.

Input Validation: Do not accept judge scores lower than 0 or higher than 10.


Code
----
#include <iostream>
using namespace std;

void getJudgeData(double&, double&, double&, double&, double&);
double calcScore();
int findLowest(double value1,double value2,double value3,double value4,double value5);
int findHighest(double value1,double value2,double value3,double value4,double value5);

int main()
{
	double val1,val2,val3,val4,val5;

	getJudgeData(val1,val2,val3,val4,val5);
	double calcScore();

	return 0;
}

void getJudgeData(double& judval1, double& judval2, double& judval3, double& judval4, double& judval5)
{
		cout << "Judge 1 : Enter the score of the performer." << endl;
		cin >> judval1;

		while (judval1 < 0 && judval1 > 10)
			{
				cout << "The score must be between 0 and 10." << endl;
				cout << "Enter it again." << endl;
				cin >> judval1;
			}
		cout << "Judge 2 : Enter the score of the performer." << endl;
		cin >> judval2;

		while (judval2 < 0 && judval2 > 10)
			{
				cout << "The score must be between 0 and 10." << endl;
				cout << "Enter it again." << endl;
				cin >> judval2;
			}
		cout << "Judge 3 : Enter the score of the performer." << endl;
		cin >> judval3;

		while (judval3 < 0 && judval3 > 10)
			{
				cout << "The score must be between 0 and 10." << endl;
				cout << "Enter it again." << endl;
				cin >> judval3;
			}
		cout << "Judge 4 : Enter the score of the performer." << endl;
		cin >> judval4;

		while (judval4 < 0 && judval4 > 10)
			{
				cout << "The score must be between 0 and 10." << endl;
				cout << "Enter it again." << endl;
				cin >> judval4;
			}
		cout << "Judge 5 : Enter the score of the performer." << endl;
		cin >> judval5;

		while (judval5 < 0 && judval5 > 10)
			{
				cout << "The score must be between 0 and 10." << endl;
				cout << "Enter it again." << endl;
				cin >> judval5;
			}
}

double calcScore()
{
	double average;

	double value1,value2,value3,value4,value5;
	findLowest(value1,value2,value3,value4,value5);
	findHighest(value1,value2,value3,value4,value5);
	cout << findLowest;
	cout << findHighest;

	cout << "The average score of the performer is " << average << endl;
}

int findLowest(double value1,double value2,double value3,double value4,double value5)
{
	double lowest;

	if (value1 < value2 && value1 < value3 && value1 < value4 && value1 < value5) 
		lowest = value1;
	if (value2 < value1 && value2 < value3 && value2 < value4 && value2 < value5)
		lowest = value2;
	if (value3 < value1 && value3 < value2 && value3 < value4 && value3 < value5)
		lowest = value3;
	if (value4 < value1 && value4 < value2 && value4 < value3 && value4 < value5)
		lowest = value4;
	if (value5 < value1 && value5 < value2 && value5 < value3 && value5 < value4)
		lowest = value5;
	return lowest;
}
	int findHighest(double value1,double value2,double value3,double value4,double value5)
{
	double highest;

	if (value1 > value2 && value1 > value3 && value1 > value4 && value1 > value5) 
		highest = value1;
	if (value2 > value1 && value2 > value3 && value2 > value4 && value2 > value5)
		highest = value2;
	if (value3 > value1 && value3 > value2 && value3 > value4 && value3 > value5)
		highest = value3;
	if (value4 > value1 && value4 > value2 && value4 > value3 && value4 > value5)
		highest = value4;
	if (value5 > value1 && value5 > value2 && value5 > value3 && value5 > value4)
		highest = value5;
	return highest;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Functions

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Problem with Functions

Posted 15 September 2011 - 12:33 PM

Moved to C/C++. Please reserve the C++ Programmers forum for topical, non-help related discussion.

What specific problems or errors are you encountering? Please define "doesn't work."
Was This Post Helpful? 0
  • +
  • -

#3 Mina-kun  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 43
  • Joined: 13-September 11

Re: Problem with Functions

Posted 15 September 2011 - 12:34 PM

You aren't making the values entered by the user available to the calcScore() function. You need to either pass them as arguments to the calcScore function (best method), or set them up as global variables.
Was This Post Helpful? 0
  • +
  • -

#4 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: Problem with Functions

Posted 15 September 2011 - 12:50 PM

View PostMina-kun, on 15 September 2011 - 12:34 PM, said:

You aren't making the values entered by the user available to the calcScore() function. You need to either pass them as arguments to the calcScore function (best method), or set them up as global variables.


give me an example..I don't understand what you wrote.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Problem with Functions

Posted 15 September 2011 - 12:50 PM

Look more closely at your assignment.

View Postqwerty159, on 15 September 2011 - 03:17 PM, said:

void getJudgeData() -- This should ask the user for a judge's score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the 5 judges.


So, something like: void getJudgeData(ing &score);

Note that findLowest and findHighest return ints, implying that your scores are ints, not double.

This assignment screams for arrays. If you've learned them, then you should be using them. If you haven't learned them, but skipping ahead in the book is permissible, you should still be using them.
Was This Post Helpful? 0
  • +
  • -

#6 Mina-kun  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 43
  • Joined: 13-September 11

Re: Problem with Functions

Posted 15 September 2011 - 12:54 PM

And if you haven't learned them and skipping ahead in the book is not permissible, then your instructor should be banned from programming. ;)

Also, baavgai, they should probably continue to be doubles (or more efficiently, floats, given that there's no need for double-precision in this sample) as the text says that fractional scores are permitted.

double calcScore(double val1, double val2, double val3, double val4, double val5)
{
//etc
}


If you need more help than that, I'll see what I can do to help from work.
Was This Post Helpful? 0
  • +
  • -

#7 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: Problem with Functions

Posted 15 September 2011 - 01:00 PM

View PostMina-kun, on 15 September 2011 - 12:54 PM, said:

And if you haven't learned them and skipping ahead in the book is not permissible, then your instructor should be banned from programming. ;)

Also, baavgai, they should probably continue to be doubles (or more efficiently, floats, given that there's no need for double-precision in this sample) as the text says that fractional scores are permitted.

double calcScore(double val1, double val2, double val3, double val4, double val5)
{
//etc
}


If you need more help than that, I'll see what I can do to help from work.


This chapter does not include arrays. This question is from the current chapter.
It is in the next chapter.We are not allowed to use arrays.

View Postbaavgai, on 15 September 2011 - 12:50 PM, said:

Look more closely at your assignment.

View Postqwerty159, on 15 September 2011 - 03:17 PM, said:

void getJudgeData() -- This should ask the user for a judge's score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the 5 judges.


So, something like: void getJudgeData(ing &score);

Note that findLowest and findHighest return ints, implying that your scores are ints, not double.

This assignment screams for arrays. If you've learned them, then you should be using them. If you haven't learned them, but skipping ahead in the book is permissible, you should still be using them.


I already have what you said getJudgeData (int &score);
I don't understand the problem still.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Problem with Functions

Posted 15 September 2011 - 01:12 PM

One problem would be this:
getJudgeData(val1,val2,val3,val4,val5);
double calcScore();



You've gathered up all your results in val1,val2,val3,val4,val5. But you're clearly not passing them to calcScore. So, you can possibly be calculating anything based on the values entered.

Instead:
double value1,value2,value3,value4,value5;
// these values are meaningless, you've never loaded any values into them.
findLowest(value1,value2,value3,value4,value5);


Was This Post Helpful? 0
  • +
  • -

#9 Mina-kun  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 43
  • Joined: 13-September 11

Re: Problem with Functions

Posted 15 September 2011 - 01:16 PM

Edit: Damn I wish D.I.C. warned when a post was made while in the process of replying. <_<

What it wants is for you to use a single function for each judge call, rather than explicitly repeating the same calls to cin/cout and the input validation.

Example:
double getScore(int judge) {
  double score = 0;
  cout << "Enter score for judge " << judge << "> ";
  cin >> score;

  while(score < 0 || score > 10) {
    cout << "Please enter a score between 0 and 10!" << endl;
    cin >> score;
  }
}

double judge1 = 0;
judge1 = getScore(1);

This post has been edited by Mina-kun: 15 September 2011 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#10 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: Problem with Functions

Posted 15 September 2011 - 01:19 PM

View Postbaavgai, on 15 September 2011 - 01:12 PM, said:

One problem would be this:
getJudgeData(val1,val2,val3,val4,val5);
double calcScore();



You've gathered up all your results in val1,val2,val3,val4,val5. But you're clearly not passing them to calcScore. So, you can possibly be calculating anything based on the values entered.

Instead:
double value1,value2,value3,value4,value5;
// these values are meaningless, you've never loaded any values into them.
findLowest(value1,value2,value3,value4,value5);



I removed the things u said and instead i added
findLowest(value1,value2,value3,value4,value5);   // Removed
findHighest(value1,value2,value3,value4,value5);  // Removed


double calcScore(double value1,double value2,double value3,double value4,double value5) // Added. Only inner part in                                                                                     // the parentheses.


This post has been edited by qwerty159: 15 September 2011 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#11 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: Problem with Functions

Posted 15 September 2011 - 01:31 PM

It will be easier for me if u guys can point to me each line where I have made mistakes and guide me on those...Thanks
Was This Post Helpful? 0
  • +
  • -

#12 Mina-kun  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 43
  • Joined: 13-September 11

Re: Problem with Functions

Posted 15 September 2011 - 01:42 PM

I'm sorry to say, but looking over the requirements for the code and what you've written, I'm afraid your entire code block is incorrect.

Give me a couple minutes and I'll try to put together some steps to help walk you through writing it up.
Was This Post Helpful? 0
  • +
  • -

#13 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: Problem with Functions

Posted 15 September 2011 - 01:44 PM

Anyone interested in helping ? I never get help...

View PostMina-kun, on 15 September 2011 - 01:42 PM, said:

I'm sorry to say, but looking over the requirements for the code and what you've written, I'm afraid your entire code block is incorrect.

Give me a couple minutes and I'll try to put together some steps to help walk you through writing it up.

Thanks :)
Was This Post Helpful? -2
  • +
  • -

#14 Mina-kun  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 43
  • Joined: 13-September 11

Re: Problem with Functions

Posted 15 September 2011 - 02:02 PM

*
POPULAR

Alright, hope this helps (I'm not posting actual code for this- sorry, but I'm already blurring the line between helping and doing the work for you.)

On a side note,

Quote

Anyone interested in helping ? I never get help...

That's what we're trying to do. You have to understand that it is quite difficult to figure out whether somebody on the Internet is simply lazy, or genuinely doesn't understand an issue, and even harder to avoid giving away the answer and finding a way to actually facilitate understanding and learning. Patience is a virtue where software engineering is concerned, and unnecessary bumps look bad.

getJudgeData - Should ask the user for a judge's score, store it in a reference parameter variable, and validate it. Should be called by main once for each of the 5 judges

Returns: Nothing (void)
Parameters: (1) - Score reference (double&)
Steps:
Ask user for judge's score (store in the parameter)
Validate score
If invalid, ask for re-entry

calcScore - Should calculate and display the average of the 3 scores that remain after droping the highest and lowest scores. Should be called only once by main, and should be passed the five scores

Returns: Score (double)
Parameters: (5) - One double for each score
Steps:
Calculate the total score (add all five parameters)
Find and store the value of the highest score
Find and store the value of the lowest score
Subtract the highest and lowest scores from the total
Divide the final value by 3 (to get the average)
Display the average score value
Return the average score (though this is not used, by the instructions..)

findLowest - Should find and return the lowest of the 5 scores passed to it

Returns: lowest Score value (double)
Parameters: (5) - One double for each score
Steps:
Create a temporary value to hold the running low
Initialize this temporary value to an invalid, high number (11)
Cycle through each parameter. If it is lower than the running low, change the running low to that value.
Return the running low

findHighest - Should find and return the highest of the 5 scores passed to it

Returns: highest score value (double)
Parameters: (5) - One double for each score
Steps:
Create a temporary value to hold the running high
Initialize this temporary value to zero (0)
Cycle through each parameter. If it is higher than the running high, change the running high to that value
Return the running high

Main - Calls all of our functions created thus far

Returns: Integer
Parameters: None (void)
Steps:
Declare five variables to hold the values
Pass each variable, in order, through a call to getJudgeData
Pass all five variables, in order, to calcScore
If you want to do anything else with the score, you can store the value returned by calcScore. Otherwise, if you're displaying the score to the user within the calcScore function, you can just call it and leave.
Return to the operating system (return 0;)

This post has been edited by Mina-kun: 15 September 2011 - 02:06 PM

Was This Post Helpful? 5
  • +
  • -

#15 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: Problem with Functions

Posted 15 September 2011 - 02:08 PM

let me try ..i really don't understand the C++ language that is the problem.

This post has been edited by JackOfAllTrades: 15 September 2011 - 03:58 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? -1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2