GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jan-2007, 14:55
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Dynamic 2D Array / Matrix


Quote:
Write a C++ program that will dynamically allocate a two-dimensional array (matrix) of integers and reports the sum of each row and column. Your program should first open a file named "prog1.dat" and read the size of the matrix. The first and second lines or the file will contain the number of rows and the number or columns respectively. The remaining lines in the file will each contain tree integers representing the row, column, and data value for and element in the matrix. If a particular matrix element is not listed the data value should be assumed to be zero.

once your program has created and filled the two-dimensional array (matrix)the row and column sums should be calculated and reported. Finally any dynamically allocated memory should be returned to the heap using the delete command.

I need a little help trying to get the matrix built, and how i would display it.

I think im setting my memory address to 0. Can some one confirm this?

CPP / C++ / C Code:
int main()
{
	//defines variables
	int row, col, sumCol, sumRow;
	int	**data;

	//Declares an inData File and opens it
	ifstream inData;
	inData.open("prog1.dat");

	//Checks to see if file open, if not closes it
	if(!inData)
	{
		cout << "Error Opening File... \nProgram will now close! \n";
		return 0;
	}

	else
	
	//Amount or row's and col's are stored
	inData >> row >> col; 

	cout << "you have " << row <<" rows and " << col << " colums. \n";
	
		//creating the matrix
	data = new int*[row];
	for(int r=0; r<row; r++)
	{
		data[r]= new int[col];
	}
	
	//intializes the matrix to 0
	for(int i=0; i<row;i++)
	{
		data[i]=0;
		for(int j=0; j<col; j++)
		{
			data[j]=0;
		}
	}
	
// does that correctly creat a matrix and set it all to 0? or am i setting the memory addresses to 0?

	// display my matrix
	cout << "your matrix looks like : \n";
	
	//loop to display my matrix
	for(int rowDown=0; rowDown<row; rowDown++)
	{
		cout << data[rowDown] << "\t";
		for(int colOver=0; colOver<col; colOver++)
		{	
			cout << data[colOver] << "\t";
		}
	}

//am i displaying the memory address or am i displaying the value at each location?

	int d, sum; //not sure what this is supposed to do teacher gave it to us
	while(inData >> r >> col >> d)
		data[r][col]=d;

	//deletes and gives back dynamic allocated memory
	for(int a=0; a<row; a++)
		delete[] data[a];
	delete [] data;

	return 0;
  #2  
Old 12-Jan-2007, 15:39
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Dynamic 2D Array / Matrix


Your loop for setting all the data in the matrix to zero is not good at all. The cells in the matrix are accessed by data[row][col]. By setting data[i] and data[j] to zero, you are throwing out the new int[col] that you allocated. This is both a memory leak and just begging for a crash later on. You have the right idea with nested loops, but you just needed one statement, inside both loops, that will look like
data[i][j] = 0;
  #3  
Old 13-Jan-2007, 12:47
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Re: Dynamic 2D Array / Matrix


Currently this is my code:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	//defines variables
	int row, col, sumCol, sumRow;
	int	**data;

	//Declares an inData File and opens it
	ifstream inData;
	inData.open("prog1.dat");

	//Checks to see if file open, if not closes it
	if(!inData)
	{
		cout << "Error Opening File... \nProgram will now close! \n";
		return 0;
	}

	else
	
	//Amount or row's and col's are stored
	inData >> row >> col; 

	cout << "you have " << row <<" rows and " << col << " colums. \n";
	
	//creating the matrix
	data = new int*[row];
	for(int r=0; r<row; r++)
	{
		data[r]= new int[col];
	}
	
	//intializes the matrix to 0
	for(int i=0; i<row;i++)
	{
		for(int j=0; j<col; j++)
		{
			data[i][j]=0;
		}
	}


	//loop to display my matrix
	for(int rowDown=0; rowDown<row; rowDown++)
	{
		cout << data[rowDown] << "  ";
		for(int colOver=0; colOver<col; colOver++)
		{	
			cout << data[colOver] << "  ";
		}
		cout << "\n";
	}

	int d,c;
	while(inData >> r >> c >> d)
		data[r][c]=d;


	//display row summery
	cout << "\n" << "Row Sum:" << "\n" << endl;

	for(int rowCount=0; rowCount<row; rowCount++)
	{
		cout << rowCount << " = " << "\n"; 
	}

	cout << "\n" << "Col Sum:" << "\n" << endl;
	for(int colCount=0; colCount<col; colCount++) 
	{
		cout << colCount << " = " << "\n";
	}

	//deletes and gives back dynamic allocated memory
	for(int a=0; a<row; a++)
		delete[] data[a];
	delete [] data;

	return 0;

}

