C++ Question: The first image is the assignment given, and the second image is s
ID: 3874222 • Letter: C
Question
C++ Question: The first image is the assignment given, and the second image is sample code for the assignment's solution. Explain in detail, (almost) line-by-line, what is happening in the program, as if you are teaching someone who never programmed before. You are welcome to include comments next to the lines using "//" operater in the program. Included below the image is the program in text form. Remember to explain in FULL detail.
#include <iostream>
using namespace std;
int main() {
float *Arr_a, *Arr_b, *Arr_c;
int i, size1, size2;
cout << "Enter the size of the first array: ";
cin >> size1;
Arr_a = new float[size1];
cout << "Enter " << size1 << " values: ";
for(i = 0; i < size1; i++){
cin >> Arr_a[i];
}
cout << "Enter size of second array: ";
cin >> size2;
Arr_b = new float[size2];
cout << "Enter " << size2 << " values: ";
for(i = 0; i<size2; i++){
cin >> Arr_b[i];
}
Arr_c = new float[size1 + size2];
cout << endl;
cout << "Contents of Array_c: " << endl;
for(i = 0; i < size2; i++) {
Arr_c[i] = Arr_b[i];
}
for(i = 0; i < size1; i++){
Arr_c[size2 + i] = Arr_a[i];
}
for(i = 0; i < size2 + size1;i++){
cout << Arr_c[i] << " ";
}
return 0;
}
(20 points) TASK3: Write a program which initializes 2 dynamic arrays of floats Arr_a and Arr_b. Arr_C is a dynamic array as well The user will input values to place into Arr_a then Arr_b. The program will then append into Arr_c the elements from Arr_b, then append into Arr c elements from Arr_a. The program will finally output the contents of Arr_c. Attach Snipping Photo of Source Code and Output belowExplanation / Answer
Let us understand the requirement
1) We need three dynamic float arrays
2) We need to have values to first two dynamic array namely Arr_a and Arr_b
3) We need to append the values of the both the array, first elements of Arr_b and then Arr_a
4) Print the third array Arr_c
Now let us see how we fulfill each requirement in the program.
#include <iostream>
using namespace std;
int main() {
//We are creating three pointers which will then be used to point to dynamic array
float *Arr_a, *Arr_b, *Arr_c;
//We are declaring two variables as we need inputs from user about the size of the arrays for which user will enter the values and store them in size1 and size2.
Also we have another variable i which will be used to iterate the loop.
int i, size1, size2;
//Ask the user input for size of array Arr_a
cout << "Enter the size of the first array: ";
cin >> size1;
//Now allocate memory to the array having size input by user
Arr_a = new float[size1];
//Now as we have the size of the array and we know the number of values it will enter, ask the user to enter the values
cout << "Enter " << size1 << " values: ";
//We are using loop as we need to store all the values entered at a index location specified by i and keep them in Arr_a
for(i = 0; i < size1; i++){
cin >> Arr_a[i];
}
//Similarly we asked for size of second array and allocated the memory and used the pointer Arr_b to point to the array.
cout << "Enter size of second array: ";
cin >> size2;
Arr_b = new float[size2];
cout << "Enter " << size2 << " values: ";
//Loop to store the values in Arr_b
for(i = 0; i<size2; i++){
cin >> Arr_b[i];
}
//Now we have the requirement to append values of both the arrays which means the size of Arr_c will be sum of size of array Arr_a and Arr_c
Arr_c = new float[size1 + size2];
cout << endl;
//Now message to the user that contents of third array are
cout << "Contents of Array_c: " << endl;
//As first we need to append elements of array Arr_b so loop will iterate till size of Arr_b and assign the values of Arr_b from 0th index from 0th index till size2-1 to Arr_c from index 0th to size2-1
for(i = 0; i < size2; i++) {
Arr_c[i] = Arr_b[i];
}
//Next we need to assign values of Arr_a from index 0th to size1-1 to Arr_c from index size2+i to size1, as we need to append values from the index where we had the last element of Arr_b
for(i = 0; i < size1; i++){
Arr_c[size2 + i] = Arr_a[i];
}
//Now display the contents of array Arr_c
for(i = 0; i < size2 + size1;i++){
cout << Arr_c[i] << " ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.