0

So this is what I would like to do. I am kind of all over the place with this but I hope you can bear with me. This is a very new concept to me.

1) In my program I wish create an array of 50 integers to hold the data that comes from the file. My program must get the path to the user's Documents folder. 2) The name of the file will be "grades.txt". Code this file name right in your program. No user input is required to get the file name. 3) Create a StreamReader object, using this path. This will open the file. Write a loop that reads data from the file, until it discovers the end of the file. 4) As each integer value is read in, I display it, and store it in the array. 5) Using the concepts of partially filled arrays, write a method that takes the array as a parameter and calculates and returns the average value of the integers stored in the array Output the average.

So right now I am having a very hard time figuring out how to get the numbers saved in the grades.txt file, save them to an array, and display them. I try to split the integers and save them as that but it doesn't seem to work.

This is the code that I have so far:

class Program
{
    const int SIZE = 50;

    static void Main()
    {

        // This line of code gets the path to the My Documents Folder

        int zero = 0;
        int counter = 0;
        int n, m;
        StreamReader myFile;
        myFile = new StreamReader("C:/grades.txt");


        string inputNum = myFile.ReadLine();

        do
        {
            Console.Write("The test scores are listed as follows:");
            string[] splitNum = myFile.Split();
            n = int.Parse(splitNum[0]);
            {
                if (n != zero)
                {
                    Console.Write("{0}", n);                      
                    counter++;
                }
            }
        } while (counter < SIZE && inputNum != null);

        // now we can use the full path to get the document




        Console.ReadLine();
    }
}

This is the grades.Txt file:
88
90
78
65
50
83
75
23
60
94

3
  • 3
    What is your specific question? Please see stackoverflow.com/help/how-to-ask Commented Dec 14, 2014 at 7:41
  • I am having an overall conceptual challenge with reading numbers from a Txt file and saving them into an array that displays all the scores from the txt file, then finds the average of said numbers. I wish to know how I might best do this. Commented Dec 14, 2014 at 7:45
  • 1
    You don't even have an array in your code where the data might be saved. This seems like this really might be a question better taken to your teacher, who can focus an answer on the specific topic this assignment is intended to illustrate. It's possible to guess and come up with just the code you would like to have, but it's not clear you'd gain what you need from the assignment that way. Commented Dec 14, 2014 at 7:50

2 Answers 2

1

For reading the file you need something like this:

var scores = new List<int>();
        StreamReader reader = new StreamReader("C:/grades.txt");
        while (!reader.EndOfStream)
        {
            int score;
            if (int.TryParse(reader.ReadLine(), out score) && score != 0)
                scores.Add(score);
        }

and you can have count of scores with scores.Count property.

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

Comments

0

1) In my program I wish create an array of 50 integers to hold the data that comes from the file.

See Arrays Tutorial (C#).

2) My program must get the path to the user's Documents folder. The name of the file will be "grades.txt". Code this file name right in your program. No user input is required to get the file name.

Use these two:

Environment.GetFolderPath Method (Environment.SpecialFolder)

Path.Combine()

3) Create a StreamReader object, using this path. This will open the file. Write a loop that reads data from the file, until it discovers the end of the file.

See StreamReader.EndOfStream().

4) As each integer value is read in, I display it, and store it in the array.

If there is only one score per line, you don't need to do any Split() calls. Use your counter variable to know where in the Array to store the value.

5) Using the concepts of partially filled arrays, write a method that takes the array as a parameter and calculates and returns the average value of the integers stored in the array Output the average.

See Methods (C# Programming Guide).

You'd pass the Array and how many values are stored in it (the counter variable).

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.