need help with a function...
Hi
I need some help creating a function that will check if each element in the array "contestantScore[]" which is defined from user input, is less or more than the variable "average" which is defined from the sum of the array "contestantScore[] / contestantScore.length" and then returns either the String "above" or "below"
I have racked my brain now for hours trying to create a function that will do this, going over course material etc....
I'm not asking anyone to write the code but a point in the right direction would great.
I'll put my code up so far so you can see what I'm doing : )
The function I have up the top of my code doesn't work and returns errors...
"average cannot be resolved into a variable"
"contestantScore cannot be resolved into a variable"
Sorry about the formatting, it looked better in eclipse : (
import java.util.Scanner;
public class pracQ3_2
{
//I need a function that will return the String "above" or "below" if each element in the array is less or more than the average.
public String aboveOrBelow ()
{
String above;
String below;
if (contestantScore < average)
{
return below;
}
else
{
return above;
}
}
public static void main(String[] args)
{
int [] contestantNum;
double [] contestantScore;
int index;
double average;
Scanner keyboard = new Scanner(System.in);
contestantNum = new int [10];
contestantNum[0] = 1;
contestantNum[1] = 2;
contestantNum[2] = 3;
contestantNum[3] = 4;
contestantNum[4] = 5;
contestantNum[5] = 6;
contestantNum[6] = 7;
contestantNum[7] = 8;
contestantNum[8] = 9;
contestantNum[9] = 10;
contestantScore = new double [10];
for (index = 0; index < 10; index = index + 1)
{
System.out.print("Enter score for contestant number " + contestantNum[index] + ": ");
contestantScore[index] = keyboard.nextDouble();
}
System.out.println();
for (index = 0; index < 10; index = index + 1)
{
System.out.println("Contestant Number " + contestantNum[index] + " rated " + contestantScore[index] + " out of 10");
}
average = (contestantScore[0] + contestantScore[1] + contestantScore[2] + contestantScore[3] + contestantScore[4] + contestantScore[5] + contestantScore[6] + contestantScore[7] + contestantScore[8] + contestantScore[9]) / contestantScore.length;
System.out.print(average);
}
}
Re: need help with a function...
you has not declared these two variables in the method of aboveOrBelow
your method could be modified as followling, than called in the main method:
Code :
public static String aboveOrBelow (double score, double average)
{
String above = "above";
String below = "blow";
if (score < average)
{
return below;
}
else
{
return above;
}
}
Re: need help with a function...
Or just get rid of the variables and use String literals.
Re: need help with a function...
Code :
public String aboveOrBelow ()
{
String above;
String below;
if (contestantScore < average)
{
return below;
}
else
{
return above;
}
}
Does your program compile????
What are the contestantScore and average variables for this function? This function doesn't even know anything about them. It's scope is not in this function.
EDIT: And also, contestantScore is not a variable but the array of integer.
Re: need help with a function...
Hi
Thanks for the replies...
I'm trying to print if the contestantScore array elements are above or below the average using a function. I know I have to pass the contestantScore array elements and average variable into the function but can't figure out how to do this.
I have also fixed the problem with declaring the string variables in the function. Thanks.
So my now looks like this:
public String aboveOrBelow (double [] contestantScore, double average)
{
String above = "above";
String below = "below";
if (contestantScore[index] < average)
{
return below;
}
else if (contestantScore[index] > average)
{
return above;
}
}
Am I on the right track here???
"[index]" comes up as an error saying "index cannot be resolved into a variable" Any suggestions? This is where I want the program to check each element of the array..
Thanks again for the replies : )
Re: need help with a function...
What is index? From where index value is initiated?
Where is index declared?
You must read the beginners tutorials first. Coz you have a lack of understanding about declaration, initialization and their usage as well as scope.
Re: need help with a function...
Have done a re-vamp of my code, first I'll put up what happens when you run the code then after that will be the actual code in eclipse.
-----------------------------------------------------------------------------------------
Enter score for contestant number 1: 1
Enter score for contestant number 2: 2
Enter score for contestant number 3: 3
Enter score for contestant number 4: 4
Enter score for contestant number 5: 5
Enter score for contestant number 6: 6
Enter score for contestant number 7: 7
Enter score for contestant number 8: 8
Enter score for contestant number 9: 9
Enter score for contestant number 10: 10
Contestant Number 1 rated 1.0 out of 10, they were above average
Contestant Number 2 rated 2.0 out of 10, they were above average
Contestant Number 3 rated 3.0 out of 10, they were above average
Contestant Number 4 rated 4.0 out of 10, they were above average
Contestant Number 5 rated 5.0 out of 10, they were above average
Contestant Number 6 rated 6.0 out of 10, they were above average
Contestant Number 7 rated 7.0 out of 10, they were above average
Contestant Number 8 rated 8.0 out of 10, they were above average
Contestant Number 9 rated 9.0 out of 10, they were above average
Contestant Number 10 rated 10.0 out of 10, they were above average
Average = 5.5
-----------------------------------------------------------------------------------------
import java.util.Scanner;
public class pracQ3_2
{
public static void main(String[] args)
{
//variables
int [] contestantNum;
double [] contestantScore;
int index;
double average;
String aboveOrBelow = "";
Scanner keyboard = new Scanner(System.in);
//Defining each element in contestNum Array
contestantNum = new int [10];
contestantNum[0] = 1;
contestantNum[1] = 2;
contestantNum[2] = 3;
contestantNum[3] = 4;
contestantNum[4] = 5;
contestantNum[5] = 6;
contestantNum[6] = 7;
contestantNum[7] = 8;
contestantNum[8] = 9;
contestantNum[9] = 10;
contestantScore = new double [10];
//loop for defining the contestantScore elements
for (index = 0; index < 10; index = index + 1)
{
System.out.print("Enter score for contestant number " + contestantNum[index] + ": ");
contestantScore[index] = keyboard.nextDouble();
}
//calculating the Variable average
average = (contestantScore[0] + contestantScore[1] + contestantScore[2] + contestantScore[3] + contestantScore[4] + contestantScore[5] + contestantScore[6] + contestantScore[7] + contestantScore[8] + contestantScore[9]) / contestantScore.length;
//loop for defining the String variable "aboveOrBelow"
if (contestantScore[index] < average)
{
aboveOrBelow = "below";
}
else if (contestantScore[index] > average)
{
aboveOrBelow = "above";
}
System.out.println();
//loop for displaying "Contestant Number --- rated --- out of 10, they were above/below average" for each contestant
for (index = 0; index < 10; index = index + 1)
{
System.out.println("Contestant Number " + contestantNum[index] + " rated " + contestantScore[index] + " out of 10, " + "they were " + aboveOrBelow + " average");
}
//This is just testing what the average is and won't be in the final program
System.out.println("Average = " + average);
}
}
------------------------------------------------------------------------------------------
Re: need help with a function...
Why don't you put this code
Code :
if (contestantScore[index] < average)
{
aboveOrBelow = "below";
}
else if (contestantScore[index] > average)
{
aboveOrBelow = "above";
}
inside this
Code :
for (index = 0; index < 10; index = index + 1)
{
System.out.println("Contestant Number " + contestantNum[index] + " rated " + contestantScore[index] + " out of 10, " + "they were " + aboveOrBelow + " average");
}
Like,
Code :
for (index = 0; index < 10; index = index + 1)
{
if (contestantScore[index] < average)
{
aboveOrBelow = "below";
}
else if (contestantScore[index] > average)
{
aboveOrBelow = "above";
}
System.out.println("Contestant Number " + contestantNum[index] + " rated " + contestantScore[index] + " out of 10, " + "they were " + aboveOrBelow + " average");
}
EIDT: And i will recommend you to study the basics.
Re: need help with a function...
Doesn't matter.
Figured it out now.
Thanks anyway : )