none
please help me in that homework RRS feed

  • Question

  • Hi everybody!!!

    i have some basic skills in c# but i can't do any thing in that homework would you please help me ....\

     

    Write a C-Sharp code to make a “Matrix”. User must be asked to enter the following information: Size of the matrix and values. The program must compute the following operations:

    1. sumMatrix                   (return the sum of all the values of array)
    2. averageMatrix             (return the average of all the values of array)
    3. shiftRowUp                (shifts all of the rows of the matrix upward)
    4. shiftColumnRight       (shift all the columns of the matrix rightward)
    5. Reverse a row/column (Reverse the user specified row/ column)
    6. Mirror row/ column     (Check the user specified row/ column to be a mirror)
    7. Exit the Program
    Use another class “testClass” which makes object of the above Matrix class in the main function to get inputs and show all the outputs on screen

    Eng.Dalia

    Saturday, February 18, 2012 9:37 AM

Answers

  • i will give you key points on how to solve this...
    a matrix is a double dimensional array...
    ex: string[,] Members = new string[2,4];
    the 2 and 4 are the nbr of rows and nbr of columns of the matrix
    so when you want the user to specify the matrix size
    you will have to replace 2 and 4 by x and y variables for the user to input...
    you will have to fill the array content at each specific value and this can be done via nested for loops
    such as : for (int i = 0 ; i< x; i++)
                   {
                       ......
                         for (int j= 0 ; j<y;j++)
                             {
                                  ...............
                             }
                    }

    to sum the matrix you will have to add up all the cells in teh double dimensional array and this is done by:
    declaring an int count = 0;
    inside the nested for loop just add it like that: count += string[x,y];
    to retrieve the average.. you will have to declare another variable inside the nested for loop that increments each time the loop is called such that you will have the nbr of cells in the matrix..
    thus you will have to simply divide the sum you previously got over the nbr of cells...
    this is a start up ... continue yourself.. ;)

    check this link for aditional and more in depth grasp of matrix development ...
    http://www.codeproject.com/Articles/3354/Efficient-Matrix-Programming-in-C


    Peter Koueik


    • Edited by Peter Koueik Saturday, February 18, 2012 9:58 AM
    • Proposed as answer by Cor Ligthert Saturday, February 18, 2012 10:55 AM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Saturday, February 18, 2012 9:52 AM

  • the answer is in the array manipulation so basically we know that x is row y is column
    so within the nested for loop you can add and if statement to define a fixed row
    such that : if(x == 2)//for second row
                   {
                         ......change the row value to 1 or 3 ...or any other row you want...
                       shifting it up would mean [x,y] = [x,y-1]
                   }
    same for columns...but you manipulate x instead of y  // [x,y] = [x+1,y]

    the mirroring means ... when you get a certain row ex.. 2 4 6 4 3
    you should have 2 4 6 4 3 as column  too ... again i cannot give you the answer just try to manipulate the arrays ... you have the basic nested for loops and with if statements and other you will be able to do this. Based on the mirroring question this would lead that the matrix should be a square matrix where the nbr of rows is equal to the nbr of columns...

    Peter Koueik




    • Edited by Peter Koueik Saturday, February 18, 2012 10:19 AM
    • Proposed as answer by Cor Ligthert Saturday, February 18, 2012 10:55 AM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Saturday, February 18, 2012 10:12 AM
  • what you created is a class ...
    so to get the object of that class you should do this:

    matrix x = new matrix();

    and you call it from testclass...
    meaning :

    test class

    {

    matrix x = new matrix(); //this is the matrix object...defined by "x" and ask the user to input the stuff and call the functions that are within the object x... meaning the following..
    x.anyfunction to call it....

    in your case the operations function does that...

    x.operations(.....);

    }


    Peter Koueik




    • Edited by Peter Koueik Wednesday, February 22, 2012 7:17 AM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Wednesday, February 22, 2012 6:45 AM
  • do you still have problems? or is it solved?

    i did notice that you didnt do the mirror question...
    the logic behind it is when the user chooses the row
    you get all the row values and copy each value to the column value...
    if row a = 1 3 4 2 4
    within the for loop you copy each element of row a to col a


    Peter Koueik



    • Edited by Peter Koueik Wednesday, February 22, 2012 9:41 AM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Wednesday, February 22, 2012 8:44 AM

  • in C# unlike VB.NET and Java, you can't access static members with instance syntax. no need for your functions to be static... only the (Main) function should.
    Bottom line remove the keyword (STATIC) from all your function declarations..
     


    Peter Koueik




    • Edited by Peter Koueik Friday, February 24, 2012 6:46 PM
    • Marked as answer by dalia_24 Friday, February 24, 2012 7:28 PM
    Friday, February 24, 2012 4:05 PM
  • Main needs to have a capital 'M' for it to be recognized as the entry point into your program.
    • Marked as answer by dalia_24 Friday, February 24, 2012 7:28 PM
    Friday, February 24, 2012 4:41 PM

