In: Computer Science
This is for c++
Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types.
For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:
from a previous program, populations and the associated flowrates for those populations (an int array of populations and a double array of the associated flowrate)
a series of values and the square roots of those integers (an int array of values and a double array of associated square roots)
a series of values and an indication of whether or not each value is odd or even (an int array and a bool array)
1. Declare and initialize a constant to represent the size of both arrays (at least 10 positions)
2. Declare and initialize your first array
3. Declare and initialize your second array, based on the data in your first array
4. Output the contents of both arrays in neat columns with headers
Optional suggestions (good to do, not required)
1. Try to break your program up into different functions
2. Use user input to initialize the first array
#include<bits/stdc++.h>
using namespace std;
int main() {
// 1 .Declaration of constant to represent size of both arrays
const int size=15;
//declaration of first array of data type int
int *arr1=new int[size];
//Initialization of values of first array
for(int i=0 ;i <size ;i++) {
arr1[i]=i;
}
//declaration of second array of data type double
double *arr2=new double[size];
//Initialization of values of second array based on first array
// represents square roots of integers of first array
for(int i=0; i< size;i++) {
arr2[i]=sqrt(arr1[i]);
}
// Output with neat columns
cout<<"Index " <<"Array1 value(an integer) "<<" Array2 value(square root of Array1 value)"<<endl;
//setw is builtin function for Setting the field width
//setprecision is used for setting the maximum number of digits that are displayed for a number
for(int i=0;i<size;i++) {
int k=0;
if(i>=10) k=1;
cout<<i<<setw(24)<<arr1[i]<<setw(28-k)<<setprecision(4)<<arr2[i]<<endl;
}
return 0;
}