12/03/13 Please help me with my homework: Problem #1 Write a function that takes
ID: 3547784 • Letter: 1
Question
12/03/13
Please help me with my homework:
Problem #1
Write a function that takes two arrays of equal lengths as input. Calculate the mean of the absolute value of the differences of the two arrays.
Problem #2
Write a program that reads a file specified by the user. Write to an output file specified by the user the length of each word in the file.
Problem #3
Write aprogram that uses for loops to perfrom the following steps:
a. Prompts the user to input three integers x, y, and z.
b. Output all multiples of z greater than or equal to x and less than or equal to y.
Explanation / Answer
//Problem #1
//Write a function that takes two arrays of equal lengths as input. //
//Calculate the mean of the absolute value of the differences of the two arrays.
#include<iostream>
#include<cstdlib>
using namespace std;
double mean_of_difference(int array1[],int array2[],int size)
{
double sum = 0;
for(int i=0; i<size; i++)
sum = sum + abs(array1[i]-array2[i]);
return sum/size;
}
int main()
{
int array1[] = {34,56,78,90,12};
int array2[] = {44,76,98,60,22};
cout << "mean of the absolute value of the differences of the two arrays is " << mean_of_difference(array1,array2,5) << endl;
}
//Problem #2
//Write a program that reads a file specified by the user.
// Write to an output file specified by the user the length of each word in the file.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
string str;
if(!infile)
{
cout <<"Unable to open file input.txt..so exiting from program " << endl;
return 0;
}
while(!infile.eof())
{
infile >> str;
outfile << str.length() << " ";
}
infile.close();
outfile.close();
return 0;
}
//Problem #3
//Write aprogram that uses for loops to perfrom the following steps:
//a. Prompts the user to input three integers x, y, and z.
//b. Output all multiples of z greater than or equal to x and less than or equal to y.
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout <<"Enter value of x,y and z";
cin >> x >> y >> z;
cout << endl;
cout <<"multiples of "<<z<< " greater than or equal to "<<x<< " and less than or equal to "<<y <<" are "<< endl;
for(int i=x; i<=y; i++)
{
if(i%z==0)
cout << i << " ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.