CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
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 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);
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. ## Re: Matrices!

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.

3. Senior Member
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?

#### Posting Permissions

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

×