Im having trouble With these two parts:

First Im just trying to read in the data from the prog1.dat file and put it in to my matrix of 0's:
Code:
int d,c; while(inData >> r >> c >> d) data[r][c]=d;

Second im trying to figure out how to add up the row and Col and post 2 summaries:
Code:
//display row summery cout << "\n" << "Row Sum:" << "\n" << endl; for(int rowCount=0; rowCount<row; rowCount++) { cout << rowCount << " = " << "\n"; } cout << "\n" << "Col Sum:" << "\n" << endl; for(int colCount=0; colCount<col; colCount++) { cout << colCount << " = " << "\n"; }

my prog1.dat file
Quote:
3
5
0 4 22
0 3 3
1 0 -3
2 2 -10
2 1 7
2 0 18
1 1 25
0 2 -15
  #4  
Old 13-Jan-2007, 13:33
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Re: Dynamic 2D Array / Matrix


CPP / C++ / C Code:
	#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	//defines variables
	int row, col, sumCol, sumRow;
	int	**data;

	//Declares an inData File and opens it
	ifstream inData;
	inData.open("prog1.dat");

	//Checks to see if file open, if not closes it
	if(!inData)
	{
		cout << "Error Opening File... \nProgram will now close! \n";
		return 0;
	}

	else
	
	//Amount or row's and col's are stored
	inData >> row >> col; 

	//display how many rows and cols you have
	cout << "you have " << row <<" rows and " << col << " colums. \n";
	
	//creating the matrix
	data = new int*[row];
	for(int r=0; r<row; r++)
	{
		data[r]= new int[col];
	}
	
	//intializes the matrix to 0
	for(int i=0; i<row;i++)
	{
		for(int j=0; j<col; j++)
		{
			data[i][j]=0;
		}
	}

	// display my matrix
	cout << "your matrix looks like : \n";

	//loop to display my matrix
	for (int ro = 0; ro < row; ++ro)
	{
		for (int co = 0; co < col; ++co)
		{
			cout << data[ro][co] << " ";
		}
		cout << endl;
	}

	int e, d,c;
	while(inData >> e >> c >> d)
		data[e][c]=d;


	// display my matrix
	cout << "Now your matrix looks like : \n";

	//loop to display my matrix
	for (int ro1 = 0; ro1 < row; ++ro1)
	{
		for (int co1 = 0; co1 < col; ++co1)
		{
			cout << data[ro1][co1] << " ";
		}
		cout << endl;
	}
	

	//display row summery
	cout << "\n" << "Row Sum:" << "\n" << endl;

	for(int rowCount=0; rowCount<row; rowCount++)
	{
		for(int rowCount2=0; rowCount2<row; rowCount2++)
		{
			sumRow	+= data[rowCount][rowCount2];
			cout << rowCount << " = " << "\n" << sumRow;
		}
	}

	cout << "\n" << "Col Sum:" << "\n" << endl;
	for(int colCount=0; colCount<col; colCount++) 
	{
		cout << colCount << " = " << "\n";
	}

	//deletes and gives back dynamic allocated memory
	for(int a=0; a<row; a++)
		delete[] data[a];
	delete [] data;

	return 0;

}

CPP / C++ / C Code:
	for(int rowCount=0; rowCount<row; rowCount++)
	{
		for(int colCount=0; colCount<col; colCount++)
		{
			sumRow	+= data[rowCount][colCount];
			
		}
			cout << rowCount << " = " << "\n" << sumRow;
	}
  #5  
