User replace value in string array with a new value - c#

Need some help with user input in array. I've created a string array with 5 elements. User types in 5 names and program prints them.
I want the user to type an index and then a new name. Program should print out all the names again but now with the new name instead of the old one on that index.
Console.WriteLine("Type in 5 names: ");
string[] input = new string[5];
for (int i = 0; i < input.Length; i++)
{
input[i] = Console.ReadLine();
}
Console.WriteLine();
for (int i = 0; i < input.Length; i++)
{
int rank = i + 1;
Console.WriteLine(rank + ". " + input[i]);
}
Here I'm suppose to ask user for index (name) to replace, and a name to replace the old one with. But I just can't figure out the code. (I know how to ask but not the code replacing the names).

void Main()
{
Console.WriteLine("Type in 5 names: ");
string[] input = new string[5];
for (int i = 0; i < input.Length; i++)
{
input[i] = Console.ReadLine();
}
Console.WriteLine();
Print(input);
Console.WriteLine("Choose an index: ");
int index = Int32.Parse(Console.ReadLine());
Console.WriteLine("Type new name: ");
string name = Console.ReadLine();
input[index - 1] = name;
Print(input);
}
void Print(string[] names)
{
for (int i = 0; i < names.Length; i++)
{
int rank = i + 1;
Console.WriteLine(rank + ". " + names[i]);
}
}

Related

Comparison of grades of students and Coins distributions

I'm stuck at this. I`m beginner into software development. You get a list of N students and then a list of ratings for each student. A student which has bigger rating than his neighbour from list gets more coins than both of them. For example:
Data input:
3
John
Dan
Sam
9
10
8
Data output:
John 1
Dan 3
Sam 1
My output is just the names of the students.
I wrote this code:
using System;
class Program
{
private static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
var numberCoins = new int[n];
string[] studentNames = new string[n];
numberCoins[0] = 1;
for (int i = 0; i < n; i++)
{
studentNames[i] = Console.ReadLine();
}
int[] grades = new int[n];
for (int i = 0; i < n; i++)
{
grades[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 1; i < n; i++)
{
if (grades[i] > grades[i - 1])
{
numberCoins[i] = numberCoins[i - 1] + 1;
}
}
for (int i = n - 2; i >= 0; i--)
{
if (grades[i] > grades[i + 1])
{
numberCoins[i] = numberCoins[i + 1] + 1;
}
}
for (int i = 0; i < n; i++)
{
Console.WriteLine(studentNames[i], " ", numberCoins[i]);
}
Console.ReadLine();
}
}
You didn't mention if there is any pattern to give coins to the students. And if we have specific number of coins or not.
If we consider at first no one has any coins the result must be something like the following:
Please specify the number of the students:
3
Please write students' name (after each name hit the ENTER):
John
Dan
Sam
Please write students' ratings (after each rating hit the ENTER):
9
10
8
The result:
John: 0
Dan: 2
Sam: 0
I wrote a piece of code which might help you approach the functionality you want.
using System;
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Please specify the number of the students:");
int n = int.Parse(Console.ReadLine());
string[] studentNames = new string[n];
int[] studentRatings = new int[n];
int[] studentCoins = new int[n];
Console.WriteLine();
Console.WriteLine("Please write students' name (after each name hit the ENTER):");
for (int i = 0; i < n; i++)
{
studentNames[i] = Console.ReadLine() ?? string.Empty;
}
Console.WriteLine();
Console.WriteLine("Please write students' ratings (after each rating hit the ENTER):");
for (int i = 0; i < n; i++)
{
studentRatings[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
Console.WriteLine("The result:");
for (int i = 0; i < n; i++)
{
int coins = 1;
studentCoins[i] = 0;
if (i != 0)
if (studentRatings[i] > studentRatings[i - 1])
coins++;
if (i != n - 1)
if (studentRatings[i] > studentRatings[i + 1])
coins++;
studentCoins[i] = coins;
}
for (int i = 0; i < n; i++)
{
Console.WriteLine($"{studentNames[i]}: {studentCoins[i]}");
}
}
}

How can I close this for loop 2d array in C#?

