Click here to Skip to main content
15,907,328 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Read in a series of numbers from a text-file (the data is given below). For each row in the file, setup a thread to calculate the sum of the values of that vector. Each of the vector sums must then be written to a new file, sorted in ascending order.
Input files contents:
6,12,2,9,17
7,3,15,19,4
10,5,8,21,13


What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.IO;

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"C:Test.txt";
            string line;

            if (File.Exists(filePath))
            {
                StreamReader file = null;
                try
                {
                    file = new StreamReader(filePath);
                    while ((line = file.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            }

            Console.ReadLine();
        }
    }
    
}
Posted
Updated 1-Jun-17 11:13am
Comments
F-ES Sitecore 1-Jun-17 5:57am    
First off we're not here to do your homework for you. Second off just dumping your code and not even explaining what the issue is is not going to get you help anyway,
Samkelo Siyabonga Ngubo 1-Jun-17 5:59am    
Have you read my question and have you checked what I have tried?
F-ES Sitecore 1-Jun-17 6:13am    
That's not your question, that is the homework cut and pasted, as it is phrased as a command or instruction, not a question. It is written a command because it is commanding you to do the work.
Samkelo Siyabonga Ngubo 1-Jun-17 6:18am    
this is from my solution and its gives me output from text file but it doesn't gives me the sum of each line
F-ES Sitecore 1-Jun-17 6:22am    
All your code does is read the file line by line and do nothing with it. You'll need to split each line into its individual numbers and add those numbers up. Further more you'll need to do each line on a new thread then have some mechanism of knowing when all of the threads have finished their work, collecting the results and creating a file with them in order, so you still have a lot of work to do.

This is your homework - so I won't give you the code.
I wouldn't bother with a StreamReader:
C#
string[] lines = File.ReadAllLines(pathToFile);
Will do it fine. You can then use a foreach loop to process each string.
Inside the loop, set up a thread (a BackgroundWorker is probably the easiest way) to process each line.
In the thread use string.Split to break the line on each comma:
C#
string[] parts = line.Split(',');

You can then use int.TryParse on each string to convert it to a number.
You can then work out the vector sum and return it to the main thread.
The main thread assembles all the sums, aggregates them in a collection, sorts the collection, and writes them to the new file.

Do it a bit at a time, and it's all pretty simple stuff!
 
Share this answer
 
Thank I did my homework here is a solution
using System;
using System.IO;

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read the data.txt textfile.
            var data = System.IO.File.ReadAllText(@"..\..\Reading.txt");

            // Create a new List of float[]
            var arrays = new List<float[]> ();

            // Split data file content into lines
            var lines = data.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            // Loop all lines
            foreach (var line in lines)
            {
                // Create a new List<float> representing all the commaseparated numbers in this line
                var lineArray = new List <float> ();

                // Slipt line by , and loop through all the numeric valus
                foreach (var s in line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    // Add converted numeric value to our lineArray 
                    lineArray.Add(Convert.ToInt64(s));
                    //lineArray.Add(Convert.ToSingle(s));
                }
                // Add lineArray to main array
                arrays.Add(lineArray.ToArray());


                // Loop repeats until there are noe more lines

                int i = 0, result = 0;
                while (i < lineArray.Count)
                {
                    result += Convert.ToInt32(lineArray[i++]);
                }

                Console.WriteLine(result);
            }

            //var numberOfRows = lines.Count();
            //var numberOfValues = arrays.Sum(s => s.Length);

            //Console.WriteLine(numberOfRows);
            //Console.WriteLine(numberOfValues);
            Console.ReadLine();

        }
    }
    
}
 
Share this answer
 
Comments
MosuliNodangala 7-Sep-17 4:48am    
Well done Siyabonga, bafo uyayiqhakazisa indlu emnyama.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900