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

LANGUAGE: C++ -------------------Sample programs are posted below---------------

ID: 3572213 • Letter: L

Question

LANGUAGE: C++

-------------------Sample programs are posted below----------------------------------------

1. Sample Program:

----------------------------------------------------------------------------------------------

2. sample_binary.dat

the quick brown fox jump

----------------------------------------------------------------------------------------------

Part 1.2: Write a program which does the following: l. Declares two integer arrays, a & b, both eleven elements long, containing the numbers: a. array a: 543516756 1667855729 191903345 5441 10447 544763750 1886221674 1986994,291 1948283493 1814062440 544832097 778530660 b. array b: 0 0 0 0 0 0 0 0 0 0 0 2. Opens a file called "binary.dat" and writes the array a to the file in binary format 3. closes the file (b e sure to do this, or the rest of the program won't work right!) 4. Opens the file "binary.dat" for reading, and read the data into the array b 5. outputs both arrays a & b to the console so you can verify that you read the data back again properly. Look at the contents of the file with a text editor and compare it to the contents of sample binary.dat. Does it make sense? (To open it with a plain-text editor, such as Notepad app on PC or textedit on Mac, you can copy it to "binary.txt', or select "all files" in the file-open dialog box.) The sample program above should help if you get stuck (or refer back to my slides). Name your program binary io.cpp

Explanation / Answer

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
string toString(int x)
{
switch(x)
{
case 0:       return "0";
case 1:       return "1";
case 2:       return "2";
case 3:       return "3";  
case 4:       return "4";
case 5:       return "5";
case 6:       return "6";
case 7:       return "7";
case 8:       return "8";
case 9:       return "9";
case 10:   return "A";
case 11:   return "B";
case 12:   return "C";
case 13:   return "D";
case 14:   return "E";
case 15:   return "F";
}
return "";
}
string decimalToOtherBase(long decimal, int base)
{
if(decimal < base)
return toString(decimal);
return decimalToOtherBase(decimal/base, base) + toString(decimal%base);
}
long otherBaseToDecimal(string value, int base)
{
long number = 0;
int exp = 0;
for(int i = value.length()-1; i >= 0; i--)
{
number += (value.at(i) - '0') * pow(base, exp);
exp++;
}
return number;
}
int main()
{
//Declares 2 integer arrays.
long int a[] = {543516756, 1667855729, 1919033451, 544110447, 544763750, 1886221674, 1986994291, 1948283493, 1814062440, 544832097, 778530660};
long int b[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
//Opens a file and writes the array a to file in binary format.
ofstream fout;
fout.open("binary.dat");
for(int i = 0; i < 11; i++)
{
fout<<decimalToOtherBase(a[i], 2);
//cout<<decimalToOtherBase(a[i], 2)<<endl;
}
//Closes the file.
fout.close();
//Opens the file for reading and read data into array b.
ifstream fin;
fin.open("binary.dat");
string binary;
for(int i = 0; i < 11; i++)
{
fin>>binary;
b[i] = otherBaseToDecimal(binary, 2);
}
//Output both arrays.
for(int i = 0; i < 11; i++)
cout<<a[i]<<" "<<b[i]<<endl;
}