Fist of, I'm new hew as the post count shows, so hello. I had this homework where we had to make a program that works with arrays. With that there were little to no problems, but I wanted to make a function for the selection of the option in the switch and was unable to. It wasn't necessary, but it left me curious. Any help? Here's the code. I may have deleted some parts by mistake when getting rid of some of the documentation, professor wants us to be really extensive with those, even if redundant. Also, what would you have done differently?

// 
// CCOM 3033
// Asig #3



// Library
#include <iostream>
#include <string>
#include <cmath>



using namespace std ;  




// Prototypes
void display_instructions () ;					// Prototype for displaying instructions                  
void display_menu () ;							// Prototype for displaying the menu                
void insert_array (float[] , int) ;             // Prototype for the insert array function          
void display_array (float[] , int) ;            // Prototype for displaying the inserted array        
void average_array (float[] , int) ;           // Prototype for calculating and displaying the average value of an array    
void standard_dev_array (float[] , int) ;       // Prototype for calculating and displaying the standard deviation of an array
void minmax_array (float[] , int) ;             // Prototype for determining the minimum and maximum values of an array

													


// Main Function
int main () {
char option;

	display_instructions() ;                       
	
	
	while (option != '6') { 
		display_menu() ;					    	
		[B]cin	>>	option	;    <--------- Part in question	[/B]						
	
	
		switch (option) {
			float array[5];							
				case '1':
				insert_array(array,5);				
				break;
				
				case '2':
				display_array(array,5);				
				break;
				
				case '3':
				average_array(array,5);				
				break;
				
				case '4':
				standard_dev_array(array,5);		
				break;								
				
				case '5':
				minmax_array(array,5);				
				break;
		}
	}
}



// Funtion definitions 

void display_instructions () {								
	cout << "	" << endl ;
	cout << "This program is dedicated to " ;
	cout << "working with \narrays" << endl ;
	cout << "	" << endl ;
}



void display_menu () {										
	cout << "\t Menu:  " << endl ;
	cout << "\t\t1. Insert array  " << endl ;
	cout << "\t\t2. Display arrray  " << endl ;
	cout << "\t\t3. Average " << endl ;
	cout << "\t\t4. Standard deviation  " << endl ;
	cout << "\t\t5. Min and Max  " << endl ;
	cout << "\t\t6. Exit  " << endl ;
	cout << "\t\tChoose your desired "; 
	cout << "operation: " << endl ;
}







void insert_array(float array[], int size) {				
	cout << "	" << endl ;
	cout << "Insert 5 decimal numbers:" << endl;
	for (int i = 0 ; i<5 ; i++ ) {
		cin >> array[i] ;									
	}
}



void display_array(float array[], int size) {				
	cout << "	" << endl ;
	cout << "The values inserted were:" << endl;
	for (int i=0 ; i < 5 ; i++ ) {
		cout << array[i] << " " ;							
	}
	cout << "	" << endl ;

}



void average_array (float array[] , int size) {		    	
	float average;											
	float sum=0;
	for (int i=0 ; i<5 ; i++) {
		sum += array[i] ;									
	}
	average = sum/size;										
	cout << "	" << endl ;
	cout << "The average is: "<< sum/size ;					
	cout << "	" << endl ;
}



void standard_dev_array (float array[] , int size) {		
	float average , sum , x ;
	for (int i=0 ; i<5 ; i++) {
		sum += array[i] ;									
	}
	average = sum/size;										
	for (int i=0 ; i<5 ; i++) {						
			x += pow(array[i]-average , 2) ;					
		}
	cout << " The standard deviation is: " << sqrt(x/5) ;
	cout << "	" << endl ;

}



void minmax_array (float array[] , int size) {				
	float min = array[0] ;									
	float max = array[0] ;									
	for (int i=0 ; i<5 ; i++) {
		if (array[i] < min) {
			min = array[i];									
		}
	}
	for (int i=0 ; i<5 ; i++) {
		if (array[i] > max)  {
			max = array[i] ;								
		}
	}
	cout << "	" << endl ;
	cout << "The minimum value is: " <<  min << ", and ";	// Displays min
	cout << "the maximum value is: " << max << endl ;		// Displays max
	cout << "	" << endl ;

}

Well, for starters I would've used code tags when posting the code! heh heh!

Other than that, I can see three main errors in the code:
1. The variable 'option' is uninitialised before it is used in the while loop in main. So I'd recommend initialising it to something..Anything will do, as long as it isn't '6'!

2. and 3. The variables sum and x in your standard_dev_array() function are also uninitialised before they are used by the += operator, both of these should be initialised at declaration too.

