1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
Name:
Date:
Program Name: Practice with Arrays
Function: The program uses two subroutines. One to initialize an array and the other
to process the array and print to screen results
*/
#include <iostream>
#include <iomanip>
// Include any other header files you may need.
const int ARRAY_SIZE = 30;
void initialize ( double list[], int index );
void square ( double list[], int index );
void threeTimes ( double list[], int index );
void output ( const double list[], int index );
void FirstSum(double list[], int index);
void SecondSum(double list[], int index);
// Include here the function prototypes remembering that the formal parameter list
// should not include names of variables declared in the main function.
using namespace std;
int main()
{
const int size = 50;
double alpha[ARRAY_SIZE];
int counter;
int firstSum;
int secondSum;
cout << "This first function initializes an array of 30 components so that" << endl
<< "the first 15 components are equal to the square of the index value and the" << endl
<< "last 15 components are equal to the index value multiplied by 3." << endl << endl;
initialize ( alpha, ARRAY_SIZE );
cout<<"The values in the array after initialization are: "<<endl;
for ( counter = 0; counter < 30; counter++ )
cout<<alpha[ counter ]<<" ";
cout<<endl<<endl;
square ( alpha, ARRAY_SIZE );
cout<<"The values in the array after using the square function are: "<<endl;
for ( counter = 0; counter < 30; counter++ )
cout<<alpha[ counter ]<<" ";
cout<<endl<<endl;
threeTimes( alpha, ARRAY_SIZE );
cout<<"The values in the array after using the function threeTimes is: "<<endl;
for ( counter = 0; counter < 30; counter++ )
cout<<alpha[ counter ]<<" ";
cout<<endl<<endl;
output( alpha,ARRAY_SIZE ); //OUTPUT FUNCTION
cout<<endl;
// Insert the call to the function that initializes the array
cout << "The second function calculates which is bigger - the sum of the first 15 elements" << endl
<< "or the sum of the last 15 elements. The output shows the sum of the first 15 elements on" << endl
<< "one line and the sum of the last 15 elements on the second line. The third line states" << endl
<< "which sum is larger or if the sums are equal. The numbers are formatted so that each number is provided" << endl
<< "in a set width of 10 spaces with two digits displayed to the right of the decimal point." << endl << endl;
FirstSum( alpha,ARRAY_SIZE );
cout << endl;
cout << endl;
SecondSum( alpha,ARRAY_SIZE );
cout << endl;
cout << endl;
// Insert the call to the function that prints out the array as specified
cout << "ALL DONE!" << endl;
return 0;
}
void initialize ( double list[], int index )
{
int counter;
for( counter = 0; counter < index; counter++ )
list[counter] = counter + 1;
}
void square ( double list[], int index )
{
int counter;
for ( counter = 1; counter <= (index / 2); counter++ )
list[counter-1] = counter * counter;
}
void threeTimes ( double list[], int index )
{
int counter;
for(counter = (index / 2); counter < 30; counter++)
list[counter] = (counter * counter)*counter;
}
void output (const double list[], int index )
{
//VARIABLES TO CONTROL THE LOOPS
int counter;
int counter2;
counter = 0;
counter2 = 10;
//SETS OUTPUT FORMAT
cout<<fixed<<showpoint;
cout<<setprecision(1);
//OUTPUTS EVERY VALUE IN THE ARRAY
for ( counter = 0; counter < index; counter++ )
{
cout<<list[counter]<<" ";
//AFTER THE ARRAY OUTPUTS 10 VALUES THE SCREEN WILL BEGIN A NEW LINE
while ( counter2 == (counter+1) && counter2 < index )
{
cout<<endl;
counter2 += 10;
}
}
}
void FirstSum(double list[], int index)
{
double sum = 0;
for(int index; index <= 15; index++)
sum = sum + list[index];
}
void SecondSum(double list[], int index)
{
double sum = 0;
for(int index; ( index >= 15 && index <= 30 ); index++)
sum = sum + list[index];
}
|