Old 14-Jan-2007, 12:03
davis
 
Posts: n/a

Re: Dynamic 2D Array / Matrix


This may be of some help to you:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int** create_2D_matrix( const unsigned int rows, const unsigned int cols )
{
    int** matrix = 0;
    if( rows && cols )
    {
        matrix = new int*[rows];
        for( unsigned int i = 0; i < rows; i++ )
        {
            matrix[i] = new int[cols];
        }
    }
    return matrix;
}

void destroy_2D_matrix( int** matrix, const unsigned int rows )
{
    if( matrix )
    {
        for( unsigned int i = 0; i < rows; i++ )
        {
            delete [] matrix[i];
        }
        delete [] matrix;
        matrix = 0;
    }
}

void bogus_matrix_initializer( int** matrix, const unsigned int rows, const unsigned int cols )
{
    int counter = 1;
    for( unsigned int i = 0; i < rows; i++ )
    {
        for( unsigned int j = 0; j < cols; j++ )
        {
            matrix[i][j] = counter++;
        }
    } 
}

void set_value_of_cell_at( int** matrix, const unsigned int row, const unsigned int col, const int value )
{
    matrix[row][col] = value;
}

int sum_row_at( int** matrix, const unsigned int row, const unsigned int cols )
{
    int result = 0;
    for( unsigned int i = 0; i < cols; i++ )
    {
        result += matrix[row][i];
    }
    return result;
}

int sum_col_at( int** matrix, const unsigned int col, const unsigned int rows )
{
    int result = 0;
    for( unsigned int i = 0; i < rows; i++ )
    {
        result += matrix[i][col];
    }
    return result;
}

void spew_matrix_contents( int** matrix, const unsigned int rows, const unsigned int cols )
{
    for( unsigned int i = 0; i < rows; i++ )
    {
        for( unsigned int j = 0; j < cols; j++ )
        {
            cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << endl;
        }
    } 
}

void display_matrix( int** matrix, const unsigned int rows, const unsigned int cols )
{
    string dashes = "";
    for( unsigned int i = 0; i < cols; i++ )
    {
        cout << "\t" << i;
        dashes += "---------";
    }
    cout << endl;
    cout << dashes << endl;
    for( unsigned int i = 0; i < rows; i++ )
    {
        cout << i << " |\t";
        for( unsigned int j = 0; j < cols; j++ )
        {
            cout << matrix[i][j] << "\t";
        }
        cout << endl;
    }
}

int main()
{
    int** matrix = create_2D_matrix( 10, 10 );
    bogus_matrix_initializer( matrix, 10, 10 );
    spew_matrix_contents( matrix, 10, 10 );
    cout << endl;
    display_matrix( matrix, 10, 10 );
    cout << endl;
    cout << "sum of row 0 = " << sum_row_at( matrix, 0, 10 ) << endl;
    cout << "sum of col 0 = " << sum_col_at( matrix, 0, 10 ) << endl;
    cout << endl;

    set_value_of_cell_at( matrix, 0, 0, 9999 );
    set_value_of_cell_at( matrix, 9, 9, -9999 );
    display_matrix( matrix, 10, 10 );
    cout << endl;
    cout << "sum of row 0 = " << sum_row_at( matrix, 0, 10 ) << endl;
    cout << "sum of col 0 = " << sum_col_at( matrix, 0, 10 ) << endl;
    destroy_2D_matrix( matrix, 10 );

    return 0;
}

Output:

