In: Computer Science
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204….
b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array.
2. a. Write another program that opens the file even.txt and read the values into an integer array of size 15.
b. Calculate and display the sum of the values of the array.
c. Calculate and display the average.
d. Create another array of size 15 and manually initialize it with any values {12, 45, 67…}
e. Write the code that will exchange the values (corresponding elements) of the two arrays
f. Display the values of each arrays separated by spaces (on different lines)
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {
//Declaring variables
ofstream dataOut;
ifstream dataIn;
int k=200,cnt=0;
const int SIZE=20;
const int SIZE1=15;
int evens[SIZE];
//Opening the output file
dataOut.open("evens.txt");
for(int i=0;i<SIZE;)
{
if(k%2==0)
{
evens[i]=k;
dataOut<<evens[i]<<endl;
i++;
}
k++;
}
dataOut.close();
int nos1[SIZE1];
//opening the input file
dataIn.open("evens.txt");
for(int i=0;i<SIZE1;i++)
{
dataIn>>nos1[i];
}
int nos2[SIZE1];
//Getting the user entered inputs and populate those
elements into array
for(int i=0;i<SIZE1;i++)
{
cout<<"Enter
Number#"<<i+1<<":";
cin>>nos2[i];
}
int temp;
//Performing exchange
for(int i=0;i<SIZE1;i++)
{
temp=nos1[i];
nos1[i]=nos2[i];
nos2[i]=temp;
}
cout<<"Displaying the even numbers array
:"<<endl;
for(int i=0;i<SIZE;i++)
{
cout<<evens[i]<<" ";
}
cout<<endl;
cout<<"Displaying the Array#1 after
Exchange:"<<endl;
for(int i=0;i<SIZE1;i++)
{
cout<<nos1[i]<<" ";
}
cout<<endl;
cout<<"Displaying the Array#2 after
Exchange:"<<endl;
for(int i=0;i<SIZE1;i++)
{
cout<<nos2[i]<<" ";
}
cout<<endl;
return 0;
}
_______________________________
Output:
_______________Could you plz rate me well.Thank You