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

Recommended Answers

All 11 Replies

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.

Ummm, OK. So do it...

I tried to do it , but the program didn't run

you can see below that I moved the variables inside the main , but what the parameters should I put in the functions to make the program run correctly ?

#include <iostream>
#include <string>
#include <iomanip> 
using namespace std;


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() {

	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"};

    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

please any suggestions :?:

Your almost there. All you have to do is set the parameter to the function, for example you can do this :

void sort_sale(double sale[], int n);
int main(){
 const int n = 7;
 //...
 double sale [ n ] = {0, 0, 0, 0, 0, 0, 0};
 sort_sale(sale,n);
 
}

You don't have to change much, just add the correct parameter to each function.

Your almost there. All you have to do is set the parameter to the function, for example you can do this :

void sort_sale(double sale[], int n);
int main(){
 const int n = 7;
 //...
 double sale [ n ] = {0, 0, 0, 0, 0, 0, 0};
 sort_sale(sale,n);
 
}

You don't have to change much, just add the correct parameter to each function.

Arrays are pointers. Send an appropriate pointer. You will also need an integer counter.

void sampleFunction(const int *, const int); //function prototype

int main() {
  const int SIZE = 10;                       //a constant to control the array size
  int myArray[SIZE] = {0};                   //the array

  sampleFunction(myArray, SIZE);             //call your function

  for (int i = 0; i < SIZE; ++i) {           //output the array
    cout << "Element " << i << ": " << myArray[i] << endl;
  }

  cin.get();

  return 0;
}

void sampleFunction(int *arrayIn, const int arraySize) {
  for (int j = 0; j < arraySize; ++j) {
    arrayIn[j] = i;
  }
}  //end function

Thanks everyone answered me . But I don't know which variables should I put as parameters .

Look at all the variables that are used by your functions and put them as parameters.

One (retarded) way to do this is to try and compile the code without any parameters to the functions. Then the compiler will throw a bunch of errors like "ErrorZZZZ: in function 'yyyy': identifier xxxx not declared in this scope" which means the function 'yyyy' uses a variable 'xxxx' that used to be global and now needs to be a parameter to the function. Just add all such variables that are reported and repeat until in compiles successfully.

If a variable was global before and you used it in a function, you must now make that variable a parameter. It's quite simple really. One example would be your index() function:

int index ( int c ) {
    for ( int i=0; i < n; i++ )
          if ( code [ i ] == c ) 
	      return i;
    return -1; 
} // end index

This used the global array "code". Since "code" is no longer global, it must now be a parameter.

ps. sorry about the bad example code. I tried to fix it, but ran out of time.

Thanks everybody , I solved it correctly .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.