Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1.1: Study, understand, compile and run the sample program: This exercise should help you get clear in your own mind how numbers are stored

Part 1.1: Study, understand, compile and run the sample program: This exercise should help you get clear in your own mind how numbers are stored inside a computer, and what the relationship between the bit patterns for chars, short-int's and int's is, as well as demonstrates how to read and write binary data for all the standard data types. Here is a sample program and data file (sample_binary.dat). It reads the data file 5 different times: first into a char array, then into a short int array, then into int, float and double arrays. It outputs the values in the arrays (in hex and in integer for the integer data types). Study the program to understand what it does, then compile and run it, and examine the output. Take some time to make sure the output makes sense to you. Note that the bytes appear reversed (from the hex values for chars) when output in hexadecimal as short ints and ints. This is because Intel processors are "little-endian" they store the least-significant byte first in memory. However, since we humans expect the most-significant digits first, when a little-endian value is output in hex, c++ outputs the most-significant byte first to keep us happy. Verify that the integer values output for the first 2 bytes of the char output, 0x74 and 0x68 are correct. You can do that by googling up the ascii table, and finding the hexadecimal and integer values of the characters. Then calculate the integer value of the first short int value, sibuf[0], which is hexadecimal 0x6874, and convince yourself that the integer value of 26740 is correct. If you're not sure how to do that: try 256 * 'h' plus 't' (256*104 + 116) and see if it does add up to the same value as the first short int, sibuf[0]. Then, explain in your own words in a text file: 1. How to calculate the value of a short int and of an int by looking at the values in hexadecimal. Give an example for a short int and an int (suggestion: work out the values for sibuf[1] and ibuf[1]). 2. How the same bytes can represent such different values when read into the different data types 3. Explain in your own words why the hexadecimal values for the chars, short ints and ints aren't in the same order. If you're not clear, google "little-endian" and do a little reading. 4. Take a stab at explaining to yourself why the float and double values can be so wildly varying. (Hint, some of the bits must represent the exponent.) You don't need to write your explanation down; but if you're curious, google IEEE 754. (Wikipedia's page is rather involved, but there's a nice (vaguely) simple example at http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie4.html . (A little googling should also turn up an IEEE 754 converter, which might be helpful to make sense of the values the sample program puts out.) Part 1.2: Write a program which does the following: 1. Declares two integer arrays, a & b, both eleven elements long, containing the numbers: a. array a: 543516756 1667855729 1919033451 544110447 544763750 1886221674 1986994291 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 (be 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 Name your explanation file binary_input_output_notes.txt

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago