I need to make a method that receives the matrix values as a string as

"[ 1/4 1/3 1/2 ; 2/3 1/5 2/5 ; 1/6 1/5 7/8 ] "

and initializes the matrix ( ex. “[ 1/4 1/3 1/2 ; 2/3 1/5 2/5 ; 1/6 1/5 7/8 ] “)

I don't how to implement the functions parameters to accept this string...
Any help would be appreciated.

If the matrix values are contained inside of a string, then just create a method that receives a string.

private void foo(string value)
{
// parse the value into its parts.
}


// Jerry

Here's the whole program... I'm having problems with that particular function...
SetValues() any help with that would be appreciated...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HomeWork1
{
    class FractionMatrix
    {
        private Fraction [,] arr = new Fraction[3, 3];

        Fraction a11 = new Fraction();
        Fraction a12 = new Fraction();
        Fraction a13 = new Fraction();

        Fraction a21 = new Fraction();
        Fraction a22 = new Fraction();
        Fraction a23 = new Fraction();

        Fraction a31 = new Fraction();
        Fraction a32 = new Fraction();
        Fraction a33 = new Fraction();

        public FractionMatrix()
        {
            Fraction frac = new Fraction();
            frac.SetFraction(0, 1);
        }

        // A method that receives the matrix values as a string
        public void SetValues(string  value)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Fraction [, ]arr = {{a11, a12, a13}, {a21, a22, a23}, {a31, a32, a33}};
                }
            }
        }

        // A 2Dimensional indexer to set and return the value 
        // of any matrix element as a string (ex.A[1,1]=“1/3”)
        public void SetValue(string value)
        { 
            
        }
    }
    

    class Program
    {
        static void Main(string[] args)
        {
            FractionMatrix A = new FractionMatrix ();
            FractionMatrix B = new FractionMatrix ();

            A.SetValues("[ 1/6 1/3 1/2 ; 2/3 1/5 2/5 ; 1/6 1/5 7/8 ] ");
            B.SetValues("[ 1/4 2/3 1/2 ; 4/3 10/5 7/5 ; 4/5 4/3 9/10 ] ");

            //A[1,1].SetValue("4/13");

            //Console.WriteLine("{0} + {1} = {2}\n\n\n", A, B, A + B);
            //Console.WriteLine("{0} * {1} = {2}\n\n\n", A, B, A * B);
            //Console.WriteLine("det(A) = {0} + det(B) = {1}", A.Det(), B.Det());

            Console.Read();
        }
    }
}

This one doesn't work but Is it something like this ??

// A method that receives the matrix values as a string
        public void SetValues(string  value)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    arr[i, j] = Int32.Parse(value);
                }
            }
        }

Where is your Fraction Class ?

Here's the full code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HomeWork1
{
    class Fraction
    {
        private int numerator;
        private int denominator;

        public Fraction()
        {
            numerator = 1;
            denominator = 1;
        }

        public Fraction(int num, int den)
        {
            numerator = num;
            denominator = den;
        }

        public void SetFraction(int num, int den)
        {
            numerator = num;
            denominator = den;
        }

        public int Numerator
        {
            get { return numerator; }
        }

        public int Denominator
        {
            get { return denominator; }
        }

        public static Fraction operator +(Fraction frac1, Fraction frac2)
        {
            Fraction temp = new Fraction();

            temp.numerator = (frac1.numerator * frac1.denominator) + (frac2.numerator * frac1.denominator);
            temp.denominator = frac1.denominator + frac2.denominator;

            return temp;
        }

        public static Fraction operator -(Fraction frac1, Fraction frac2)
        {
            Fraction temp = new Fraction();

            temp.numerator = (frac1.numerator * frac1.denominator) - (frac2.numerator * frac1.denominator);
            temp.denominator = frac1.denominator - frac2.denominator;

            return temp;
        }

        public static Fraction operator *(Fraction frac1, Fraction frac2)
        {
            Fraction temp = new Fraction();

            temp.numerator = frac1.numerator * frac2.numerator;
            temp.denominator = frac1.denominator * frac2.denominator;

            return temp;
        }

        public static Fraction operator /(Fraction frac1, Fraction frac2)
        {
            Fraction temp = new Fraction();

            temp.numerator = frac1.numerator * frac2.denominator;
            temp.denominator = frac1.denominator * frac2.numerator;

            return temp;
        }

        public override string ToString()
        {
            return this.numerator + "/" + this.denominator;
        }
    }
    
    class FractionMatrix
    {
        private Fraction [,] arr = new Fraction[3, 3];

        Fraction a11 = new Fraction();
        Fraction a12 = new Fraction();
        Fraction a13 = new Fraction();

        Fraction a21 = new Fraction();
        Fraction a22 = new Fraction();
        Fraction a23 = new Fraction();

        Fraction a31 = new Fraction();
        Fraction a32 = new Fraction();
        Fraction a33 = new Fraction();

        public FractionMatrix()
        {
            Fraction frac = new Fraction();
            frac.SetFraction(0, 1);
        }

        // A method that receives the matrix values as a string
        public void SetValues(string  value)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    arr[i, j] = Int32.Parse(value);
                }
            }
        }

        // A 2Dimensional indexer to set and return the value 
        // of any matrix element as a string (ex.A[1,1]=“1/3”)
        public void SetValue(string value)
        { 
            
        }

        public override string ToString()
        {
            return "[" + arr + "]";
        } 
    }
    

    class Program
    {
        static void Main(string[] args)
        {
            FractionMatrix A = new FractionMatrix ();
            FractionMatrix B = new FractionMatrix ();

            A.SetValues("[ 1/6 1/3 1/2 ; 2/3 1/5 2/5 ; 1/6 1/5 7/8 ] ");
            B.SetValues("[ 1/4 2/3 1/2 ; 4/3 10/5 7/5 ; 4/5 4/3 9/10 ] ");

            Console.WriteLine(A);

            //A[1,1].SetValue("4/13");

            //Console.WriteLine("{0} + {1} = {2}\n\n\n", A, B, A + B);
            //Console.WriteLine("{0} * {1} = {2}\n\n\n", A, B, A * B);
            //Console.WriteLine("det(A) = {0} + det(B) = {1}", A.Det(), B.Det());

            Console.Read();
        }
    }
}

