User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,425 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,888 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2187 | Replies: 1
Reply
Join Date: Feb 2005
Posts: 7
Reputation: DaveSS is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
DaveSS DaveSS is offline Offline
Newbie Poster

Help Need Help Revising Array program: Visual C++

  #1  
Mar 22nd, 2005
i got the program to work without any errors before i started to try to get the sort function to work in the main. function.

I need to get the function call for the sortIndex.cpp to work so i can sort the user output by descending order of profit.
The sort function has 5 arguments and i am getting confused on how to get it to work right.

I also need to get the numbers that print out the Average to have only two places after the decimal. The last problem is getting the columns of numbers in the tables to line up.

The errors im getting are:
G:\CSIS240\LAB 04\main.cpp(115) : error C2660: 'sortIndex' : function does not take 3 parameters i know there are 5 paramaters
sortIndex.cpp
G:\CSIS240\LAB 04\sortIndex.cpp(9) : error C2082: redefinition of formal parameter 'k'


Here is my code:

   // Output needs to be sorted 
// Need to document function arguments and return values

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <iomanip>

using std::setprecision; // sets numeric output precision
using std::setw;


float boxSurfaceArea(float, float, float);
float calcTotal(float[], int);
float calcAverage (float[], int, int);
float boxCost (int, float);
float boxProfit (float, float);
float boxPrice (int, float, float);
void sortIndex (int[], float [], int, int);
int number = 0;


int main()
{
	const int arraySize = 25;
	int i, type, numberOfBoxes = 0, boxType[arraySize];
	int index[arraySize];
	float length[arraySize], width[arraySize], height[arraySize];
	float surfaceArea[arraySize];
	float cost[arraySize];
	float profit[arraySize];
	float price[arraySize];
	float hold[arraySize]; //just added

	

	do
	{
		cout << "Enter a box type ( 1 - 4) or 0 to exit: ";
		cin >> type;

		switch(type)
		{
			case 0:
				for(i = 0; i < numberOfBoxes; i++)
					index[i] = i;

				

				cout << "Type   Length   Width   Height  Area   Cost   Price  Profit"<< endl;
				for(i = 0; i < numberOfBoxes; i++)
					cout << setw(2) << boxType[index[i]]
					     << setw(8) << length[index[i]]
						 << setw(8) << width[index[i]]
						 << setw(8) << height[index[i]]
						 << setw(8) << surfaceArea[index[i]]
						 << setw(8) << cost[index[i]] 
						 << setw(8) << price[index[i]]
						 << setw(8) << profit[index[i]]
					     << endl; 
						 

				cout << "Totals" // calculates totals for box values
					 << setw(28)<< calcTotal(surfaceArea, numberOfBoxes)//area
					 << setw(8) << calcTotal(cost, numberOfBoxes) //cost
			   	     << setw(8) << calcTotal(price, numberOfBoxes)//price
					 << setw(8) << calcTotal(profit, numberOfBoxes)//profit 
					 << endl; 
				
				cout << "Average" //setprecision( 2 ) // calculates averages for box values
					 << setw(28) << calcAverage(surfaceArea, numberOfBoxes, number)//area
					 << setw(8) << calcAverage(cost, numberOfBoxes, number) //cost
					 << setw(8) << calcAverage(price, numberOfBoxes, number)//price
					 << setw(8) << calcAverage(profit, numberOfBoxes, number) //profit
					 << endl;
				break;
			case 1:
			case 2:
			case 3:
			case 4:
				boxType[numberOfBoxes] = type;

				cout << "Enter length: ";
				cin >> length[numberOfBoxes];

				cout << "Enter width: ";
				cin >> width[numberOfBoxes];

				cout << "Enter height: ";
				cin >> height[numberOfBoxes];

				surfaceArea[numberOfBoxes] 
					= boxSurfaceArea(length[numberOfBoxes], 
					                 width[numberOfBoxes],
									 height[numberOfBoxes]);
				cost[numberOfBoxes]
					=boxCost(boxType[numberOfBoxes], surfaceArea[numberOfBoxes]);
								//type					//area

				price[numberOfBoxes]
					=boxPrice(boxType [numberOfBoxes],surfaceArea[numberOfBoxes],cost [numberOfBoxes]);
							 //type						//area

				profit[numberOfBoxes]
					=boxProfit(cost[numberOfBoxes], price[numberOfBoxes]);

				
			sortIndex(profit [numberOfBoxes],hold[numberOfBoxes],number); //to sort by profit
					// int n - the number of values ; int k - sort type 1. ascending , 2.  descending


				numberOfBoxes++;
				break;
			default:
				cout << "\n\n***Invalid box type***\n" << endl;
		} // end switch

		number = number+1; // I couldn't figure out any other way to get the average to work

	} while (type != 0);

	return 0;
}    

 // Lab 4 - boxSurfaceArea Function


float boxSurfaceArea(float length, float width, float height) //function for area of box. 


{
	
	float surfaceArea = 2 * ( (length * width) 
					+ (width * height) 
					+ (length * height) );
	
	
	return surfaceArea;

} // end surfaceArea function




// Lab 4 - boxCost Function

				//type			//area
float boxCost (int type, float surfaceArea) 
{
	float cost = 0;
			//type
	switch (type)
	{

	case 1: 
			cost = (.11 * surfaceArea)//Wood cost
				+ (.09 * surfaceArea) //Labor cost
				 + (2.25); //Hardware cost 
			break;

	case 2: 
			cost = (.13 * surfaceArea)//Wood cost
				 + (.11 * surfaceArea) //Labor cost
				 + (3.15); //Hardware cost 
			break;

	case 3: 
			cost = (.14 * surfaceArea)//Wood cost
				 + (.26 * surfaceArea) //Labor cost
				 + (4.00); //Hardware cost 
			break;
	case 4: 
			cost = (.17 * surfaceArea)//Wood cost
				 + (.22 * surfaceArea) //Labor cost
				 + (4.95);//Hardware cost 
			break; 


	}		//type
	while (type !=0)

	return cost;
} // end Boxcost function


// Lab 4 - boxPrice Function


float boxPrice (int type, float area, float cost) //Function prototype

{
	
	float price = 0;

		switch (type) {

		case 1: 
			price = (cost + (.25 * area)); 
			break;

		case 2:
			price = (.50 * area) + 5.25;
			break;
		case 3: 
			price = cost + (.36 * area) + 7.50;
			break;

		case 4: 
			price = cost + (.32 * area) + 6.25;

			break;

	
	}

	
	return price;
} // end boxPrice function


// Lab 4 - Profit Function

float boxProfit (float cost, float price) //Function prototype
{
		
	float profit = 0;

	profit = ( price - cost);
	
	return profit;
} // end boxProfit function


//Function to calculate the average of a list of variables

float calcAverage (float a[], int n, int number)// float a[] - a list of values to total
								   // int n - number of values
{

	float total = 0;
	int numberOfBoxes = 0;
	 
	 for (int i =0; i < n; i++)
	 total+= a[i];
	 float average = (total / (number));

	return average;
}//end calcAverage function


// Function to calculate the total of a list of values.
float calcTotal (float a[], int n) 
// float a[] - a list of values to total
// int n - number of values
{
	 float total = 0;
	 
	 for (int i =0; i < n; i++)
	 total+= a[i];


	return total;
} // end calcTotal function

 // function to sort table of values

void sortIndex (int[], float a[], int n, int k)// int[] - a list of indexes that will be sorted with the sort values.
{											// float [] - the list of values to sort.
											// int n - the number of values ; int k - sort type 1. ascending , 2.  descending

int m;
int k = 0, number = j; 
float hold;	

if(number == 2)
{
   //ascending sort
  //k temprorary location of array in order swap
	for (m = 0; k < n; m++)
	{

	for  (k = 0; k < n; k++) // 
	{
			if ( a[k] > a[k+1] )
			{
				hold = a[k];
				a[k] = a[k+1];
				a[k+1] = hold;
			} // end if
	}//end for
	
	}//end for
}

if(number == 1)
{
   //descending sort
	for (m = 0; k < n; m++)
	{
	for  (k = 0; k < n; k++) // 
	{
			if ( a[k] < a[k+1] )
			{
				hold = a[k];
				a[k] = a[k+1];
				a[k+1] = hold;
			} // end if
	}//end for
	} //end for
} //end if num == 1
	
} 
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,470
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need Help Revising Array program: Visual C++

  #2  
Mar 22nd, 2005
int number = 0;
   float profit[arraySize];
   float hold[arraySize]; //just added
         sortIndex(profit [numberOfBoxes],hold[numberOfBoxes],number); //to sort by profit
void sortIndex (int[], float a[], int n, int k)// int[] - a list of indexes that will be sorted with the sort values.
{                                // float [] - the list of values to sort.
                                 // int n - the number of values ; int k - sort type 1. ascending , 2.  descending
Call your function like you've defined it.

Regarding the redefinition...
void sortIndex (int[], float a[], int n, int k)// int[] - a list of indexes that will be sorted with the sort values.
{                                // float [] - the list of values to sort.
                                 // int n - the number of values ; int k - sort type 1. ascending , 2.  descending

   int m;
   int k = 0, number = j;
Which k is k?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:29 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC