Question

Write a program that works with two arrays of the same size that are related to...

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

Use this code as a guide;

#include <iostream>

#include <string>

#include <iomanip>

#include <cmath>

using namespace std;

#define PI 3.14159265

const int ARRAY_SIZE = 25;

void initializeArray(double arr[], int size);

void printArrays(int arrOne[], double arrTwo[], int size);

int main(int argc, char *argv[])

{

int arrayOne[ARRAY_SIZE];

double arrayTwo[ARRAY_SIZE];

int index;

for (index = 0; index < ARRAY_SIZE; index++)

{

arrayOne[index] = index + 1;

}

initializeArray(arrayTwo, ARRAY_SIZE);

printArrays(arrayOne, arrayTwo, ARRAY_SIZE);

cin.ignore();

cin.get();

}

void initializeArray(double arr[], int size)

{

for (int index = 0; index < size; index++)

{

arr[index] = cos((index+1) * PI / 180);

}

}

void printArrays(int arrOne[], double arrTwo[], int size)

{

cout << setw(6) << "degree" << setw(10) << "cosine" << endl;

for (int index = 0; index < size; index++)

{

cout << setw(3) << arrOne[index] << setw(14) <<

arrTwo[index] << endl;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

const int ARRAY_SIZE = 20;

void initializeArray(bool arr[], int size);

void printArrays(int arrOne[], bool arrTwo[], int size);

int main(int argc, char *argv[])

{

int arrayOne[ARRAY_SIZE];

bool arrayTwo[ARRAY_SIZE];

int index;

for (index = 0; index < ARRAY_SIZE; index++)

{

arrayOne[index] = index + 1;

}

initializeArray(arrayTwo, ARRAY_SIZE);

printArrays(arrayOne, arrayTwo, ARRAY_SIZE);

cin.ignore();

cin.get();

}

void initializeArray(bool arr[], int size)

{

for (int index = 0; index < size; index++)

{

// compute even or odd using % operator
// where a%b returns reminder after dividing a by b
arr[index] = index%2;

}


}


void printArrays(int arrOne[], bool arrTwo[], int size)

{

cout << setw(6) << "NUMBER" << setw(10) << "EVEN\?" << endl;
cout << "----------------"<< endl;
for (int index = 0; index < size; index++)

{

cout << setw(3) << arrOne[index]<<setw(12);

//formatting output from 1 or 0 to words true or false respectively
if(arrTwo[index]==1){
cout<<" True";
}
else{
cout<<" False";
}

cout<<endl;
}

cout << "----------------"<< endl;
}

E-susususususususususu -Ir-r-r-r-r-r-r-r-r-r- E - aT a T a T a T a T a T a T a T a T a T M | 1 2 4 5 6 7 8 9 0 1 2 3 4 5 6 7

COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP

Add a comment
Answer #2

Code

#include <iostream>

#include <string>

#include<iomanip>

using namespace std;

const int ARRAY_SIZE = 10;

void initializeFirstArray(int arr[]);

void initializeSeondArray(int arry[], bool isPrime[]);

void printArrays(int arrOne[], bool arrTwo[]);

bool isPrime(int);

int main()

{

int arrayOne[ARRAY_SIZE];

bool primeCheck[ARRAY_SIZE];

initializeFirstArray(arrayOne);

initializeSeondArray(arrayOne,primeCheck);

printArrays(arrayOne,primeCheck);

}

void initializeFirstArray(int arr[])

{

for(int i=0;i<ARRAY_SIZE;i++)

{

cout<<"Enter a intger : ";

cin>>arr[i];

}

}

void initializeSeondArray(int arryInt[], bool prime[])

{

for(int i=0;i<ARRAY_SIZE;i++)

{

if(isPrime(arryInt[i]))

prime[i]=true;

else

prime[i]=false;

}

}

bool isPrime(int a)

{

for(int i=2;i<a/2;i++)

if(a%i==0)

return false;

return true;

}

void printArrays(int arrOne[], bool arrTwo[])

{

cout << setw(6) << "NUMBER" << setw(10) << "Prime\?" << endl;

cout << "----------------"<< endl;

for (int index = 0; index < ARRAY_SIZE; index++)

{

cout << setw(3) << arrOne[index]<<setw(12);

//formatting output from 1 or 0 to words true or false respectively

if(arrTwo[index]==1)

{

cout<<" True";

}

else

{

cout<<" False";

}

cout<<endl;

}

cout << "----------------"<< endl;

}

output

Enter a intger Enter a intger 23 Enter a intger 17 Enter a intger : 200 Enter a intger 19 Enter a intger39 Enter a intger:5 EIf you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Write a program that works with two arrays of the same size that are related to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT