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.
Related
im new to programming and I am stuck on a part of a question.
After i create two methods-one for inputing Array and one for summing the elements that are odd and can be dividible to 5.I have to do this to three arrays-A,B and C.I've done all of this,but this time i have to find the elements of C(again),but with this formula:
((An-Bn)(A1-B1),(An-1 - Bn-1)(A2-B2),....(A1-B1)(An-Bn))
or in other words:
((A[n]-B[n])(A[0]-B[0]),(A[n-1]-B[n-1])(A[2]-B[2]),....,(A[1]-B[1])(A[n]-B[n])
If someone can help me to figure this out.I've tried a couple of things and most of them give the "Index out of bounds".
Thank you in advance.
namespace MethodsArrays
{
class Program
{
//Tochka 1 a/ i b/
static void Array(int[] p)
{
for (int i = 0; i < p.Length; i++)
{
p[i] = int.Parse(Console.ReadLine());
}
}
static void Output(int [] p)
{
for (int i = 0; i < p.Length; i++)
{
Console.WriteLine();
Console.Write(p[i] + "");
Console.WriteLine();
}
}
static int Multiple(int[] p)
{
int br = 0;
for (int i = 0; i < p.Length; i++)
{
if (p[i] % 2 == 1 && p[i] % 5 == 0)
{
//for (int j = 0; j < p.Length;j++)
//{
br += p[i];
//}
}
}
return br;
}
//Tochka 2
public static void Main()
{
Console.WriteLine("Type the legth of the array.");
Console.WriteLine();
int n = int.Parse(Console.ReadLine());
Console.Write("n= "+ n);
Console.WriteLine();
int[] a = new int[n];
Array(a);
Console.Write("Array A: ");
Output(a);
Console.WriteLine();
Console.Write("Length of B = "+ n);
Console.WriteLine();
int[] b = new int[n];
Array(b);
Console.Write("Array B: ");
Output(b);
Console.WriteLine();
Console.Write("Length of C= " + n);
Console.WriteLine();
int[] c = new int[n];
Array(c);
Console.Write("Array C: ");
Output(c);
Console.WriteLine();
int br1 = Multiple(a); Console.WriteLine("br1=" + br1);
int br2 = Multiple(b); Console.WriteLine("br2=" + br2);
int br3 = Multiple(c); Console.WriteLine("br3=" + br3);
double srgeo = (br1 * br2 * br3);
double srg = Math.Pow(srgeo, (double)1 / 3);
Console.WriteLine("Srednogeometrichno= "+ srg);
There there are n elements in a and b then the i-th element of c is
c[i] = (a[n-i-1]-b[n-i-1])*(a[i]-b[i])
Or more specifically, try the method below for composing c
static int[] ComposeArray(int[] a, int[] b)
{
if (a.Length == b.Length)
{
int n = a.Length;
int[] c = new int[n];
for (int i = 0; i < n; i++)
{
c[i] = (a[n-i-1]-b[n-i-1])*(a[i]-b[i]);
}
return c;
}
throw new ArgumentException();
}
since no test data was given in the question, I made up some on my own:
static void Main(string[] args)
{
var a = Enumerable.Range(1, 5).Select((i) => i).ToArray();
// [1,2,3,4,5]
var b = Enumerable.Range(4, 5).Select((i) => i).ToArray();
// [4,5,6,7,8]
var c = ComposeArray(a, b);
// [9,9,9,9,9]
}
I'm trying to make a binary search for a random array which has 10 numbers. When I run my code, and the number I input is a number within the random array, instead of just outputting "Found it" once, it will continuously output "Found it" until I close the program, but I can't understand what I've done to make it keep outputting "Found it".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Binary_Search
{
class Program
{
static void Main(string[] args)
{
int n = 10; //10 values in array
Random r = new Random();
int b; //value to search
int i; //loop control value
int[] a = new int[n + 1];
a[0] = 0; //starts at 0
for (i = 1; i <= n; i++) // set the array up
a[i] = a[i - 1] + r.Next(1, 10); // + random numbers from 1 to 10
for (i = 1; i <= n; i++)
Console.WriteLine(" a[" + i + "] + " + a[i] + ": "); // outputs the numbers for each value of array
Console.WriteLine();
Console.ReadLine();
Console.WriteLine("What number are you looking for?");
b = int.Parse(Console.ReadLine()); // enter value that you want to find
i = 1;
Console.ReadLine();
int min = 1;
int max = n - 1;
do
{
int mid = (min + max) / 2;
if (a[mid] == b)
{
Console.WriteLine("Found it");
}
else if (a[mid] > b)
max = mid;
else if (a[mid] < b)
min = mid;
} while (min <= max);
Console.ReadLine();
}
}
}
You need to break/stop the loop once you've found it.
if (a[mid] == b)
{
Console.WriteLine("Found it");
break; // add this
}
The "Found it" message keeps getting printed because of your do-while condition. You get into an infinite loop because after several iterations, min equals to max.
Set the condition to while (min < max); instead of while (min <= max); and have the if condition after the loop.
This should do the trick:
do
{
int mid = (min + max) / 2;
if (a[mid] > b)
max = mid;
else if (a[mid] < b)
min = mid;
} while (min < max);
if (a[mid] == b)
{
Console.WriteLine("Found it");
}
The number 124 has the property that it is the smallest number whose first three multiples contain the digit 2. Observe that
124*1 = 124, 124*2 = 248, 124*3 = 372 and that 124, 248 and 372 each contain the digit 2. It is possible to generalize this property to be the smallest number whose first n multiples each contain the digit 2. Write a function named smallest(n) that returns the smallest number whose first n multiples contain the digit 2. Hint: use modulo base 10 arithmetic to examine digits.
Its signature is
int smallest(int n)
Examples
If n is return because
4 624 because the first four multiples of 624 are 624, 1248, 1872, 2496 and they all contain the
digit 2. Furthermore 624 is the smallest number whose first four multiples contain the digit 2.
5 624 because the first five multiples of 624 are 624, 1248, 1872, 2496, 3120. Note that 624 is also
the smallest number whose first 4 multiples contain the digit 2.
6 642 because the first five multiples of 642 are 642, 1284, 1926, 2568, 3210, 3852
7 4062 because the first five multiples of 4062 are 4062, 8124, 12186, 16248, 20310, 24372, 28434.
Note that it is okay for one of the multiples to contain the digit 2 more than once (e.g., 24372).
I tried to solve this by this way
//Its a incomplete code
public static int smallest(int n)
{
int i = 1;
for (; ; )
{
int temp = 0;
int myNum = 0;
for (int j = 1; j <= n; j++)
{
myNum = i * j;
//check for condition
}
//if condition ture break
}
}
But I am stick to the problem is I cannot create hard coded n times variable.. Can you help me proceed that?
You may assume that such a number is computable on a 32 bit machine, i.e, you do not have to detect integer overflow in your answer.
using System;
using System.Collections.Generic;
namespace firstconsoleproject
{
class MainClass
{
public static void Main (string[] args)
{
int first=4;
int c=0;
int ax;
int ai;
Console.WriteLine ("please enter n");
ax = Convert.ToInt32( Console.ReadLine());
for (int i=11 ; ax>0 ;)
{ if (first==1)
{
c = ax+1;
i++;
}
c--;
ai=i*c;
for (int ten=10 ; ; )
{
if(ai%ten==2)
{
first=0;
break;
}else if (ai==0)
{
first=1;
break;
}
ai/=10;
}
if (c==0){Console.WriteLine("number is "+i);break;}
}Console.ReadKey ();
}
}
}
// Function smallest n
public int smallest(int a)
{
int temp = 0, holder = 0, k = 0;
if (a <= 0) return 0;
else
{
for (int i = 100; i < Int16.MaxValue; i++)
{
int count = 0;
k = 0;
int[] array = new int[a];
for (int j = 1; j < 9; j++)
{
holder = i * j;
temp = holder;
while (temp > 0)
{
int rem = temp % 10;
if (rem == 2)
{
count++;
if (k < a)
{
array[k] = j;
k++;
break;
}
}
temp /= 10;
}
if (count == a)
{
int countTemp = 0;
for (int h = 0; h < a; h++)
{
if (h + 1 < a)
{
if (array[h + 1] == array[h] + 1 && array[0] == 1 && array[h] > 0)
{
countTemp++;
if (countTemp == a - 1)
return i;
}
}
}
}
}
}
}
return 0;
}
public static int smallest(int n)
{
int i = 1;
for (; ; )
{
int contain = 0;
int temp = 0;
int myNum = 0;
for (int j = 1; j <= n; j++)
{
myNum = i * j;
temp = myNum;
while (true)
{
if (temp % 10 == 2)
{
contain++;
break;
}
temp = temp / 10;
if (temp <= 0)
break;
}
}
if (contain == n)
break;
i++;
}
return i;
}
using System;
namespace Shapes
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Character: ");
string symbol = (Console.ReadLine());
Console.WriteLine("Peak of Triangle: ");
int peak = Int32.Parse(Console.ReadLine());
int i = 0;
while (i <= peak) // spaces for triangle
{
Console.WriteLine(" ");
int z = 1;
while (z <= i) // Symbols for triangle
{
Console.Write(symbol);
z++;
}
i++;
}
Console.ReadLine();
}
}
}
Output: should start at one (*) then increase until the peak is met. Then should decrease back down to one of whatever input.
The output I am getting is not decreasing it stops at the peak input
You have to reverse the counter i to count back to 0.
Console.WriteLine("Character: ");
string symbol = (Console.ReadLine());
Console.WriteLine("Peak of Triangle: ");
int peak = Int32.Parse(Console.ReadLine()); // spaces for triangle
int i = 0;
int n = 1;
while (i != -1) // do it until i is negative
{
Console.WriteLine(" ");
int z = 1;
while (z <= i) // Symbols for triangle
{
Console.Write(symbol);
z++;
}
i += n; // increments when n = 1. decrements when n = -1
if (i >= peak) // reverse counter when it reaches peak
{
n = -1;
}
}
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;
}
}