Here when I sum the average of four subjects of student1 separately and like that when I proceed with student2 his average is being added with student1. Why is a separate average for each student not calculated? Please help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class stud
{
static void Main (string[] args)
{
double[,] studentavg = new double[3, 4];
double total = 0;
int ch = 0;
int i, j;
while (ch == 0)
{
for (i = 0; i < studentavg.GetLength(0); i++)
{
Console.WriteLine("Enter mark of student : {0}", i + 1);
for (j = 0; j < studentavg.GetLength(1); j++)
{
Console.WriteLine("Enter mark : {0}", j + 1);
studentavg[i, j] = Convert.ToDouble(Console.ReadLine());
total += studentavg[i, j];
}
Console.WriteLine("Average is: {0}", (total / studentavg.GetLength(1)));
Console.Write("Enter 1 for exit OR 0 for continue: ");
ch = Convert.ToInt16(Console.ReadLine());
}
}
Console.ReadLine();
}
}
}
You are never resetting total
back to 0 between students. Try adding
total = 0
after
ch = Convert.ToInt16(Console.ReadLine());
You placed the line "double total = 0;" outside the while loop. Don't you need to initialize it each time to zero?
You should also avoid "Convert.ToInt16" and replace it by "if (!int.TryParse(Console.ReadLine(), out ch)) { error, re-enter number } else all ok" Try to avoid predefined integer sizes. And a conversion from string to a number can always fail.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.