C# Print an array of strings in the reverse order - c#

so I have a problem where when I open my program and select 4 which is supposed to make it display all of the strings in the array but in reverse.
using System.Linq;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Homework3
{
class Program
{
static void Main(string[] args)
{
int optionIntOne;
int optionIntTwo;
int i;
int num = 0;
decimal k;
string[] names = {"Keyhan", "Adolf", "Niklas", "Jenny",
"Elisa", "Rudolf", "Bengt", "Hassan", "Kajsa", "Maggan"};
Console.WriteLine("Please Select an Option Below:\n1 - Find a student by Index \n" +
"2 - Find student Index by name\n3 - Display all student names with their Index \n" +
"4 - Display all the students in reverse order");
string optionStringone = Console.ReadLine();
bool isNumericOne = Int32.TryParse(optionStringone, out optionIntOne);
Console.Clear();
switch (optionIntOne)
{
case 1:
Console.WriteLine("***FIND Student by ID" + "\n" + "Please select a number between 0 - 9:");
string studentNumber = (Console.ReadLine());
bool isNumericTwo = Int32.TryParse(studentNumber, out optionIntTwo);
if (isNumericTwo == true && optionIntTwo < names.Length)
Console.WriteLine(names[optionIntTwo]);
else
Console.WriteLine("Please enter a valid number");
break;
case 2:
Console.WriteLine("*** FIND Student Index by Name \n Please Print one of the above names");
foreach (string name in names)
Console.WriteLine((name));
Console.WriteLine("\n");
string studentName = (Console.ReadLine().ToUpper());
//ToUpper så slipper loopen göra det på repeat
Console.Clear();
bool b = false;
if (decimal.TryParse(studentName, out k) || studentName == string.Empty)
{
Console.WriteLine("Please Enter a Valid Name");
}
else
{
for (i = 0; i < names.Length; i++)
{
if (names[i].ToUpper() == studentName)
{
b = true;
Console.WriteLine(names[i] + " Has the index of " + i);
break;
}
}
if (b == false)
Console.WriteLine("The Student does not Exist in the List");
}
break;
case 3:
while (num < names.Length)
{
Console.WriteLine(num + " - " + names[num]);
num++;
}
break;
case 4:
while (num < names.Length)
{
Console.WriteLine(names[num].Reverse());
num++;
}
break;
default:
Console.WriteLine("Enter a Valid Number");
break;
}
Console.ReadKey();
}
}
}
(option 3 but to print them in reverse order)
Visual Studio isnt giving me any errors but it outputs like this 1 instead of the reverse order of this 2
Anyone know how to fix?/could point out the problem

First of all this line:
Console.WriteLine(names[num].Reverse());
Is reversing the strings inside the list (so names[0].Reverse() would be "nahyeK") because string is also a list (a list of chars). If you want to reverse the whole list you only write names.Reverse();
read more at: Microsoft docs - Reverse()
So the next step would be to print it out.
When you do list.Reverse(); it will return IEnumerable<TSource> which you can use in a loop to get the names.
string[] names = {"Keyhan", "Adolf", "Niklas", "Jenny",
"Elisa", "Rudolf", "Bengt", "Hassan", "Kajsa", "Maggan"};
IEnumerable<string> reversedList = names.Reverse();
foreach(string name in reversedList)
{
Console.WriteLine(name);
}
I usually use list.Reverse().ToList() so i get a "normal" list again because it's in my opinion easier to use/understand.
There is some options, the namespace system.linq.enumerable got some stuff for that:
.ToArray() - converts it to an Array.
.ToList() - converts it to a List
So the complete solution would be to keep the .Reverse outside the loop like this:
foreach(name in names.Reverse().ToList())
{
Console.WriteLine(name);
}

Here is one option:
static void Main(string[] args)
{
int num = 0;
string[] names = { "Alice", "Bob", "Charles", "Delilah" };
while (num < names.Length)
{
Console.WriteLine(names.Reverse().ElementAt(num));
num++;
}
// Prints
//
// Delilah
// Charles
// Bob
// Alice
Console.ReadKey();
}
As per the comments it is not optimal to call Reverse() in each loop. Instead, perform the Reverse() outside of the loop.
A better alternative might be:
static void Main(string[] args)
{
string[] names = { "Alice", "Bob", "Charles", "Delilah" };
for (int num = names.Length - 1; num >= 0; num--)
{
Console.WriteLine(names[num]);
}
Console.ReadKey();
}

