0

I don't know how to calculate the average. I am getting the output I am expecting, except for the average in which I want to enter in a column beside all my other info. Thanks so much to whoever can look at it for me.

using System;
using System.IO;
public static class Program
{

static void Main()
{
    string file="marks2D.txt";
     string outfile="average.csv";

    StreamReader sr= new StreamReader(file);
    StreamWriter outstream= new StreamWriter(outfile);

    double[,] temp=new double[5,6];
    int num=0;
    double ave=0;


for(int i=0; i<temp.GetLength(0); i++)
    {
        double sum=0;
        string line=sr.ReadLine();

        for(int j=0; j<temp.GetLength(1); j++)
        {


          double m=double.Parse(line);
          temp[i,j]=m;
          sum+=m;
          ave = sum/5;

            //temp[i,j]=line;
            outstream.WriteLine(ave);
            outstream.WriteLine("{0,1}", temp[i,j]);
        }


    }
    outstream.WriteLine();
    Console.WriteLine();


    sr.Close();
    outstream.Close();


}
}

2 Answers 2

1

You have declared temp as 2-dimensional array.

string[,] temp=new string[5,6];

You then try to set a value of the 2D array using one index

temp[num]=line;

This is not possible. For a 2D array, you must use 2 indexes

temp[i, j]=line;

You are getting the "the name field does not exist in the current context" error because you have commented out the field variable, so it does not exist.

Sign up to request clarification or add additional context in comments.

1 Comment

In my outstream file, how do I declare commas or spaces in between numbers? Thanks so much.
0

You have declare a two-dimensional string array however you are referencing it as single dimensional array inside loop.

Change the line

   temp[num]=line;

to this:

   temp[i,j]=line;

Also, you forgot to comment the second outstream.WriteLine, I believe as the variable field is commented out.

//outstream.WriteLine(field[num]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.