Using C++ Assignment Plan and code a modular program utilizing arrays. A file co
ID: 3917070 • Letter: U
Question
Using C++
Assignment
Plan and code a modular program utilizing arrays.
A file contains an even number of integers, 2 integers per record.
· Input the first number of each record into one array.
· Input the second number of each record into a second array.
· Multiply the first element of each array and store the product in the third array.
· Multiply the remaining elements of the first two arrays storing the product in the third array.
Input
Input a set of numbers. Use input from the textfile below. Create the data file below in text editor or Notepad.
Data File
3 130 82 90 6 117 95
15 94 126 4 558 571 87
42 60 412 721 263 47 119
441 190 85 214 509 2 51
77 81 68 61 995 93 74
310 9 95 561 29 14 28
466 64 82 8 76 34 69
151 6 98 13 67 834 369
Output
Label and output all three arrays.
i already made a text file named DATA FILE which has all the numbers listed above stored.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int readFile(string filename, int arr1[], int arr2[]); //return no. of records read
void multiply(int arr1[], int arr2[], int arr3[], int n);
void print(int arr1[], int arr2[], int arr3[], int n);
int main(){
int arr1[50], arr2[50], arr3[50];
int n;
string filename;
cout << "Enter input filename: ";
cin >> filename;
n = readFile(filename, arr1, arr2);
multiply(arr1, arr2, arr3, n);
print(arr1, arr2, arr3, n);
}
int readFile(string filename, int arr1[], int arr2[]) //return no. of records read
{
ifstream infile(filename.c_str());
if(infile.fail())
{
cout << "ERROR: could not read input file " << filename << endl;
exit(1);
}
int n = 0;
while(infile >> arr1[n])
{
infile >> arr2[n];
n++;
}
return n;
}
void multiply(int arr1[], int arr2[], int arr3[], int n)
{
for(int i = 0; i < n; i++)
arr3[i] = arr1[i] * arr2[i];
}
void print(int arr1[], int arr2[], int arr3[], int n){
cout << setw(10) << "Array 1" << setw(10) << "Array 2" << setw(10) << "Array 3" << endl;
for(int i = 0; i < n; i++)
cout << setw(10) << arr1[i] << setw(10) << arr2[i] << setw(10) << arr3[i] << endl;
}
input file: nums.txt
------
3 130 82 90 6 117 95
15 94 126 4 558 571 87
42 60 412 721 263 47 119
441 190 85 214 509 2 51
77 81 68 61 995 93 74
310 9 95 561 29 14 28
466 64 82 8 76 34 69
151 6 98 13 67 834 369
output
------
Enter input filename: nums.txt
Array 1 Array 2 Array 3
3 130 390
82 90 7380
6 117 702
95 15 1425
94 126 11844
4 558 2232
571 87 49677
42 60 2520
412 721 297052
263 47 12361
119 441 52479
190 85 16150
214 509 108926
2 51 102
77 81 6237
68 61 4148
995 93 92535
74 310 22940
9 95 855
561 29 16269
14 28 392
466 64 29824
82 8 656
76 34 2584
69 151 10419
6 98 588
13 67 871
834 369 307746
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.