Ok, to make this work more efficiently, I added another constructor to your Fraction class:
Just allows me to pass it a string. You can move the parsing up into FractionMatrix if you have to.

public Fraction(string value)
        {
            string[] nd = value.Split('/');
            SetFraction(Int32.Parse(nd[0]), Int32.Parse(nd[1]));
        }

Next, I modified your FractionMatrix.SetValues method to this:

public void SetValues(string  value)        
        {
            string[] elements = value.Replace("[", string.Empty).Replace("]", string.Empty).Trim().Split(';');
            string[] fSet;

            for (int i = 0; i < 3; i++)            
            {
                fSet = elements[i].Trim().Split(' ');
                for (int j = 0; j < 3; j++)                
                {
                    arr[i, j] = new Fraction(fSet[j]);
                }            
            }        
        }

Above you will see that I condition the incoming data into a 3 element string array.
This gets split into the 3 individual fraction strings. Finally each of those strings are passed to the Fraction constructor.

And Finally, I modified your FractionMatrix.ToString() override method to this:

public override string ToString()        
        {
            string result = "[";
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    result += " " + arr[i, j].ToString() + " ";
                }
                if(i<2)
                    result += ";";
            }
            result += "]";

            return result;
        }

I can pass in the string, watch the debuger move the data aound, and finally have the ToString() pass the data back out, and it comes out just like it was sent in (round trip confirms the data munging was done correctly).

Have Fun,
Jerry

PS: Don't forget to mark this as solved !

Thank u v. much 4 the help... m8 5/5

I've solved everything but the Determinat functin Det() is not working very well:

