I am new to programming and I would be delighted if someone could help me with the following question: "Write a program that ask for the class and number of students. after asking for the name of student, ask for the grades by the Name of student, show average and show highest grade". I am stuck at part 3, and can't the link the names to next part. This is what I got so far:
static void Main(string[] args)
{
// 1 give a name and number of students
Console.Write("Class = ");
Convert.ToString(Console.ReadLine());
Console.Write("Number of students = ");
int aantalStudenten = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
// 2 ask for the names
int[] aantal = new int[aantalStudenten];
Random RandomNumber = new Random();
for (int i = 0; i < aantalStudenten; i++)
{
Console.Write("Geef naam van de {0}e student : ", i + 1);
string studentNaam = Convert.ToString(Console.ReadLine());
}
Console.WriteLine();
// 3 give the grade of each student by the name
for (int j = 0; j < aantalStudenten; j++)
{
Console.Write("Cijfer van {0} : "); // here i need the students name)//
int cijfers = Convert.ToInt32(Console.ReadLine());
}
//class avarege here
//highest grade of the class
int hoogste = stuCijfer.Max();
Console.WriteLine("De hoogste cijfer is {}," hoogste;
//name and grade of each student
Console.WriteLine();
Console.WriteLine("Press any key to stop");
Console.ReadKey();
}
Console.Write("Class = ");
Convert.ToString(Console.ReadLine());
Console.Write("Number of students = ");
int aantalStudenten = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
// 2 ask for the names
string[] namen = new string[aantalStudenten];
int[] cijfers = new int[aantalStudenten];
Random RandomNumber = new Random();
for (int i = 0; i < aantalStudenten; i++){
Console.Write("Geef naam van de {0}e student : ", i + 1);
namen[i] = Convert.ToString(Console.ReadLine());
}
Console.WriteLine();
// 3 give the grade of each student by the name
for (int i = 0; i < aantalStudenten; i++){
Console.Write("Cijfer van {0} : ", namen[i]); // here i need the students name)//
cijfers[i] = Convert.ToInt32(Console.ReadLine());
}
double gemiddeld = 0;
//class avarege here
for (int i = 0; i < cijfers.Count(); i++) {
gemiddeld += cijfers[i];
}
gemiddeld = gemiddeld / cijfers.Count();
Console.WriteLine("Het gemiddelde van de klas is: {0}", gemiddeld);
//highest grade of the class
int hoogste = 0;
for (int i = 0; i < cijfers.Count(); i++) {
if (cijfers[i] > hoogste) {
hoogste = cijfers[i];
}
}
Console.WriteLine("De hoogste cijfer is {0}",hoogste);
//name and grade of each student
Console.WriteLine();
Console.WriteLine("Press any key to stop");
Console.ReadKey();
You should put the names of the students in a list the way you are working now you are overwriting each time the user enters a new name. and then u can loop over the names storing the score in an array in this case u can use two separate one's but it would be better to use a dictionary or a 2D array.
but good luck further
It looks like your trying to get a string where you're picking out the name but it might have been converted to an int. Change the part where you converted it to a int when you received the name.
It also looks like you have for int j. Before you named it int i. Make sure it references back to the exact same name for what you identified each time.
on a side note you could try to store the names into an array and pull them out be referencing the array. That might help.
If I were to code such a program properly I would create a new Student class to contain a name and a score, then create a list or array to contain multiple instances of Student. Hopefully you're familiar with the concepts of creating new classes, and with lists or arrays. If you need help understanding any of those, let me know.
Related
At last line i am trying to get the names of the childrens
Console.WriteLine("How many children do you have?");
int X = Convert.ToInt32(Console.ReadLine());
List<string> nameOfChild = new List<string>();
int i = 1;
while(i <= X)
{
Console.WriteLine("What is the name of your {0} child?", i);
nameOfChild.Add(Convert.ToString(Console.ReadLine()));
i++;
**Console.WriteLine("The name of your i child is {0} and was born in the year of {1}.", nameOfChild[i]);**
}
Indexed collections and arrays start at index 0, not 1. Also, you are incrementing the i variable before printing the name.
Try this:
Console.WriteLine("How many children do you have?");
int X = Convert.ToInt32(Console.ReadLine());
List<string> nameOfChild = new List<string>();
int i = 0;
while(i < X)
{
Console.WriteLine("What is the name of your {0} child?", i + 1);
nameOfChild.Add(Console.ReadLine());
Console.WriteLine("The name of your {0} child is {1}.", i + 1, nameOfChild[i]);
i++;
}
You do not need a while loop for something already determined. Same goes for using a List instead of an array. If your teacher insisted on using a List than by all means use it.
For future reference, use variable names that reflect their purpose in the program. Try childList instead of nameOfChild where the name of the child would be an element of the list.
Console.WriteLine("How many children do you have?");
int X = Convert.ToInt32(Console.ReadLine());
string[] nameOfChild = new string[X];
for (int i = 1; i <= X; i++)
{
Console.WriteLine("What is the name of your {0} child?", i);
nameOfChild[i - 1] = Console.ReadLine();
Console.WriteLine("The name of your {0} child is {1} and was born in the year of (insert year).",i, nameOfChild[i - 1]);
}
Also your string formatting is missing a year, so I added that in case you forget.
I'm a relatively new student to C# programming and I'm having a lot of trouble with 2-D arrays. In this program the user is supposed to input names, verbs, and objects for an insult generator, but the problem is that I have to pointlessly store them all in one 2-D string array and generate the insults in a 1-D string array and I'm just completely lost. Can I store name, verb, and object into a single 2-D string array? Any help would be appreciated.
My issue is with initializing and storing the 2D string array and then converting to the 1D array.
private static void Generate(int insultnum, int sentnum)
{
int Ncounter = 1;
int Vcounter = 1;
int Ocounter = 1;
string[,] name = new string[sentnum,?];
string[,] verb = new string[sentnum,?];
string[,] insult = new string[sentnum,?];
do
{
Console.Write("Please enter name of #{0}: ", Ncounter);
//length and height 2D array for loop text is just a placeholder from an earlier project
for (int i = 0; i < length; i++)
{
for (int j = 0; j < height; j++)
{
name[Ncounter - 1, ??] = Console.ReadLine();
}
}
//
Ncounter++;
} while (Ncounter < sentnum);
do
{
Console.Write("Please enter name of #{0}: ", Vcounter);
verb[Vcounter-1, ?] = Console.ReadLine();
//2D array loop text
Vcounter++;
} while (Vcounter < sentnum);
do
{
Console.Write("Please enter name of #{0}: ", Ocounter);
insult[Ocounter-1, ?] = Console.ReadLine();
//2D array loop text
Ocounter++;
} while (Ocounter < sentnum);
Ncounter = 0;
Vcounter = 0;
Ocounter = 0;
string[] insults = new string[insultnum];
//insults = name[?,?] + " " + verb[?,?] + " " + object[?,?];
}
Example Output:
Enter the number of insults to generate: 3
Enter the number of names, verbs, and objects: 3
Please enter name #1: Bob
Please enter name #2: Rhys
Please enter name #3: Kaa
Please enter verb #1: licks
Please enter verb #2: touches
Please enter verb #3: tastes
Please enter object #1: dirt
Please enter object #2: cars
Please enter object #3: asphalt
Insults have been generated
Kaa licks dirt
Bob tastes asphalt
Bob licks cars
2D array i.e. string[,] is a bit strange collection to store the data, since it requires that
NameCounter == VerbCounter == ObjectCounter
More natural choice is a jagged array string[][] which allows arbitrary NameCounter, VerbCounter, ObjectCounter. And, please, decompose your solution, split it to number of easy to implement and test methods:
// Random generator for insulting
private Random rgen = new Random();
// 1st index - category (name/verb/insult), 2nd index item within category
private static string[,] data;
private static string[] categories = new string[] {
"name", "verb", "insult",
};
// Input single categore, e.g. Names or Verbs
private static void InputCategory(int category) {
for (int i = 0; i < data.GetLength(1); ++i) {
Console.Write($"Please enter {categories[category]} #{i + 1}: ");
data[category, i] = Console.ReadLine();
}
}
// Input all categories
private static void InputData() {
Console.Write($"Enter the number of names, verbs, and objects: ");
// simplest; more accurate implementation uses int.TryParse()
int n = int.Parse(Console.ReadLine());
data = new string[categories.Length, n];
foreach(var int i = 0; i < categories.Length; ++i)
InputCategory();
}
To write an insult generator you should combine random items from each category
private static string[] Insults(int count) {
string[] result = new string[count];
// take count times a item from each category
for (int i = 0; i < count; ++i) {
StringBuilder sb = new StringBuilder
for (int c = 0; c < categories.Length; ++c) {
if (c > 0)
sb.Append(' ');
sb.Append(data[c, rgen.Next(data.GetLength(1))]);
}
result[i] = sb.ToString();
}
return ressult;
}
Having these methods you can put easily
private static void Main() {
int numberOfInsults = ....
InputData();
foreach (var insult in Insults(numberOfInsults))
Console.Write(insult);
...
}
I have 3 methods to 1. Take in strings as an array [zipCodes], 2. Output a menu for the user and 3. display the string array back to the user. The first 2 options are working and after testing I can say that the array is working and taking in strings, however I am having bother displaying them back to the user.
I have used this method with ints, it makes me think that the [i] is only for 1 character, an explanation would be greatly appreciated.
// Here is the code so far
static void Main(string[] args)
{
string[] zipCodes = new string[10];
string zCounter;
for (int i = 0; i < zipCodes.Length; i++)
{
Console.WriteLine("Please enter 10 destinations:");
zCounter = Convert.ToString(Console.ReadLine());
zCounter = zipCodes[i];
}
int sentinalNo;
Console.Clear();
Console.WriteLine("Please enter from the following options: ");
Console.WriteLine("1. Display order zipcodes.");
Console.WriteLine("2. Search zipcode.");
Console.WriteLine("3. Exit.");
sentinalNo = Convert.ToInt32(Console.ReadLine());
while (sentinalNo != 3)
{
switch (sentinalNo)
{
case 1:
DisplayZips(zipCodes);
break;
}
}
}
private static void DisplayZips(string[] zipCodes)
{
for (int i = 0; i < zipCodes.Length; i++)
{
// Why doesnt this work?
Console.WriteLine(zipCodes[i]);
}
You should assign input into the array items:
// array of 10 strings each of them is null
string[] zipCodes = new string[10];
...
for (int i = 0; i < zipCodes.Length; i++)
{
Console.WriteLine("Please enter 10 destinations:");
// Convert.ToString is redundant here
zCounter = Convert.ToString(Console.ReadLine());
// swapped: user input is assigned to array items
zipCodes[i] = zCounter;
}
I want to enter students' Id and Marks into an array. Problem statement is that if user enters the Id of student then Mark of that student should be displayed. Below is my code so far. Could you please help me?
int [] mark = new int [5] ;
string [] studentsid = new string [5];
string userInput = "";
bool found = false;
int i = 0;
string[] answer = new string[5];
for (i = 0; i < answer.Length; i++)
{
Console.WriteLine("Enter Student " + (i + 1) + " 's ID Number: ");
studentsid[i] = Console.ReadLine();
Console.WriteLine("Enter student" + (i + 1) + "'s mark: ");
mark[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter one of you student's id number");
userInput = Console.ReadLine();
if (studentsid[i].ToUpper() == userInput.ToUpper())
{
found = true;
Console.WriteLine(mark[i]);
}
if (mark[i] >=85 && mark[i] <= 100 )
{
Console.WriteLine("Distinction");
}
Console.ReadKey();
You need to put a second loop around the block of code that checks if the student's ID matches.
Right now, you are only checking if one student (the last student in the array) matches the user's input. You are also using a for-loop's control variable outside of the loop, which is generally considered bad practice.
Consider something like this:
Console.WriteLine("Enter one of you student's id number");
userInput = Console.ReadLine();
for (int i = 0; i < studentsid.length; i++)
{
if (studentsid[i].ToUpper() == userInput.ToUpper())
{
found = true;
Console.WriteLine(mark[i]);
}
}
Also, your "answers" array serves no purpose. You only create it to check its hard-coded length of 5. Use the length of studentsid instead.
Finally, two arrays aren't really the ideal way to store this type of data.
A map, using the student ID as the key and the mark as the value, would be a much more efficient way to store and access this data.
I'm trying to fill 3 arrays with user input, it should look like this:
Enter account number 1: 29384
Enter the account balance: 1111
Enter the account holder last name: lastname
Enter account number 2: 34938
Enter the account balance: 2222
Enter the account holder last name: lastname2
Enter account number 3: 46372
Enter the account balance: 3333
Enter the account holder last name: lastname3
and so on...
I have my program set up with for loops to fill a 5 line array but it asks for all five account numbers first then goes onto the balance and so on
using System;
public class Array1
{
public static void Main()
{
int[] scores = new int[5];
int x;
string inputString;
int[] balance = new int[5];
int y;
for(x=0; x < scores.Length; ++x)
{
Console.Write("Enter account number {0} ", x + 1);
inputString = Console.ReadLine();
scores[x] = Convert.ToInt32(inputString);
}
for(y=0; y < balance.Length; ++y)
{
Console.Write("Enter the account balance ");
inputString = Console.ReadLine();
}
}
}
I'm sure there is a much better way to write this. Any help would be appreciated.
Yes.
Why not have a class with a structure
class DataHolder
{
public String account_number;
public int balance;
public String lastname;
}
Then you're at least holding relevant data together
Your code then does as you've instructed, its asked to get all the accounts, then all the balances..
Where as 1 loop would have been sufficient
If you had an array of DataHolder class
DataHolder[] testData = new DataHolder[5];
For (int x=0; < x testData.length; x++)
{
Console.Write("Account Number");
testData[x].account_number= Console.ReadLine();
Console.Write("Balance");
testData[x].balance=Convert.ToInt32(Console.ReadLine());
Console.Write("LastName");
testData[x].lastname=Console.ReadLine();
}
I think you just want one for loop.
for(x=0; x < scores.Length; ++x)
{
Console.Write("Enter account number {0} ", x + 1);
inputString = Console.ReadLine();
scores[x] = Convert.ToInt32(inputString);
Console.Write("Enter the account balance ");
inputString = Console.ReadLine();
// and so on
}