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