Arrays? having some trouble!!


i'm trying to write a simple program to perform matrix multiplication, using the array structure effectively as a matrix.

i ran into trouble because i don't want to rewrite the program each time i want to use a new set of arrays with different dimensions and set of elements.

so to start of the program asks you how many matrices are needed, defines their dimensions, then populates their elements.

this 1st step is a little troublesome: this is what i've done:

usually to test i chose 2 matrices, each 2x2 for simplicity:

the problem is that when i test at the end, i can see that the elemnts have not been stored properly in the array and i don't know why:

if you know why please tell me:


#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
int a=1,n=0,m=0,x=0, v[x], c=0, d=0, e=0,f,g,h,i,t,p,q,r,s,l,z,j,k;
string y;

cout << "\n Please tell me how many matrices you want to use: ";

cin >> t;

lp1:

cout << "\n Please give me the dimensions of matrix: " << a << "\n";

cout << "\n N is: ";// obtaining dimensions for the matrix
cin >> n;
cout << "\n M is: ";
cin >> m;


int M[a] [n][m];// label the matrix and define it's size

// time to fill the elements of this matrix across from left to right

cout << "\n Moving from left to right \n";
lp2:
cout << "\n Please give me the value for Matrix [" << a << "] element [" << d << e << "]: " ;
cin >> M[a] [d][e];
e++;
if (e < m ) goto lp2;

// filled out the top shelf (row) so need to shift down to fill out next row

e = 0; // starting from the left again
d++;// next row down
if ( d < n ) goto lp2;

a++; // increase a for new matrix
d = e = 0; // setting the parameters to zero so thay have be used again if a loop is required
if (a < t + 1) goto lp1;


// quick test

cout << "\n" << M[1] [0][0];
cout << "\n" << M[1] [0][1];
cout << "\n" << M[1] [1][0];
cout << "\n" << M[1] [1][1];
cout << "\n" << M[2] [0][0];
cout << "\n" << M[2] [0][1];
cout << "\n" << M[2] [1][0];
cout << "\n" << M[2] [1][1];





Last edited on
you cant define arrays with a variable without using dynamic memory. instead what you can do is declare a an array, an make sure your input is smaller than the array size.

you may find this useful:
http://www.cplusplus.com/doc/tutorial/arrays/
thanks

is there anyway dynamic memory can be employed, if so how.

there is, but i have never used it. if you search this site i'm sure you will find something on it.
ok thanks, looking at it now and trying to make sense of it.
Topic archived. No new replies allowed.