Hi everybody
I wrote this code in my university assignment . But I want to change the global variable (the variables that above the main) to local variable (inside the main) and add the parameters in the functions.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int const n = 7;
int code [ n ] = {11, 10, 22, 32, 41, 55, 01};
double price [ n ] = {600, 245, 105, 120, 350, 135, 100};
double sale [ n ] = {0, 0, 0, 0, 0, 0, 0};
string name[ n ] = {"Silicate","Sulfate","Carbonate","Phosphate","Sulfide","Glass","Plastic"};
void sort_sale ( void ); // sort the arrays on sale
void calculate_sale ( void ); // calculate the sale vaue for each product
void print_sale ( void ); // print the arrays
int index ( int ); // locate a given product in treh array
int main() {
calculate_sale ();
sort_sale ();
print_sale ();
system("pause");
} // end main
void calculate_sale ( void ) {
int quantity, productId, i;
print_sale ();
cout << "Enter product id ";
cin >> productId;
i = index (productId );
while ( i != -1 ) {
cout << "Enter quantity ";
cin >> quantity;
sale [ i ] += quantity * price [ i ];
print_sale ();
cout << "Enter product id ";
cin >> productId;
i = index (productId );
} // end while
} // end calculate_sale
int index ( int c ) {
for ( int i=0; i < n; i++ )
if ( code [ i ] == c )
return i;
return -1;
} // end index
void print_sale ( void ) {
for (int i=0; i < n; i++)
cout << setw(8) << code[i] << setw(15) << name[i] << setw(15)
<< price[i] << setw(8) << sale[i] << endl;
cout << endl;
} // end print_sale
void sort_sale ( void ) {
for ( int i = 0; i < n-1 ; i++ )
for (int j=i+1; j < n; j++ )
if (sale [ j ] < sale [ i ]) {
double temp1 = sale [ j ];
sale [ j ] = sale [ i ];
sale [ i ] = temp1;
int temp2 = code [ j ];
code [ j ] = code [ i ];
code [ i ] = temp2;
double temp3 = price [ j ];
price [ j ] = price [ i ];
price [ i ] = temp3;
string temp4 = name [ j ];
name [ j ] = name [ i ];
name [ i ] = temp4;
}
} // end sort_sale