Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Multidimensional c++

Posted on 2011-10-26
9
Medium Priority
?
292 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:businessesatoz
  • 5
  • 4
9 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 37029620
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 37029625
Sorry, that's even wrong, it should be:
matrix[i][m]=initVal;

Open in new window


ZOPPO
0
 

Author Comment

by:businessesatoz
ID: 37029639
lol funny i figured it out right when i posted the question.. silly me..
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

Expert Comment

by:Zoppo
ID: 37029642
:o)
0
 

Author Comment

by:businessesatoz
ID: 37029692
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 37029720
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
 

Author Comment

by:businessesatoz
ID: 37029731
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 37029734
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
 

Author Closing Comment

by:businessesatoz
ID: 37029791
thanks for the help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question