Monday, 21 March 2016

Test Your Programming in C Language

Purpose:
The main purpose of this programming assignment is to revise the basic topics of C programming,that are conditional statements and repetitive statements. In this assignment, you will also practice using different data types and also you will be able to work with ASCII code.

Description :
RAID is a technique that can be used to replicate data in different disks to ensure that data can still be accessed in case of a failure in one of the disks where data is stored. For example, data can be stored in disk 1, another data can be stored in disk 2, and a technique can be used to combine these two data in another disk to ensure that data can be recovered in case of a failure in disk 1 or 2. This particular technique is known as parity data.

For example,
Disk 1 data: 01101101
Disk 2 data: 11010100
Then parity data can be calculated with XOR:
          01101101
XOR 11010100
________________
          10111001
 Disk 3 data then becomes: 10111001
In case, there is a failure in disk 1, then the data can be retrieved as follows:
Disk 1 data: 01101101
Disk 3 data: 10111001
         10111001
XOR 01101101
________________
11010100 (Disk 2 data)

In this programming assignment, your task is to calculate the parity data for a given data in disk 1 and disk 2. This means you will mainly calculate what should be stored in disk 3. Programming Requirements: Your program will work as follows. First of all, you need to ask the user, the length of the data. The maximum length is 10 bits. This is to find out the number of bits that will be stored in disks. In this case, you need to make an assumption that all disks will store the same length of data. You will then ask the user to enter the data for disk 1. They can enter the data either in base 10, base 2 or base 16 (but the data should be stored in base 2 in the disks). Therefore, you need to first get the data and make sure that the data is valid for the given base. You will then ask the user to enter the data for disk 2. Similar to disk 1 data, data can be entered in different bases. Therefore, you also need to consider the maximum number that can be entered in base 2, base 10 and base 16.

Your program will read the new number by reading one character at a time. Each new character needs to be checked whether it is legal in the given base. If the base is 2 then allowed digits are 0/1, if the base is 10 then allowed digits are 0...9, and if the base is in 16 then allowed digits are 0..9 and A...F.

A sample run for this part is as follows:
Please enter the length of the data to be stored: 8
Please specify the base (2, 10, 16): 10
Please enter disk 1 data: 12A
WARNING: Your number is not valid in base 10!!!
Please enter disk 1 data: 22
Disk 1 data in base 2 is: 00010110
Please specify the base (2, 10, 16): 10
Please enter disk 1 data: 22
Disk 2 data in base 2 is: 00010110
Once both numbers are converted to base 2, you need to then compute the parity data that needs to be stored in disk 3. For this part, you need to do Bitwise XOR (exclusive or), which is given by the table below:

X(where X is a single bit)     Y (where Y is a single bit)    X XOR Y
1                                          1                                        0
1                                          0                                        1
0                                          1                                        1
0                                          0                                        0



A sample run for this part is as follows:
Disk 1 = 00010110 & Disk 2 = 00010110 => Disk 3 = 00000000

Please note that you cannot use the bitwise and/or/xor operations provided in C.


Full Sample Run:

RAID Calculator V1.0
Enter Data length [1..10] :12
ERROR: 12 is NOT valid data length! Please enter between 1 and 10!
Enter Data length [1..10] :3
For disk 1 enter base (2, 10, 16) :4
ERROR: 4 is NOT valid base!
For disk 1 enter base (2, 10, 16) :10
For disk 1 enter data in base 10 :43
ERROR: Invalid data length: 3 bits expected but 43 (101011) is 6 bits long!
For disk 1 enter data in base 10 :7
Disk 1 data in base 2 is: 111
For disk 2 enter base (2, 10, 16) :3
ERROR: 3 is NOT valid base!
For disk 2 enter base (2, 10, 16) :16
For disk 2 enter data in base 16 :s
ERROR: This is invalid input for base 16!
For disk 2 enter data in base 16 :A
ERROR: Invalid data length: 3 bits expected but 10 (1010) is 4 bits long!
For disk 2 enter data in base 16 :6
Disk 2 data in base 2 is: 110
Disk 1 = 111 & Disk 2 = 110 => Disk 3 = 001
Another (Y/N):y
RAID Calculator V1.0
Enter Data length [1..10] :1
For disk 1 enter base (2, 10, 16) :2
For disk 1 enter data in base 2 :1
Disk 1 data in base 2 is: 1
For disk 2 enter base (2, 10, 16) :16
For disk 2 enter data in base 16 :0
Disk 2 data in base 2 is: 0
Disk 1 = 1 & Disk 2 = 0 => Disk 3 = 1
Another (Y/N):n
BYE!


 

No comments:

Post a Comment