Related

C# I want to use either a switch or if statement to select a sorting algorithm

I am writing a program that will take user input and run it through whichever type of sort they choose. If I try to use a switch I cannot figure out how to add arguments to a switch or if I use an if statement how do I implement that with the user's input?
Here is the code and thank you all for your help.
using System;
namespace ASortAboveTheRest
{
internal class Program
{
static void Main(string[] args)
{
MainMenu();
}
static void MainMenu()
{
Console.Clear();
Console.WriteLine("Choose a sort algorithm to perform on the Array");
Console.WriteLine("");
Console.WriteLine("Option 1: Heap Sort");
Console.WriteLine("Option 2: Bubble Sort");
Console.WriteLine("Option 3: Shell Sort");
Console.WriteLine("Please type: 1, 2, or 3");
string myOption;
myOption = Console.ReadLine();
int[] arr = new int[10];
int i;
Console.Write("Input 10 elements in the array :\n");
for (i = 0; i < 10; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nElements in array are: ");
for (i = 0; i < 10; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\n");
...
}
}
}
The following code should do the trick:
using System;
namespace ASortAboveTheRest {
internal class Program
{
static void Main(string[] args)
{
MainMenu();
}
static void MainMenu()
{
Console.Clear();
Console.WriteLine("Choose a sort algorithm to perform on the Array");
Console.WriteLine("");
Console.WriteLine("Option 1: Heap Sort");
Console.WriteLine("Option 2: Bubble Sort");
Console.WriteLine("Option 3: Shell Sort");
Console.WriteLine("Please type: 1, 2, or 3");
// Take Option input as an INT
int myOption;
myOption = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[10];
int i;
Console.Write("Input 10 elements in the array :\n");
for (i = 0; i < 10; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nElements in array are: ");
for (i = 0; i < 10; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\n");
//Use switch case after taking array input from the user
switch(myOption) {
case 1:
//Call Heap Sort Function and pass your array
break;
case 2:
// Call Bubble Sort Function and pass your array
break;
case 3:
//Call Shell Sort Function and pass your array
break;
default:
break;
}
}
}
}
You shall:
read the option as a string and then try to parse it, handling the mistake of conversion
also handle cases when a user enters a number that doesn't match your options
rule of thumb: never use 'if' you can use 'switch', never used 'switch' if the mapping can be specified as a dictionary
string optionTyped = Console.ReadLine();
int sortOption = Convert.ToInt32(Console.ReadLine());
var sortFunctions = new Dictionary<int, Func<int[], int[]>>
{
{ 1, HeapSort },
{ 2, BubbleSort },
{ 3, ShellSort }
};
while(!Int32.TryParse(optionTyped, out sortOption)
or (!sortFunctions.Keys.Contains(sortOption)))
{
Console.WriteLine("Not a valid option, try again.");
optionTyped = Console.ReadLine();
}
Func<int[], int[]> sort = sortFunctions[sortOption]
...
int[] resultArray = sort(inputArray)

Why cant I return the index of a value I have searched for in an array?

I have code to ask the user to enter a search value, which should then be compared against every value in the array.
If it is found then it should return every position it is found at in the array.
However when I enter a value which I know should be in the array the code will still say that it isn't.
static void Main(string[] args)
{
string[] low256 = File.ReadAllLines(#"C:\Users\Dingo Ice\Downloads\Low_256.txt");
Console.WriteLine("Choose a file to use, enter 1: Low, 2: High or 3: Mean");
int choice = Convert.ToInt32(Console.ReadLine());
//Sort Arrays into ascending order
Array.Sort(low256);
//Sort Arrays into descending order
Array.Reverse(low256);
if (choice == 1)
{
for (int i = 0; i < low256.Length; i++)
{
if (i % 10 == 0)
{
Console.WriteLine(low256[i]);
}
}
}
else
{
Console.WriteLine("Enter 1, 2 or 3");
}
Console.WriteLine("Enter an exact value to search for in the selected array");
string searchValue = Console.ReadLine();
int found = 0;
int counter = 0;
if (choice == 1)
{
foreach (var value in low256)
{
if (searchValue == value)
{
Console.WriteLine("Search value found at position: " + value);
found = 1;
}
else if (found == 0 && counter >= low256.GetUpperBound(0))
{
Console.WriteLine("The search value was not found in the selected array");
}
counter += 1;
}
}
Console.ReadLine();
}
Can you change this:
if (searchValue == value)
To:
if (searchValue.ToLower().Trim() == value.ToLower().Trim())
And see if this causes a match to be found?
Also, you mentioned that you want every position found to be returned, but in your example this doesn't happen as you only mention via a Console.WriteLine that it is found (but not even on which index). I have changed your log to the Console to include at which index it was found and the index gets added to a list of indexes found.
Does this solve your question?
static void Main(string[] args)
{
string[] low256 = File.ReadAllLines(#"C:\Users\Dingo Ice\Downloads\Low_256.txt");
Console.WriteLine("Choose a file to use, enter 1: Low, 2: High or 3: Mean");
int choice = Convert.ToInt32(Console.ReadLine());
//Sort Arrays into ascending order
Array.Sort(low256);
//Sort Arrays into descending order
Array.Reverse(low256);
if (choice == 1)
{
for (int i = 0; i < low256.Length; i++)
{
if (i % 10 == 0)
{
Console.WriteLine(low256[i]);
}
}
}
else
{
Console.WriteLine("Enter 1, 2 or 3");
}
Console.WriteLine("Enter an exact value to search for in the selected array");
string searchValue = Console.ReadLine();
searchValue = searchValue.ToLower().Trim();
List<int> indexes = new List<int>();
if (choice == 1)
{
for (int i = 0; i < low256.Length; i++)
{
string value = low256[i].ToLower().Trim();
if (searchValue == value)
{
Console.WriteLine($"Search value '{value}' found at position: '{i}'.");
indexes.Add(i);
}
}
if (indexes.Count == 0)
{
Console.WriteLine("The search value was not found in the selected array");
}
}
Console.ReadLine();
}
You could do this with LINQ by projecting your array into a ValueTuple with Select, using Where to filter, then selecting the index.
Note : The string.Compare method just gives you the option on how you want to compare the strings, remove or change the option to taste
var ary1 = new string[] { "asd", "sdf", "sdf", "sgfdfg", "dsf", "asd" };
var someValue = "asd";
var indices = ary1.Select((value, index) => (value, index)) // convert to value index tuple
.Where(pair => string.Compare(pair.value, someValue,CultureInfo.CurrentCulture, CompareOptions.OrdinalIgnoreCase) == 0)
.Select(pair => pair.index);
foreach(var index in indices)
Console.WriteLine(index);
Output
0
5
Maybe you are looking for Array.IndexOf method.
private void button1_Click(object sender, EventArgs e)
{
string[] names = { "Mathew", "Mark", "Luke", "John" };
int index = Array.IndexOf(names, "Luke");
Debug.Print(index.ToString());
//Prints 2
}

Find the index of an object in an array with unknown objects

In a program that asks the name, surname and age of 10 student, at the end we have to enter the name of one of them and the program has to give their place in the array. It always tells me that the place is 0. Here is the code:
public class Program_pinakes
{
public static void Main(string[] args)
{
int i;
string[] a = new string[10];
string[] b = new string[10];
int[] c = new int[10];
int index = 0;
for (i = 0; i < 10; i++)
{
Console.Write("Name: ");
a[i] = Console.ReadLine();
Console.Write("Surname: ");
b[i] = Console.ReadLine();
Console.Write("Age: ");
c[i] = Int32.Parse(Console.ReadLine());
}
Console.Write("Name of student you are looking for: ");
string name = Console.ReadLine();
if (name == a[i])
index = i;
Console.Write("Student"+a[i]+" is located at"+index+" place.");
}
}
Edit: Thank you all for your answers. I found out about the IndexOutOfRange problem and solved it easily. The main problem was about the index returning 0. Now, after I put it on the loop to search the array again, it returns 0 for the first name, 1 for the second and so on. Is this the right way, or it should be returning 1 for the first name, 2 for second etc?
At the end of the loop i has the value of 10. (This is the reason why the cancel condition: i < 10 becomes true and the loop exits).
So when you try to access a[i] after the loop it will throw an IndexOutOfRange exception. Because you have not enough elements in your array.
Solution: To find an element you need to loop again through the array and compare each value with the search-name:
for(int i = 0; i < 10; i++ )
{
if (name == a[i])
{
index = i;
break; // finish the loop, you have found the first occurence
}
}
Console.Write("The first occurence of Student: "+name+" is located at"+index+" place.");
Edit: There can be a case of course that the search-name is not in the list. You can handle such a case by initializing the index value with -1 and check for it after the search loop:
int index = -1;
// search loop here
if (index > -1)
{
Console.Write("The first occurence of Student: " + name + " is located at" + index + " place.");
}
else
{
Console.Write("Student: "+ name +" could not be found!");
}
This code you have posted is not working, as it will throw an exception System.IndexOutOfRangeException.
However apart from this exeption, my answer would be:
becuase,
if (name == a[i])
index = i;
Console.Write("Student"+a[i]+" is located at"+index+" place.");
by doing this you are checking if ith entry of the array is the one you desired or not, if yes then you are setting index with i.
But what about it doesn't match? index will stay with the value as it was initialized with. Here it is zero what you are getting in output.
you should be doing like below.
index = -1;
for(int j = 0; i < a.Length; j++ )
{
if (name == a[j])
{
index = j;
break;
}
}
if(index != -1)
Console.Write("Student" + name + " is located at" + (index + 1) + " place.");
else
Console.Write("Student" + name + " is not in list");
Consider creating a Student object which holds all your student specific data:
class Student
{
public string Name { get; set; }
public string Surename { get; set; }
public int Age { get; set; }
}
Now create a list of Student:
var students = new List<Student>();
And in your loop create a new Student object for every repetition:
for (i = 0; i < 10; i++)
{
Console.Write("Name: ");
var name = Console.ReadLine();
Console.Write("Surname: ");
var surname = Console.ReadLine();
Console.Write("Age: ");
var age = Int32.Parse(Console.ReadLine());
students.Add(new Student {Name = name, Surname = surname, Age = age});
}
After the loop you ask for the name of the student you want the index for and use LINQ to find him in the list:
Console.Write("Name of student you are looking for: ");
string name = Console.ReadLine();
var foundStudent = students.FirstOrDefault(s => s.Name.Equals(name));
if(foundStudent != null)
{
var index = students.IndexOf(foundStudent);
Console.Write($"Student {foundStudent.Name} is located at {index} place.");
}
else
{
Console.WriteLine($"No student for name {name} found");
}
NOTE:
Check out C#'s string interpolation feature for building strings with variables. I find it way more cleaner.
The index always is 0 because your if (name == a[i]) statement is not in a lopp. So you do not actually search in the array but only check if it is like the 11th (array starts to count at 0) element. (i is set to 10 after your for loop)

checking integers in String

I'm checking a string whether it has integers or anything else in function Parse().
Here is my Code
static public int input()
{
Console.WriteLine("Enter The Number Of Student You Want to get Record");
int x;
string inputString = Console.ReadLine();
if (int.TryParse(inputString, out x))
{
Console.WriteLine(inputString + " Is Integer");
return x= Convert.ToInt32(inputString);
}
else
{
input();
}
return x;
}
And full code is:
static void Main(string[] args)
{
int num = 0;
string[] names = new string[] { };
long[] fee = new long[] { };
string[] className = new string[] { };
do
{
Console.WriteLine("Enter Option You Want: \nA:Enter Student Record\nB:Display Student Record\nQ:Exit");
string option =null;
option =Console.ReadLine();
switch (option)
{
case "A":
case "a":
{
num = input();
names = new string[num];
fee = new long[num];
className = new string[num];
for (int i = 0; i < num; i++)
{
Console.WriteLine("Enter Name Of Student:{0}",i);
Console.Write("Enter Student Name: "); names[i] = Console.ReadLine();
Console.Write("Enter Student Fee: "); fee[i] = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Student Class Name: "); className[i] = Console.ReadLine();
}
break;
}
case "B":
case "b":
{
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine("Record of Student: {0}",i);
Console.WriteLine("Name: "+names[i]+ "\nFee: " + fee[i]+ "\nClass Name: " + className[i]);
//Console.WriteLine("Name: {0}\n Class Name: {1}\n Fee: {3}\n",names[i],className[i],fee[i]);
}
break;
}
case "Q":
case "q":
{
Environment.Exit(1);
break;
}
default:
{
Console.WriteLine("Invalid Option");
break;
}
}
} while (true);
}
But The problem is when I enters char instead of int and it works fine and calls itself again but if 2nd time or after 2nd time I input int then does not take input of students and instead it repeats the LOOP again.
So what's the problem, is problem in Input Function????
You could use a regular expression to find the INTs. Also you should call
return input();
instead of
input();
new method:
static public int input(){
Console.WriteLine("Enter The Number Of Student You Want to get Record");
string input = Console.ReadLine();
if (Regex.IsMatch(input, #"\d+"))
{
return int.Parse(Regex.Match(input, #"\d+").Value);
}
else
{
return input();
}
}
I'm assuming you're a student. I started out with C# doing the same stuff. Which I wouldn't do anymore but since you are doing it. I'd recommend using goto, making this method recursive is a no no.
static public int input()
{
Prompt:
Console.WriteLine("Enter The Number Of Student You Want to get Record");
int x;
string inputString = Console.ReadLine();
if (int.TryParse(inputString, out x))
{
Console.WriteLine(inputString + " Is Integer");
return x;
}
else
{
goto Prompt;
}
}

reading two integers in one line using C#

i know how to make a console read two integers but each integer by it self like this
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
if i entered two numbers, i.e (1 2), the value (1 2), cant be parse to integers
what i want is if i entered 1 2 then it will take it as two integers
One option would be to accept a single line of input as a string and then process it.
For example:
//Read line, and split it by whitespace into an array of strings
string[] tokens = Console.ReadLine().Split();
//Parse element 0
int a = int.Parse(tokens[0]);
//Parse element 1
int b = int.Parse(tokens[1]);
One issue with this approach is that it will fail (by throwing an IndexOutOfRangeException/ FormatException) if the user does not enter the text in the expected format. If this is possible, you will have to validate the input.
For example, with regular expressions:
string line = Console.ReadLine();
// If the line consists of a sequence of digits, followed by whitespaces,
// followed by another sequence of digits (doesn't handle overflows)
if(new Regex(#"^\d+\s+\d+$").IsMatch(line))
{
... // Valid: process input
}
else
{
... // Invalid input
}
Alternatively:
Verify that the input splits into exactly 2 strings.
Use int.TryParse to attempt to parse the strings into numbers.
You need something like (no error-checking code)
var ints = Console
.ReadLine()
.Split()
.Select(int.Parse);
This reads a line, splits on whitespace and parses the split strings as integers. Of course in reality you would want to check if the entered strings are in fact valid integers (int.TryParse).
Then you should first store it in a string and then split it using the space as token.
Read the line into a string, split the string, and then parse the elements. A simple version (which needs to have error checking added to it) would be:
string s = Console.ReadLine();
string[] values = s.Split(' ');
int a = int.Parse(values[0]);
int b = int.Parse(values[1]);
string[] values = Console.ReadLine().Split(' ');
int x = int.Parse(values[0]);
int y = int.Parse(values[1]);
in 1 line, thanks to LinQ and regular expression (no type-checking neeeded)
var numbers = from Match number in new Regex(#"\d+").Matches(Console.ReadLine())
select int.Parse(number.Value);
string x;
int m;
int n;
Console.WriteLine("Enter two no's seperated by space: ");
x = Console.ReadLine();
m = Convert.ToInt32(x.Split(' ')[0]);
n = Convert.ToInt32(x.Split(' ')[1]);
Console.WriteLine("" + m + " " + n);
This Should work as per your need!
public static class ConsoleInput
{
public static IEnumerable<int> ReadInts()
{
return SplitInput(Console.ReadLine()).Select(int.Parse);
}
private static IEnumerable<string> SplitInput(string input)
{
return Regex.Split(input, #"\s+")
.Where(x => !string.IsNullOrWhiteSpace(x));
}
}
int a, b;
string line = Console.ReadLine();
string[] numbers= line.Split(' ');
a = int.Parse(numbers[0]);
b = int.Parse(numbers[1]);
Try this:
string numbers= Console.ReadLine();
string[] myNumbers = numbers.Split(' ');
int[] myInts = new int[myNumbers.Length];
for (int i = 0; i<myInts.Length; i++)
{
string myString=myNumbers[i].Trim();
myInts[i] = int.Parse(myString);
}
Hope it helps:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SortInSubSet
{
class Program
{
static int N, K;
static Dictionary<int, int> dicElements = new Dictionary<int, int>();
static void Main(string[] args)
{
while (!ReadNK())
{
Console.WriteLine("***************** PLEASE RETRY*********************");
}
var sortedDict = from entry in dicElements orderby entry.Key/3 , entry.Value ascending select entry.Value;
foreach (int ele in sortedDict)
{
Console.Write(ele.ToString() + " ");
}
Console.ReadKey();
}
static bool ReadNK()
{
dicElements = new Dictionary<int, int>();
Console.WriteLine("Please entere the No. of element 'N' ( Between 2 and 9999) and Subset Size 'K' Separated by space.");
string[] NK = Console.ReadLine().Split();
if (NK.Length != 2)
{
Console.WriteLine("Please enter N and K values correctly.");
return false;
}
if (int.TryParse(NK[0], out N))
{
if (N < 2 || N > 9999)
{
Console.WriteLine("Value of 'N' Should be Between 2 and 9999.");
return false;
}
}
else
{
Console.WriteLine("Invalid number: Value of 'N' Should be greater than 1 and lessthan 10000.");
return false;
}
if (int.TryParse(NK[1], out K))
{
Console.WriteLine("Enter all elements Separated by space.");
string[] kElements = Console.ReadLine().Split();
for (int i = 0; i < kElements.Length; i++)
{
int ele;
if (int.TryParse(kElements[i], out ele))
{
if (ele < -99999 || ele > 99999)
{
Console.WriteLine("Invalid Range( " + kElements[i] + "): Element value should be Between -99999 and 99999.");
return false;
}
dicElements.Add(i, ele);
}
else
{
Console.WriteLine("Invalid number( " + kElements[i] + "): Element value should be Between -99999 and 99999.");
return false;
}
}
}
else
{
Console.WriteLine(" Invalid number ,Value of 'K'.");
return false;
}
return true;
}
}
}
I have a much simpler solution, use a switch statement and write a message for the user in each case, using the Console.write() starting with a ("\n").
Here's an example of filling out an array with a for loop while taking user input. * Note: that you don't need to write a for loop for this to work*
Try this example with an integer array called arrayOfNumbers[] and a temp integer variable. Run this code in a separate console application and Watch how you can take user input on the same line!
int temp=0;
int[] arrayOfNumbers = new int[5];
for (int i = 0; i < arrayOfNumbers.Length; i++)
{
switch (i + 1)
{
case 1:
Console.Write("\nEnter First number: ");
//notice the "\n" at the start of the string
break;
case 2:
Console.Write("\nEnter Second number: ");
break;
case 3:
Console.Write("\nEnter Third number: ");
break;
case 4:
Console.Write("\nEnter Fourth number: ");
break;
case 5:
Console.Write("\nEnter Fifth number: ");
break;
} // end of switch
temp = Int32.Parse(Console.ReadLine()); // convert
arrayOfNumbers[i] = temp; // filling the array
}// end of for loop
The magic trick here is that you're fooling the console application, the secret is that you're taking user input on the same line you're writing your prompt message on. (message=>"Enter First Number: ")
This makes user input look like is being inserted on the same line. I admit it's a bit primitive but it does what you need without having to waste your time with complicated code for a such a simple task.

Categories