# A method that returns a value?

### Question

• Hi!

If I want to write a Method, that have 3 parametres (x, x1, x2) of the datatype double, that should return "true" if x is between x1 and x2, otherwise it should return "false".

This means that the user should enter a value for x, x1 and x2, then the method should check the values, and return etiher a "true" och "false".

How can I do this?

Thnx!
Thursday, August 28, 2008 10:21 AM

• public bool XIsBetween(double x, double x1, double x2)
{
return (x > x1) && (x < x2);
}
• Marked as answer by Thursday, August 28, 2008 10:46 AM
• Edited by Thursday, August 28, 2008 11:01 AM Has code
Thursday, August 28, 2008 10:26 AM
• Each of the conditions inside the brackets is evaluated separately and will either produce a True or False value depending on the values of the parameters passed to the method.

e.g. if x is 2, x1 is 1 and x2 is 4

so... (2 > 1) (True)
(2 < 4) (True)

The && operator is then used to evaluate these results and produce a final outcome. Search for boolean algebra truth tables on Google to see how these final values are calculated.

The basic premise is:

For && (AND) : Both values must be True to return True else False is returned

For || (OR) : If either value is True (or both values are True) True is returned else False is returned

So in this case True && True returns true.
• Marked as answer by Thursday, August 28, 2008 10:46 AM
Thursday, August 28, 2008 10:38 AM
• The method evaluates the expression (x > x1) && (x < x2) and returns the result. The expression can be explained as something like this:
x is larger than x1 and smaller than x2. The mathematical expression will look like this. x1 < x < x2.

Anyway, the expression is evaluated as a boolean (true or false) and the result is returned.
• Marked as answer by Thursday, August 28, 2008 10:47 AM
Thursday, August 28, 2008 10:44 AM
• Hamedeus said:

Thank you very much for your descriptive answer! - "descriptive" I don't even know if there is such a word in english???

Anyway.

So how should the complete prog look like, if it only should do the above mentioned operation?

I have this code so far:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace methodtest01 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 } 13 14 static bool XIsBetween(double x, double x1, double x2) 15 { 16 return (x > x1) && (x < x2); 17 } 18 } 19 } 20

Thanks again!

In your 'Main' method, call the XIsBetween method passing it the values for x, x1 and x2 as below:

XIsBetween(2, 4, 3);

I presume you would want to do some other action based on this result, so your code will be like this:

if (XIsBetween(2, 4, 3))
{
// returns true, do some action
}
else
{
// returns false, do some action
}
• Marked as answer by Thursday, August 28, 2008 10:49 AM
Thursday, August 28, 2008 10:47 AM
• Here is a complete console application, which tests the method.

 using System; namespace methodtest01 { class Program { static void Main(string[] args) { Console.Write("Write the value of x: "); double x = double.Parse(Console.ReadLine()); Console.Write("Write the value of x1: "); double x1 = double.Parse(Console.ReadLine()); Console.Write("Write the value of x2: "); double x2 = double.Parse(Console.ReadLine()); Console.WriteLine("The method returned: " + XIsBetween(x, x1, x2).ToString()); Console.WriteLine("Press any key to quit"); Console.Read(); } static bool XIsBetween(double x, double x1, double x2) { return (x > x1) && (x < x2); } } }

• Marked as answer by Thursday, August 28, 2008 11:08 AM
Thursday, August 28, 2008 10:53 AM
• Yes, you can.

Wrap the call to XIsBetween in a call to Console.WriteLine() as below:

Console.WriteLine(XIsBetween(x, x1, x2));

To stop the console window from closing immediately add Console.ReadKey(); under this line.
• Edited by Thursday, August 28, 2008 10:57 AM Add code
• Marked as answer by Thursday, August 28, 2008 11:08 AM
Thursday, August 28, 2008 10:54 AM
• The method written by you, Hamedus, is more complete than mine, because it takes into account the possibility that x could be smaller than x1 but larger than x2, and not just the other way arround. Nice Work :-)
• Edited by Thursday, August 28, 2008 11:00 AM Spelling
• Marked as answer by Thursday, August 28, 2008 11:08 AM
Thursday, August 28, 2008 10:56 AM

 if (xMellan(x, x1, x2)) Console.WriteLine("True"); else Console.WriteLine("False");

you could just write:
 Console.WriteLine(xMellan(x, x1, x2));
• Marked as answer by Thursday, August 28, 2008 11:20 AM
Thursday, August 28, 2008 11:09 AM
• Something like this, will print the values

 for (int i = 0; i < arrayTest.Length; i++) { Console.WriteLine("array value with index " + i + " has value: " + arrayTest[i]); }
