The experienced C programmer will probably be a bit frustrated with the attention to details required by the Ada compiler. You will not have your favorite “tricks” available to fool the compiler into doing something out of the ordinary. The Ada compiler cannot be fooled. – http://perso.telecom-paristech.fr/~pautet/Ada95/intro.htm
For this programming assignment, you will be writing an ada program that will do the following:
- Create a binary array of 1’s and 0’s that is of length 16.
- Your binary array should be a user-defined type called BINARY_ARRAY that can ONLY store numbers that are of type BINARY_NUMBER
- BINARY_NUMBER is another user-defined type that is a numeric value of either 0 or 1.
- To populate your array, you must generate 16 random 0’s or 1’s using a for loop
- Your program must have the following FUNCTIONS:
- Bin_To_Int accepts one BINARY_ARRAY as a parameter and returns the integer equivalent of that binary value. You must calculate this using a loop. DO NOT use a function built-in to ada to do this.
- Int_To_Bin accepts an integer value (you may want to limit the size as we only have 16 “bits” to store it in) and returns a BINARY_ARRAY set to the binary equivalent of that integer value. The binary array should be in the correct order (least significant bit at the far right).
- Overloaded “+”
- One version of this should add two BINARY_ARRAYs and return a BINARY_ARRAY as a result. Don’t worry about overflows.
- The other version of this should accept a BINARY_ARRAY and an integer and return a BINARY_ARRAY as a result.
- Overloaded “-“ (same as above except subtract the second from the first parameter)
- Your program must have the following procedures:
- Reverse_Bin_Arr accepts a BINARY_ARRAY and simply reverses it.
- Print_Bin_Arr accepts a BINARY_ARRAY and prints it to the console.
- Your “main” should do the following:
- #1 described above
- Call your Bin_To_Int function from 2a and display the result on the console
- Call your Int_To_Bin function and save the return value into a second BINARY_ARRAY.
- Call your Print_Bin_Arr procedure from 3b to display the newly created binary array.
- Use your overloaded + function on arrays 1 and 2, and save the return value into a third BINARY_ARRAY. Print this as well using your print procedure
- Use your overloaded – function on arrays 1 and 2, and save the return value into a fourth BINARY_ARRAY. Print this as well.
- Finally, reverse the fourth BINARY_ARRAY using your Reverse_Bin_Arr procedure from 3a, and print the result.
TIP: You will need to create more variables than the one single array I mentioned in the instructions above.
Download here. You want GNAT ada 2016: http://libre.adacore.com/download/
Old but very useful (careful with the chapter on packages. Newer versions of ada require you to write them in two separate files. The specification belongs in .ads, and the implementation belongs in .adb):
Other Useful Links: