Data Structures

Computing

Quiz 2 :
Introduction to Data Structures and Abstract Data Type sñc-Style Types

bookmark
Unbookmark

Quiz 2 :
Introduction to Data Structures and Abstract Data Type sñc-Style Types

Program Plan: Main method: • Declare the necessary variable to the program. • Get the input from the user. • If the user input is equal to "-1" exist the program. • Call the function "printBinary" with the user input as the argument "n". "printBinary" method: • Assign the hexadecimal representation value to the unsigned integer variable "mask". • The for loop will iterate up to 32 bits binary form. • If the "mask" and "a" value is added and the result is not equal to 0 display "1" as the output otherwise "0" as the output. • Finally shift the value of "mask" one bit right side. Program: / ***********************************************************Program to test the "printBinary()" function * ********************************************************** / / / Include required header files #include using namespace std; / / declare the function "printBinary" void printBinary(int a); / / main method int main() {        / / declare the variable "n" as integer        int n;        / / the loop will execute infinity time        for (;;)        {               / / get the input from the user cout "\nEnter the decimal number (-1 to stop): ";               cin n;               / / if the given input is -1 exist the program               if (n == -1)                break; / *call the function "printBinary" with the argument "n"* /               printBinary(n);               cout endl;        } } / / function definition void printBinary(int a) { / *assign the hexadecimal representation to the variable "mask" as declared as an unsigned* /        unsigned mask = 0x80000000;        / / print the binary value of the input        for (int i = 0; i 8 * sizeof(int); i++)        {               if ((mask a) != 0)                      cout '1';               else                      cout '0';               / / shift the "mask" value one bit right side               mask = 1;        } } Sample Output: Enter the decimal number (-1 to stop): 17 00000000000000000000000000010001 Enter the decimal number (-1 to stop): -1

Program Plan: Main method: • Declare the necessary variable to the program. • Get the input from the user. • If the user input is equal to "-1" exist the program. • Call the function "printBinary" with the user input as the argument "n" and display the output. "printBinary" method: • Assign the hexadecimal representation value to the unsigned integer variable "mask". • The for loop will iterate up to 32 bits binary form. • If the "mask" and "a" value is added and the result is not equal to 0 display "1" as the output otherwise "0" as the output. • Finally shift the value of "mask" one bit right side. Program: / ***********************************************************Program to test the "printBinary()" function * ********************************************************** / / / Include required header files #include using namespace std; / / declare the function "printBinary" void printBinary(int a); / / main method int main() {        / / declare the variable number as integer        int n;        / / the loop will execute infinity time        for (;;)        {               / / get the input from the user cout "\nEnter the decimal number (-1 to stop): ";               cin n;               / / if the given input is -1 exist the program               if (n == -1)                break; / *call the function "printBinary" with the argument "n"* /               printBinary(n);               cout endl;        } } / / function definition void printBinary(int a) { / *assign the hexadecimal representation to the variable "mask" as declared as an unsigned* /        unsigned mask = 0x80000000;        / / print the binary value of the input        for (int i = 0; i 8 * sizeof(int); i++)        {               if ((mask a) != 0)                      cout '1';               else                      cout '0';               / / shift the "mask" value one bit right side               mask = 1;        } } Sample Output: Enter the decimal number (-1 to stop): 99 00000000000000000000000001100011 Enter the decimal number (-1 to stop): 5280 00000000000000000001010010100000 Enter the decimal number (-1 to stop): 255 00000000000000000000000011111111 Enter the decimal number (-1 to stop): -255 11111111111111111111111100000001 Enter the decimal number (-1 to stop): 1024 00000000000000000000010000000000 Enter the decimal number (-1 to stop): -1024 11111111111111111111110000000000 Enter the decimal number (-1 to stop): -1

Program Plan: Main method: • Declare the necessary variable to the program. • Get the operator and operand from the user. • If the user input is equal to "#" exist the program. • If the user input is not equal to "~" get another number from the user. • Switch case operation is performed based on the operator. o " " operator is used to AND the two input o "|" operator is used to OR the two input o "^" operator is used to XOR the two input o " " operator is used to left shift the two input o " " operator is used to right shift the two input • Call the function "printBinary" with the user input as the argument "n1" and display the result. • If the given input is equal to "~" again call the "printBinary" function with the argument "n2" and display the result. • After calculating binary value for "n1" and "n2" calculating the binary value for the result • Display the result on the screen. "printBinary" method: • Assign the hexadecimal representation value to the unsigned integer variable "mask". • The for loop will iterate up to 32 bits binary form. • If the "mask" and "a" value is added and the result is not equal to 0 display "1" as the output otherwise "0" as the output. • Finally shift the value of "mask" one bit right side. Program: / ***********************************************************Program to test the "printBinary()" function * ********************************************************** / / / Include required header files #include #include #include using namespace std; / / declare the function "printBinary" void printBinary(int n); / / main method int main() {        / / declare the variable "a" and "b" as string        string a, b;        / / declare the variable "n1","n2" and "r" as integer        int n1, n2, r;        / / the loop will execute up to infinity time        for (;;)        { / * get the operator and operand from the user* / cout "Enter operator (# to stop) and operands ";               cin a; / *if the given input is "#" exist the program* /               if (a == "#") break;               cin n1; / *if the operator is not equal to "~" get the another number from the user* /               if (a != "~")                      cin n2; / *switch case operation is performed based on operator* /               switch (a[0])               {               case ' ': r = n1 n2;                      b = "AND";                      break;               case '|': r = n1 | n2;                      b = "OR";                      break;               case '^': r = n1 ^ n2;                      b = "XOR";                      break;               case ' ': r = n1 n2;                      b = "LEFT-SHIFT";                      break;               case ' ': r = n1 n2;                      b = "RIGHT-SHIFT";                      break; case '~': r = ~n1;                      b = "NOT";                      break;               }               / / display the output  cout "Applying the bitwise " b " operator " a " to the following:";               cout endl setw(10) n1 " = "; / *call the "printBinary" function with the argument "n1"* /               printBinary(n1); / *if "a" is not equal to "~" calculate the binary value for the "n2"* /               if (a != "~")               { cout endl setw(10) n2 " = "; / *call the "printBinary" function with the argument "n2"* /                      printBinary(n2);               }               / / display the output               cout "\nproduces";               cout endl setw(10) r " = "; / *call the "printBinary" function with the argument "r"* /               printBinary(r);               cout endl endl;        } } / / function definition void printBinary(int a) { / *assign the hexidecimal representation to the variable "mask" as declared as a unsigned* /        unsigned mask = 0x80000000;        / / print the binary value of the input        for (int i = 0; i 8 * sizeof(int); i++)        {               if ((mask a) != 0)                      cout '1';               else                      cout '0';               / / shift the "mask" value one bit right side               mask = 1;        } } Sample Output: Enter operator (# to stop) and operands 13 27 Applying the bitwise AND operator to the following: 13 = 00000000000000000000000000001101 27 = 00000000000000000000000000011011 produces 9 = 00000000000000000000000000001001 Enter operator (# to stop) and operands ~ 1 Applying the bitwise NOT operator ~ to the following: 1 = 00000000000000000000000000000001 produces -2 = 11111111111111111111111111111110 Enter operator (# to stop) and operands #