[DEV C++] With using of programing language of DEV C++, (it is different program
ID: 3774573 • Letter: #
Question
[DEV C++] With using of programing language of DEV C++, (it is different programming language other than normal C++.)
2. If two arrays have the same size, we can add" them as we do with vectors. For example: x1 Sum 1.1 2.7 3.8 3.7 5.4 1.7 3.2 5.3 2.1 2.0 3.0 1.0 3.0 3.0 0.0 rite a program, addarrays.c., which reads two arrays xi and x2 from the file addarraysin.txt, and writes their sum to the file addarraysout.txt. In more detail, the main carries out the following steps: opens the two files addarraysin.txt and addarraysout.txt. reads the pairs of values from the file addar raysin.txt into the two arrays x1 and x2 calls the function add which adds the two arrays and stores the result into the array sum writes the size of sum followed by the values of sum to the file addarraysout.txtExplanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void sum(float a1[],float a2[])
{
float sum[5];
for(int i=0;i<5;i++)
{
sum[i]=a1[i]+a2[i];
}
ofstream outfile ("addarraysout.txt");
if (outfile.is_open())
{
outfile<<"Size of sum array :"<<sizeof(sum)<<endl;
for(int j=0;j<5;j++)
{
outfile <<sum[j]<<" ";
}
}
else cout << "Unable to open file";
outfile.close();
}
int main () {
float number;
float x1[5];
float x2[5];
ifstream myfile ("addarraysin.txt");
if (myfile.is_open())
{
int i=0;
int j=0;
int k=0;
while (myfile >> number)
{
if(k%2==0)
{
x1[i]=number;
i++;
}
else
{
x2[j]=number;
j++;
}
k++;
}
sum(x1,x2);
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.