Question

In: Computer Science

1. a. In C++, Write a program that creates an array of 20 integers and initializes...

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)

Solutions

Expert Solution

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


Related Solutions

use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
Write a small C program connect.c that: 1. Initializes an array id of N elements with...
Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same...
In C, write a program that initializes a 3D array (the exact size doesn't matter) that...
In C, write a program that initializes a 3D array (the exact size doesn't matter) that contain what ever arbitrary integers and print out the array including the elements and use different functions so that the main only contains variables and function calls.
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }.  For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this assignment,...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT