Multidimensional c++

Hello.. i'm back again trying to do program that prompts the user what they would like to do.. so far i have coded two functions which will allow the user to enter in an array value or display values.. but if user enters in 3 for example for the array value it should populate the array with 3 as it's a [3][5] array.. but it's not doing that..here is the current code for that

void    InitMatrix(int  matrix[][COLS], int  numRows, int  initVal)
{
   int i;
   int m;
   

   for(i=0; i<numRows; i++)
   {
    for(m=0; m<COLS; m++)
	{

		//take input for the array.
		initVal=matrix[3][5];
	}
}
}

Open in new window


also if the user selects to view the array it should allow the user to enter the field width using (setw() ) function. but it's not showing up correctly..

i have included the code below here

void    DispMatrix(int  matrix[][COLS], int  numRows, int  width)
{

	int i;
	int m;

	for(i=0;i<numRows;i++)
	{
	for (m=0; m<COLS;m++)
	{
		setw(width);
		cout <<matrix[i][m];
	}
}
}

Open in new window


here is my entire code so far.

//
// Description:
//  This program allows the user to manipulate a two-dimensional array of ints.
//  A menu is presented in a loop, and the user has the option to:
//
//     1) set all the elements of the 2D array to a user-entered value;
//     2) display the 2D array to stdout (using a user-entered field width);
//     3) display the sum of all the ints in the 2D array
//     4) quit the program.

// ============================================================================

#include    <iostream>
#include    <iomanip>
using namespace std;


// defined constants
const   int         ROWS = 3;
const   int         COLS = 5;
const   int         WIDTH = 5;


// function prototypes
void    DispMatrix(int  matrix[][COLS], int  numRows, int  width);
void    DispMenu(void);
int     GetMatrixSum(int  matrix[][COLS], int  numRows);
void    InitMatrix(int  matrix[][COLS], int  numRows, int  initVal);

// ==== main ==================================================================
//
// ============================================================================

int     main(void)
{
        bool        bContinue = true;
        int         myInts[ROWS][COLS];
        char        selection;
		int        assign;
		int       width;
    // announce ourselves to the user
    cout << "Welcome to the Matrix Manipulation Program!" << endl;

    // loop, display the menu and handle user selections until it's time to
    // quit!
    do  {
        // display the menu and get the user selection
        DispMenu();
        cin >> selection;

        // switch on the user's selection to call the appropriate function;
        // if the function to call requires additional input from the user,
        // get it here in the switch, not in the called function
		
       
        switch(toupper(selection))
		{
		case 'I':
			cout <<"enter value to assign to all matrix: ";
			cin>> assign;
			  InitMatrix(myInts,5,assign);
			break;
		case 'D':
			cout <<"enter the width u like to set ";
			cin >> width;
			DispMatrix(myInts,5,width);
			break;
		case 'G':
			break;
		case 'Q':
			exit(1);
			break;
		default:
			cout <<"You did not pick a valid choice";
		}


        } while (bContinue);

    return 0;

}  // end of "main"

// ==== DispMenu ==============================================================
//
// This function is responsible for writing the menu options to stdout for the
// user.
//
// Input:  nothing
//
// Output: nothing
//
// ============================================================================

void    DispMenu(void)
{
    // display the menu
    cout << endl;
    cout << "I)nitialize the matrix" << endl;
    cout << "D)isplay the matrix" << endl;
    cout << "G)et the sum of all values in the matrix" << endl;
    cout << "Q)uit" << endl;
    cout << "Please enter a selection: ";

}  // end of "DispMenu"

void    InitMatrix(int  matrix[][COLS], int  numRows, int  initVal)
{
   int i;
   int m;
   

   for(i=0; i<numRows; i++)
   {
    for(m=0; m<COLS; m++)
	{

		//take input for the array.
		initVal=matrix[3][5];
	}
}
}
void    DispMatrix(int  matrix[][COLS], int  numRows, int  width)
{

	int i;
	int m;

	for(i=0;i<numRows;i++)
	{
	for (m=0; m<COLS;m++)
	{
		setw(width);
		cout <<matrix[i][m];
	}
}
}

Open in new window

businessesatozAsked:
Who is Participating?
 
ZoppoCommented:
Hi businessesatoz,

IMO the line 119 is wrong:
initVal=matrix[3][5];

Open in new window

I guess this should be the other way round:
matrix[3][5]=initVal;

Open in new window

Hope that helps,

ZOPPO
0
 
ZoppoCommented:
Sorry, that's even wrong, it should be:
matrix[i][m]=initVal;

Open in new window


ZOPPO
0
 
businessesatozAuthor Commented:
lol funny i figured it out right when i posted the question.. silly me..
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
ZoppoCommented:
:o)
0
 
