Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program that: Declares two integer arrays, a & b, both eleven elements l

ID: 3691483 • Letter: W

Question

write a program that:

Declares two integer arrays, a & b, both eleven elements long, containing the numbers:

array a:   543516756  1667855729 1919033451   544110447  544763750 1886221674 1986994291 1948283493 1814062440  544832097  778530660

array b: 0 0 0 0 0 0 0 0 0 0 0

Opens a file called "binary.dat" and writes the array a to the file in binary format

closes the file (be sure to do this, or the rest of the program won't work right!)

Opens the file "binary.dat" for reading, and read the data into the array b

outputs both arrays a & b to the console so you can verify that you read the data back again properly.

c++

Explanation / Answer

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>

void main()
{
   int a[11], b[11], i;

   ofstream out("binary.dat", ios::bin);

   for(i=0;i<11;i++)
   {
       out<<a[i]<<endl;
   }

   out.close();

   instream in("binary.dat", ios::int);

   for(i=0;i<11;i++)
   {
       in<<b[i];
   }
  
   in.close();

   cout<<"array a:"<<endl;
   for(i=0;i<11;i++)
   {
       cout<<a[i]<<endl;
   }

   cout<<"array b:"<<endl;
   for(i=0;i<11;i++)
   {
       cout<<b[i]<<endl;
   }
   system("pause");
}