3 Replies - 3762 Views - Last Post: 12 October 2011 - 08:56 AM Rate Topic: -----

#1 nicca90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-October 11

Reading a file into a 2D matrix array

Posted 12 October 2011 - 08:17 AM

Having trouble in reading a file into a matrix array, the question was really confusing to understand. I then need to open it into a textbox. I've tried looking up examples but still can't get my head around it. I'm also confused in how the maxfinder is meant to work.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public static void int Main()
{
	MaxArray MaxFinder = new MaxArray("myfile.txt", 10, 5);
}	
    public class MaxArray
{
	private int[,] m_matrix;
	
	public MaxArray(string arrayFileName, int m, int n)
	{
        m_matrix = new int[m,n];



		//FileStream file = new FileStream(arrayFileName, FileMode.Open, FileAccess.Read);
        //StreamReader reader = new StreamReader(file);
	
                    DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(openFileDialog1.arrayFileName))
                {  
                    string[] data= null;
                    string ReadFromReadLine;
                    ReadFromReadLine = sr.ReadLine();
                    string[,] MaxArray = new string[ReadFromReadLine.Length, data.Length];

                    for (int i = 0; i < m_matrix .GetLength(1); i++)
	                        {
                            for (int y = 0; y < m_matrix .GetLength(0); y++)
                            {
                                Console.Write(m_matrix[m,n]);
                            }
		Console.WriteLine();

                }

		// verify "arrayFileName" is actually a valid file that exists
		// open the file
		// read the contents into m_matrix[][]
		// make sure you are safe not to accidentally go out of bounds on array
	}

	public int MaxFinder(int m, int n)
	{
		int maxValue = 0;

        if(m > maxValue)
        {
            maxValue = m;
        }
        else if (n > maxValue)
        {
            maxValue = n;
        }
        return maxValue;
    

		// loop through all rows & columns of m_matrix
		// if (value > maxValue)
		// store biggest value
		// PRINT OUT the value
	}
}


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Reading a file into a 2D matrix array

#2 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2506
  • View blog
  • Posts: 4,615
  • Joined: 24-September 10

Re: Reading a file into a 2D matrix array

Posted 12 October 2011 - 08:29 AM

Ummm, your question is a little vague.

Note, the word 'matrix' is a very abstract construct. A matrix and an array are kind of interchangeable words depending context. So really I'm not sure what you mean by a "matrix array"...

You also reference a 'question'. Is this a question in your class? What is that question?

Where did this code come from? Did you write it? What is it attempting to do?



Give us more details to work with. We could investigate your code and piece together what you're asking by making some assumptions, but we shouldn't have to do that. Ask your question more directly please.
Was This Post Helpful? 0
  • +
  • -

#3 nicca90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-October 11

Re: Reading a file into a 2D matrix array

Posted 12 October 2011 - 08:35 AM

View Postlordofduct, on 12 October 2011 - 08:29 AM, said:

Ummm, your question is a little vague.

Note, the word 'matrix' is a very abstract construct. A matrix and an array are kind of interchangeable words depending context. So really I'm not sure what you mean by a "matrix array"...

You also reference a 'question'. Is this a question in your class? What is that question?

Where did this code come from? Did you write it? What is it attempting to do?



Give us more details to work with. We could investigate your code and piece together what you're asking by making some assumptions, but we shouldn't have to do that. Ask your question more directly please.


Sorry I should be more specific, its 3 oclock in the morning, can't think so tired lol.

This is the question which I found way too confusing. I started using a template I made to guide myself but its not making much sense lol.

(a) Write a class called MaxArray that has a public method called MaxFinder which finds and displays the maximum value in a two dimensional array of integers called matrix. The array is read from a file whose name, as a parameter, is introduced when an object of this class is to be instantiated. Also the size of each dimension is given as a parameter at the time of instantiation of the object. For consistency purposes, you can assume that m shows the number of rows and n shows the number of columns. After writing C# code fragments for creating this class, instantiate an object of this class and call its MaxFinder method. A TextBox on your form will show this value.

This post has been edited by nicca90: 12 October 2011 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 ChrisKellyDev  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 05-October 11

Re: Reading a file into a 2D matrix array

Posted 12 October 2011 - 08:56 AM

So, just for a little clarification...

You have a file (I assume a text file) that you need to read into your program using a 2-dimensional array (matrix).

Do you have an example text file to work with that we could see (or just some formatting rules)? Because you could have a file like this:

1,2,3,4,5
6,7,4,33,2
9,12,18,15,6

(obviously goes into a 5x3 array)

or a file like this (delimiter is subject to change):

1,2,3,4,5,6,7,12,55,9001

(and be told that it has to go into a 5x2 array where you would pass 5 and 2 to m and n).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1