Question

In: Computer Science

This is for c++ Write a program that works with two arrays of the same size...

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

Solutions

Expert Solution

#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;

}


Related Solutions

In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Write a program in java which has two arrays of size 4 and 5; merge them...
Write a program in java which has two arrays of size 4 and 5; merge them and sort.
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It should display the contents of the first array, then call a function to sort it using the most efficient descending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using descending order selection sort, modified to print out the...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT