Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having Trouble with matrix input (const variables)

Status
Not open for further replies.

sparkythetiger

Programmer
Nov 22, 2002
1
0
0
US
I ran into a problem while trying to write a matrix program. Basically, what I'm trying to do is make a function INPUT that inputs a matrix from the user. I am having a lot of trouble with the const term at the beginning. Here is what I have so far. Is there any way you can help me?


#include <iostream>

using namespace std;

const int size = 10; // *THIS LINE HAS CAUSED MANY ERRORS

void INPUT( double[][ size ], int, int );
void OUTPUT( double[][ size ], int, int );

int main()
{
char stay = 'y';
int i, j;
int A[ i ][ j ]; // *SAME FOR THIS ONE

cout << &quot;This program manipulates matrices.&quot; << endl;
while ( stay == 'y' ) {

cout << &quot;How many rows?&quot;;
cin >> i;
cout << &quot;How many columns?&quot;;
cin >> j;
INPUT( A, i, j ); // *WHY DOES THE COMPILER SAY i AND j
OUTPUT( A, i, j ); // ARE SUPPOSED TO BE CONSTANTS???


cout << &quot;Enter the number of what you want to do:&quot; << endl << endl
<< &quot;1) Add a single variable to the matrix&quot;
<< &quot;2) Subtract a single variable from the matrix&quot;
<< &quot;3) Add another matrix to the matrix&quot;
<< &quot;4) Subtract another matrix from the matrix&quot;
<< &quot;5) Multiply by another matrix&quot; << endl << endl;

cin >> object;

switch ( object ) {

case 1:

break;

case 2:

break;

case 3:

break;

case 4:

break;

case 5:

break;

default:
cout << &quot;Incorrect number.. Enter 1, 2, 3, 4, or 5!&quot; << endl;
continue;

}

cout << endl << &quot;Do you wish to continue with a new matrix? (y or n)&quot; << endl;
cin >> stay;
cout << endl;
}

return 0;
}

void INPUT( double M[][ size ], int r, int c)
{
for( int i = 0; i < r; i++ ) {
for( int j = 0; j < c; j++ )
cout << &quot;Enter array element at row &quot; << i
<< &quot; and at column &quot; << j << &quot; &quot;;
cin >> M[ i ][ j ];
}
}

void OUTPUT( double M[][ size ], int r, int c )
{
for( int i = 0; i < r; i++ ) {
for( int j = 0; j < c; j++ )
cout << M[ i ][ j ] << &quot; &quot;;
cout << endl;
}
}
 
There are two problem I see.

1. You are trying to use i and j before initializing them when you use them in the &quot;int A[ i ][ j ];&quot; delaration, but this doesn't matter because...

2. When initializing an array, the number of elements needs to be a constant integer. i and j are not constants, so &quot;int A[ i ][ j ]; &quot; blows up.

I can't remember if I've constanted casted at run-time to define array initialization (does anyone out there know if this is possible?), but try to do so first (search for const_cast on the internet), then tell me if you are unsuccessful, and I'll tell you how to initialize arrays using pointers and &quot;new&quot;.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top