businessesatozAuthor Commented:
thanks.. i can't seem to display it correct should show 3 rows with 5 cols... i did the code below

void    DispMatrix(int  matrix[][COLS], int  numRows, int  width)
{

	int i;
	int m;

	for(i=0;i<numRows;i++)
	{
	for (m=0; m<COLS;m++)
	{
	
		cout <<matrix[i][m]<<" "<<setw(width);
	}
}
}

Open in new window


it show up as Here are the current matrix contents:
      1      1      1      1      1
      1      1      1      1      1
      1      1      1      1      1
don't know why i can't get mine to show up like that..
0
 
ZoppoCommented:
How does your printout differ from what you posted?

One thing is strange with you code - you wrote should be 3 rows with 5 columns, but in fact you COLS is 5 and you pass 5 as 'numRows' to the functions 'InitMatrix' and 'DispMatrix', so in fact you have a 5x5 matrix.

ZOPPO
0
 
businessesatozAuthor Commented:
thanks now i changed the code so only 15 values are inputted so it's a 3 by 5 3 rows with 5 colmns. in the mean time my output comes out "3 3 3 3 ....." not
3  3 .....
3  3 .....
3  3 .....

am I missing like a new line character or something..?

here is the latest code u will see that you can complie and run the code fine.

//
// Description:
//  This program allows the user to manipulate a two-dimensional array of ints.
//  A menu is presented in a loop, and the user has the option to:
//
//     1) set all the elements of the 2D array to a user-entered value;
//     2) display the 2D array to stdout (using a user-entered field width);
//     3) display the sum of all the ints in the 2D array
//     4) quit the program.

// ============================================================================

#include    <iostream>
#include    <iomanip>
using namespace std;


// defined constants
const   int         ROWS = 3;
const   int         COLS = 5;
const   int         WIDTH = 5;


// function prototypes
void    DispMatrix(int  matrix[][COLS], int  numRows, int  width);
void    DispMenu(void);
int     GetMatrixSum(int  matrix[][COLS], int  numRows);
void    InitMatrix(int  matrix[][COLS], int  numRows, int  initVal);

// ==== main ==================================================================
//
// ============================================================================

int     main(void)
{
        bool        bContinue = true;
        int         myInts[ROWS][COLS];
        char        selection;
		int        assign;
		int       width;
    // announce ourselves to the user
    cout << "Welcome to the Matrix Manipulation Program!" << endl;

    // loop, display the menu and handle user selections until it's time to
    // quit!
    do  {
        // display the menu and get the user selection
        DispMenu();
        cin >> selection;

        // switch on the user's selection to call the appropriate function;
        // if the function to call requires additional input from the user,
        // get it here in the switch, not in the called function
		
       
        switch(toupper(selection))
		{
		case 'I':
			cout <<"enter value to assign to all matrix: ";
			cin>> assign;
			  InitMatrix(myInts,3,assign);
			break;
		case 'D':
			cout <<"enter the width u like to set ";
			cin >> width;
			DispMatrix(myInts,3,width);
			break;
		case 'G':
			break;
		case 'Q':
			exit(1);
			break;
		default:
			cout <<"You did not pick a valid choice";
		}


        } while (bContinue);

    return 0;

}  // end of "main"

// ==== DispMenu ==============================================================
//
// This function is responsible for writing the menu options to stdout for the
// user.
//
// Input:  nothing
//
// Output: nothing
//
// ============================================================================

void    DispMenu(void)
{
    // display the menu
    cout << endl;
    cout << "I)nitialize the matrix" << endl;
    cout << "D)isplay the matrix" << endl;
    cout << "G)et the sum of all values in the matrix" << endl;
    cout << "Q)uit" << endl;
    cout << "Please enter a selection: ";

}  // end of "DispMenu"

void    InitMatrix(int  matrix[][COLS], int  numRows, int  initVal)
{
   int i;
   int m;
   

   for(i=0; i<numRows; i++)
   {
    for(m=0; m<COLS; m++)
	{

		//take input for the array.
		matrix[i][m]=initVal;
	}
}
}
void    DispMatrix(int  matrix[][COLS], int  numRows, int  width)
{

	int i;
	int m;

	for(i=0;i<numRows;i++)
	{
	for (m=0; m<COLS;m++)
	{
	
		cout <<matrix[i][m]<<" "<<setw(width);
	}
}
}

Open in new window

0
 
ZoppoCommented:
ok, you just need to insert a line break after each row, i.e.:
for(i=0;i<numRows;i++)
{
	for (m=0; m<COLS;m++)
	{
		cout <<matrix[i][m]<<" "<<setw(width);
	}
	cout << endl; // ADD THIS
}

Open in new window


ZOPPO
0
 
businessesatozAuthor Commented:
thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.