Other than that it should work OK. Perhaps tidying the output to the screen may be the only tweaks you want to make to this!

I've just double checked your original code with VS2008 and the 3 errors I spotted were the only problems it found too!

Here's your code again. I've put additional comments next to the most relevant bits.

// 
// CCOM 3033
// Asig #3



// Library
#include <iostream>
#include <string>
#include <cmath>



using namespace std ;  




// Prototypes
void display_instructions () ;					// Prototype for displaying instructions                  
void display_menu () ;							// Prototype for displaying the menu                
void insert_array (float[] , int) ;             // Prototype for the insert array function          
void display_array (float[] , int) ;            // Prototype for displaying the inserted array        
void average_array (float[] , int) ;           // Prototype for calculating and displaying the average value of an array    
void standard_dev_array (float[] , int) ;       // Prototype for calculating and displaying the standard deviation of an array
void minmax_array (float[] , int) ;             // Prototype for determining the minimum and maximum values of an array

													


// Main Function
int main () {
char option='0';             // <------- Option initialised

	display_instructions() ;                       
	
	
	while (option != '6') {  // <-------- avoiding a problem here
		display_menu() ;					    	
		cin	>>	option	;    //<--------- This should now work!							
	
	
		switch (option) {
			float array[5];				
				case '1':
				insert_array(array,5);				
				break;
				
				case '2':
				display_array(array,5);				
				break;
				
				case '3':
				average_array(array,5);				
				break;
				
				case '4':
				standard_dev_array(array,5);		
				break;								
				
				case '5':
				minmax_array(array,5);				
				break;
		}
	}
}



// Funtion definitions 

void display_instructions () {								
	cout << "	" << endl ;
	cout << "This program is dedicated to " ;
	cout << "working with \narrays" << endl ;
	cout << "	" << endl ;
}



void display_menu () {										
	cout << "\t Menu:  " << endl ;
	cout << "\t\t1. Insert array  " << endl ;
	cout << "\t\t2. Display arrray  " << endl ;
	cout << "\t\t3. Average " << endl ;
	cout << "\t\t4. Standard deviation  " << endl ;
	cout << "\t\t5. Min and Max  " << endl ;
	cout << "\t\t6. Exit  " << endl ;
	cout << "\t\tChoose your desired "; 
	cout << "operation: " << endl ;     // <---- perhaps lose this endl??
}







void insert_array(float array[], int size) {				
	cout << "	" << endl ;
	cout << "Insert 5 decimal numbers:" << endl;
	for (int i = 0 ; i<5 ; i++ ) {
		cin >> array[i] ;									
	}
}



void display_array(float array[], int size) {				
	cout << "	" << endl ;
	cout << "The values inserted were:" << endl;
	for (int i=0 ; i < 5 ; i++ ) {
		cout << array[i] << " " ;							
	}
	cout << "	" << endl ;

}



void average_array (float array[] , int size) {		    	
	float average;											
	float sum=0;
	for (int i=0 ; i<5 ; i++) {
		sum += array[i] ;									
	}
	average = sum/size;										
	cout << "	" << endl ;
	cout << "The average is: "<< sum/size ;					
	cout << "	" << endl ;
}



void standard_dev_array (float array[] , int size) {		
	float average , sum=0.0 , x=0.0f ;   // <<------- Sum and x initialised here..
	for (int i=0 ; i<5 ; i++) {
		sum += array[i] ;                // <<------- avoids a problem here									
	}
	average = sum/size;										
	for (int i=0 ; i<5 ; i++) {						
			x += pow(array[i]-average , 2) ; // <<--- and here!					
		}
	cout << " The standard deviation is: " << sqrt(x/5) ;
	cout << "	" << endl ;

}



void minmax_array (float array[] , int size) {				
	float min = array[0] ;									
	float max = array[0] ;									
	for (int i=0 ; i<5 ; i++) {
		if (array[i] < min) {
			min = array[i];									
		}
	}
	for (int i=0 ; i<5 ; i++) {
		if (array[i] > max)  {
			max = array[i] ;								
		}
	}
	cout << "	" << endl ;
	cout << "The minimum value is: " <<  min << ", and ";	// Displays min
	cout << "the maximum value is: " << max << endl ;		// Displays max
	cout << "	" << endl ;

}

Cheers for now,
Jas.

Thanks for the tips and help, newb mistake regarding post structure, I'll keep those things in mind for the next posts. Also, sorry for the late reply, and subsequent unnecessary bump to an old thread.

Edited 6 Years Ago by acos.carlos: n/a

This article has been dead for over six months. Start a new discussion instead.