9 Replies - 2812 Views - Last Post: 07 March 2010 - 07:20 PM Rate Topic: -----

#1 cyfuzhi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Pass 2D array into function

Posted 06 March 2010 - 11:53 PM

Hi all,
I'm having trouble getting the nested loop to work correctly. The first for loop is not repeating the "Enter the sales for year" statement.

const unsigned int MONTHS = 12;
const unsigned int YEARS = 3;

int main()								
{										
	displayHeader();
	float yearlyArray[12][3];
	inputAllSales(yearlyArray, MONTHS, YEARS);
	system("pause");											
	return 0;													
}

void inputAllSales(float monthlySales[12][3], const unsigned int numberOfRows, const unsigned int numberofColumns)
{
	char* p_months[MONTHS] =
	{
		"January", "February", "March", "April", "May", 
		"June", "July", "August", "September", "October",
		"November", "December"
	};	
	
	for (int year = 0; year < YEARS; year++)
	{	
		cout << "Enter the sales for year " << year << ":\n";
			for (int month = 0; month <= MONTHS; month++)
			{	
				cout << p_months[month] << " sales were:\t$";
				float monthlyAmount[MONTHS];
				cin >> monthlyAmount[month];
			}
	}
	return;
}



Anyone able to help me out with this?

Is This A Good Question/Topic? 0
  • +

Replies To: Pass 2D array into function

#2 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Pass 2D array into function

Posted 07 March 2010 - 12:15 AM

Of course it doesn't, because you prompt the user to enter "year" inside the loop. I'd move that cout << outside the loop, like this:
int year; // declare "year" first
cout << "Enter the sales for year " << year << ":\n";
for (int i = 0; i < year; i++) // take another counter, year is already used
{       
    // is month <= MONTHS correct? I think it should be month < MONTHS
    for (int month = 0; month <= MONTHS; month++)
    { 
       // more code below
    }
}


This post has been edited by sarmanu: 07 March 2010 - 12:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 cyfuzhi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: Pass 2D array into function

Posted 07 March 2010 - 12:54 AM

Actually leaving the cout within the first for loop is fine.

It was the <= in the second for loop's test expression that caused it to error out.

Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#4 cyfuzhi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: Pass 2D array into function

Posted 07 March 2010 - 12:38 PM

Hi all, now i'm having trouble calculating the 2D array after the values are inputted. Here's the code:

/*****************************************************************
	predefined code to include in program
*****************************************************************/
#include <iostream>		
#include <iomanip>		
using namespace std;	        

/*****************************************************************
	prototype of the function(s) included in the program
*****************************************************************/
				
void inputAllSales(float monthlySales[12][3], const unsigned int numberOfRows, 
				   const unsigned int numberOfColumns);

void calculateAndDisplayTotalSales(float monthlySales[12][3], const unsigned int 
		numberOfRows, const unsigned int numberOfColumns);

/*****************************************************************
	main() function definition
*****************************************************************/

const unsigned int MONTHS = 12;
const unsigned int YEARS = 3;

int main()								
{										
	float monthlyArray[12][3];
	inputAllSales(monthlyArray, MONTHS, YEARS);
	calculateAndDisplayTotalSales(monthlyArray, MONTHS, YEARS);
	system("pause");											
	return 0;													
}																			

void inputAllSales(float monthlySales[12][3], const unsigned int numberOfRows, 
				   const unsigned int numberofColumns)
{
	const char* p_months[MONTHS] =
	{
		"January", "February", "March", "April", "May", 
		"June", "July", "August", "September", "October",
		"November", "December"
	};	
	
	for (int year = 0; year < YEARS; year++)
	{		
		cout << "\nEnter the sales for year " << year << ":\n";
		for (int month = 0; month < MONTHS; month++)
			{	
				cout << p_months[month] << " sales were:\t" << "$";
				float monthlyAmount[MONTHS];
				cin >> monthlyAmount[month];
			}
	}
	return;
}

void calculateAndDisplayTotalSales(float monthlySales[12][3], const unsigned int 
		numberOfRows, const unsigned int numberOfColumns)
{
	float totalYearlySales[MONTHS];
	for (int year = 0; year < YEARS; year++)
	{
		cout << "\n\nYear " << year << " sales were $" << totalYearlySales[year];
		for (int month = 0; month < MONTHS; month++)
		{
			int monthlyAmount[MONTHS];
			totalYearlySales[year] += monthlyAmount[month];
		}
	}
	return;
}