Code:
matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3 matrix[0][3] = 4 matrix[0][4] = 5 matrix[0][5] = 6 matrix[0][6] = 7 matrix[0][7] = 8 matrix[0][8] = 9 matrix[0][9] = 10 matrix[1][0] = 11 matrix[1][1] = 12 matrix[1][2] = 13 matrix[1][3] = 14 matrix[1][4] = 15 matrix[1][5] = 16 matrix[1][6] = 17 matrix[1][7] = 18 matrix[1][8] = 19 matrix[1][9] = 20 matrix[2][0] = 21 matrix[2][1] = 22 matrix[2][2] = 23 matrix[2][3] = 24 matrix[2][4] = 25 matrix[2][5] = 26 matrix[2][6] = 27 matrix[2][7] = 28 matrix[2][8] = 29 matrix[2][9] = 30 matrix[3][0] = 31 matrix[3][1] = 32 matrix[3][2] = 33 matrix[3][3] = 34 matrix[3][4] = 35 matrix[3][5] = 36 matrix[3][6] = 37 matrix[3][7] = 38 matrix[3][8] = 39 matrix[3][9] = 40 matrix[4][0] = 41 matrix[4][1] = 42 matrix[4][2] = 43 matrix[4][3] = 44 matrix[4][4] = 45 matrix[4][5] = 46 matrix[4][6] = 47 matrix[4][7] = 48 matrix[4][8] = 49 matrix[4][9] = 50 matrix[5][0] = 51 matrix[5][1] = 52 matrix[5][2] = 53 matrix[5][3] = 54 matrix[5][4] = 55 matrix[5][5] = 56 matrix[5][6] = 57 matrix[5][7] = 58 matrix[5][8] = 59 matrix[5][9] = 60 matrix[6][0] = 61 matrix[6][1] = 62 matrix[6][2] = 63 matrix[6][3] = 64 matrix[6][4] = 65 matrix[6][5] = 66 matrix[6][6] = 67 matrix[6][7] = 68 matrix[6][8] = 69 matrix[6][9] = 70 matrix[7][0] = 71 matrix[7][1] = 72 matrix[7][2] = 73 matrix[7][3] = 74 matrix[7][4] = 75 matrix[7][5] = 76 matrix[7][6] = 77 matrix[7][7] = 78 matrix[7][8] = 79 matrix[7][9] = 80 matrix[8][0] = 81 matrix[8][1] = 82 matrix[8][2] = 83 matrix[8][3] = 84 matrix[8][4] = 85 matrix[8][5] = 86 matrix[8][6] = 87 matrix[8][7] = 88 matrix[8][8] = 89 matrix[8][9] = 90 matrix[9][0] = 91 matrix[9][1] = 92 matrix[9][2] = 93 matrix[9][3] = 94 matrix[9][4] = 95 matrix[9][5] = 96 matrix[9][6] = 97 matrix[9][7] = 98 matrix[9][8] = 99 matrix[9][9] = 100 0 1 2 3 4 5 6 7 8 9 ------------------------------------------------------------------------------------------ 0 | 1 2 3 4 5 6 7 8 9 10 1 | 11 12 13 14 15 16 17 18 19 20 2 | 21 22 23 24 25 26 27 28 29 30 3 | 31 32 33 34 35 36 37 38 39 40 4 | 41 42 43 44 45 46 47 48 49 50 5 | 51 52 53 54 55 56 57 58 59 60 6 | 61 62 63 64 65 66 67 68 69 70 7 | 71 72 73 74 75 76 77 78 79 80 8 | 81 82 83 84 85 86 87 88 89 90 9 | 91 92 93 94 95 96 97 98 99 100 sum of row 0 = 55 sum of col 0 = 460 0 1 2 3 4 5 6 7 8 9 ------------------------------------------------------------------------------------------ 0 | 9999 2 3 4 5 6 7 8 9 10 1 | 11 12 13 14 15 16 17 18 19 20 2 | 21 22 23 24 25 26 27 28 29 30 3 | 31 32 33 34 35 36 37 38 39 40 4 | 41 42 43 44 45 46 47 48 49 50 5 | 51 52 53 54 55 56 57 58 59 60 6 | 61 62 63 64 65 66 67 68 69 70 7 | 71 72 73 74 75 76 77 78 79 80 8 | 81 82 83 84 85 86 87 88 89 90 9 | 91 92 93 94 95 96 97 98 99 -9999 sum of row 0 = 10053 sum of col 0 = 10458


:davis:
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2d dynamic array mrt_cnr C++ Forum 0 18-Oct-2006 13:47
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 07:17
saving matrix from file to array amina17 C Programming Language 5 03-Jun-2005 13:04
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:17.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.