943,696 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 482
  • C++ RSS
Feb 20th, 2009
0

2d array question

Expand Post »
Why is this array not printing in 2D?

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

const int COLS = 5;
const int ROWS = 5;
void showArray(int [][COLS], int);
void arrayDef(int [][COLS], int);
void totalCol(int [][COLS], int);
void totalRow(int [][COLS], int);
void totalDiag(int [][COLS], int);
void inputAmt(int [][COLS], int, int, int);
void inputZ(int [][COLS], int, int);
double rTotal = 0;
int cTotal = 0;
int diagTotal= 0;
void gameWinner(int [][COLS]);

int main()
{
	int array[ROWS][COLS] = {{0,0,0,0,0},
		{0,0,0,0,0},
		{0,0,0,0,0},
		{0,0,0,0,0},
		{0,0,0,0,0}};
	int menu;
	int input;
	char answer;
	int rowNumber;
	int inRow = 0;
	int inCol = 0;
	int zRow = 0;
	int zCol = 0;
	int diag = 0;
	
	int colNumber = 0;
	
	
	
	
	while (menu != 9)
	{
		cout << "Menu"<< endl;
		cout << "1: Print the Square" << endl; // DONE
		cout << "2: Input a number" << endl;  //DONE
		cout << "3: Clear a square" << endl;  //DONE
		cout << "4: Total a Row" << endl;	//DONE
		cout << "5: Total a Column" << endl;  //DONE
		cout << "6: Total a diagonal" << endl;   //DONE
		cout << "7: Check for a Win" << endl;  //DONE
		cout << "8: Reset the game" << endl;  //DONE
		cout << "9: Exit the game" << endl;  //DONE
		
		
		cout<< "Enter your selection: ";
		cin >> menu;
		
		if(menu == 1)
		{
			cout<< "Here is the current square: " << endl;
			showArray(array, ROWS);
		}
		
		if(menu == 2)
		{
			cout << "Enter Row: " ;
			cin >> inRow;
			cout << "Enter Column: ";
			cin >> inCol;
			cout << "Enter the amount: ";
			cin >> input;
			while(input < 1 or input > 25)
			{
				cout<< "Invalid";
				cin >> input;
			}
			inputAmt(array, inRow, inCol, input);
			
		}
		
		if(menu == 3)
		{
			cout << "Enter Row: " ;
			cin >> zRow;
			cout << "Enter Column: ";
			cin >> zCol;
			inputZ(array, zRow, zCol);
		}
		
		if (menu == 4)
		{
			cout << "Enter Row to total: ";
			cin >> rowNumber;
			totalRow(array, rowNumber);
			
		}
		
		if (menu == 5)
		{
			cout << "Enter Column to total: ";
			cin >> colNumber;
			totalCol(array, colNumber);
		}
		
		if (menu == 6)
		{
			cout << "To total top-left to bottom right diagonal ( \ ), press 1: " << endl;
			cout << "To total top-right to bottom left diagonal, ( / ) press 2: " << endl;
			cin >> diag;
			totalDiag(array, diag);
		}
		
		if (menu == 7)
		{
			cout << "Checking for Win" << endl;
			gameWinner(array);
			
			
		}
		
		if (menu == 8)
		{
			cout << "Are you sure you want to reset? Y or N?";
			cin >> answer;
			if (answer == 'Y')
			{
				cout << "Here is your blank square..." << endl;
				arrayDef(array, ROWS);
				
				
			}
			
			if (answer == 'N')
			{
				
				
			}
			while(answer != 'Y' && answer != 'N')
			{
				cout << "Invalid, please retry: ";
				cin >> answer;
			}
		}
		
	}
	
	
	
	return 0;
}

void showArray(int array[][COLS], int rows)
{
	
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < COLS; y++)
		{
			cout << setw(4)  << array[x][y];
		}
		
	}
}

void arrayDef(int array[][COLS], int ROWS)
{
	for (int x = 0; x < ROWS; x++)
	{
		for (int y = 0; y < COLS; y++)
		{ 
			array[x][y] = 0;
		}
	}
	
	
	
}

void inputAmt(int array[][COLS], int inRow, int inCol, int input)
{	
	array[inRow - 1][inCol - 1] = input;
	cout << input << endl;
	
}

void totalRow(int array[][COLS], int rowNumber)
{
	if (rowNumber == 1)
	{
		rTotal = array[0][0]+ array[0][1] + array[0][2] + array[0][3] + array[0][4];
		cout << "Row Total: " << rTotal << endl;
	}
	
	if (rowNumber == 2)
	{
		rTotal = array[1][0]+ array[1][1] + array[1][2] + array[1][3] + array[1][4];
		cout << "Row Total: " << rTotal<< endl;
	}
	
	if (rowNumber == 3)
	{
		rTotal = array[2][0]+ array[2][1] + array[2][2] + array[2][3] + array[2][4];
		cout << "Row Total: " << rTotal<< endl;
	}
	
	if (rowNumber == 4)
	{
		rTotal = array[3][0]+ array[3][1] + array[3][2] + array[3][3] + array[3][4];
		cout << "Row Total: " << rTotal<< endl;
	}
	
	if (rowNumber == 5)
	{
		rTotal = array[4][0]+ array[4][1] + array[4][2] + array[4][3] + array[4][4];
		cout << "Row Total: " << rTotal<< endl;
	}
	
	
}