Was This Post Helpful? 0
  • +
  • -

#5 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: Pass 2D array into function

Posted 07 March 2010 - 12:40 PM

What kind of trouble do you have? please mention the details of your problem when you have a question :)
Was This Post Helpful? 0
  • +
  • -

#6 cyfuzhi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: Pass 2D array into function

Posted 07 March 2010 - 12:51 PM

Anarion,
The values for each month that are being inputted from the function inputAllSales are not being added at the end of years 0, 1, and 2 in the function calculateAndDisplayTotalSales.
Was This Post Helpful? 0
  • +
  • -

#7 Munawwar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: Pass 2D array into function

Posted 07 March 2010 - 01:09 PM

Check the comments.
void inputAllSales(float monthlySales[12][3], const unsigned int numberOfRows, 
                                   const unsigned int numberofColumns)
{
...
...
        for (int year = 0; year < YEARS; year++)
        {               
                cout << "\nEnter the sales for year " << year << ":\n";
                for (int month = 0; month < MONTHS; month++)
                        {       
                                cout << p_months[month] << " sales were:\t" << "$";
                                float monthlyAmount[MONTHS];  //If you declare this array here...
                                cin >> monthlyAmount[month];  
                        }
                //...then you cannot access it from here.
                //You cannot do (say) monthlyAmount[0]=10; It will give you an error.
        }
        return;
}



So what do you do? Since what you are trying to ask user for input, use the monthlySales array which you sent as argument :
void inputAllSales(float monthlySales[12][3], const unsigned int numberOfRows, 
                                   const unsigned int numberofColumns)
{
...
...
         for(int year=0; year<3;year++)
                for (int month = 0; month < MONTHS; month++)
                        {       
                                cout << p_months[month] << " sales were:\t" << "$";
                                cin >> monthlySales[month][year];  
                        }
...
...


You also have done a similar mistake in the calculateAndDisplayTotalSales function. Hope this helps.

EDIT: oops! cin >> monthlySales[month]; was incorrect.I changed it.

This post has been edited by Munawwar: 08 March 2010 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

#8 cyfuzhi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: Pass 2D array into function

Posted 07 March 2010 - 04:28 PM

Ok, so I've made some adjustments and tried nested loops in the calculate function but the output is still not working properly. Do I need to set totalYearlySales as additional variables or an array?

void inputAllSales(float monthlySales[12][3], const unsigned int numberOfRows, 
				   const unsigned int numberofColumns)
{
	const char* p_months[MONTHS] =
	{
		"January", "February", "March", "April", "May ", 
		"June", "July", "August", "September", "October",
		"November", "December"
	};	
	
	for (int year = 0; year < YEARS; year++)
	{		
		cout << "\nEnter the sales for year " << year << ":\n";
		for (int month = 0; month < MONTHS; month++)
			{	
				cout << p_months[month] << " sales were:\t" << "$";
				cin >> monthlySales[MONTHS][YEARS];
			}
	}
	return;
}

void calculateAndDisplayTotalSales(float monthlySales[12][3], const unsigned int 
		numberOfRows, const unsigned int numberOfColumns)
{
	double totalYearlySales = 0.0;
	for (int month = 0; month < MONTHS; month++)
		{
			totalYearlySales += monthlySales[MONTHS][YEARS];
		}
	for (int year = 0; year < YEARS; year++)
	{
		cout << "\n\nYear " << year << " sales were $" << totalYearlySales; 
	}
	return;
}


Was This Post Helpful? 0
  • +
  • -

#9 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Pass 2D array into function

Posted 07 March 2010 - 06:28 PM

when passing a 2D array there is no need to pass in the the rows subscript, you could simplify it to this
void inputAllSales(float monthlySales[][3], const unsigned int numberOfRows, 
                                   const unsigned int numberofColumns)



or your can use a
float **monthlySales 

This post has been edited by ImaSexy: 07 March 2010 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#10 cyfuzhi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: Pass 2D array into function

Posted 07 March 2010 - 07:20 PM

yea, i'm dumb, finally figured it out, i mixed up my constants and array elements together.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1