0

I have read in a file, sorted the file by into three columns and put all of the data in each columns into 3 arrays. I now need to make a method to get the max value out of the second column array. This is the code:

static void Main(string[] args) 
{ 
    string file1 = System.IO.File.ReadAllText(@"C:\file1.txt");

    List<double> Array1 = new List<double>();
    List<double> Array2 = new List<double>();
    List<double> Array3 = new List<double>();

    IEnumerable<string> lines = File.ReadLines(@"C:\File1.txt");

    foreach (string line in lines)
    {
        string[] columns = line.Split(',');

        if (columns.Length != 3)
        {
            continue; // skips this line
        }

        Array1.Add(Convert.ToDouble(columns[0]));
        Array2.Add(Convert.ToDouble(columns[1]));
        Array3.Add(Convert.ToDouble(columns[2]));
    }

Console.WriteLine(Max(Array1.ToArray()));
}
static double Max (double[] x)
{
    double maxValue = x.Max();
    return maxValue;
}

ERROR: Type or namespace definition, or end-of-file expected.

I need to fix the Max Method and also I do not know how to Say that it is Array 2 that we need to get the max value of. I then need to go back up to the main and Use the Max Method to get the maximum value of array 2. Clearly their are beginners errors in the Max function but I have researched the problem andI am unsure of how to proceed.

1
  • 1
    I feel this code is not yours. please read some tutorial about c# first before stepping into much advanced programming. Nov 29, 2015 at 18:03

2 Answers 2

0
static double Max (double[] x)
{
    double[] x // This line shouldn't be here

    double maxValue = x.Max();
    return maxValue;
}

You're declaring double[] x twice, in the parameter as well as in the method. This will mess things up.

Also, try not to have the same name for your method as the one from the API, just to make code easier to read.

Lastly, you never called the method from main. :P

15
  • Thank you this works. Also in the main I wrote:'Max(Array1) and console.writeline(Max(array1));' what is wrong here? Nov 29, 2015 at 18:02
  • Nice to hear ;D - Could you show the stack trace or tell me what is/isn't happening?
    – Xyexs
    Nov 29, 2015 at 18:06
  • 1
    console.writeline is wrong. coding is case sensitive. do this Console.Writeline(Max(array1)); Nov 29, 2015 at 18:07
  • Console.WriteLine(Max(Array1)); that is
    – Xyexs
    Nov 29, 2015 at 18:09
  • In main if I called the function and inside it put Array1 I thought this would get the max value of the array but it returns an error: Max(Array1); Nov 29, 2015 at 18:10
0
namespace Your_Name_Space
{
    class Your_Program
    {
        public static void Main(string[] args)
        {
            string file1 = System.IO.File.ReadAllText(@"D:\file1.txt");

            List<double> Array1 = new List<double>();
            List<double> Array2 = new List<double>();
            List<double> Array3 = new List<double>();

            IEnumerable<string> lines = File.ReadLines(@"D:\File1.txt");

            foreach (string line in lines)
            {
                string[] columns = line.Split(',');

                if (columns.Length != 3)
                {
                    continue; // skips this line
                }

                Array1.Add(Convert.ToDouble(columns[0]));
                Array2.Add(Convert.ToDouble(columns[1]));
                Array3.Add(Convert.ToDouble(columns[2]));
            }

            Console.WriteLine(Max(Array1.ToArray()));
            Console.ReadKey();
        }

        public static double Max(double[] x)
        {
            double maxValue = x.Max();
            return maxValue;
        }
    }
}

file1.text's content is like below :

1, 2, 3 
4, 5, 6
7, 8, 9

All your codes ran. When the above code is compiled and executed, it produces the following result:

7

'end-of-file expected' error's reason is braces excess. You should control your braces. And value 7 isn't second coloumn.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.