I am new in this fascinating world of programming. I have done this array, but when I type a non integer it crashes. I have tried many ways like int.Parse(console.readLine)), tryparse(text, out int) and ConvertTo32 ,However it continues saying that "Input string was not in correct format." Thanks
using System;
namespace BubbleSort
{
class Program
{
public static void HelpME(int[] a, int t)
{
for (int j = 0; j <= a.Length - 2; j++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
}
static void Main(string[] args)
{
int[] num = { 1, 2, 3, 4, 5 };
int[] a = new int[5];
for (int x = 0; x < 5; x++)
{
Console.WriteLine($"Input enter {num[0 + x]} of five");
a[0 + x] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("The Array is : ");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
{
HelpME(num, 5);
}
Console.WriteLine("The Sorted Array :");
foreach (int aray in a)
{
Console.Write(aray + " ");
}
Console.ReadLine();
}
}
}
you should validate the user unput by using int.TryParse method. If the entered string can be converted to int then only it should be inserted into the array, otherwise the program should ignore that value.
static void Main(string[] args)
{
int[] num = { 1, 2, 3, 4, 5 };
int[] a = new int[5];
for (int x = 0; x < 5; x++)
{
Console.WriteLine($"Input enter {num[0 + x]} of five");
int temp = 0;
string input = Console.ReadLine();
if(int.TryParse(input, out temp))
{
a[0 + x] = Convert.ToInt32(input);
}
}
Console.WriteLine("The Array is : ");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
{
HelpME(num, 5);
}
Console.WriteLine("The Sorted Array :");
foreach (int aray in a)
{
Console.Write(aray + " ");
}
Console.ReadLine();
}
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;
}
}
}
}
}
}
I'm very new to c# and programming in general. I'm having some issues trying to get this program to work.
I've to input 5 names from a text file, add 5 scores to each name, remove the max and min score for each name, and then display the winner. What I have done so far only seems to be adding up all the scores. Could anyone point me in the right direction?
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diving_Championship
{
class Program
{
static void Main(string[] args)
{
string[] divers = File.ReadAllLines(#"F:\1 -C# Programming\Coursework\DiverName.txt");
string DiverScore;
int score = 0;
int max = 0;
int min = 10;
int Totalscore = 0;
int[] Finalscore = new int[5];
int max1 = 0;
for (int j = 0; j < 5; j++)
{
for (int i = 0; i < 5; i++)
{
DiverScore = divers[i];
Console.WriteLine("Please Enter a Score between 0 and 10 for {0}", DiverScore);
score = Convert.ToInt16(Console.ReadLine());
while (score < 0 || score > 10)
{
Console.WriteLine("Score Is Invalid, Please Re-Enter a score between 0 and 10");
score = Convert.ToInt16(Console.ReadLine());
}
if (score > max)
{
max = score;
}
if (score < min)
{
min = score;
}
Totalscore += score;
}
}
for (int i = 0; i < 5; i++)
{
Finalscore[i] = Totalscore - max - min;
if (Finalscore[0] > max1)
{
max1 = Finalscore[0];
}
if (Finalscore[1] > max1)
{
max1 = Finalscore[1];
}
if (Finalscore[2] > max1)
{
max1 = Finalscore[2];
}
if (Finalscore[3] > max1)
{
max1 = Finalscore[3];
}
if (Finalscore[4] > max1)
{
max1 = Finalscore[4];
}
}
Console.WriteLine("the max score is {0}", max1);
}
}
}
Looks like i was completely barking up the wrong tree, i have finally got my code to work, but thanks for all your input. It's not the prettiest code but it works :P
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DivingChampionship_Coursework
{
class Program
{
static void Main(string[] args)
{
string[] divers = File.ReadAllLines(#"F:\1 -C# Programming\Coursework\DiverName.txt");
int score1 = 0;
int score2 = 0;
int score3 = 0;
int score4 = 0;
int score5 = 0;
Diver1(ref divers, ref score1);
Diver2(ref divers, ref score2);
Diver3(ref divers, ref score3);
Diver4(ref divers, ref score4);
Diver5(ref divers, ref score5);
FindWinner(ref divers, ref score1, ref score2, ref score3, ref score4, ref score5);
}
static void Diver1(ref string[] divers, ref int score1)
{
int[] diver1 = new int[5];
int max1 = 0;
int min1 = 10;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Please Enter a score for {0}", divers[0]);
diver1[i] = Convert.ToInt16(Console.ReadLine());
while (diver1[i] < 0 || diver1[i] > 10)
{
Console.WriteLine("Opps, the score you have entered is incorrect, please enter valid score");
diver1[i] = Convert.ToInt16(Console.ReadLine());
}
if (diver1[i] < min1)
{
min1 = diver1[i];
}
if (diver1[i] > max1)
{
max1 = diver1[i];
}
}
score1 = diver1[0] + diver1[1] + diver1[2] + diver1[3] + diver1[4] - max1 - min1;
Console.WriteLine("Total score is {0} from {1}", score1, divers[0]);
Console.WriteLine();
}
static void Diver2(ref string[] divers, ref int score2)
{
int[] diver2 = new int[5];
int max2 = 0;
int min2 = 10;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Please Enter a score for {0}", divers[1]);
diver2[i] = Convert.ToInt16(Console.ReadLine());
while (diver2[i] < 0 || diver2[i] > 10)
{
Console.WriteLine("Opps, the score you have entered is incorrect, please enter valid score");
diver2[i] = Convert.ToInt16(Console.ReadLine());
}
if (diver2[i] < min2)
{
min2 = diver2[i];
}
if (diver2[i] > max2)
{
max2 = diver2[i];
}
}
score2 = diver2[0] + diver2[1] + diver2[2] + diver2[3] + diver2[4] - max2 - min2;
Console.WriteLine("Total score is {0} from {1}", score2, divers[1]);
Console.WriteLine();
}
static void Diver3(ref string[] divers, ref int score3)
{
int[] diver3 = new int[5];
int max3 = 0;
int min3 = 10;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Please Enter a score for {0}", divers[2]);
diver3[i] = Convert.ToInt16(Console.ReadLine());
while (diver3[i] < 0 || diver3[i] > 10)
{
Console.WriteLine("Opps, the score you have entered is incorrect, please enter valid score");
diver3[i] = Convert.ToInt16(Console.ReadLine());
}
if (diver3[i] < min3)
{
min3 = diver3[i];
}
if (diver3[i] > max3)
{
max3 = diver3[i];
}
}
score3 = diver3[0] + diver3[1] + diver3[2] + diver3[3] + diver3[4] - max3 - min3;
Console.WriteLine("Total score is {0} from {1}", score3, divers[2]);
Console.WriteLine();
}
static void Diver4(ref string[] divers, ref int score4)
{
int[] diver4 = new int[5];
int max4 = 0;
int min4 = 10;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Please Enter a score for {0}", divers[3]);
diver4[i] = Convert.ToInt16(Console.ReadLine());
while (diver4[i] < 0 || diver4[i] > 10)
{
Console.WriteLine("Opps, the score you have entered is incorrect, please enter valid score");
diver4[i] = Convert.ToInt16(Console.ReadLine());
}
if (diver4[i] < min4)
{
min4 = diver4[i];
}
if (diver4[i] > max4)
{
max4 = diver4[i];
}
}
score4 = diver4[0] + diver4[1] + diver4[2] + diver4[3] + diver4[4] - max4 - min4;
Console.WriteLine("Total score is {0} from {1}", score4, divers[3]);
Console.WriteLine();
}
static void Diver5(ref string[] divers, ref int score5)
{
int[] diver5 = new int[5];
int max5 = 0;
int min5 = 10;
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Please Enter a score for {0}", divers[4]);
diver5[i] = Convert.ToInt16(Console.ReadLine());
while (diver5[i] < 0 || diver5[i] > 10)
{
Console.WriteLine("Opps, the score you have entered is incorrect, please enter valid score");
diver5[i] = Convert.ToInt16(Console.ReadLine());
}
if (diver5[i] < min5)
{
min5 = diver5[i];
}
if (diver5[i] > max5)
{
max5 = diver5[i];
}
}
score5 = diver5[0] + diver5[1] + diver5[2] + diver5[3] + diver5[4] - max5 - min5;
Console.WriteLine("Total score is {0} from {1}", score5, divers[4]);
Console.WriteLine();
}
static void FindWinner(ref string[] divers, ref int score1, ref int score2, ref int score3, ref int score4, ref int score5)
{
int MaximumScore = 0;
int x = 0;
if (score1 > MaximumScore)
{
MaximumScore = score1;
}
if (score2 > MaximumScore)
{
MaximumScore = score2;
x = 1;
}
if (score3 > MaximumScore)
{
MaximumScore = score3;
x = 2;
}
if (score4 > MaximumScore)
{
MaximumScore = score4;
x = 3;
}
if (score5 > MaximumScore)
{
MaximumScore = score5;
x = 4;
}
Console.WriteLine("Max score is {0} from {1}", MaximumScore, divers[x]);
}
}
}
Appreciate you've already solved your problem, but thought I'd post this short example solution:
Console.WriteLine("The max score is " +
File.ReadAllLines(#"F:\1 -C# Programming\Coursework\DiverName.txt")
.Select(driver =>
{
int score;
do Console.WriteLine("Please Enter a Score between 0 and 10 for " + driver);
while (!int.TryParse(Console.ReadLine(), out score) || score < 0 || score > 10);
return score;
}).Max());
It's not completely clear from the code what do you really aim. At least, the code differs from the task description you provided. Anyway, I'd suggest such a wild guess, for example:
Update - I fixed the code to fit your needs, according to the working code you provided.
using System.IO;
using System.Linq;
namespace Diving_Championship
{
class Program
{
private class Diver
{
internal Diver(string name)
{
Name = name;
}
internal readonly string Name;
internal readonly int[] Scores = new int[5];
internal int TotalScore
{
get { return Scores.OrderBy(sc => sc).Skip(1).Take(3).Sum(); }
}
}
static void Main(string[] args)
{
string[] diverNames = File.ReadAllLines(#"F:\1 -C# Programming\Coursework\DiverName.txt");
List<Diver> divers = new List<Diver>();
for (int j = 0; j < 5; j++)
{
var diver = new Diver(diverNames[j]);
for (int i = 0; i < 5; i++)
{
int score;
do
{
Console.WriteLine("Please Enter a Score between 0 and 10 for {0}", diver.Name);
score = Convert.ToInt32(Console.ReadLine());
} while (score < 0 || score > 10);
diver.Scores[i] = score;
divers.Add(diver);
}
}
var MaxScore = divers.Max(d => d.TotalScore);
var Winner = divers.First(d => d.TotalScore == MaxScore);
Console.WriteLine("The winner is {0} with score {1}", Winner.Name, MaxScore); }
}
}
}
I need to add binary number logic into this snippet. I just cannot wrap my head around how to implement binary numbers, I could just add 0s and 1s but that does not seem to be right
namespace Star_Pyramid
{
class Program
{
static void Main(string[] args)
{
int num;
Console.WriteLine("enter level");
num = Int32.Parse(Console.ReadLine());
int count = 1;
for (int lines = num; lines >= 1; lines--)
{
for (int spaces = lines - 1; spaces >= 1; spaces--)
{
Console.Write(" ");
}
for (int star = 1; star <= count; star++)
{
Console.Write("*");
Console.Write(" ");
}
count++;
Console.WriteLine();
}
Console.ReadLine();
}
}
}
you can use modulo (%)
c = star % 2; // will print first the '1'
c = (star + 1) % 2; // will print first the '0'
int num;
Console.WriteLine("enter level");
num = Int32.Parse(Console.ReadLine());
int count = 1;
int c = 0;
for (int lines = num; lines >= 1; lines--)
{
for (int spaces = lines - 1; spaces >= 1; spaces--)
{
Console.Write(" ");
}
for (int star = 1; star <= count; star++)
{
c = star % 2; //this will return 1 if the value of star is odd then 0 if even
Console.Write(c);
Console.Write(" ");
}
count++;
Console.WriteLine();
}
Console.ReadLine();
VIEW DEMO
When I run this code:
Console.Write("How many numbers do you wish to enter? ");
int n = int.Parse(Console.ReadLine());
int[] arrayOfNumbers = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write("Enter number [{0}]: ", i + 1);
arrayOfNumbers[i] = int.Parse(Console.ReadLine());
}
int minNumber = arrayOfNumbers[0];
int maxNumber = arrayOfNumbers[0];
int sumOfNumbers = 0;
for (int i = 0; i < n; i++)
{
if (arrayOfNumbers[n] < minNumber)
{
minNumber = arrayOfNumbers[n];
}
else if (arrayOfNumbers[n] > maxNumber)
{
maxNumber = arrayOfNumbers[n];
}
sumOfNumbers += arrayOfNumbers[n];
}
double sumDouble = (double)sumOfNumbers;
double average = sumDouble / n;
Console.Write("The min number is : {0}", minNumber);
Console.Write("The max number is : {0}", maxNumber);
Console.Write("The sum of the numbers is : {0}", sumOfNumbers);
Console.Write("The average sum of the numbers is : {0:f2}", average);
It gives me an error : Index was outside of the bounds of the array.
The error is at line 28.
I have a task to find min and max number + sum and average of 'n' numbers.
Inside the for, you must replace the variable 'n' by 'i'.
Like this:
(...)
for (int i = 0; i < n; i++)
{
if (arrayOfNumbers[i] < minNumber)
{
minNumber = arrayOfNumbers[i];
}
else if (arrayOfNumbers[i] > maxNumber)
{
maxNumber = arrayOfNumbers[i];
}
sumOfNumbers += arrayOfNumbers[i];
}
(...)