I tried to have a console app that takes 5 numbers and fills an array with it but The issue with this code is that, it only accepts 4 numbers and fills the last index with null, how could I fix that?
string[] numbers = new string[4];
Console.WriteLine("Hi, Enter 5 numbers!");
ConsoleKeyInfo key;
for (int i = 0; i < numbers.Length; i++)
{
string _val = "";
do
{
key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace)
{
double val = 0;
bool _x = double.TryParse(key.KeyChar.ToString(), out val);
if (_x)
{
_val += key.KeyChar;
Console.Write(key.KeyChar);
}
}
else
{
if (key.Key == ConsoleKey.Backspace && _val.Length > 0)
{
_val = _val.Substring(0, (_val.Length - 1));
Console.Write("\b \b");
}
}
}
while (key.Key != ConsoleKey.Enter);
Console.WriteLine();
numbers[i] = _val;
}
Console.WriteLine();
for(int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(i + " : " + numbers[i]);
}
Console.WriteLine(numbers.Length);
Console.ReadKey();
}
In the code string[] numbers = new string[4]; means numbers array size is 4 and array starts with 0 index. Hence last index is not null. Change the string[4] to string[5]. Hope this will solve your problem.
Related
I'm creating a lottery program that users can choose from a series of numbers between a particular range and the program then creates its own version of numbers and compares those user values against the program's random numbers which any matches that the program then finds are then displayed to the user.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assessment1.Lottery
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please insert 6 lottery numbers:");
int range = 100;
int minValue = 0;
int maxValue = 100;
int a = 0;
Random rnd = new Random();
int[] myArray = new int[6];
for ( int i=0; i < myArray.Length; i++ )
{
int randomNumber = rnd.Next(minValue, maxValue);
myArray[i] = randomNumber;
Console.WriteLine(randomNumber);
myArray[i] = int.Parse(Console.ReadLine());
Console.WriteLine(myArray[i]);
while (a < 5)
{
if (int.TryParse(Console.ReadLine(), out myArray[a]))
a++;
else
Console.WriteLine("invalid integer");
}
double arry = myArray.Average();
if(arry > 0)
Console.WriteLine("found");
else
Console.WriteLine("not found");
int BinarySearch(int[] array, int value, int low, int high)
{
if ( high >= low)
{
int mid = (low + high) / 2;
if (array[mid] == value) return mid;
if (array[mid] == value) return BinarySearch(array, value, low, mid - 1);
return BinarySearch(array, value, mid + 1, high);
}
return -1;
}
}
Console.ReadLine();
}
}
}
when I run the program it shows the following
Please insert 6 lottery numbers: 64
how can I fix this so that the array can be filled with user input
Here is a version of something I wrote a while back and modified to your parameters, if there are bugs please don't ask me to fix.
static void Main(string[] args)
{
int stop = 0;
int[] chosenNum = new int[6];
while (stop == 0)
{
Console.Clear();
string con = "";
Console.WriteLine("Enter six numbers between 1-100 separated by a comma with no duplicates:");
string numbers = Console.ReadLine();
string[] userNumbers = numbers.Replace(" ", "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToArray();;//remove ex: ,, issues
string[] checkDup = userNumbers.Distinct().ToArray();//remove duplicates
if (userNumbers.Count() < 6)
{
Console.WriteLine("You entered less than six numbers");
Console.WriteLine("Try Again Y or N");
con = Console.ReadLine();
if (con.ToUpper() != "Y")
{
stop = 1;
}
}
else if (userNumbers.Count() > 6)
{
Console.WriteLine("You entered more than 6 numbers");
Console.WriteLine("Try Again Y or N");
con = Console.ReadLine();
if (con.ToUpper() != "Y")
{
stop = 1;
}
}
else if (checkDup.Count() < 6)
{
Console.WriteLine("You entered duplicate numbers");
Console.WriteLine("Try Again Y or N");
con = Console.ReadLine();
if (con.ToUpper() != "Y")
{
stop = 1;
}
}
else if (!isNumeric(userNumbers))
{
Console.WriteLine("You entered non-numeric value(s)");
Console.WriteLine("Try Again Y or N");
con = Console.ReadLine();
if (con.ToUpper() != "Y")
{
stop = 1;
}
}
else if (isInRange(userNumbers) < 6)
{
Console.WriteLine("You entered out of range value(s)");
Console.WriteLine("Try Again Y or N");
con = Console.ReadLine();
if (con.ToUpper() != "Y")
{
stop = 1;
}
}
else
{
var lowerBound = 1;
var upperBound = 100;
var random = new Random();
int[] randomNum = new int[6];
int count = 0;
foreach(string str in userNumbers){
var rNum = random.Next(lowerBound, upperBound);
randomNum[count] = rNum;
count++;
}
string[] ourPicks = Array.ConvertAll(randomNum, s => s.ToString()).ToArray();
Array.Sort(userNumbers);
Array.Sort(ourPicks);
//string[] ourpicks = { "1", "2" };//for testing
Console.WriteLine("Your Numbers: {0}", string.Join(", ", userNumbers));
Console.WriteLine("Our Numbers: {0}", string.Join(", ", ourPicks));
string[] result = userNumbers.Intersect(ourPicks).ToArray();
if(result.Count() > 0)
{
Console.WriteLine("Matchs: {0}", string.Join(", ", result));
}
else
{
Console.WriteLine("Match's = 0");
}
stop = 1;
}
}
Console.ReadLine();
}
public static bool isNumeric(string[] num)
{
foreach (string str in num)
{
bool check = int.TryParse(str, out int test);
if (!check)
{
return false;
}
}
return true;
}
public static int isInRange(string[] num)
{
int count = 0;
foreach (string str in num)
{
int.TryParse(str, out int test);
if (test > 0 & test <= 100)
{
count++;
}
}
return count;
}
I'm supposed to make a code in c# (microsoft visual studio 2017) that lets the user input six numbers, and then compare them to an array of six randomly generated numbers(with no duplicates).
If the user has one match, he's supposed to get a message saying he had one match, and different messages for two or three matches, and so on.
This is what I got so far:
static bool isValueInArray(int value, int[] a)
{
for (int i = 0; i < a.Length; i++)
{
if (a[i] == value)
{
return true;
}
}
return false;
}
static void Main(string[] args)
{
int min = 0;
int max = 6;
Random randnum = new Random();//random number generator
int[] numbers = new int[6];// generates six numbers
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = randnum.Next(min, max);//sets the numbers between 0 to 6
if(isValueInArray( i, numbers))
{
numbers[i] = randnum.Next(min, max);
}
}
try
{
Console.WriteLine("Hello and Welcome to our little game of lottery! lets see just how much luck you got!"); // greetings and instructions
Console.WriteLine("You'll now get to choose 6 different numbers between 0 to 6 to play with.");
Console.WriteLine("Go ahead and type them in.");
int[] lottery = new int[6];
for (int i = 0; i < lottery.Length; i++)
{
lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
if (lottery[i] > 6)//checking if the numbers fit between 0 and 6
{
Console.WriteLine("whoops! the number you enetered isn't in range! please try again ^^");
break;
}
int x = 6;
for (int a = 0; a < lottery.Length; a++)
{
for (int b = 0; b < numbers.Length; b++)
{
if (lottery[a] == numbers[b])
{
a++;
x--;
if (x == 6)
{
Console.WriteLine("six match");
break;
}
else if (x == 5)
{
Console.WriteLine("five match");
break;
}
else if (x == 4)
{
Console.WriteLine("four match");
break;
}
else if (x == 3)
{
Console.WriteLine("three match");
break;
}
else if (x == 2)
{
Console.WriteLine("two match");
break;
}
else if (x == 1)
{
Console.WriteLine("one match");
break;
}
}
}
}
}
}
catch (FormatException)// checking if the input is in char format
{
Console.WriteLine("only numbers please!");
}
}
My problem is with the output. The program seems to go over all of the "else if" options and print all of them, instead of picking and printing just one of them.
You have everything mixed together. Write out the steps:
Generate numbers
Get input from user
Count number of matches
Print results
Each step should be performed separately.
// These should be the min/max lottery numbers
int min = 1;
int max = 100;
int numberOfLotteryNumbers = 6;
// Renamed for clarity
int[] lotteryNumbers = new int[numberOfLotteryNumbers];
int[] userNumbers = new int[numberOfLotteryNumbers];
// Step 1 - generate numbers
for (int i = 0; i < lotteryNumbers.Length; i++) {
int randomNumber;
do {
randomNumber = randnum.Next(min, max);
} while (isValueInArray(randomNumber, lotteryNumbers));
lotteryNumbers[i] = randomNumber;
}
// Step 2 - get numbers from user
for (int i = 0; i < lottery.Length; i++) {
int userInput;
do {
userInput = int.Parse(Console.ReadLine());
} while (userInput < min || userInput > max || isValueInArray(userInput, userNumbers));
userNumbers[i] = userInput;
}
// Step 3 - calc matches
int matches = 0;
for (int i = 0; i < userNumbers.Length; i++) {
if (isValueInArray(userNumbers[i], lotteryNumbers) {
matches += 1;
}
}
// Step 4 - print results
Console.WriteLine("There are {0} matches.", matches);
You may have to rearrange the code blocks in a way to get user input first, calculate the number of matches first, then display results as following code:
Note: Your approach to guarantee unique numbers in the randomly generated number ain't going to work as you expect to, you are passing "i" where you may want to pass "numbers[i]" instead to the isValueInArray" function. Let aside the idea that you will always end with values 0-5 in the array since you want 6 numbers.
int min = 0;
int max = 6;
Random randnum = new Random();//random number generator
int[] numbers = new int[6];// generates six numbers
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = randnum.Next(min, max);//sets the numbers between 0 to 6
if (isValueInArray(i, numbers))
{
numbers[i] = randnum.Next(min, max);
}
}
try
{
Console.WriteLine("Hello and Welcome to our little game of lottery! lets see just how much luck you got!"); // greetings and instructions
Console.WriteLine("You'll now get to choose 6 different numbers between 0 to 6 to play with.");
Console.WriteLine("Go ahead and type them in.");
int[] lottery = new int[6];
int x = 0;
//read user numbers
for (int i = 0; i < lottery.Length; i++)
{
lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
while (lottery[i] > 6)//checking if the numbers fit between 0 and 6
{
Console.WriteLine("whoops! the number you enetered isn't in range! please try again ^^");
lottery[i] = int.Parse(Console.ReadLine()); // array to catch six numbers input
}
}
//count number of matches
for (int a = 0; a < lottery.Length; a++)
{
for (int b = 0; b < numbers.Length; b++)
{
if (lottery[a] == numbers[b])
{
//a++;
x++;
break;
}
}
}
//display results
if (x == 6)
{
Console.WriteLine("six matches");
}
else if (x == 5)
{
Console.WriteLine("five matches");
}
else if (x == 4)
{
Console.WriteLine("four matches");
}
else if (x == 3)
{
Console.WriteLine("three matches");
}
else if (x == 2)
{
Console.WriteLine("two matches");
}
else if (x == 1)
{
Console.WriteLine("one match");
}
}
catch (FormatException)// checking if the input is in char format
{
Console.WriteLine("only numbers please!");
}
Console.Read();
}
You can use a counter to achieve your goal. Just increment counter value on match:
int counter = 0;
for (int i = 0; i < lottery.Length; i++)
{
// ..
if (x == number)
{
counter++;
break;
}
// ..
}
Console.WriteLine("You have " + counter + " matches");
I need to find the difference of each number value in an array from an average value. I need to loop through each value and subtract each value FROM the average and display the difference. I have tried several different ways but the difference always comes out as 0 at the end. What am i doing wrong here?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace AvgNDiff
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[10];
int x = 0;
int i;
string entryString = "";
int counter = 0;
int countdown = 10;
int sum = 0;
int average = 0;
while (counter < numbers.Length && numbers[x] != 10 && entryString != "0")
{
if (x == 0)
Write("Enter up to 10 numbers or type 0 to stop > ");
else if (x == 9)
Write("Enter {0} more number or type 0 to stop > ", countdown);
else
Write("Enter up to {0} more numbers or type 0 to stop > ", countdown);
entryString = ReadLine();
numbers[x] = Convert.ToInt32(entryString);
if (entryString != "0")
{
sum += numbers[x];
counter++;
x++;
}
countdown--;
}
average = sum / x;
WriteLine("\n\nYou entered {0} numbers with a sum of {1}", x, sum);
WriteLine("The average of your numbers is " + average);
WriteLine("\n\nNumber Difference");
WriteLine("-------------------------------");
for (i=0; i < x; i++)
{
int value = numbers[i];
int diff = average-value;
WriteLine(String.Format("{0,-10} {1,-10}", (numbers[i]), diff));
}
ReadKey();
}
}
}
Take a look here
int value = numbers[i];
int diff = value - average;
WriteLine(String.Format("{0,-10} {1,-10}", (numbers[i]), value));
the key issue here is the writeline statement.
Youve told it to display numbers[i], and oh wait.. numbers[i] (as thats what value is)
yet diff contains the variance from the average...
static void Main(string[] args)
{
List<int> numberList = new List<int>();
Console.WriteLine("Enter up to 10 numbers or type 0 to stop:");
for (int i = 0; i < 10; i++)
{
int userInput = 0;
while (!int.TryParse(Console.ReadLine(), out userInput))
{
Console.WriteLine("Only integer numbers accepted, let's try again...:");
}
if (userInput == 0)
{
break;
}
else
{
numberList.Add(userInput);
if (i < 9)
{
Console.WriteLine("Yeah, {0} more number{1} to go!", (9 - i), (i == 8 ? "" : "s"));
}
}
}
double average = numberList.Average();
for (int i = 0; i < numberList.Count; i++)
{
Console.WriteLine("#{0}: {1} - {2} = {3}", (i+1), numberList[i], average, (numberList[i] - average));
}
Console.ReadLine();
}
Do not cram the entire soultion into the single Main, extract (and debug) a method:
// Subtract each item from the average
private static double[] MyNormalize(int[] source) {
double sum = 0.0;
foreach (var item in source)
sum += item;
double[] result = new double[source.Length];
for (int i = 0; i < source.Length; ++i)
result[i] = sum / source.Length - source[i];
return result;
}
...
static void Main(string[] args) {
int[] numbers = new int[10];
...
// Input
while (counter < numbers.Length && numbers[x] != 10 && entryString != "0") {
...
entryString = ReadLine();
numbers[x] = Convert.ToInt32(entryString);
}
// Business logic
double[] norm = MyNormalize(numbers);
// Output
for (int i = 0; i < numbers.Length; ++i)
WriteLine(String.Format("{0,-10} {1,-10}", numbers[i], norm[i]));
}
I am trying to create a program that is will take letters as input only and not duplicated. I am getting error when i put one letter in an input.
This is what i need to do, I need to get the user input in each line (like a enter, b enter, etc), if there is a duplication value i need a error message and continues with the input, and if there is incorrect value i get another error stating such. I cannot use LINQ, Hashset, nor list, it has to be arrays.
static void Main(string[] args)
{
char[] Array = new char[5];
Console.WriteLine("Please Enter 5 Letters B/W a through j only: ");
string letters = "abcdefghij";
char[] read = Console.ReadLine().ToLower().ToCharArray();
//loop through array
for (int i = 0; i < 5; i++)
{
if (letters.Contains(read[i]) && !Array.Contains(read[i]))
{
Array[i] = read[i];
}
else
{
Console.WriteLine("You have entered an incorrect value");
}
}
Console.WriteLine("You have Entered the following Inputs: ");
for (int i = 0; i < Array.Length; i++)
{
Console.WriteLine(Array[i]);
}
Console.ReadKey();
}
I think this more or less does what you want:
var max = 5;
var array = new char[max];
var letters = "abcdefghij";
var count = 0;
while (count < 5)
{
Console.WriteLine("Please Enter {0} Letters B/W a through j only: ", max);
var key = Console.ReadKey();
var read = key.KeyChar
if (!letters.Contains(read))
{
Console.WriteLine("You have entered an incorrect value");
continue;
}
var found = false;
for (int i = 0; i < count; i++)
{
if (array[i] == read)
{
found = true;
}
}
if (found)
{
Console.WriteLine("You have entered an duplicate value");
continue;
}
array[count++] = read;
}
Console.WriteLine("You have Entered the following Inputs: ");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
If you want the user to enter the values individually then you would need to request each character in a loop.
Something like:
static void Main(string[] args)
{
const string validValues = "abcdefghij";
var enteredCharacters = new char[5];
for (int i = 0; i < enteredCharacters.Length; i++)
{
Console.WriteLine("Please enter a unique character between a and j");
var input = Console.ReadLine();
if (input.Length == 0)
{
Console.WriteLine("You did not enter a value.");
return;
}
if (input.Length > 1)
{
Console.WriteLine("You have entered more than 1 character");
return;
}
var character = input[0];
if (!validValues.Contains(character))
{
Console.WriteLine("You have entered an invalid character");
return;
}
if (enteredValues.Contains(character))
{
Console.WriteLine("You have already entered this character");
return;
}
enteredCharacters[i] = character;
}
// process numbers.
}
this is you problem
for (int i = 0; i < 5; i++)
this is your fix:
static void Main(string[] args)
{
char[] Array = new char[5];
Console.WriteLine("Please Enter 5 Letters B/W a through j only: ");
string letters = "abcdefghij";
char[] read = Console.ReadLine().ToLower().ToCharArray();
//loop through array
for (int i = 0; i < read.Length; i++)
{
if (letters.Contains(read[i]) && !Array.Contains(read[i]))
{
Array[i] = read[i];
}
else
{
Console.WriteLine("You have entered an incorrect value");
}
}
Console.WriteLine("You have Entered the following Inputs: ");
for (int i = 0; i < Array.Length; i++)
{
Console.WriteLine(Array[i]);
}
Console.ReadKey();
}
Can you help with this?
I need to compare lexicographically two arrays: if one of them is shorter than other, it is lexicographically first. If their length are same, they had to be compared element by element. If an element is before the other in the alphabet, this array is lexicographically first.
Here is my code:
using System;
internal class CompareTwoCharArraysLexicographically
{
private static void Main()
{
char[] firstArray = {'a', 'b', 'c', 'z'};
int firstArrayLength = firstArray.Length;
char[] secondArray = {'a', 'b', 'c', 'd'};
int secondArrayLength = secondArray.Length;
int length = Math.Min(firstArray.Length, secondArray.Length);
if (firstArray.Length > secondArray.Length)
{
Console.WriteLine("Second array is earlier.");
}
else if (firstArray.Length == secondArray.Length)
{
for (int i = 0; i < length; i++)
{
if (firstArray[i] > secondArray[i])
{
Console.WriteLine("2 array is earlier.");
break;
}
else if (secondArray[i] > firstArray[i])
{
Console.WriteLine("1 array is earlier.");
break;
}
else
{
Console.WriteLine("Two arrays are equal.");
}
}
}
else
{
Console.WriteLine("First array is earlier.");
}
}
}
How I can avoid three time repeated message "Two arrays are equal."?
Yet another structured way to do it:
class LexicographicCharArrayComparer : Comparer<char[]>
{
public override int Compare(char[] x, char[] y)
{
if (x == null || y == null)
return Default.Compare(x, y);
int lengthComp = x.Length.CompareTo(y.Length);
if (lengthComp != 0)
return lengthComp;
return StringComparer.Ordinal.Compare(new string(x), new string(y));
}
}
Usage:
char[] firstArray = { 'a', 'b', 'c', 'z', };
char[] secondArray = { 'a', 'b', 'c', 'd', };
var comparer = new LexicographicCharArrayComparer();
var result = comparer.Compare(firstArray, secondArray);
if (result < 0)
Console.WriteLine("1st array is earlier");
else if (result == 0)
Console.WriteLine("The two arrays are equal");
else
Console.WriteLine("2nd array is earlier");
Your own approach can be repaired, of course. Correct the middle block to something like:
else if (firstArray.Length == secondArray.Length)
{
bool resolved = false;
for (int i = 0; i < length; i++)
{
if (firstArray[i] > secondArray[i])
{
Console.WriteLine("2 array is earlier.");
resolved = true;
break;
}
if (secondArray[i] > firstArray[i])
{
Console.WriteLine("1 array is earlier.");
resolved = true;
break;
}
}
if (!resolved)
{
Console.WriteLine("Two arrays are equal.");
}
}
I think you will need to use a tracking variable to know whether or not the arrays are equal. Currently you are saying they are equal each time two items in the same index are equal. Instead, consider the following code:
else if (firstArray.Length == secondArray.Length)
{
var largerArray = 0;
// Compare each item in the array
for (int i = 0; i < length; i++)
{
// As soon as two items are not equal, set the comparison value and break
if (firstArray[i] > secondArray[i])
{
largerArray = 1;
break;
}
else if (secondArray[i] > firstArray[i])
{
largerArray = 2
break;
}
}
// Check the largerArray value. If it was never changed, the arrays are equal
// Otherwise, display a message based on the value of the largerArray.
if (largerArray == 0)
{
Console.WriteLine("Two arrays are equal.");
}
else
{
Console.WriteLine("{0} array is earlier.", largerArray);
}
}
Here is my suggestion:
Console.Write("Enter a positive number for length of the first array: ");
int n = int.Parse(Console.ReadLine());
Console.Write("Enter a positive number for length of the second array: ");
int m = int.Parse(Console.ReadLine());
// Declare and create the arrays
char[] firstArray = new char[n];
char[] secondArray = new char[m];
Console.WriteLine("Enter values of the arrays: ");
for (int i = 0; i < firstArray.Length; i++)
{
firstArray[i] = char.Parse(Console.ReadLine());
}
Console.WriteLine();
for (int i = 0; i < m; i++)
{
secondArray[i] = char.Parse(Console.ReadLine());
}
Console.WriteLine();
// Print them on the console
for (int i = 0; i < n; i++)
{
Console.Write(" " + firstArray[i]);
}
Console.WriteLine();
for (int i = 0; i < m; i++)
{
Console.Write(" " + secondArray[i]);
}
Console.WriteLine();
int minLength = Math.Min(firstArray.Length, secondArray.Length);
bool equalValues = true;
// Search which of the arrays is lexicographically earlier
for (int i = 0; i < minLength; i++)
{
if (firstArray[i] == secondArray[i])
{
continue;
}
else if (firstArray[i] < secondArray[i])
{
Console.WriteLine("The first array is earlier.");
break;
}
else if (firstArray[i] > secondArray[i])
{
Console.WriteLine("The second array is earlier.");
break;
}
}
for (int i = 0; i < minLength; i++)
{
if (firstArray[i] != secondArray[i])
{
equalValues = false;
}
}
// This is to indicate the values of the two arrays till the element of index minLength-1 are equal
for (int i = 0; i < minLength; i++)
{
if (equalValues && n < m)
{
Console.WriteLine("The first array is earlier.");
break;
}
else if (equalValues && n > m)
{
Console.WriteLine("The second array is earlier.");
break;
}
else if (equalValues && n == m)
{
Console.WriteLine("The two arrays aree equal.");
break;
}
}
Here is a relatively simple implementation using the sums of the ASCII values of the array characters as a lexicographical comparison criteria:
/* Method: compareLexicographically(a, b);
Compare lexicographically the two arrays passed as parameters and print result.
Comparison criteria:
1. Arrays lengths.
2. Accumulated ASCII values of the array characters.
*/
static void compareLexicographically(char[] a, char[] b)
{
if (a.Length == b.Length) // same lengths
{
int suma = 0;
int sumb = 0;
for (int i = 0; i < a.Length; i++)
{
suma += a[i];
sumb += b[i];
}
if (suma == sumb)
{
Console.WriteLine("Two arrays are lexicographically equal.\n");
}
else
{
if (suma < sumb)
{
Console.WriteLine("First array lexicographically smaller than second.\n");
}
else
{
Console.WriteLine("First array lexicographically greater than second.\n");
}
}
}
else // different lengths
{
if (a.Length < b.Length)
{
Console.WriteLine("First array lexicographically smaller than second.\n");
}
else
{
Console.WriteLine("First array lexicographically greater than second.\n");
}
}
}
I think that this fixes your problem :)
Hope I helped you as well u helped me find an answer of an exercise
using System;
internal class CompareTwoCharArraysLexicographically
{
private static void Main()
{
char[] firstArray = { 'a', 'b', 'c', 'z' };
int firstArrayLength = firstArray.Length;
char[] secondArray = { 'a', 'b', 'c', 'd' };
int secondArrayLength = secondArray.Length;
int length = Math.Min(firstArray.Length, secondArray.Length);
bool same = false;
if (firstArray.Length > secondArray.Length)
{
Console.WriteLine("Second array is earlier.");
}
else if (firstArray.Length == secondArray.Length)
{
for (int i = 0; i < length; i++)
{
if (firstArray[i] != secondArray[i])
{
same = false;
if (firstArray[i] > secondArray[i])
{
Console.Clear();
Console.WriteLine("2 array is earlier.");
break;
}
if (secondArray[i] > firstArray[i])
{
Console.Clear();
Console.WriteLine("1 array is earlier.");
break;
}
}
else same = true;
if (same == true)
{
Console.Clear();
Console.WriteLine("Two arrays are equal.");
}
else
{
Console.WriteLine("First array is earlier.");
}
}
}
}
}