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

this lab should be done with the gcc compiler under Cygwin. You must write a com

ID: 3785856 • Letter: T

Question

this lab should be done with the gcc compiler under Cygwin.

You must write a command line C program that takes two positive decimal numbers as arguments when

the program is executed and multiplies the two numbers using Russian or “a la russe” multiplication.

This will require the use of the left and right shift operation in C as well as the modulus operator. You

will also need the atoi() function which requires the inclusion of the stdlib.h header file in the program.

The range of each number at the command line should be restricted to between zero and 255 decimal.

Your program should check for this condition and report any input error to the user. Your program

should check for any other garbage input from the command line and prompt the user appropriately.

In addition to printing out the result as a decimal value using the printf() function, your program must

print out the result in binary form. This cannot be done directly with printf(), so you will have to write a

function that takes in the ?? bit value and prints each bit of the value to the screen. This will require the

use of C bitwise operators.   

The binary value should be written with the MSB to the left and LSB to the right as you would write it

out on paper.

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print(int result);

int main(int argc, char **argv)
{
   int num1, num2, result = 0, i;
   //declare mask
   int MASK = 0xffffffff;
   char c = 48;

   //check if command line args less than 3
   if (argc < 3)
   {
       printf("argument less than 3, Usage: program number1 number2 ");
       exit(1);
   }

//convert argument 1 and argument 2 to decimal using atoi function
   num1 = atoi(argv[1]);
   num2 = atoi(argv[2]);
   while (num2 != 0) // Iterate the loop till num2==0
   {
  if (num2 % 2 != 0)
       {
           result = result + num1; // Add a to result if num2 is odd .
       }
       num1 <<= 1; // Left shifting the value contained in num1 by 1
       // multiplies a by 2 for each loop
       num2 >>= 1; // Right shifting the value contained in num2 by 1.
   }
   printf(" Result in decimal:%d ", result);
   //print in binary
   printf("result in binary: ");
   print(result);
   system("pause");
}

void print(int result)
{
   //to hold 4 byte string
   char str[33];
   int i = 0, j;
   do
   {
       str[i++] = result % 2 + 48;
   } while (result /= 2);
   str[i] = '';
   //print string in reverse order
   for (i = strlen(str) - 1; i >= 0; i--)
       printf("%c", str[i]);
   printf(" ");
}

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

//output

main 8 9                                                                                                                                                                

                                                                                                                                                                                

Result in decimal:72                                                                                                                                                            

result in binary: 1001000

-----------

output2

main 200 19                                                                                                                                                             

                                                                                                                                                                                

Result in decimal:3800                                                                                                                                                          

result in binary: 111011011000