Here's the full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace HomeWork_1_2
{

    class Fraction
    {
        /// <summary>
        /// Class attributes/members
        /// </summary>
        private int numerator;
        private int denominator;

        public Fraction()
        {
            numerator = 1;
            denominator = 1;
        }

        public Fraction(int num, int den)
        {
            numerator = num;
            denominator = den;
        }

        public Fraction(string value)
        {
            string[] nd = value.Split('/');
            SetFraction(Int32.Parse(nd[0]), Int32.Parse(nd[1]));
        }

        public void SetFraction(int num, int den)
        {
            numerator = num;
            denominator = den;
        }

        public void SetFraction(string fracString)
        {
            string[] temp = new string[2];
            temp = fracString.Split('/');
            numerator = Int32.Parse(temp[0]);
            denominator = Int32.Parse(temp[1]);
        }

        public int Numerator
        {
            get { return numerator; }
        }

        public int Denominator
        {
            get { return denominator; }
        }

        public static Fraction operator +(Fraction frac1, Fraction frac2)
        {
            Fraction temp = new Fraction();

            temp.numerator = (frac1.numerator * frac2.denominator) + (frac2.numerator * frac1.denominator);
            temp.denominator = frac1.denominator * frac2.denominator;

            return temp;
        }

        public static Fraction operator -(Fraction frac1, Fraction frac2)
        {
            Fraction temp = new Fraction();

            temp.numerator = (frac1.numerator * frac1.denominator) - (frac2.numerator * frac1.denominator);
            temp.denominator = frac1.denominator - frac2.denominator;

            return temp;
        }

        public static Fraction operator *(Fraction frac1, Fraction frac2)
        {
            Fraction temp = new Fraction();

            temp.numerator = frac1.numerator * frac2.numerator;
            temp.denominator = frac1.denominator * frac2.denominator;

            return temp;
        }

        public static Fraction operator /(Fraction frac1, Fraction frac2)
        {
            Fraction temp = new Fraction();

            temp.numerator = frac1.numerator * frac2.denominator;
            temp.denominator = frac1.denominator * frac2.numerator;

            return temp;
        }

        public override string ToString()
        {
            return this.numerator + "/" + this.denominator;
        }
    }

    class FractionMatrix
    {
        /// <summary>
        /// Class attributes/members
        /// </summary>
        Fraction[,] element = new Fraction[3,3];

        public FractionMatrix()
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    element[i, j] = new Fraction(0, 1); 
                }
            }
        }

        public void SetValues(string value)
        {
            string[] elements = value.Replace("[", string.Empty).Replace("]", string.Empty).Trim().Split(';');
            string[] fSet;

            for (int i = 0; i < 3; i++)
            {
                fSet = elements[i].Trim().Split(' ');
                for (int j = 0; j < 3; j++)
                {
                    element[i, j] = new Fraction(fSet[j]);
                }
            }
        }

        /// <summary>
        /// Indexer
        /// </summary>
        public Fraction this[int x, int y]
        {
            get
            {
                if (x >= 0 && x < 3 && y >= 0 && y < 3)
                {
                    return element[x, y];
                }
                else
                {
                    Console.WriteLine("Element is out of bounds");
                    return null;
                }
            }

            set
            {
                if (x >= 0 && x < 3 && y >= 0 && y < 3)
                {
                    element[x, y] = value;
                }
                else
                {
                    Console.WriteLine("Element is out of bounds");
                }
            }
        }

        public override string ToString()
        {
            string result = "";
            for (int i = 0; i < 3; i++)
            {
                result += "[";
                for (int j = 0; j < 3; j++)
                {
                    result += " " + element[i, j].ToString() + " ";

                }
                result += "]\n";
            }

            return result;
        } 

        public static FractionMatrix operator +(FractionMatrix matrix1, FractionMatrix matrix2)
        {
            FractionMatrix result = new FractionMatrix();

            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    result[i, j] = matrix1[i, j] + matrix2[i, j];

            return result;
        }

        public static FractionMatrix operator *(FractionMatrix matrix1, FractionMatrix matrix2)
        {
            FractionMatrix result = new FractionMatrix();

            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    result[i, j] = matrix1[i, j] * matrix2[i, j];

            return result;
        }

        /// <summary>
        /// The function returns the determinent of a Matrix object as Fraction
        /// </summary>
        public Fraction Det()
        {
            FractionMatrix matrix = new FractionMatrix();
            Fraction det = new Fraction();

            det += (matrix[0, 0] * (matrix[1, 1] * matrix[2, 2] - matrix[2, 1] * matrix[1, 2])) -
                   (matrix[0, 1] * (matrix[1, 0] * matrix[2, 2] - matrix[2, 0] * matrix[1, 2])) +
                   (matrix[0, 2] * (matrix[1, 0] * matrix[2, 1] - matrix[2, 0] * matrix[1, 2]));

            return det;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            FractionMatrix A = new FractionMatrix();
            FractionMatrix B = new FractionMatrix();

            A.SetValues("[ 1/6 1/3 1/2 ; 2/3 1/5 2/5 ; 1/6 1/5 7/8 ] ");
            B.SetValues("[ 1/4 2/3 1/2 ; 4/3 10/5 7/5 ; 4/5 4/3 9/10 ] ");

            A[1,1].SetFraction("4/13");

            Console.WriteLine("MATRIX ADDITION:\n\n{0} + \n{1} = \n{2}\n\n", A, B, A + B);
            Console.WriteLine("MATRIX MULTIPLICATION\n\n{0} * \n{1} = \n{2}\n\n\n", A, B, A * B);
            Console.WriteLine("det(A) = {0} + det(B) = {1}", A.Det(), B.Det());

            Console.Read();
        }
    }
}

I've tried to make it this way but it gives me bad resutls:

/// <summary>
        /// The function returns the determinent of a Matrix object as Fraction
        /// </summary>
        public Fraction Det()
        {
            Fraction det = new Fraction();

            det = ((element[0, 0] * (element[1, 1] * element[2, 2] - element[2, 1] * element[1, 2])) -
                    (element[0, 1] * (element[1, 0] * element[2, 2] - element[2, 0] * element[1, 2])) +
                    (element[0, 2] * (element[1, 0] * element[2, 1] - element[2, 0] * element[1, 1])));

            return det;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.