help with a course assigment matrices

Hello, my course assigment is:
Given the 2-dimensional arrays (matrices) X and Y

a) if X,Y are both of size M by M, swap each even row of X with the column in Y of the same number.

b)if X,Y are both of size M by N, compute the matrx Zo=X+Y

c) if X is M by N and Y is N by P, compute Q=M*N

I would be glad if someone can help me.
And your problems are what?
Have you tried anything yet? Are you given the dimensions of the arrays, or do you need to figure them out somehow?
well i know how to declarate the 2-dim. arrays and the most difficult part for me is a)
this is what I did :
#include<iostream.h>
#include<math.h>
double X[50][50];
double Y[50][50];
double Z[50][50];
int M,N,P,Q,i,j;

main () {
cout<<"Broi redove v matr.X=";
cin>>M;
cout<<"Broi stulbove v matr.X=";
cin>>N;

for (i=0;i<M;i++)
for (j=0;j<N;j++) {
cout<<"X["<<i<<"]["<<j<<"]=";
cin>>X[i][j];
}


cout<<"Broi redove v matr.Y=";
cin>>P;
cout<<"Broi stulbove v matr.Y=";
cin>>Q;

for (i=0;i<P;i++)
for (j=0;j<Q;j++) {
cout<<"Y["<<i<<"]["<<j<<"]=";
cin>>Y[i][j];
}
First of all, wrap code in [code][/code] tags to make it pretty.

So, a.
Step 1. Write a program that prints even rows of X.
Step 2. Write a function that, given an integer i, prints the i'th row of X and i'th column of Y.
Step 3. Write a function that swaps the first row of X with the first row of Y (or any two arrays).
Step 4. Combine the steps before into the solution to a. You should mostly only need ctrl+C, ctrl+V to do that.
Extra awesome step. Realize that 2d arrays are just fancy syntax for 1d arrays ([x][y] becomes [x+y*width]). Use this knowledge to replace static arrays with dynamic ones and allow the user to enter M.
Last edited on
I don`t know how to do step 1 , is it sth like this ?
#include<iostream.h>
#include<math.h>
double X[50][50];
double Y[50][50];
double Z[50][50];
int M,N,P,Q,S,k,i,j;

main () {
cout<<"Broi redove i stulbove v matr.X=";
cin>>M;


for (i=0;i<M;i++)
for (j=0;j<M;j++) {
cout<<"X["<<i<<"]["<<j<<"]=";
cin>>X[i][j];
}


for (i=0;i<M;i++)
for (j=0;j<M;j++) {
cout<<"Y["<<i<<"]["<<j<<"]=";
cin>>Y[i][j];} this is declaration

for (i=0;i<M;i++)
{
if (M%2==0)
cout<<X[i][j]<<endl;} is that how i print the even rows of X?
Know that I am not very good at this program , that is why I need help.
The last loop is missing something. What is j in it?
Not sure, now i think it is not cout<<X[i][j] but cout<<M
and I tried different ways but it`s not working. Can you help?
What is missing is another for loop. You need to iterate through the row to print it.
I hadn't noticed before. Why are you checking M % 2? M is the size of your matrix. It does not indicate the row you are in or anything else. 'i' does.
Topic archived. No new replies allowed.