Write a C++ program that declares an array alpha of 50 components of type double
ID: 3677378 • Letter: W
Question
Write a C++ program that declares an array alpha of 50 components of type double in the main function.
Create a function initialize() that initializes the array so that the first 25 components are equal to the square of the index variable, and the last 25 components are equal to three times the index variable.
Create a second function print() that outputs the array so that 10 elements per line are printed.
#include <iostream>
#include <iomanip>
using namespace std;
void initialize(double list[], int size);
void print(double list[], int size);
int main()
{
/* Type your code here */
return 0;
}
void initialize(double list[], int size)
{
/* Type your code here */
}
void print(double list[], int size)
{
/* Type your code here */
}
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
void initialise_array(double alpha[]);
void print_array(const double alpha[]);
int main()
{
double alpha[50];
initialise_array(alpha);
print_array(alpha);
return 0;
}
void initialise_array(double alpha[]) {
for (int i = 0; i < 50; i++) {
if (i < 25)
alpha[i] = i * i;
else
alpha[i] = i * 3;
}
}
void print_array(const double alpha[]) {
int counter = 0;
for (int j = 1; j <= 5; j++) {
for (int i = 1; i <= 10; i++) {
if (counter < 50)
cout << setw(5) << alpha[counter];
else
break;
counter++;
}
cout << endl;
}
}
sample output
0 1 4 9 16 25 36 49 64 81
100 121 144 169 196 225 256 289 324 361
400 441 484 529 576 75 78 81 84 87
90 93 96 99 102 105 108 111 114 117
120 123 126 129 132 135 138 141 144 147
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.