Matrices!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Matrices!

  1. #1
    Join Date
    Jun 2012
    Posts
    1

    Matrices!

    Hello everyone.

    I'm doing some practice problems, and the question requires three 4x4 matrices. The first matrix is entered by the user, The second is a Transpose of the first, and the third is both the first and second matrices added together. I have already built the program. However, whenever I run it and enter
    the elements for the first matrix. The display would show that the first 4 values are always 0. The following 12 values are perfectly fine, but for some
    reason the first 4 is set to 0. My transpose matrix does not seem to be working, but I'm sure if i can figure out the problem with the first matrix
    I will be fine.

    This is my output display:

    Enter the values for the 4 x 4 matrix!
    --------------------------------------

    Please enter the element found in row [1], column [1] : 1
    Please enter the element found in row [1], column [2] : 1
    Please enter the element found in row [1], column [3] : 1
    Please enter the element found in row [1], column [4] : 1
    Please enter the element found in row [2], column [1] : 1
    Please enter the element found in row [2], column [2] : 1
    Please enter the element found in row [2], column [3] : 1
    Please enter the element found in row [2], column [4] : 1
    Please enter the element found in row [3], column [1] : 1
    Please enter the element found in row [3], column [2] : 1
    Please enter the element found in row [3], column [3] : 1
    Please enter the element found in row [3], column [4] : 1
    Please enter the element found in row [4], column [1] : 1
    Please enter the element found in row [4], column [2] : 1
    Please enter the element found in row [4], column [3] : 1
    Please enter the element found in row [4], column [4] : 1


    -------------
    First Matrix!
    -------------

    0 0 0 0
    1 1 1 1
    1 1 1 1
    1 1 1 1


    -------------
    Second Matrix!
    -------------

    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0


    -------------
    Third Matrix!
    -------------

    0 0 0 0
    1 1 1 1
    1 1 1 1
    1 1 1 1




    Here is my Code:

    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #define row 4
    #define column 4

    using namespace std;

    void getArray1(float [][column]); // getArray1 function prototype
    void Transpose(float [][column], float [][column]); // Transpose function prototype
    void addArrays(float [][column], float [][column], float [][column]); // addArrays function prototype
    void displayArrays(float [][column], float [][column], float [][column]); // addArrays function prototype

    int main()
    {
    static float Yuri[row][column]; // First array
    static float Yoona[row][column]; // Second array
    static float Sunny[row][column]; // Third array

    getArray1(Yuri);
    Transpose(Yuri,Yoona);
    addArrays(Yuri,Yoona,Sunny);
    displayArrays(Yuri,Yoona,Sunny);

    cin.get();
    cin.get();
    return 0;
    }

    //////////////////////////////////////////////////////////////////////

    void getArray1(float Yuri[][column]) // used to get array from user
    {
    int i; // counter
    int x; // counter

    cout << "Enter the values for the 4 x 4 matrix!" << endl;
    cout << "--------------------------------------" << endl;
    cout << endl;

    for(i = 0; i < row; i++)
    {
    for(x = 0; x < column; x++)
    {
    cout << "Please enter the element found in row [" << i+1 << "], column [" << x+1 << "] : ";
    cin >> Yuri[i][x];
    }
    }
    }

    //////////////////////////////////////////////////////////////////////

    void Transpose(float Yuri[][column], float Yoona[][column]) // used to transpose the first matrix
    {
    int i; // counter
    int x; // counter

    for(i = 0; i < row; i = column)
    {
    for(x = 0; x < column; x++)
    {
    Yuri[i][x] = Yoona[x][i];
    }
    }
    }

    //////////////////////////////////////////////////////////////////////

    void addArrays(float Yuri[][column], float Yoona[][column], float Sunny[][column]) // used to add the two vectors
    {
    int i; // counter
    int x; // counter

    for(i = 0; i < row; i++)
    {
    for(x = 0; x < column; x++)
    {
    Sunny[i][x] = Yuri[i][x] + Yoona[i][x];
    }
    }
    }

    //////////////////////////////////////////////////////////////////////

    void displayArrays(float Yuri[][column], float Yoona[][column], float Sunny[][column]) // used to display Matrices
    {
    cout << endl << endl;
    cout << "-------------" << endl;
    cout << "First Matrix!" << endl;
    cout << "-------------" << endl;
    cout << endl;

    int i; // counter
    int x; // counter

    for(i = 0; i < row; i++)
    {
    for(x = 0; x < column; x++)
    {
    cout << setw(5) << Yuri[i][x];
    }

    cout << endl;
    }

    cout << endl << endl;
    cout << "-------------" << endl;
    cout << "Second Matrix!" << endl;
    cout << "-------------" << endl;
    cout << endl;

    for(i = 0; i < row; i++)
    {
    for(x = 0; x < column; x++)
    {
    cout << setw(5) << Yoona[i][x];
    }

    cout << endl;
    }

    cout << endl << endl;
    cout << "-------------" << endl;
    cout << "Third Matrix!" << endl;
    cout << "-------------" << endl;
    cout << endl;

    for(i = 0; i < row; i++)
    {
    for(x = 0; x < column; x++)
    {
    cout << setw(5) << Sunny[i][x];
    }

    cout << endl;
    }
    }

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,999

    Re: Matrices!

    Please use code tags, your code is unreadable like that.

    Since you are using VC++, you have a great debugger available. Set a breakpoint in your code, step through it and watch the values of the variables to figure out what's going wrong.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Matrices!

    At first glance, I don't see where you initialize your Yoona array. Could this be the problem?
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center