All replies

  • i will give you key points on how to solve this...
    a matrix is a double dimensional array...
    ex: string[,] Members = new string[2,4];
    the 2 and 4 are the nbr of rows and nbr of columns of the matrix
    so when you want the user to specify the matrix size
    you will have to replace 2 and 4 by x and y variables for the user to input...
    you will have to fill the array content at each specific value and this can be done via nested for loops
    such as : for (int i = 0 ; i< x; i++)
                   {
                       ......
                         for (int j= 0 ; j<y;j++)
                             {
                                  ...............
                             }
                    }

    to sum the matrix you will have to add up all the cells in teh double dimensional array and this is done by:
    declaring an int count = 0;
    inside the nested for loop just add it like that: count += string[x,y];
    to retrieve the average.. you will have to declare another variable inside the nested for loop that increments each time the loop is called such that you will have the nbr of cells in the matrix..
    thus you will have to simply divide the sum you previously got over the nbr of cells...
    this is a start up ... continue yourself.. ;)

    check this link for aditional and more in depth grasp of matrix development ...
    http://www.codeproject.com/Articles/3354/Efficient-Matrix-Programming-in-C


    Peter Koueik


    • Edited by Peter Koueik Saturday, February 18, 2012 9:58 AM
    • Proposed as answer by Cor Ligthert Saturday, February 18, 2012 10:55 AM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Saturday, February 18, 2012 9:52 AM
  • thank you very much for help but please tell me how to shift rows up and shift cols right

    i think i can reverse row/col by convert A[i,j ] to A[j,i] if it is right how to mirror


    Eng.Dalia

    Saturday, February 18, 2012 9:59 AM

  • the answer is in the array manipulation so basically we know that x is row y is column
    so within the nested for loop you can add and if statement to define a fixed row
    such that : if(x == 2)//for second row
                   {
                         ......change the row value to 1 or 3 ...or any other row you want...
                       shifting it up would mean [x,y] = [x,y-1]
                   }
    same for columns...but you manipulate x instead of y  // [x,y] = [x+1,y]

    the mirroring means ... when you get a certain row ex.. 2 4 6 4 3
    you should have 2 4 6 4 3 as column  too ... again i cannot give you the answer just try to manipulate the arrays ... you have the basic nested for loops and with if statements and other you will be able to do this. Based on the mirroring question this would lead that the matrix should be a square matrix where the nbr of rows is equal to the nbr of columns...

    Peter Koueik




    • Edited by Peter Koueik Saturday, February 18, 2012 10:19 AM
    • Proposed as answer by Cor Ligthert Saturday, February 18, 2012 10:55 AM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Saturday, February 18, 2012 10:12 AM
  • hi , i followed your instructions and write this program

     

    class matrix

        {

            public matrix();

             public static void operation(string[] args)

            {

                Console .Write Line(" Enter number of rows ");

                int rows = Convert.ToInt32(Console .Read Line());

                Console .Write Line(" Enter number of columns ");

                int cols = Convert.ToInt32(Console .ReadLine());

                int[,] A = new int[rows, cols];

                for (int i=0; i< rows ; i++ )

                    for (int j = 0; j < cols; j++)

                    {

                        Console .Write Line(" Enter the matrix elements one by one ");

                        A[i, j] = Convert.ToInt32(Console. Read Line());

                       

                    }

                for (int i = 0; i < rows; i++)

                {

                    for (int j = 0; j < cols; j++)

                    {

                        Console. Write("{0,3}",A[i, j]);

                    }

                    Console .Write Line(" ");

                }

               Console .Write Line (" Sum = " + sum(rows , cols, A));

               Console .Write Line(" ");

               Console .Write Line(" Average = "+ average(rows, cols, A));

               Console .Write Line(" ");

               Shift row(rows, cols, A);

               Console .Write Line("");

               Shift col(rows, cols, A);

               Console .Write Line("");

               reverse(rows, cols, A);

               Console .Write Line(" ");

              

            }

            public static int sum(int row, int col, int[,] arr)

            {

                int x = row;

                int y = col;

                int[,] a = arr;

                int total = 0;

                for (int i = 0; i < x; i++)

                    for (int j = 0; j < y; j++)

                        total += a[i, j];

                return total;

            }

            public static double average(int row, int col, int[,] arr)

            {

                int x = row;

                int y = col;

                int[,] a = arr;

                int count = 0;

                for (int i = 0; i < x; i++)

                    for (int j = 0; j < y; j++)

                    {

                        ++count ;

                    }

                double avg = (double)sum(x, y, a) / count;

                return avg ;

            }

            public static void shift row(int row, int col, int[,] arr)

            {

                int x = row ;

                int y = col ;

                int[,] a = arr ;

                int[,] b = new int[x - 1, y];

                for (int i = 0; i < x - 1; i++)

                {

                    for (int j = 0; j < y; j++)

                    {

                        b[i, j] = a[i + 1, j];

                        Console .Write("{0,3}", b[i, j]);

                    }

                    Console .Write Line();

                }

            }

                 public static void shift col(int row, int col, int[,] arr)

            {

                int x = row;

                int y = col;

                int[,] a = arr;

                int[,] b = new int [x,y-1];

                for (int i = 0; i < x ; i++)

                {

                    for (int j = 0; j < y-1; j++)

                    {

                        b[i, j] = a[i , j+1];

                        Console .Write("{0,3}", b[i, j]);

                    }

                    Console .Write Line();

                }

     

            }

                 public static void reverse(int row, int col, int[,] arr)

                 {

                     int x = row;

                     int y = col;

                     int[,] a = arr;

                     int[,] b = new int[y, x];

                     for (int i = 0; i < y; i++)

                     {

                         for (int j = 0; j < x ; j++)

                         {

                             b[i, j] = a[j,i];

                             Console .Write("{0,3}", b[i, j]);

                         }

                         Console .Write Line();

                     }

                 }

                

        }

    but i still don't know how to make a test class which use a matrix object

    thank you for help


    Eng.Dalia

    Wednesday, February 22, 2012 6:40 AM
  • what you created is a class ...
    so to get the object of that class you should do this:

    matrix x = new matrix();

    and you call it from testclass...
    meaning :

    test class

    {

    matrix x = new matrix(); //this is the matrix object...defined by "x" and ask the user to input the stuff and call the functions that are within the object x... meaning the following..
    x.anyfunction to call it....

    in your case the operations function does that...

    x.operations(.....);

    }


    Peter Koueik




    • Edited by Peter Koueik Wednesday, February 22, 2012 7:17 AM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Wednesday, February 22, 2012 6:45 AM
  • do you still have problems? or is it solved?

    i did notice that you didnt do the mirror question...
    the logic behind it is when the user chooses the row
    you get all the row values and copy each value to the column value...
    if row a = 1 3 4 2 4
    within the for loop you copy each element of row a to col a


    Peter Koueik



    • Edited by Peter Koueik Wednesday, February 22, 2012 9:41 AM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Wednesday, February 22, 2012 8:44 AM
  • hi

    thank you very much your answers are very helpfull

    i will try last part of code and will tell you what i get

    thanx again

    regards


    Eng.Dalia

    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    • Unmarked as answer by dalia_24 Thursday, February 23, 2012 4:19 PM
    Thursday, February 23, 2012 12:13 PM
  • :) plz dont forget to mark as answer if it solved your problem. your welcome...

    Peter Koueik


    • Edited by Peter Koueik Thursday, February 23, 2012 12:23 PM
    • Marked as answer by dalia_24 Thursday, February 23, 2012 4:18 PM
    • Unmarked as answer by dalia_24 Thursday, February 23, 2012 4:18 PM
    Thursday, February 23, 2012 12:23 PM
  • hi

    i tried to write this at the same namespace

    class testmatrix

    {

    public static void main(string[] args

    {

    matrix test = new matrix();

    test.operation();

    }

    }then this message appears        Error 1 Member 'ConsoleApplication10.matrix.operation()' cannot be accessed with an instance reference; qualify it with a type name instead c:\users\المميز\documents\visual studio 2010\Projects\ConsoleApplication10\ConsoleApplication10\Program.cs 133 13 ConsoleApplication10


    Eng.Dalia

    Friday, February 24, 2012 2:18 PM

  • in C# unlike VB.NET and Java, you can't access static members with instance syntax. no need for your functions to be static... only the (Main) function should.
    Bottom line remove the keyword (STATIC) from all your function declarations..
     


    Peter Koueik




    • Edited by Peter Koueik Friday, February 24, 2012 6:46 PM
    • Marked as answer by dalia_24 Friday, February 24, 2012 7:28 PM
    Friday, February 24, 2012 4:05 PM
  • Main needs to have a capital 'M' for it to be recognized as the entry point into your program.
    • Marked as answer by dalia_24 Friday, February 24, 2012 7:28 PM
    Friday, February 24, 2012 4:41 PM
  • thanx to you all

    Eng.Dalia

    Friday, February 24, 2012 7:29 PM