I've created an array which generate 10 random numbers, and I want it to be compared to user input of 10 numbers. If 6 numbers are equal to the random generated numbers (from 1 to 25) then it should show a 6.
Also, the order should not matter. If the user input the number 8 as his or her first selection, it should be able to be compared to the random generated number if it generated number 8 as its last selection. Which in the end should show the result of minimum 1.
Random r = new Random();
int MyRandomNr = r.Next(1, 26);
// Random Generator
var ArrayRandom = new int[] { MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr };
Console.Write("HELLO AND WELCOME!"
+ System.Environment.NewLine + "Type in 10 numbers."
+ System.Environment.NewLine + "A number between from 1 to 25, one number at the time " + System.Environment.NewLine);
string[] ArrayUser = new string[10];
for (int i = 0; i < 10; i++)
{
Console.Write((i + 1) + ". Next number is: ");
ArrayUser [i] = Console.ReadLine();
}
I've looked through the Stackoverflow and the following code doesn't really compare the two arrays in the way I want it to compare to each other. The following code works more like an exam, but I'm looking for something similar to this...
int[] answer = { 1, 3, 4};
int[] exam = { 4, 1, 3};
int correctAnswers = 0;
int wrongAnswers = 0;
for (int index = 0; index < answer.Length; index++)
{
if (answer[index] == exam[index])
{
correctAnswers += 1;
}
else
{
wrongAnswers += 1;
}
}
Console.Write("The matching numbers are " + correctAnswers +
"\n" + "The non matching numbers are " + wrongAnswers);
Related
The problem I have is that when I enter an element from the array, the user input resets to the start of the loop, for example when i user input the third element from the array, the user input question two more times to continue to the next user input question, how can I fix this? everybody know what I can adjust here.
worked loop image with 1st element entered
looping error
while (true)
{
String[] bgtype = { "cheeseburger","tlc", "bbq" };
int[] bgtypeprice = { 15, 25, 10 };
int max = bgtype.Length;
String[] product = new string[max];
String[] type = new string[max];
int[] qty = new int[max];
int[] disc = new int[max];
Console.WriteLine("");
for (int i = 0; i < max; i++)
{
Console.Write("What PRODUCT would you like to buy ?: "); ;
product[i] = Console.ReadLine();
if (product[i].Equals("burger", StringComparison.CurrentCultureIgnoreCase))
{
{
Console.Write("What TYPE of product would you like to buy?: ");
type[i] = Console.ReadLine();
if (bgtype[i].Contains(type[i]))
{
Console.Write("Enter your discount % (5% for adults & 7% for minors): ");
disc[i] = Convert.ToInt32(Console.ReadLine());
Console.Write("How many will you buy? ");
qty[i] = Convert.ToInt32(Console.ReadLine());
float total = bgtypeprice[i]; total *= qty[i];
Console.WriteLine("Total cost of " + type[i] + " " + product[i] + " is: " + qty[i] + " pieces x P" + bgtypeprice[i] + "= P" + total);
float totaldisc = 0; totaldisc = (total * disc[i]) / 100;
Console.WriteLine("Total amount of discount: P " + totaldisc);
float totalamt = 0; totalamt = total - totaldisc;
Console.WriteLine("Total cost of order: P " + totalamt);
Console.WriteLine("-------------------ORDER CONFIRMATION-------------------");
}}}}}
if (bgtype[i].Contains(type[i])) should be changed to if (bgtype.Contains(type[i]))
Array.Contains, verifies if an item is contained within an Array, not an Array value. By asking if bgtype[i] contains type[i], you are asking if cheesburger contains tlc, which it doesn't, hence why it starts from the beginning. By verifying if bgtype contains type[i], you are asking if ["cheesburger", "tlc", "bbq"] contains tlc, which it does. I hope this was clear enough.
IN C# i am trying to solve a problem :
Write a program that checks whether the product of the odd elements is equal to the product of the even elements.
The only thing left is:
On the second line you will receive N numbers separated by a whitespace.
I am unable to get this working. I have tried with Split but it keeps breaking. Can someone help?
Example:
Input
5
2 1 1 6 3
Output
yes 6
static void Main(string[] args)
{
long N = long.Parse(Console.ReadLine());
long[] array = new long[N];
long ODD = 1;
long EVEN = 1;
for (int i = 0; i < N; i++)
{
array[i] = int.Parse(Console.ReadLine());
if ((i + 1) % 2 == 0)
{
EVEN *= array[i];
}
else
{
ODD *= array[i];
}
}
if (EVEN == ODD)
{
Console.WriteLine("yes" + " " +
ODD);
}
else
{
Console.WriteLine("no" + " " + ODD + " " + EVEN);
}
}
Read from console input and keep it to an string array, Then convert each array element to long and apply the Odd Even logic like below:
static void Main(string[] args)
{
string input = Console.ReadLine();
string[] inputArray = input.Split(' ');
long element;
long odd = 1;
long even = 1;
foreach (var i in inputArray)
{
element = long.Parse(i);
if (element % 2 == 0)
{
even *= element;
}
else
{
odd *= element;
}
}
Console.WriteLine("\nOdd product = " + odd + ", Even product = " + even);
if (odd == even)
{
Console.WriteLine("ODD == EVEN \n");
Console.WriteLine("Yes" + " " + odd);
}
else
{
Console.WriteLine("ODD != EVEN \n");
Console.WriteLine("No" + " " + odd + " " + even);
}
Console.ReadKey();
}
long[] nums = input.Split(' ').Select(x => long.Parse(x))..ToArray(); //split numbers by space and cast them as int
int oddProduct = 1, evenProduct = 1; // initial values
foreach(long x in nums.Where(a => a%2 == 1))
oddProduct *= x; // multiply odd ones
foreach(long x in nums.Where(a => a%2 == 0))
evenProduct *= x; // multiply even ones
I made [Hashtable hash] such as
hash(i, 1)
hash(j, 2)
Also I made an [arraylist sArray] which include "i" or "j" such as
sArray[0] : hello
sArray[1] : first number is i
sArray[2] : second number is j
sArray[3] : bye
Now, I want to change the "i" and "j" in the sArray to the values of the hash.
How can I do it?
If I understand properly, I think this is the code in c#
//Your example
int i = 1;
int j = 2;
var hash = new System.Collections.Hashtable();
hash[i] = -173.5;
hash[j] = 37;
var sArray = new System.Collections.ArrayList();
sArray.Add("hello");
sArray.Add("first number is " + hash[i].ToString());
sArray.Add("second number is " + hash[j].ToString());
sArray.Add("bye");
// more general, you could have different i and j position
i = 3;
j = 4;
hash[i] = 33.3;
hash[j] = -44.4;
sArray[1] = "number in " + i.ToString() + " position is " + hash[i].ToString();
sArray[2] = "number in " + j.ToString() + " position is " + hash[j].ToString();
// I think following option is more easy to read and fast if iterated
i = 5;
j = 6;
hash[i] = 55.5;
hash[j] = -66.6;
sArray[1] = String.Format("number in {0} position is {1}", i, hash[i]);
sArray[2] = String.Format("number in {0} position is {1}", j, hash[j]);
In C# how do i ask user for starting and stopping point within the array?
Below is my code so far:
class Program
{
static void Main(string[] args)
{
double[] num = { 10, 20, 30, 40, 50 };
double n = num.Length;
Console.Write("Elements of, arrary are:" + Environment.NewLine);
for (int i = 0; i < n; i++)
{
Console.WriteLine(num[i]);
}
double sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + num[i];
}
Console.WriteLine("The sum of elements:" + sum);
Console.ReadKey();
}
}
You'll take the sum of the elements between starting and stopping point, as I guess. Take two inputs from the user and assign them to starting and ending points to the for-loop. Such as:
int startingPoint = Convert.ToInt32(Console.ReadLine());
int endingPoint = Convert.ToInt32(Console.ReadLine());
for(int i = startingPoint; i <= endingPoint; i++)
{
//take sum etc.
}
Don't forget to inform the user about the element values in the array and what input value they are entering at that moment.
Another important thing here is to control the inputs. They should be numeric and between 0-n, starting point should be smaller than ending point.
For numeric control you can write like follows:
if (int.TryParse(n, out startingPoint))
{
// operate here
}
else
{
Console.WriteLine("That's why I don't trust you, enter a numeric value please.");
}
startingPoint should be between 0-n and cannot be n. To control it:
if (startingPoint >= 0 && startingPoint < n)
{
// operate here
}
else
{
Console.WriteLine("Please enter a number between 0 and " + n + ".");
}
After taking startingPoint successfully, you should control if endingPoint. It should be between startingPoint-n. After controlling for being numeric you can write as follows:
if (endingPoint >= startingPoint && endingPoint < n)
{
// operate here
}
else
{
Console.WriteLine("Please enter a number between " + startingPoint + " and " + n + ".");
}
I don't know what can I explain more for this question. Please let me know for further problems.
If you want to prompt the user for the start and end indexes:
Console.WriteLine("Please enter start index");
string startIndexAsString = Console.ReadLine();
int startIndex = int.Parse(startIndexAsString);
Console.WriteLine("Please enter end index");
string endIndexAsString = Console.ReadLine();
int endIndex = int.Parse(endIndexAsString);
var sum = num.Skip(startIndex).Take(endIndex - startIndex + 1).Sum();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Random number generator in C# - unique values
I'm trying to write a C# program that will generate a random number, check if this number is in my array, if it is, repeat generating the number, else insert this number into slot [i] of my array.
Here is my code so far:
int check = 0;
Random rand = new Random();
int[] lotto = new int[6];
for (int i = 0; i < lotto.Length; )
{
check = rand.Next(1, 41);
while (!(lotto.Contains(check)))
{
lotto[i] = check;
i++;
}
Console.WriteLine("slot " + i + " contains " + check);
}
Console.Read();
}
UPDATE: Thanks figured it out, replaced the if with while :)
I'm Guessing your question is what is not working, I am guessing that you have forgotten one ! and used an undeclared variable i:
if (!lotto.Contains(check)) // Ensure the number has not been chosen
{
lotto[count] = check; // Set the number to its correct place
count=count+1
}
If you want to generate random numbers without repeats, use a FIPS 140-2 validated random number generator (see section 4.9.3 on page 36 of http://www.hhs.gov/ocr/privacy/hipaa/administrative/securityrule/fips1402.pdf).
If this is being used for any serious gaming purpose, as your variable naming suggests, I would recommend something with better randomness than Random.
You can try this:
for (int i = 0; i < lotto.Length;)
{
check = rand.Next(1, 41);
Console.WriteLine("Random number to be checked is -> "+check);
if (!lotto.Contains(check))
{
lotto[i] = check;
i++;
}
Console.WriteLine("slot " + i + " contains " + check);
}
Notice that I have removed i++ from the for statement and has been put inside the if block.
You can try this with other loop constructs also, but this is to have the least amount of edit to your code.
Edit:
Well, I tried the code and seems to be working for me. Here is the complete code:
int check = 0;
int[] lotto = new int[6];
Random rand = new Random();
for (int i = 0; i < lotto.Length; )
{
check = rand.Next(1, 41);
Console.WriteLine("Random number to be checked is -> " + check);
if (!lotto.Contains(check))
{
lotto[i] = check;
i++;
}
Console.WriteLine("slot " + i + " contains " + check);
}
Console.ReadKey();
You can also use the while construct:
int check = 0;
int[] lotto = new int[6];
Random rand = new Random();
int i = 0;
while (i < lotto.Length)
{
check = rand.Next(1, 41);
Console.WriteLine("Random number to be checked is -> " + check);
if (!lotto.Contains(check))
{
lotto[i] = check;
i++;
}
Console.WriteLine("slot " + i + " contains " + check);
}
Console.ReadKey();
Basically, this is functions same as the previous code.