I am new to programming I am struggling with how to end this loop.
The teacher is not helping to clarify.
The question I need to answer is "Ask the user to enter five days of the week and rainfall data for each day. Store the data in a two dimensional string array named rainfallData[]" Thanks so much!
String[,] rainFallData = new String[5, 2];
String name;
String rainFall;
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Enter the name of day " + i);
name = Console.ReadLine();
rainFallData[i - 1, 0] = name;
Console.WriteLine("Enter rainFall of day " + i);
rainFall = Console.ReadLine();
rainFallData[i - 1, 1] = rainFall;
for (int row = 0; row < 5; row++)
You can use break. When you want to end the loop just write break;.
Here is a solution that "closes the loop" by only asking for the info 5 times. It also shows how to write the data from the 2d array to the console after.
String[,] rainFallData = new String[5, 2];
for (int i = 0; i < rainFallData.GetLength(0); i++)
{
string name;
string rainFall;
Console.WriteLine("Enter the name of day " + i);
name = Console.ReadLine();
Console.WriteLine("Enter rainFall of day " + i);
rainFall = Console.ReadLine();
rainFallData[i, 0] = name;
rainFallData[i, 1] = rainFall;
}
for (int i = 0; i < rainFallData.GetLength(0); i++)
{
Console.WriteLine("Weekday: " + rainFallData[i,0]);
Console.WriteLine("Rainfall: " + rainFallData[i,1]);
}

When I ref bubble sort method and try to print only gives one value?

I am fetching the Grade of the Students. I am trying to fetch Student Grades in sorted order. But when i printed the testgrade variable its just showing only one record at the end.
namespace EX4_GradesMethod
{
class Program
{
static void Main(string[] args)
{
int namelength;
string names;
double grade, max, min, testgradeaverage;
Console.WriteLine("Please enter the amount of names you wish to enter: ");
namelength = Convert.ToInt32(Console.ReadLine());
string[] name = new string[namelength];
double[] testgrades = new double[namelength];
for (int i = 0; i < testgrades.Length; i++)
{
Console.WriteLine("Please enter the names of each student");
names = Convert.ToString(Console.ReadLine());
name[i] += names;
}
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine("Please enter the final grade for " + name[i]);
grade = Convert.ToDouble(Console.ReadLine());
testgrades[i] += grade;
}
max = testgrades.Max();
min = testgrades.Min();
testgradeaverage = testgrades.Average();
Console.WriteLine("The highest grade is: " + max);
Console.WriteLine("The Lowest Grade is:" + min);
Console.WriteLine("The class average is: " + testgradeaverage);
for ( int k = 0; k < namelength - namelength + 1; k++)
{
Console.WriteLine(testgrades[k]);
}
Console.ReadLine();
}
public static void sorted(ref double [] testgrades)
{
double temp = 0;
for (int write = 0; write < testgrades .Length; write++)
{
for (int sort = 0; sort < testgrades.Length - 1; sort++)
{
if (testgrades [sort] > testgrades [sort + 1])
{
temp = testgrades[sort + 1];
testgrades [sort + 1] = testgrades [sort];
testgrades [sort] = temp;
}
}
}
}
}
}

C# Need inputs in different lines

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();
}

How to print my array after a string horizontally?

There is my code:
static void Main(string[] args)
{
int i;
int[] numbers= new int[5];
for (i = 0; i < 5; i++)
{
Console.Write("Insert 5 numbers:");
numeros[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\n");
for (i = 0; i < 5; i++)
{
Array.Sort(numeros);
Console.WriteLine("Ascendant numbers {0}",numbers[i]);
Array.Reverse(numbers);
Console.WriteLine("Descendant numbers {0}", numbers[i]);
}
Console.ReadLine();
}
So at this moment for each loop my app prints my string per item of my array, followed by the number.
What I want is, to print the string only once, followed by the complete array in a single line.
You can (1) Sort and reverse only once (2) Print the string outside the loop (3) Print only the digit inside loop (4) use Console.Write to print digits in the same line. Something like
Array.Sort(numeros);
Console.WriteLine("Numeros ingresados de forma ascendente :");
for (int i = 0; i < 5; i++)
{
Console.Write("{0} ", numeros[i]);
}
Array.Reverse(numeros);
Console.WriteLine(Environment.NewLine + "Numeros ordenados de forma descendente :");
for (int i = 0; i < 5; i++)
{
Console.Write("{0} ", numeros[i]);
}
N.B. I do not understand the language you are printing, so not sure the meaning remains same after code change.

Categories

Resources