2 dimensional array

2 dimensional array

Page 1 of 1

4 Replies - 1494 Views - Last Post: 23 July 2009 - 10:39 PM Rate Topic: -----

#1 djustus1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-July 09

2 dimensional array

Post icon  Posted 23 July 2009 - 09:33 PM

:crazy: I need to fill in the code to read and store the next value in the array and fill in the code to print the table. Any help is greatly appreciated.
#include <iostream>
#include <iomanip>
using namespace std;
const MAXROWS = 10;
const MAXCOLS = 10;
typedef float PriceType[MAXROWS][MAXCOLS]; // creates a new data type
// of a 2D array of floats
void getPrices(PriceType, int&, int&); // gets the prices into the array
void printPrices(PriceType, int, int); // prints data as a table
int main()
{
int rowsUsed; // holds the number of rows used
int colsUsed; // holds the number of columns used
PriceType priceTable; // a 2D array holding the prices
getPrices(priceTable, rowsUsed, colsUsed); // calls getPrices to fill the array
printPrices(priceTable, rowsUsed, colsUsed);// calls printPrices to display array
return 0;
}
//*******************************************************************************
// getPrices
//
// task: This procedure asks the user to input the number of rows and
// columns. It then asks the user to input (rows * columns) number of
// prices. The data is placed in the array.
// data in: none
// data out: an array filled with numbers and the number of rows
// and columns used.
//
//*******************************************************************************
void getPrices(PriceType table, int& numOfRows, int& numOfCols)
{
cout << "Please input the number of rows from 1 to "<< MAXROWS << endl;
cin >> numOfRows;
cout << "Please input the number of columns from 1 to "<< MAXCOLS << endl;
cin >> numOfCols;

for (int row = 0; row < numOfRows; row++)
{
	for (int col = 0; col < numOfCols; col++)
	   //THIS IS WHERE I NEED THE CODE TO READ AND FILL THE NEXT  VALUE IN THE ARRAY
//***************************************************************************
// printPrices
//
// task: This procedure prints the table of prices
// data in: an array of floating point numbers and the number of rows
// and columns used.
// data out: none
//
//****************************************************************************
void printPrices(PriceType table, int numOfRows, int numOfCols)
{
cout << fixed << showpoint << setprecision(2);
for (int row = 0; row < numOfRows; row++)
{
for (int col = 0; col < numOfCols; col++)
// FILL IN THE CODE TO PRINT(READ) THE TABLE.



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: 2 dimensional array

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: 2 dimensional array

Posted 23 July 2009 - 09:58 PM

Quote

I need to fill in the code to read and store the next value in the array and fill in the code to print the table.


What?

Please explain a little bit more and indicate which sections of code need fixing.

Also you need the backslash before the word "code" in your closing code tag.
Was This Post Helpful? 0
  • +
  • -

#3 djustus1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-July 09

Re: 2 dimensional array

Posted 23 July 2009 - 10:04 PM

Thank you. I need the code to read in the values. I am having trouble with finishing the code for printprices
and get prices.
 
void printPrices(PriceType table, int numOfRows, int numOfCols)
{
cout << fixed << showpoint << setprecision(2);
for (int row = 0; row < numOfRows; row++)
{
for (int col = 0; col < numOfCols; col++)
// [b]Fill in the code to print the table[/b]


}

and


void getPrices(PriceType table, int& numOfRows, int& numOfCols)
{
cout << "Please input the number of rows from 1 to "<< MAXROWS << endl;
cin >> numOfRows;
cout << "Please input the number of columns from 1 to "<< MAXCOLS << endl;
cin >> numOfCols;
for (int row = 0; row < numOfRows; row++)
{
for (int col = 0; col < numOfCols; col++)
[b]// Fill in the code to read and store the next value in the array[/b]



Does that help?
#include <iostream>
#include <iomanip>
using namespace std;
const MAXROWS = 10;
const MAXCOLS = 10;
typedef float PriceType[MAXROWS][MAXCOLS]; // creates a new data type
// of a 2D array of floats
void getPrices(PriceType, int&, int&); // gets the prices into the array
void printPrices(PriceType, int, int); // prints data as a table
int main()
{
int rowsUsed; // holds the number of rows used
int colsUsed; // holds the number of columns used
PriceType priceTable; // a 2D array holding the prices
getPrices(priceTable, rowsUsed, colsUsed); // calls getPrices to fill the array
printPrices(priceTable, rowsUsed, colsUsed);// calls printPrices to display array

system("Pause");
return 0;
}
//*******************************************************************************
// getPrices
//
// task: This procedure asks the user to input the number of rows and
// columns. It then asks the user to input (rows * columns) number of
// prices. The data is placed in the array.
// data in: none
// data out: an array filled with numbers and the number of rows
// and columns used.
//
//*******************************************************************************
void getPrices(PriceType table, int& numOfRows, int& numOfCols)
{
cout << "Please input the number of rows from 1 to "<< MAXROWS << endl;
cin >> numOfRows;
cout << "Please input the number of columns from 1 to "<< MAXCOLS << endl;
cin >> numOfCols;

for (int row = 0; row < numOfRows; row++)
{

}
	
}
//***************************************************************************
// printPrices
//
// task: This procedure prints the table of prices
// data in: an array of floating point numbers and the number of rows
// and columns used.
// data out: none
//
//****************************************************************************
void printPrices(PriceType table, int numOfRows, int numOfCols)
{
cout << fixed << showpoint << setprecision(2);
for (int row = 0; row < numOfRows; row++)
{
	 for (int col = 0; col < numOfCols; col++)
{
	
}

}
}

Was This Post Helpful? 0
  • +
  • -

#4 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Re: 2 dimensional array

Posted 23 July 2009 - 10:06 PM

This looks like you want us to do your homework

READ THIS FIRST:

1. We will not do your homework for you! Do not ask us to give you code!
2. Use proper English. Correct grammar and spelling is required.
3. No Programming Questions in Introduce Yourself or the Caffeine Lounge
4. Use BBCode when posting. For example:
Paste Your Code Here

5. Give your topic a descriptive title, NOT "Help" or "Plz Help Me" or "I'm a noob"
6. Describe any errors you are encountering. Help us help you!
7. Search for your problem before posting a new topic (Example: c++ mortgage):

These are the rules
Was This Post Helpful? 0
  • +
  • -

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: 2 dimensional array

Posted 23 July 2009 - 10:39 PM

To store or print 2 dimensional arrays the best way is to use nested loops.

i.e
int table[2][2];

int temp = 0;

for(int i = 0; i < 2; ++i)
{
  for(int j = 0; j < 2; ++j)
  {
	cout << "Enter a value for [" << i+1 << "][" << j+1 << "]: ";
	cin >> temp;
	table[i][j] = temp;
  }
}



Use the same method for printing a 2 dimensional array

BTW: In your case you need to pass the array in for getting the values by reference so the inputted values will be placed into the original array and not a copy.

This post has been edited by eker676: 23 July 2009 - 10:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1