void totalCol(int array[][COLS], int colNumber)
{
	if (colNumber == 1)
	{
		cTotal = array[0][0]+ array[1][0] + array[2][0] + array[3][0] + array[4][0];
		cout << "Column Total: " << cTotal << endl;
	}
	if (colNumber == 2)
	{
		cTotal = array[0][1]+ array[1][1] + array[2][1] + array[3][1] + array[4][1];
		cout << "Column Total: " << cTotal << endl;
	}
	if (colNumber == 3)
	{
		cTotal = array[0][2]+ array[1][2] + array[2][2] + array[3][2] + array[4][2];
		cout << "Column Total: " << cTotal << endl;
	}
	if (colNumber == 4)
	{
		cTotal = array[0][3]+ array[1][3] + array[2][3] + array[3][3] + array[4][3];
		cout << "Column Total: " << cTotal << endl;
	}
	if (colNumber == 5)
	{
		cTotal = array[0][4]+ array[1][4] + array[2][4] + array[3][4] + array[4][4];
		cout << "Column Total: " << cTotal << endl;
	}
}

void inputZ(int array[][COLS], int zRow, int zCol)
{	
	array[zRow - 1][zCol - 1] = 0;
	
	
}

void totalDiag(int array[][COLS], int diag)
{
	
	if(diag == 1)
	{
		diagTotal = array[0][0]+ array[1][1] + array[2][2] + array[3][3] + array[4][4];
		cout << "Diagonal total: " << diagTotal <<endl;
	}
	
	if(diag ==2)
	{
		diagTotal = array[4][0]+ array[3][1] + array[2][2] + array[1][3] + array[0][4];
		cout << "Diagonal total: " << diagTotal <<endl;
	}
}


void gameWinner(int array[][COLS])
{
	int row1 = array[0][0]+ array[0][1] + array[0][2] + array[0][3] + array[0][4];
	int row2 = array[1][0]+ array[1][1] + array[1][2] + array[1][3] + array[1][4];
	int row3 = array[2][0]+ array[2][1] + array[2][2] + array[2][3] + array[2][4];
	int row4 = array[3][0]+ array[3][1] + array[3][2] + array[3][3] + array[3][4];
	int row5 = array[4][0]+ array[4][1] + array[4][2] + array[4][3] + array[4][4];
	int col1 = array[0][0]+ array[1][0] + array[2][0] + array[3][0] + array[4][0];
	int col2 = array[0][1]+ array[1][1] + array[2][1] + array[3][1] + array[4][1];
	int col3 = array[0][2]+ array[1][2] + array[2][2] + array[3][2] + array[4][2];
	int col4 = array[0][3]+ array[1][3] + array[2][3] + array[3][3] + array[4][3];
	int col5 = array[0][4]+ array[1][4] + array[2][4] + array[3][4] + array[4][4];
	int diag1 = array[0][0]+ array[1][1] + array[2][2] + array[3][3] + array[4][4];
	int diag2 = array[4][0]+ array[3][1] + array[2][2] + array[1][3] + array[0][4];
	
	if(row1 ==65 && row2 ==65&& row3 ==65 &&row4 ==65 &&row5 ==65 &&col1 ==65 &&col2 ==65 &&col3==65 &&col4==65 &&col5==65 &&diag1 ==65 &&diag2==65)
	{
		
		cout << "You Win"<< endl;
	}
}
Similar Threads
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Feb 20th, 2009
0

Re: 2d array question

This program shouldn't run at all. Check this:
C++ Syntax (Toggle Plain Text)
  1.  
  2. int menu;
  3. [...]
  4. while (menu != 9)

You're using the variable 'menu' without it being initialized. That could (and probably will) crash your program.
Change the initialization to give it a initial value: int menu=0;
Another thing: the word 'or' isn't used in standard c++. Change it to || (two pipes)

Now for your original question "Why is this array not printing in 2D?"
If you look at your 'showArray' function, you can see that you use 2 nested loops correctly to loop through your 2d array. But you have to keep in mind that 2d-array's aren't stored as a big squares in the memory, that's just how we humans describe something 2d Think about it: How would it store a 5-d array? (they are legal to use in C++)

So you'll have to tell your program to jump to another line after each row is done. This can be done by adding one line to your function:

void showArray(int array[][COLS], int rows)
{
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < COLS; y++)
		{
			cout << setw(4)  << array[x][y];
		}
		cout << '\n'; // print a 'newline'
	}
}
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 20th, 2009
0

Re: 2d array question

You used defines in C++, most people look down on that for the reason that you can also use (static) const unsigned ints in your case. It has the benefit of type checking.

The array initialization is okay with the brackets, but if you make it a loop you won't have to update it manually each time. Maybe you can read up about memset().

The menu decision part now consists of if... if... It's more natural to make that a switch statement.

All of these tips won't have impact on the output of the program, but they are useful nonetheless.

Horribly off topic, what game is this? X in a row? Sudoku? The rest is looking good.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compiler won't accept char as xor operand
Next Thread in C++ Forum Timeline: string replace or erase better?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC