/****************************************************** ** Name: ** Filename: arrays.cpp ** Project #: Deitel & Deitel 4.8 ** Project Description: Write a program that implements the 6 items of problem 4.8 as functions that are called from main. Declare/Initialize arrays in main and pass the array to the function. ** Output: The function selected to be performed. ** Input: Select which function to be performed a) Display the value of the seventh element of character array f. b) Input a value into element 4 of single-subscripted floating point array b. c) Initialize each of the 5 elements of single-subscripted integer array g to 8. d) Total and print the elements of floating-point array c of 100 elements. e) Copy array a into the first portion of array b. Assume double a[ 11 ], b[ 34 ]. f) Determine and print the smallest and largest values contained in 99-element floating-point array w. ** Algorithm: Beginning statement with program purpose. Input statement for user to input number of function to be performed and seen. Create a switch for selections. Create function for each statement In switch have the case call up the function. User inputs selection. Selection goes in switch and calls appropriate function. Function is displayed. If users selects -1 exit switch. End program ******************************************************/ // Include files #include #include #include #include #include using namespace std; // Global Type Declarations // Function Prototypes void instruct (void); void pause (); void A(char []); //Function A void B(float []); //Function B void C(int []); //Function C void D(float []); //Function D void E(double [] , double []); //Function E void F(float []); //Function F //Global Variables - should not be used without good reason. int main () { // Declaration section srand (time (0) ); int function; //Function to be selected char f[8] = {'a','b','c','d', 'e','f','g','h'}; //Char array f(Used in function A) int g[5]; //Integer array g(Used in function C) float b[3] = { 5,4,3 }, //Float array b(Used in function B) c[100], //Float array c(Used in function D) w[99]; //Float array w(Used in function F) double a[11] = {1,2,3,4,5,6, 7,8,9,10,11}, //Double array a(Used in function E) bb[34]; //Double array bb(Used in function E) // Executable section instruct (); cout << "-------------------------------------------------------" << "-------------------------" << "\t\t Select which function to be performed \n" << "1) Display the value of the seventh element of character" << " array f.\n" << "2) Input a value into element 4 of single-subscripted " << "floating point array b.\n" << "3) Initialize each of the 5 elements of single-subscripted" << " integer array g to 8." << "4) Total and print the elements of floating-point array " << "c of 100 elements.\n" << "5) Copy array a into the first portion of array b. Assume" << " double a[11], b[34].\n" << "6) Determine and print the smallest and largest values" << " contained in 99-element \n floating-point array w. " << "\n-----------------------------------------------------" << "---------------------------\n" << "Select function to be performed : "; cin >> function; while( function != -1 ){ switch( function ) { case 1: //Calls Function A cout << "\nThe value of the seventh element of character " << "array f is "; A(f); cout << "\n" << endl; break; case 2: //Calls Function B cout << "\nThe first three elements of array b: \n" << "b[0] = " << b[0] << "\n" << "b[1] = " << b[1] << "\n" << "b[2] = " << b[2] << "\n" << "Input the number for the four array: "; B(b); cout << "The fourth element of array b is: " << "b[3] = " << b[3] << "\n" << endl; break; case 3: //Calls Function C cout << "\nThe five elements of single-subscripted integer " << "array g: \n"; C(g); break; case 4: //Calls Function D cout << "\nThe total of 100 elements of array c.\n"; D(c); break; case 5: //Calls Function E cout << "The 11 elements of array a are: \n" << "a[0] = " << a[0] << "\n" << "a[1] = " << a[1] << "\n" << "a[2] = " << a[2] << "\n" << "a[3] = " << a[3] << "\n" << "a[4] = " << a[4] << "\n" << "a[5] = " << a[5] << "\n" << "a[6] = " << a[6] << "\n" << "a[7] = " << a[7] << "\n" << "a[8] = " << a[8] << "\n" << "a[9] = " << a[9] << "\n" << "a[10] = " << a[10] << "\n" << endl; cout << "Now these 11 elements will be copied into the first " << "portion of array b" << endl; pause(); cout << "The first 11 elements of array b are now: " << endl; E(a,bb); break; case 6: //Calls Function F F(w); break; default: cout << "\n**************You entered an invalid selection." << " Please try again.**************\n" << endl; break; } cout << "-------------------------------------------------------" << "-------------------------" << "\t\t Select which function to be performed \n" << "1) Display the value of the seventh element of character" << " array f.\n" << "2) Input a value into element 4 of single-subscripted " << "floating point array b.\n" << "3) Initialize each of the 5 elements of single-subscripted" << " integer array g to 8." << "4) Total and print the elements of floating-point array " << "c of 100 elements.\n" << "5) Copy array a into the first portion of array b. Assume" << " double a[11], b[34].\n" << "6) Determine and print the smallest and largest values" << " contained in 99-element \n floating-point array w. " << "\n-----------------------------------------------------" << "---------------------------\n" << "Select function to be performed : "; cin >> function; } cout<< "\n\n\t\t ********** End Program **********" << endl; pause (); return 0; } void A(char x[]) //Function A { cout << x[6] << endl; } void B(float x[]) //Function B { cin >> x[3]; } void C(int x[]) //Function C { int i; for (i = 0; i < 5; ++i) x[i] = 8; cout << "g[0] = " << x[0] << "\n" << "g[1] = " << x[1] << "\n" << "g[2] = " << x[2] << "\n" << "g[3] = " << x[3] << "\n" << "g[4] = " << x[4] << "\n" << endl; } void D(float x[]) //Function D { int row = 0; int i; int total = 0; cout << "The values contained in the elements are: \n"; for (i = 0; i < 100 ; ++i) x[i] = i + 1; cout << "\nElement" << " Value\n" << endl; for (i = 0; i < 100; ++i){ cout << "[" << i << "] = " << x[i] << endl; ++row; if (row % 10 == 0 ) pause (); } cout << "\nTotal of " << i << " Elements" << endl; for (i = 0; i < 100; ++i) total += x[i]; cout << "\nSum of the combined elements: " << total << "\n" << endl; } void E(double x[] , double y[]) //Function E { for (int i = 0; i < 11; ++i) y[i] = x[i]; for ( i = 0; i < 11; ++i) cout << "[" << i << "] = " << y[i] << endl; } void F(float x[]) //Function F { float min, max; for ( int i = 0; i < 99; ++i){ min = x[0]; max = x[0]; x[i] = rand() ; if (x[i] < min) min = x[i]; else if (x[i] > max) max = x[i]; } cout << "\nSmallest value of the array is " << min ; cout << "\nLargest value of the array is " << max << endl; } void instruct (void) { // Declaration section cout << " This program will implement and show the results of " << "6 different statements \n listed below that are called " << "from functions. Input the function you would \n " << "like to see performed. To exit the program input -1\n" << endl; // Executable section } void pause () { // Declaration section // Executable section cout << "\nPress any key to continue..."; getch(); cout << "\r"; cout << " "; cout << "\r"; } /* Program Output This program will implement and show the results of 6 different statements listed below that are called from functions. Input the function you would like to see performed. To exit the program input -1 -------------------------------------------------------------------------------- Select which function to be performed 1) Display the value of the seventh element of character array f. 2) Input a value into element 4 of single-subscripted floating point array b. 3) Initialize each of the 5 elements of single-subscripted integer array g to 8. 4) Total and print the elements of floating-point array c of 100 elements. 5) Copy array a into the first portion of array b. Assume double a[11], b[34]. 6) Determine and print the smallest and largest values contained in 99-element floating-point array w. -------------------------------------------------------------------------------- Select function to be performed : 1 The value of the seventh element of character array f is g Select function to be performed : 2 The first three elements of array b: b[0] = 5 b[1] = 4 b[2] = 3 Input the number for the four array: 478 The fourth element of array b is: b[3] = 478 Select function to be performed : 3 The five elements of single-subscripted integer array g: g[0] = 8 g[1] = 8 g[2] = 8 g[3] = 8 g[4] = 8 Select function to be performed : 4 The total of 100 elements of array c. The values contained in the elements are: Element Value [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 .............. [95] = 96 [96] = 97 [97] = 98 [98] = 99 [99] = 100 Total of 100 Elements Sum of the combined elements: 5050 Select function to be performed : 5 The 11 elements of array a are: a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 a[4] = 5 a[5] = 6 a[6] = 7 a[7] = 8 a[8] = 9 a[9] = 10 a[10] = 11 Now these 11 elements will be copied into the first portion of array b The first 11 elements of array b are now: [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = 10 [10] = 11 Select function to be performed : 6 Smallest value of the array is 3953 Largest value of the array is 19689 Select function to be performed : -1 ********** End Program ********** Press any key to continue... */