• Marked as answer by Thursday, August 28, 2008 11:20 AM
Thursday, August 28, 2008 11:13 AM
• This will calculate the mean value:

 int sumOfValues; double mean; foreach (int arrayValue in arrayTest) { sumOfValues += arrayValue; } mean = (double)sumOfValues / (double)arrayTest.Length; Console.WriteLine("The mean value: " + mean.ToString());
• Edited by Thursday, August 28, 2008 11:17 AM A variable was renamed in the example
• Marked as answer by Thursday, August 28, 2008 11:20 AM
Thursday, August 28, 2008 11:16 AM
• Something like this:

 using System; namespace arra01 { class Program { static void Main(string[] args) { int arraySize;              //Denna variabel bestämmer storleken på den array som skall skapas. Console.Write("Ange storlek på array: "); arraySize = Int32.Parse(Console.ReadLine());    //Användaren anger arrayens storlek. int[] arrayTest = new int[arraySize];           //Array datatyp "integer", namn "arrayTest", for (int i = 0; i < arrayTest.Length; i++)      //Så länge "i är mindre än storleken på arrayen. { Console.Write("Ange tal " + (i + 1) + ":\t"); arrayTest[i] = Int32.Parse(Console.ReadLine());    //Mata in värden till array. } for (int i = 0; i < arrayTest.Length; i++) { Console.WriteLine("array value with index " + i + " has value: " + arrayTest[i]); } int sumOfValues = 0; double mean; foreach (int arrayValue in arrayTest) { sumOfValues += arrayValue; } mean = (double)sumOfValues / (double)arrayTest.Length; Console.WriteLine("The mean value: " + mean.ToString()); Console.WriteLine("Press any key to quit"); Console.Read(); } } }

• Marked as answer by Thursday, August 28, 2008 11:29 AM
Thursday, August 28, 2008 11:25 AM
• Yes it does!

This piece of code calculates the sum of all values:
 foreach (int arrayValue in arrayTest) { sumOfValues += arrayValue; }

This peace of code calculates the mean value:
 mean = (double)sumOfValues / (double)arrayTest.Length;

The mean value is determined as the total sum of all values divided with the total number of values (The array length).
• Edited by Thursday, August 28, 2008 11:35 AM a liddle addition
• Marked as answer by Thursday, August 28, 2008 11:51 AM
Thursday, August 28, 2008 11:34 AM

### All replies

• public bool XIsBetween(double x, double x1, double x2)
{
return (x > x1) && (x < x2);
}
• Marked as answer by Thursday, August 28, 2008 10:46 AM
• Edited by Thursday, August 28, 2008 11:01 AM Has code
Thursday, August 28, 2008 10:26 AM

But I dont think that I really got your "message"? Could you please elaborate on the lines of code that you just wrote please?

Thank you!
Thursday, August 28, 2008 10:28 AM
• Each of the conditions inside the brackets is evaluated separately and will either produce a True or False value depending on the values of the parameters passed to the method.

e.g. if x is 2, x1 is 1 and x2 is 4

so... (2 > 1) (True)
(2 < 4) (True)

The && operator is then used to evaluate these results and produce a final outcome. Search for boolean algebra truth tables on Google to see how these final values are calculated.

The basic premise is:

For && (AND) : Both values must be True to return True else False is returned

For || (OR) : If either value is True (or both values are True) True is returned else False is returned

So in this case True && True returns true.
• Marked as answer by Thursday, August 28, 2008 10:46 AM
Thursday, August 28, 2008 10:38 AM
• Thank you very much for your descriptive answer! - "descriptive" I don't even know if there is such a word in english???

Anyway.

So how should the complete prog look like, if it only should do the above mentioned operation?

I have this code so far:
 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace methodtest01 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 } 13 14 static bool XIsBetween(double x, double x1, double x2) 15 { 16 return (x > x1) && (x < x2); 17 } 18 } 19 } 20

Thanks again!
Thursday, August 28, 2008 10:41 AM
• The method evaluates the expression (x > x1) && (x < x2) and returns the result. The expression can be explained as something like this:
x is larger than x1 and smaller than x2. The mathematical expression will look like this. x1 < x < x2.

Anyway, the expression is evaluated as a boolean (true or false) and the result is returned.
• Marked as answer by Thursday, August 28, 2008 10:47 AM
Thursday, August 28, 2008 10:44 AM
• Hamedeus said:

Thank you very much for your descriptive answer! - "descriptive" I don't even know if there is such a word in english???

Anyway.

So how should the complete prog look like, if it only should do the above mentioned operation?

I have this code so far:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace methodtest01 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 } 13 14 static bool XIsBetween(double x, double x1, double x2) 15 { 16 return (x > x1) && (x < x2); 17 } 18 } 19 } 20

Thanks again!

In your 'Main' method, call the XIsBetween method passing it the values for x, x1 and x2 as below:

XIsBetween(2, 4, 3);

I presume you would want to do some other action based on this result, so your code will be like this:

if (XIsBetween(2, 4, 3))
{
// returns true, do some action
}
else
{
// returns false, do some action
}
• Marked as answer by Thursday, August 28, 2008 10:49 AM
Thursday, August 28, 2008 10:47 AM
• This is my code so far:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace methodtest01 { class Program { static void Main(string[] args) { double x, x1, x2; Console.Write("x: "); x = Double.Parse(Console.ReadLine()); Console.Write("x1: "); x1 = Double.Parse(Console.ReadLine()); Console.Write("x2: "); x2 = Double.Parse(Console.ReadLine()); XIsBetween(x, x1, x2); } static bool XIsBetween(double x, double x1, double x2) { return ((x > x1) && (x < x2) || (x > x2) && (x < x1)); } } }

Notice that I changed the "return value".

But i cant print the return value "true" or "false" to screen? Based on the values inserted by the user?
Thursday, August 28, 2008 10:52 AM
• Here is a complete console application, which tests the method.

 using System; namespace methodtest01 { class Program { static void Main(string[] args) { Console.Write("Write the value of x: "); double x = double.Parse(Console.ReadLine()); Console.Write("Write the value of x1: "); double x1 = double.Parse(Console.ReadLine()); Console.Write("Write the value of x2: "); double x2 = double.Parse(Console.ReadLine()); Console.WriteLine("The method returned: " + XIsBetween(x, x1, x2).ToString()); Console.WriteLine("Press any key to quit"); Console.Read(); } static bool XIsBetween(double x, double x1, double x2) { return (x > x1) && (x < x2); } } }

• Marked as answer by Thursday, August 28, 2008 11:08 AM
Thursday, August 28, 2008 10:53 AM
• Yes, you can.

Wrap the call to XIsBetween in a call to Console.WriteLine() as below:

Console.WriteLine(XIsBetween(x, x1, x2));

To stop the console window from closing immediately add Console.ReadKey(); under this line.
• Edited by Thursday, August 28, 2008 10:57 AM Add code
• Marked as answer by Thursday, August 28, 2008 11:08 AM
Thursday, August 28, 2008 10:54 AM
• The method written by you, Hamedus, is more complete than mine, because it takes into account the possibility that x could be smaller than x1 but larger than x2, and not just the other way arround. Nice Work :-)
• Edited by Thursday, August 28, 2008 11:00 AM Spelling
• Marked as answer by Thursday, August 28, 2008 11:08 AM
Thursday, August 28, 2008 10:56 AM
• So this is my code now:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Uppgift03 7 { 8 class Uppgift03 9 { 10 /* ***************************************** */ 11 //Programet kontrollerar om x ligger mellan x1 och x2 12 //Skapat av Erik Lydecker den 28/8 2008 13 14 static void Main(string[] args)             //Main metoden 15 { 16 double x, x1, x2;                       //Variablerna x, x1 och x2 är av typen double. 17 18 Console.Write("x: "); 19 x = Double.Parse(Console.ReadLine());   //Mata in värdet för x 20 Console.Write("x1: "); 21 x1 = Double.Parse(Console.ReadLine());  //Mata in värdet för x1 22 Console.Write("x2: "); 23 x2 = Double.Parse(Console.ReadLine());  //Mata in värdet för x2 24 25 if (xMellan(x, x1, x2))                 //Om return värdet från metoden "xMellan" = true 26 Console.WriteLine("True");          //Skriv "true" 27 else 28 Console.WriteLine("False");         //Annars skriv "false" 29 } 30 31 static bool xMellan(double x, double x1, double x2)         //Metoden "xMellan" som har tre parametrar. 32 { 33 return ((x > x1) && (x < x2) || (x > x2) && (x < x1));  //Här kontrollerar om x ligger mellan x1 resp. x2 34 } 35 } 36 } 37

(The comments are written in Swedish.)

So now I got the app working as it should thanks to you guys!

Thanks again for your help! ;)
Thursday, August 28, 2008 11:04 AM
• I was also wondering how it is possible to "print the values inserted to an array" to the console screen?

This is my code so far:

 1 using System; 2 3 namespace arra01 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 int arraySize;              //Denna variabel bestämmer storleken på den array som skall skapas. 10 11 Console.Write("Ange storlek på array: "); 12 arraySize = Int32.Parse(Console.ReadLine());    //Användaren anger arrayens storlek. 13 14 int[] arrayTest = new int[arraySize];           //Array datatyp "integer", namn "arrayTest", 15 16 for (int i = 0; i < arrayTest.Length; i++)      //Så länge "i är mindre än storleken på arrayen. 17 { 18 Console.Write("Ange tal " + (i + 1) + ":\t"); 19 arrayTest [i] = Int32.Parse(Console.ReadLine());    //Mata in värden till array. 20 } 21 22 Console.WriteLine(arrayTest[(i / arraySize)]); 23 } 24 } 25 } 26

As you can see I let the user determine the size o the array, and then insert values to array.

The only think missing here is how I should print the values that have been saved and then also calculate the mean of the inserted values?

Thanks!
Thursday, August 28, 2008 11:08 AM

 if (xMellan(x, x1, x2)) Console.WriteLine("True"); else Console.WriteLine("False");

you could just write:
 Console.WriteLine(xMellan(x, x1, x2));
• Marked as answer by Thursday, August 28, 2008 11:20 AM
Thursday, August 28, 2008 11:09 AM
• Something like this, will print the values

 for (int i = 0; i < arrayTest.Length; i++) { Console.WriteLine("array value with index " + i + " has value: " + arrayTest[i]); }
• Marked as answer by Thursday, August 28, 2008 11:20 AM
Thursday, August 28, 2008 11:13 AM
• This will calculate the mean value:

 int sumOfValues; double mean; foreach (int arrayValue in arrayTest) { sumOfValues += arrayValue; } mean = (double)sumOfValues / (double)arrayTest.Length; Console.WriteLine("The mean value: " + mean.ToString());
• Edited by Thursday, August 28, 2008 11:17 AM A variable was renamed in the example
• Marked as answer by Thursday, August 28, 2008 11:20 AM
Thursday, August 28, 2008 11:16 AM
• Thank you!

But where should those lines of code be inserted?
Thursday, August 28, 2008 11:21 AM
• Something like this:

 using System; namespace arra01 { class Program { static void Main(string[] args) { int arraySize;              //Denna variabel bestämmer storleken på den array som skall skapas. Console.Write("Ange storlek på array: "); arraySize = Int32.Parse(Console.ReadLine());    //Användaren anger arrayens storlek. int[] arrayTest = new int[arraySize];           //Array datatyp "integer", namn "arrayTest", for (int i = 0; i < arrayTest.Length; i++)      //Så länge "i är mindre än storleken på arrayen. { Console.Write("Ange tal " + (i + 1) + ":\t"); arrayTest[i] = Int32.Parse(Console.ReadLine());    //Mata in värden till array. } for (int i = 0; i < arrayTest.Length; i++) { Console.WriteLine("array value with index " + i + " has value: " + arrayTest[i]); } int sumOfValues = 0; double mean; foreach (int arrayValue in arrayTest) { sumOfValues += arrayValue; } mean = (double)sumOfValues / (double)arrayTest.Length; Console.WriteLine("The mean value: " + mean.ToString()); Console.WriteLine("Press any key to quit"); Console.Read(); } } }

• Marked as answer by Thursday, August 28, 2008 11:29 AM
Thursday, August 28, 2008 11:25 AM
• What if I would like to print the values and the mean IN THE END. Not efter each time the user inserts a value to the array, as it is now.

How can this be done?

Thank you!
Thursday, August 28, 2008 11:30 AM
• Sorry but the above code only sums up the total values, it dosnt provide us with a "mean value" of all the inserted values???
Thursday, August 28, 2008 11:32 AM
• Yes it does!

This piece of code calculates the sum of all values:
 foreach (int arrayValue in arrayTest) { sumOfValues += arrayValue; }

This peace of code calculates the mean value:
 mean = (double)sumOfValues / (double)arrayTest.Length;

The mean value is determined as the total sum of all values divided with the total number of values (The array length).
• Edited by Thursday, August 28, 2008 11:35 AM a liddle addition
• Marked as answer by Thursday, August 28, 2008 11:51 AM
Thursday, August 28, 2008 11:34 AM
• I am not English so maybe I read it wrong, but I think you calculate the average and not the mean.

The mean is calculated as follows:

Array.Sort(arrayTest);
int mean = arrayTest[arrayTest.Length / 2];

edit:
Sorry I was wrong mean and average looks to be the same, the code above determines the Median.
• Edited by Thursday, August 28, 2008 12:02 PM Mistake (lost in translation)
Thursday, August 28, 2008 11:53 AM
• English is not my native language either, but I would think that what you describe is called the median.

"mean" and "average" are the same thing, but the word "average" is used in more everyday terms (He is of average height), and mean is more of a mathematical term.

http://dictionary.reference.com/browse/mean
Thursday, August 28, 2008 12:03 PM