-
Arrays in C#
I am new to the site and also to programming. I am having problems with an assignment due on tomorrow and was hoping to get advice on what I may be doing wrong or missing. The assignment and code is included below:
1. A two-dimensional array for storing the three quiz grades for a class of 5 students; this array must be initialized to the following:
{ { 87, 68, 94 }, { 100, 83, 78 }, { 85, 91, 76 }, { 65, 81, 66 }, { 65, 71, 56 } }
2. A one-dimensional array for storing the average quiz grade for each student.
Your application should perform the following tasks:
1. Calculate and store the average grade for each student.
2. Print to the console a nicely formatted two-dimensional table with headings listing the student number (0 to 4), the grade for each quiz (Quiz 1, ... Quiz 3), and the average.
3. Bonus: For 2 bonus points, also provide the data structures and processing for storing last names for the students and including these last names in the two-dimensional table printed out on the console
CODE:
//Two-Dimensional Arrays.cs
//Student Quiz Grades
using System;
class StudentQuizGrades
{
static void Main(string[] args)
{
//array of student grades
decimal[,] Quizzes = { { 87, 68, 94 }, { 100, 83, 78 }, { 85, 91, 76 }, { 65, 81, 66 }, { 65, 71, 56 } };
Quizzes[4, 2];
//average quiz grade array
decimal[,] QuizAverage = {{}, {}, {}, {}, {}};
//student name array
string[] Name = new string {{James}, {Johnson}, {Smith}, {Michaels}, {Roberts}};
count NumQuiz = 3;
Decimal total = 0;
for (student = 0; student < QuizLength; student++)
{
for (quizCount = 0; quizCount < NumQuiz; quizCount++)
total = total + Quiz[student.quizCount];
Average[student] = total / NumQuiz;
}
}
}
-
Re: Arrays in C#
Declaring a two-dimensional array has the following form:
Code:
type arr-name[NUM_ROWS][NUM_COLS];
where type is the data type for the array, arr-name is the variable name for the array, NUM_ROWS is the maximum number of rows for the array, and NUM_COLS is the maximum number of columns for the array. To declare an integer array of five rows and three columns, the following code could be used:
More information can be found here.
-
Re: Arrays in C#
hello friends:thumbup1:
i am new to this forum. i am jim smith and i am web designer,
i want to know,
does anyone know how to define 3 dimensional array in c . also how to fetch the value from array.
-
Re: Arrays in C#
here is the solution
using System;
class Program
{
static void Main(string[] args)
{
int[,] quiz = new int[,] { { 87, 68, 94 }, { 100, 83, 78 }, { 85, 91, 76 }, { 65, 81, 66 }, { 65, 71, 56 } };
float[] avg = new float[5];
string[] names = { " James", "Johnson", "Smith", "Michaels", "Roberts" };
for (int x = 0; x < 5; x++)
{
int total = 0;
Console.WriteLine("Quiz Marks for : " + names[x]);
Console.WriteLine("Quiz1\tQuiz2\tQuiz3");
for (int y = 0; y < 3; y++)
{
total += quiz[x,y];
Console.Write(quiz[x,y] + "\t");
}
avg[x] = total/3;
Console.WriteLine();
}
int p = 0;
foreach (int t in avg)
{
Console.WriteLine("Average for " + names[p] + " : " + t);
p++;
}
Console.Read();
}
}
Page generated in 1,750,843,636.08752 seconds with 10 queries