Need Help Revising Array program: Visual C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 7
Reputation: DaveSS is an unknown quantity at this point 
Solved Threads: 0
DaveSS DaveSS is offline Offline
Newbie Poster

Need Help Revising Array program: Visual C++

 
0
  #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:

  1. // Output needs to be sorted
  2. // Need to document function arguments and return values
  3.  
  4. #include <iostream>
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8.  
  9. #include <iomanip>
  10.  
  11. using std::setprecision; // sets numeric output precision
  12. using std::setw;
  13.  
  14.  
  15. float boxSurfaceArea(float, float, float);
  16. float calcTotal(float[], int);
  17. float calcAverage (float[], int, int);
  18. float boxCost (int, float);
  19. float boxProfit (float, float);
  20. float boxPrice (int, float, float);
  21. void sortIndex (int[], float [], int, int);
  22. int number = 0;
  23.  
  24.  
  25. int main()
  26. {
  27. const int arraySize = 25;
  28. int i, type, numberOfBoxes = 0, boxType[arraySize];
  29. int index[arraySize];
  30. float length[arraySize], width[arraySize], height[arraySize];
  31. float surfaceArea[arraySize];
  32. float cost[arraySize];
  33. float profit[arraySize];
  34. float price[arraySize];
  35. float hold[arraySize]; //just added
  36.  
  37.  
  38.  
  39. do
  40. {
  41. cout << "Enter a box type ( 1 - 4) or 0 to exit: ";
  42. cin >> type;
  43.  
  44. switch(type)
  45. {
  46. case 0:
  47. for(i = 0; i < numberOfBoxes; i++)
  48. index[i] = i;
  49.  
  50.  
  51.  
  52. cout << "Type Length Width Height Area Cost Price Profit"<< endl;
  53. for(i = 0; i < numberOfBoxes; i++)
  54. cout << setw(2) << boxType[index[i]]
  55. << setw(8) << length[index[i]]
  56. << setw(8) << width[index[i]]
  57. << setw(8) << height[index[i]]
  58. << setw(8) << surfaceArea[index[i]]
  59. << setw(8) << cost[index[i]]
  60. << setw(8) << price[index[i]]
  61. << setw(8) << profit[index[i]]
  62. << endl;
  63.  
  64.  
  65. cout << "Totals" // calculates totals for box values
  66. << setw(28)<< calcTotal(surfaceArea, numberOfBoxes)//area
  67. << setw(8) << calcTotal(cost, numberOfBoxes) //cost
  68. << setw(8) << calcTotal(price, numberOfBoxes)//price
  69. << setw(8) << calcTotal(profit, numberOfBoxes)//profit
  70. << endl;
  71.  
  72. cout << "Average" //setprecision( 2 ) // calculates averages for box values
  73. << setw(28) << calcAverage(surfaceArea, numberOfBoxes, number)//area
  74. << setw(8) << calcAverage(cost, numberOfBoxes, number) //cost
  75. << setw(8) << calcAverage(price, numberOfBoxes, number)//price
  76. << setw(8) << calcAverage(profit, numberOfBoxes, number) //profit
  77. << endl;
  78. break;
  79. case 1:
  80. case 2:
  81. case 3:
  82. case 4:
  83. boxType[numberOfBoxes] = type;
  84.  
  85. cout << "Enter length: ";
  86. cin >> length[numberOfBoxes];
  87.  
  88. cout << "Enter width: ";
  89. cin >> width[numberOfBoxes];
  90.  
  91. cout << "Enter height: ";
  92. cin >> height[numberOfBoxes];
  93.  
  94. surfaceArea[numberOfBoxes]
  95. = boxSurfaceArea(length[numberOfBoxes],
  96. width[numberOfBoxes],
  97. height[numberOfBoxes]);
  98. cost[numberOfBoxes]
  99. =boxCost(boxType[numberOfBoxes], surfaceArea[numberOfBoxes]);
  100. //type //area
  101.  
  102. price[numberOfBoxes]
  103. =boxPrice(boxType [numberOfBoxes],surfaceArea[numberOfBoxes],cost [numberOfBoxes]);
  104. //type //area
  105.  
  106. profit[numberOfBoxes]
  107. =boxProfit(cost[numberOfBoxes], price[numberOfBoxes]);
  108.  
  109.  
  110. sortIndex(profit [numberOfBoxes],hold[numberOfBoxes],number); //to sort by profit
  111. // int n - the number of values ; int k - sort type 1. ascending , 2. descending
  112.  
  113.  
  114. numberOfBoxes++;
  115. break;
  116. default:
  117. cout << "\n\n***Invalid box type***\n" << endl;
  118. } // end switch
  119.  
  120. number = number+1; // I couldn't figure out any other way to get the average to work
  121.  
  122. } while (type != 0);
  123.  
  124. return 0;
  125. }

  1. // Lab 4 - boxSurfaceArea Function
  2.  
  3.  
  4. float boxSurfaceArea(float length, float width, float height) //function for area of box.
  5.  
  6.  
  7. {
  8.  
  9. float surfaceArea = 2 * ( (length * width)
  10. + (width * height)
  11. + (length * height) );
  12.  
  13.  
  14. return surfaceArea;
  15.  
  16. } // end surfaceArea function
  17.  
  18.  
  19.  
  20.  
  21. // Lab 4 - boxCost Function
  22.  
  23. //type //area
  24. float boxCost (int type, float surfaceArea)
  25. {
  26. float cost = 0;
  27. //type
  28. switch (type)
  29. {
  30.  
  31. case 1:
  32. cost = (.11 * surfaceArea)//Wood cost
  33. + (.09 * surfaceArea) //Labor cost
  34. + (2.25); //Hardware cost
  35. break;
  36.  
  37. case 2:
  38. cost = (.13 * surfaceArea)//Wood cost
  39. + (.11 * surfaceArea) //Labor cost
  40. + (3.15); //Hardware cost
  41. break;
  42.  
  43. case 3:
  44. cost = (.14 * surfaceArea)//Wood cost
  45. + (.26 * surfaceArea) //Labor cost
  46. + (4.00); //Hardware cost
  47. break;
  48. case 4:
  49. cost = (.17 * surfaceArea)//Wood cost
  50. + (.22 * surfaceArea) //Labor cost
  51. + (4.95);//Hardware cost
  52. break;
  53.  
  54.  
  55. } //type
  56. while (type !=0)
  57.  
  58. return cost;
  59. } // end Boxcost function
  60.  
  61.  
  62. // Lab 4 - boxPrice Function
  63.  
  64.  
  65. float boxPrice (int type, float area, float cost) //Function prototype
  66.  
  67. {
  68.  
  69. float price = 0;
  70.  
  71. switch (type) {
  72.  
  73. case 1:
  74. price = (cost + (.25 * area));
  75. break;
  76.  
  77. case 2:
  78. price = (.50 * area) + 5.25;
  79. break;
  80. case 3:
  81. price = cost + (.36 * area) + 7.50;
  82. break;
  83.  
  84. case 4:
  85. price = cost + (.32 * area) + 6.25;
  86.  
  87. break;
  88.  
  89.  
  90. }
  91.  
  92.  
  93. return price;
  94. } // end boxPrice function
  95.  
  96.  
  97. // Lab 4 - Profit Function
  98.  
  99. float boxProfit (float cost, float price) //Function prototype
  100. {
  101.  
  102. float profit = 0;
  103.  
  104. profit = ( price - cost);
  105.  
  106. return profit;
  107. } // end boxProfit function
  108.  
  109.  
  110. //Function to calculate the average of a list of variables
  111.  
  112. float calcAverage (float a[], int n, int number)// float a[] - a list of values to total
  113. // int n - number of values
  114. {
  115.  
  116. float total = 0;
  117. int numberOfBoxes = 0;
  118.  
  119. for (int i =0; i < n; i++)
  120. total+= a[i];
  121. float average = (total / (number));
  122.  
  123. return average;
  124. }//end calcAverage function
  125.  
  126.  
  127. // Function to calculate the total of a list of values.
  128. float calcTotal (float a[], int n)
  129. // float a[] - a list of values to total
  130. // int n - number of values
  131. {
  132. float total = 0;
  133.  
  134. for (int i =0; i < n; i++)
  135. total+= a[i];
  136.  
  137.  
  138. return total;
  139. } // end calcTotal function

  1. // function to sort table of values
  2.  
  3. void sortIndex (int[], float a[], int n, int k)// int[] - a list of indexes that will be sorted with the sort values.
  4. { // float [] - the list of values to sort.
  5. // int n - the number of values ; int k - sort type 1. ascending , 2. descending
  6.  
  7. int m;
  8. int k = 0, number = j;
  9. float hold;
  10.  
  11. if(number == 2)
  12. {
  13. //ascending sort
  14. //k temprorary location of array in order swap
  15. for (m = 0; k < n; m++)
  16. {
  17.  
  18. for (k = 0; k < n; k++) //
  19. {
  20. if ( a[k] > a[k+1] )
  21. {
  22. hold = a[k];
  23. a[k] = a[k+1];
  24. a[k+1] = hold;
  25. } // end if
  26. }//end for
  27.  
  28. }//end for
  29. }
  30.  
  31. if(number == 1)
  32. {
  33. //descending sort
  34. for (m = 0; k < n; m++)
  35. {
  36. for (k = 0; k < n; k++) //
  37. {
  38. if ( a[k] < a[k+1] )
  39. {
  40. hold = a[k];
  41. a[k] = a[k+1];
  42. a[k+1] = hold;
  43. } // end if
  44. }//end for
  45. } //end for
  46. } //end if num == 1
  47.  
  48. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #2
Mar 22nd, 2005
  1. int number = 0;
  2. float profit[arraySize];
  3. float hold[arraySize]; //just added
  1. sortIndex(profit [numberOfBoxes],hold[numberOfBoxes],number); //to sort by profit
  1. void sortIndex (int[], float a[], int n, int k)// int[] - a list of indexes that will be sorted with the sort values.
  2. { // float [] - the list of values to sort.
  3. // 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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC