I have the current coding which used to be a goto but I was told to not use goto anymore as it is frowned upon. I am having troubles changing it into for say a while loop. I am fairly new to C# and programming in general so some of this is completely new stuff to me. Any help would be appreciated. The actual question is input two numbers and find the lowest common multiple.
Here is the original with goto:
BOB:
if (b < d)
{
a++;
myInt = myInt * a;
b = myInt;
myInt = myInt / a;
if (b % myInt2 == 0)
{
Console.Write("{0} ", h);
Console.ReadLine();
}
}
if (d < b)
{
c++;
myInt2 = myInt2 * c;
d = myInt2;
myInt2 = myInt2 / c;
if (d % myInt == 0)
{
Console.Write("{0} ", t);
Console.ReadLine();
}
else
{
goto BOB;
}
}
else
{
goto BOB;
}
}
Here's a more efficient and concise implementation of the Least Common Multiple calculation which takes advantage of its relationship with the Greatest Common Factor (aka Greatest Common Divisor). This Greatest Common Factor function uses Euclid's Algorithm which is more efficient than the solutions offered by user1211929 or Tilak.
C#, C++, C:
static int gcf(int a, int b)
{
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}
static int lcm(int a, int b)
{
return (a / gcf(a, b)) * b;
}
For more information see the Wikipedia articles on computing LCM and GCF.
Try This:
using System;
public class FindLCM
{
public static int determineLCM(int a, int b)
{
int num1, num2;
if (a > b)
{
num1 = a; num2 = b;
}
else
{
num1 = b; num2 = a;
}
for (int i = 1; i < num2; i++)
{
int mult = num1 * i;
if (mult % num2 == 0)
{
return mult;
}
}
return num1 * num2;
}
public static void Main(String[] args)
{
int n1, n2;
Console.WriteLine("Enter 2 numbers to find LCM");
n1 = int.Parse(Console.ReadLine());
n2 = int.Parse(Console.ReadLine());
int result = determineLCM(n1, n2);
Console.WriteLine("LCM of {0} and {1} is {2}",n1,n2,result);
Console.Read();
}
}
Output:
Enter 2 numbers to find LCM
8
12
LCM of 8 and 12 is 24
Hey if anyone need something more modern :)
public static class MathHelpers
{
public static T GreatestCommonDivisor<T>(T a, T b) where T : INumber<T>
{
while (b != T.Zero)
{
var temp = b;
b = a % b;
a = temp;
}
return a;
}
public static T LeastCommonMultiple<T>(T a, T b) where T : INumber<T>
=> a / GreatestCommonDivisor(a, b) * b;
public static T LeastCommonMultiple<T>(this IEnumerable<T> values) where T : INumber<T>
=> values.Aggregate(LeastCommonMultiple);
}
Try this
int number1 = 20;
int number2 = 30;
for (tempNum = 1; ; tempNum++)
{
if (tempNum % number1 == 0 && tempNum % number2 == 0)
{
Console.WriteLine("L.C.M is - ");
Console.WriteLine(tempNum.ToString());
Console.Read();
break;
}
}
// output -> L.C.M is - 60
Here is one recursive solution. It might be on some interview question. I hope it helps
public static int GetLowestDenominator(int a, int b, int c = 2)
{
if (a == 1 | b == 1) {
return 1;
}
else if (a % c == 0 & b % c == 0)
{
return c;
}
else if (c < a & c < b)
{
c += 1;
return GetLowestDenominator(a, b, c);
}
else
{
return 0;
}
}
int num1, num2, mull = 1;
num1 = int.Parse(Console.ReadLine());
num2 = int.Parse(Console.ReadLine());
for (int i = 1; i <= num1; i++)
{
for (int j = 1; j <= num2; j++)
{
if (num1 * j == num2 * i)
{
mull = num2 * i;
Console.Write(mull);
return;
}
}
}
Here is much optimized solution for finding LCM.
private static int lcmOfNumbers(int num1, int num2)
{
int temp = num1 > num2 ? num1 : num2;
int counter = 1;
while (!((temp* counter++) % num1 == 0 && (temp* counter++) % num2 == 0)) {
}
return temp* (counter-2);
}
int n1 = 13;
int n2 = 26;
for (int i = 2; i <= n1; i++)
{
if (n1 % i == 0 && n2 % i == 0)
{
Console.WriteLine("{0} is the LCM of {1} and
{2}",i,n1,n2);
break;
}
}
Related
I am trying to raise a number to a power using only addition but it does not work, it just raises a number bigger than the original.Here is my code:
private void ExpOperation()
{
result = 0;
num01 = Int32.Parse(inpu01.Text);
num02 = Int32.Parse(inpu02.Text);
int a = num02;
num02 = num01;
int i = 1;
while (i <= a)
{
result = SimpleMulti(num01,num02);
num01 = result;
i++;
}
result_Text.Text = result.ToString();
}
private int SimpleMulti (int num1, int num2)
{
int c = 0;
int i = 1;
while (i <= num2)
{
c += num1;
i++;
}
return c;
}
private int SimpleMulti (int x, int y)
{
int product = 0; //result of multiply
for (int i = 0; i<y; i++){
product += x;
}
//multiplication is repeated addition of x, repeated y times
//the initial solution with a while loop looks correct
return product;
}
private int ExpOperation(int x, int exponent)
{
int result = 1;
if (exponent == 0) {
return result; //anything that powers to 0 is 1
}
else
{
for (int i = 0; i < exponent; i++){
result = SimpleMulti(result, x);
//loop through exponent, multiply result by initial number, x
//e.g. 2^1 = 2, 2^2 = result of 2^1 x 2, 2^3 = result of 2^2 x 2
}
}
return result;
}
Keep in mind that this method does not support negative exponent, which deals with division, but instead of using SimpleMulti, you can create a method for SimpleDivide which uses subtraction instead. The principle is the same
I don't think this question is quite relevant to the main reason of this site, however I got a solution:
public long ExpOperation(int a, int b)
{
long result = 0;
long temp = 0;
for (int i = 1; i <= b; i++) // Executes a full loop when we have successfully multiplied the base number "a" by itself
{
for (int j = 1; j <= a; j++) // Increase the result by itself for a times to multiply the result by itself
result += temp;
temp = result:
}
return result;
}
Because x^y = x * x^(y-1), it can be solved recursively. Since SimpleMulti in question returns integer, I assume both base and exponent are non-negative integer.
private static int PowerWithAddition(int x, int y)
{
if(y == 0){
return 1;
}
var y1 = PowerWithAddition(x, y - 1);
var sum = 0;
for (int i = 0; i < y1; i++)
{
sum += x;
}
return sum;
}
Can someone help me fix this? I tried everthing i know. I made this code but nothing seems to appear when i run it. Thanks
class Program
{
static void Main()
{
int num1, num2, num3, num4, num5;
int Ahighest, b, c, d, Elowest;
Console.Write("Enter number : ");
num1 = int.Parse(Console.ReadLine());
Console.Write("Enter number : ");
num2 = int.Parse(Console.ReadLine());
Console.Write("Enter number : ");
num3 = int.Parse(Console.ReadLine());
Console.Write("Enter number : ");
num4 = int.Parse(Console.ReadLine());
Console.Write("Enter number : ");
num5 = int.Parse(Console.ReadLine());
//highest
if (num1 > num2)
{
if (num1 > num3)
{
if (num1 > num4)
{
if (num1 > num5)
{
Ahighest = num1;
//b
if (num2 > num3)
{
if (num2 > num4)
{
if (num2 > num5)
{
b = num2;
}
if (num3 > num2)
{
if (num3 > num4)
{
if (num3 > num5)
{
b = num3;
if (num4 > num2)
{
if (num4 > num3)
{
if (num4 > num5)
{
b = num4;
if (num5 > num2)
{
if (num5 > num3)
{
if (num5 > num4)
{
b = num5;
//c
if (b > num2)
{
c = num2;
if (b > num3)
{
c = num3;
if (b > num4)
{
c = num4;
if (b > num5)
{
c = num5;
//d
if (c > num2)
{
d = num2;
if (c > num3)
{
d = num3;
if (c > num4)
{
d = num4;
if (c > num5)
{
d = num5;
//lowest
if (d > num2)
{
Elowest = num2;
if (d > num3)
{
Elowest = num3;
if (d > num4)
{
Elowest = num4;
if (d > num5)
{
Elowest = num5;
Console.WriteLine(Ahighest + "," + b + "," + c + "," + d + "," + Elowest);
Console.ReadLine();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Instead of comparing every time with the others you could let the framework sort:
int[] orderedNumbers = { num1, num2, num3, num4, num5 };
Array.Sort( orderedNumbers );
Now you can find the lowest at:
orderedNumbers.First();
and the highest at:
orderedNumbers.Last();
or access every number via index (exception if there aren't so many items in the array):
int lowest = orderedNumbers[0];
int b = orderedNumbers[1];
int c = orderedNumbers[2];
int d = orderedNumbers[3];
int highest = orderedNumbers[4];
int[] nums = new int[5];
for(int i = 0; i < nums.Length; i++)
{
Console.Write($"Enter number {i}: ");
int tempInt;
bool number = int.TryParse(Console.ReadLine(), out tempInt);
nums[i] = number ? tempInt : int.MinValue;
}
Array.Sort(nums);
Console.WriteLine(string.Join(",", nums.Select(x => x == int.MinValue ? "NaN" : x.ToString()).ToArray()));
This code will ask the user to input each number, then check that it is actually an integer. If not it will set its value to -2147483647, the lowest value possible for an int.
After this it inserts that number into the array at the given index. The array is then sorted, after the string is output in the console, with each number split by a comma.
This code of course runs the risk that if someone, for whatever reason, inputs -2147483647, it will say it is not a number. A way around this is to use int? like so
int?[] nums = new int?[5];
for(int i = 0; i < nums.Length; i++)
{
Console.Write($"Enter number {i}: ");
int tempInt;
bool number = int.TryParse(Console.ReadLine(), out tempInt);
nums[i] = number ? tempInt : null;
}
Array.Sort(nums);
Console.WriteLine(string.Join(",", nums.Select(x => x == null ? "NaN" : x.ToString()).ToArray()));
In this code, if the conversion fails - the int is set to null instead of MinValue, this is because int? is a nullable type. Doing this allows us to have a fallback value without risking someone actually using it as input
"The greatest common divisor of two integers is the largest integer that evenly divides each of the two numbers. Write method Gcd that returns the greatest common divisor of two integers. Incorporate the method into an app that reads two values from the user and displays the result."
(this is not homework, just an exercise in the book I'm using)
can you help me solve this? Here's what I've got so far.
(edit - I can submit the two numbers but it won't calculate the Gcd for me)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Greatest_Common_Divisor
{
class Program
{
static int GetNum(string text)
{
bool IsItANumber = false;
int x = 0;
Console.WriteLine(text);
do
{
IsItANumber = int.TryParse(Console.ReadLine(), out x);
} while (!IsItANumber);
return x;
}
static void Main(string[] args)
{
string text = "enter a number";
int x = GetNum(text);
text = "enter a second number";
int y = GetNum(text);
int z = GCD(x, y);
Console.WriteLine(z);
}
private static int GCD(int x, int y)
{
int v = 0;
int n = 0;
v = GetGreatestDivisor(x, y);
return v;
}
static int GetGreatestDivisor(int m, int h)
{
do
{
for (int i = m; i <= 1; i--)
if (m%i == 0 && h%i == 0)
{
int x = 0;
x = i;
return x;
}
} while (true);
return m;
}
}
}
Here's an implementation of the Euclidean algorithm that returns the greatest common divisor without performing any heap allocation.
You can substitute ulong for uint if needed. An unsigned type is used, as the technique does not work for signed values. If you know your a and b values are not negative, you can use long or int instead.
private static ulong GCD(ulong a, ulong b)
{
while (a != 0 && b != 0)
{
if (a > b)
a %= b;
else
b %= a;
}
return a | b;
}
This method is used in my metadata-extractor library, where it has associated unit tests.
Using LINQ's Aggregate method:
static int GCD(int[] numbers)
{
return numbers.Aggregate(GCD);
}
static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
Note: answer above borrowed from accepted answer to Greatest Common Divisor from a set of more than 2 integers.
You can try using this:
static int GreatestCommonDivisor(int[] numbers)
{
return numbers.Aggregate(GCD);
}
static int GreatestCommonDivisor(int x, int y)
{
return y == 0 ? x : GreatestCommonDivisor(y, x % y);
}
Try this:
public static int GCD(int p, int q)
{
if(q == 0)
{
return p;
}
int r = p % q;
return GCD(q, r);
}
public class GCD
{
public int generalizedGCD(int num, int[] arr)
{
int gcd = arr[0];
for (int i = 1; i < num; i++) {
gcd = getGcd(arr[i], gcd);
}
return gcd;
}
public int getGcd(int x, int y)
{
if (x == 0)
return y;
return getGcd(y % x, x);
}
}
Here is a simple solution.
You can use BigInteger to get the greatest common divisor. Just do not forget to add using System.Numerics; to the top of your code.
using System.Numerics;
public class Program{
public static void Main(String[] args){
int n1 = 1;
int n2 = 2;
BigInteger gcd = BigInteger.GreatestCommonDivisor(n1,n2);
Console.WriteLine(gcd);
}
}
Offical Documentation
By using this, you can pass multiple values as well in the form of array:-
// pass all the values in array and call findGCD function
int findGCD(int arr[], int n)
{
int gcd = arr[0];
for (int i = 1; i < n; i++) {
gcd = getGcd(arr[i], gcd);
}
return gcd;
}
// check for gcd
int getGcd(int x, int y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}
List<int> gcd = new List<int>();
int n1, n2;
bool com = false;
Console.WriteLine("Enter first number: ");
n1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter second number: ");
n2 = int.Parse(Console.ReadLine());
for(int i = 1; i <= n1; i++)
{
if(n1 % i == 0 && n2% i == 0)
{
gcd.Add(i);
}
if(i == n1)
{
com = true;
}
}
if(com == true)
{
Console.WriteLine("GCD of {0} and {1} is {2}.", n1, n2, gcd[gcd.Count - 1]);
}
Console.ReadLine();
If efficiency is not a big concern this will do the job.
// gets greatest common divisor of A and B.
var GCD=Enumerable.Range(1,Math.Min(A,B)).Last(n=>(A%n | B%n)==0);
int[] nums = new int[] {6,12,24,48};
int GCD(int a, int b) => b == 0 ? a : GCD(b, a % b);
int FindGCD(int[] numbers) => numbers.Aggregate(GCD);
Console.WriteLine($"List of numbers ({String.Join(',',nums)})");
Console.WriteLine($"Smallest number: {nums.Min()}");
Console.WriteLine($"Largest number: {nums.Max()}");
Console.WriteLine($"Greatest common devisor of {nums.Min()} and {nums.Max()}: {GCD(nums.Min(),nums.Max())}");
Console.WriteLine($"Aggregate common devisor of array ({String.Join(',',nums)}): {FindGCD(nums)}");
List of numbers (6,12,24,48)
Smallest number: 6
Largest number: 48
Greatest common devisor of 6 and 48: 6
Aggregate common devisor of array (6,12,24,48): 6
using System;
//Write a function that returns the greatest common divisor (GCD) of two integers
namespace GCD_of_Two_Numbers
{
class Program
{
public static void Gcd(int num1, int num2)
{
int[] temp1 = new int[num1];
int[] temp2 = new int[num2];
int[] common = new int[10];
for(int i=2;i<num1/2;i++)
{
if(num1 % i ==0)
{
temp1[i] = i;
}
}
for (int i = 2; i < num2/2; i++)
{
if (num2 % i == 0)
{
temp2[i] = i;
}
}
int len = temp1.Length + temp2.Length;
for(int i=0;i<len;i++)
{
if(temp1[i]==temp2[i])
{
common[i] = temp1[i];
}
}
int max_number = common[0];
for(int i=0;i<common.Length;i++)
{
if(max_number < common[i])
{
max_number = common[i];
}
}
Console.WriteLine($"The Greatest Common Diviser is {max_number}");
}
static void Main(string[] args)
{
Gcd(32, 8);
}
}
}
int a=789456;
int b=97845645;
if(a>b)
{
}
else
{
int temp=0;
temp=a;
a=b;
b=temp;
}
int x=1;
int y=0 ;
for (int i =1 ; i < (b/2)+1 ; i++ )
{
if(a%i==0)
{
x=i;
}
if(b%i==0)
{
y=i;
}
if ((x==y)& x==i & y==i & i < a)
{
Console.WriteLine(i);
}
}
Currently I have this set of code and its meant to calculate factorials.
int numberInt = int.Parse(factorialNumberTextBox.Text);
for (int i = 1; i < numberInt; i++)
{
numberInt = numberInt * i;
}
factorialAnswerTextBox.Text = numberInt.ToString();
For some reason it doesn't work and i have no clue why. For example i will input 3 and get the answer as -458131456 which seems really strange.
Any help appreciated. Thanks
int numberInt = int.Parse(factorialNumberTextBox.Text);
int result = numberInt;
for (int i = 1; i < numberInt; i++)
{
result = result * i;
}
factorialAnswerTextBox.Text = result.ToString();
on a side note: this would normally NOT be the correct way to calculate factorials.
You'll need a check on the input before you can begin calculation, in case your starting value is 1 or below, in that case you need to manually return 1.
On another side note: this is also a perfect example of where recursive methods can be useful.
int Factorial(int i)
{
if (i <= 1)
return 1;
return i * Factorial(i - 1);
}
A little late to the party:
Func<int, int> factorial = n => n == 0 ? 1 :
Enumerable.Range(1, n).Aggregate((acc, x) => acc * x);
You can use this (rather elegant) solution:
Func<int, int> factorial = null;
factorial = x => x <= 1 ? 1 : x * factorial(x-1);
int numberInt = int.Parse(factorialNumberTextBox.Text);
factorialAnswerTextBox.Text = factorial(numberInt).ToString();
public static int Factorial(int facno)
{
int temno = 1;
for (int i = 1; i <= facno; i++)
{
temno = temno * i;
}
return temno;
}
i am late to the party but here it is
public ulong Factorial(uint numb)
{
if (numb <= 1) return 1;
ulong final = 1;
for (uint i = 1; i <= numb; i++)
{
final *= i;
}
return final;
}
Note:
i used un-signed types for better range
as this calculates up to Factorial(65), while normal signed types will give negative values
Trying to make a more bulletproof solution for n factorial. Here is one that guards for overflows, as well as negative and zero values of n. Using a result variable of type long (instead of int) allows for "larger" values to be calculated (for long, you can calculate up to and including n = 20).
This code returns 0 if an overflow occurred, but you can change it to do whatever is more appropriate.
static long nFactorial(int n)
{
if (n <= 1)
{
return 1;
}
long result = 1;
try
{
for (int i = 1; i <= n; i++)
{
result = checked(result * i);
}
}
catch (OverflowException)
{
return 0;
}
return result;
}
I had to create a factorial method for calculating combinations and tripped over the fact that factorials get very big very fast with relatively small inputs. Here's my solution without using recursion to avoid stack overflow and implemented using System.Numerics.BigInteger.
static BigInteger factorial(int num) {
BigInteger result = 1;
while (num > 1) {
result *= num--;
}
return result;
}
Obviously, you could also using BigInteger for input but my use case was that I was processing int values.
use factorial function:
static long Factorial(long number)
{
if( number <= 1 )
return 1;
else
return number * Factorial(number - 1);
}
and then call the function:
long result = Factorial(int.Parse(factorialNumberTextBox.Text));
factorialAnswerTextBox.Text = result.ToString();
int numberInt=1 ;
for (int i = 1; i <= int.Parse(factorialNumberTextBox.Text); i++)
{
numberInt = numberInt * i;
}
factorialNumberTextBox.Text = numberInt.ToString();
Try this,
int numberInt = int.Parse(textBox1.Text);
int answer = 1;
for (int i = 1; i <= numberInt; i++)
{
answer = answer * i;
}
textBox1.Text = answer.ToString();
Two methods are implemented: Recursive and Basic factorial calculation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication50
{
class Program
{
static void Main(string[] args)
{
NumberManipulator manipulator = new NumberManipulator();
Console.WriteLine("Please Enter Factorial Number:");
int a= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("---Basic Calling--");
Console.WriteLine("Factorial of {0} is: {1}" ,a, manipulator.factorial(a));
Console.WriteLine("--Recursively Calling--");
Console.WriteLine("Factorial of {0} is: {1}", a, manipulator.recursively(a));
Console.ReadLine();
}
}
class NumberManipulator
{
public int factorial(int num)
{
int result=1;
int b = 1;
do
{
result = result * b;
Console.WriteLine(result);
b++;
} while (num >= b);
return result;
}
public int recursively(int num)
{
if (num <= 1)
{
return 1;
}
else
{
return recursively(num - 1) * num;
}
}
}
}
static void Main()
{
int numberFactorial = int.Parse(Console.ReadLine());
int result = numberFactorial;
for (int i = 1; i < numberFactorial; i++)
{
result = result * i;
Console.WriteLine("{0}*{1}",numberFactorial,i);
}
Console.WriteLine(result);
}
A nice factorial solution for your nice evening.
int num = Convert.ToInt32(Console.ReadLine());
int fact = 1;
for (int i = num; i > 0; --i)
fact *= i;
Console.WriteLine(fact);
public static void Main(string[] args)
{
string result = Convert.ToString(GetFactorial(5));
Console.WriteLine(result);
}
internal static int GetFactorial(int factNumber)
{
int factorial =1;
int i = factNumber;
while(factNumber>=1)
{
factorial = factNumber * factorial;
factNumber--;
}
return factorial;
}
How about this?
public int FactorialFunction(int Factorial){
int Product = Factorial -1;
for(int Number = Factorial - 1; Number < Factorial; Number++ ) {
Factorial = Product * Factorial;
Product--;
}
return Factorial;
}
I wrote a program that asks a person to input a team number (assumming 0, 1 or 2) and how many goals the team scored for a certain game. I have two problems. First, the if statement that tries to see if there is a tie is completely ignored and, second, team 2 is always scored as the lowest, middle and highest team in a tie.
I am extremely new to C# so please answer for a newbie:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void updateScores(int x, int myTeam1, int myGoal1, int myTeam2, int myGoal2, int[,] values)
{
values[myTeam1, x] = myGoal1;
values[myTeam2, x] = myGoal2;
}
static void Main(string[] args)
{
//declare variables and integer array
//the different locations are set by default to 0
int highest;
int middle;
int lowest;
int counter = 0;
int x;
int y;
int z;
int team1;
int team2;
int goals1;
int goals2;
int[,] teamsGoalArray = new int[3, 4];
//get information about teams playing and goals scored
while (counter <= 2)
{
Console.WriteLine("Please enter the first team playing in the {0} game", counter+1);
team1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the number of goals for the team playing in the {0} game", counter +1);
goals1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the second team playing in the {0} game", counter+1);
team2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the number of goals for the team playing in the {0} game", counter +1);
goals2 = Convert.ToInt32(Console.ReadLine());
updateScores(counter, team1, goals1, team2, goals2, teamsGoalArray);
++counter;
}
int a = teamsGoalArray[0, 1] + teamsGoalArray[0, 2] + teamsGoalArray[0, 3];
int b = teamsGoalArray[1, 1] + teamsGoalArray[1, 2] + teamsGoalArray[1, 3];
int c = teamsGoalArray[2, 1] + teamsGoalArray[2, 2] + teamsGoalArray[2, 3];
if (a == b && a == c && b == c)
{
Console.WriteLine("All three teams had a total of {0} goals", a);
}
if (a >= b && a >= c)
{
highest = a;
x = 0;
}
else
if (b >= a && b >= c)
{
highest = b;
x = 1;
}
else
{
highest = c;
x = 2;
}
Console.WriteLine("Team {0} had the highest score with {1} goals", x, highest);
if (a < b && a > c || a > b && a < c)
{
middle = a;
y = 0;
}
else
if (b < a && b > c || b > a && b < c)
{
middle = b;
y = 1;
}
else
{
middle = c;
y = 2;
}
Console.WriteLine("Team {0} had the middle score with {1} goals", y, middle);
if (a < b && a < c)
{
lowest = a;
z = 0;
}
else
if (b < a && b < c)
{
lowest = b;
z = 1;
}
else
{
lowest = c;
z = 2;
}
Console.WriteLine("Team {0} had the lowest score with {1} goals", z, lowest);
}
}
}
The first problem I see is that when you call updateScores, you pass the value of counter, which will be 0, 1, or 2. So you're filling teamsGoalArray[team, 0] through teamsGoalArray[team, 2]. But when you're adding up the scores, you're adding teamsGoalArray[team, 1] through teamsGoalArray[team, 3]. So you are missing the scores from the first game for each team.