Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Re-write Homework 5, but this time the variables for the two numbers and the result must be kept in local variables defined in the main

Re-write Homework 5, but this time the variables for the two numbers and the result must be kept in local variables defined in the main function. And none of these variables may be returned from a function call. This means that to change the value in these variables, they must be passed by reference to a function and the function must update the value at the variables address location. All other specifications for homework 4 still must be satisfied.

This needs to be done with the "call by reference" concept. Needs to be done with C programming language (*.c).

Main Program:

#include

// Macros

#define ZERO (0)

#define ONE (1)

#define TWO (2)

#define MASKEIGHT (0x80)

#define MASKONESIX (0x8000)

#define MASKTWOFOUR (0x800000)

#define MAXEIGHT (0xFF)

#define MAXONESIX (0xFFFF)

#define MAXTWOFOUR (0xFFFFFF)

#define DATASIZE8 (8)

#define DATASIZE16 (16)

#define DATASIZE24 (24)

#define LOOPSIZE8 (DATASIZE8 - 1)

#define LOOPSIZE16 (DATASIZE16 - 1)

#define LOOPSIZE24 (DATASIZE24 - 1)

#define USERMAX (4000)

// Global Variables

int first;

int second;

int ans;

int x;

// Prototype Functions

extern void dectobin(unsigned int num);

extern void printbanner();

extern void prompt();

extern void multiply ();

int main ()

{

printbanner(); //print banner to screen

while (ONE) {

prompt(); // prompt user

if ( first == x) { //Check for user to end program

return ZERO;

}

else { //main function of program

printf("\t Decimal Answer = \t ");

multiply();

printf(" ");

printf("\t Binary Answer= \t ");

dectobin(ans);

printf(" ");

ans = ZERO; //remove value from ans

}

}

return ZERO;

}

Program with Functions:

#include

// Macros

#define ZERO (0)

#define ONE (1)

#define TWO (2)

#define MASKEIGHT (0x80)

#define MASKONESIX (0x8000)

#define MASKTWOFOUR (0x800000)

#define MAXEIGHT (0xFF)

#define MAXONESIX (0xFFFF)

#define MAXTWOFOUR (0xFFFFFF)

#define DATASIZE8 (8)

#define DATASIZE16 (16)

#define DATASIZE24 (24)

#define LOOPSIZE8 (DATASIZE8 - 1)

#define LOOPSIZE16 (DATASIZE16 - 1)

#define LOOPSIZE24 (DATASIZE24 - 1)

#define USERMAX (4000)

// External global Variables from main file

extern int first;

extern int second;

extern int ans;

extern int x;

//Functions

void dectobin(unsigned int num) //decimal to binary

{

int op;

printf("0b");

// Check value of the number

if( num <= MAXEIGHT ) { //Eight bit

op = 'a';

}

else if( num <= MAXONESIX ) { //Sixteen Bit

op = 'b';

}

else if( num <= MAXTWOFOUR ) { // Twenty-Four Bit

op = 'c';

}

switch (op)

{

case 'a': //Decimal to Binary Eight bit

for ( int i= LOOPSIZE8; i >= ZERO; i--) {

if(num & MASKEIGHT ) {

printf("1");

}

else {

printf("0");

}

num = num << ONE ; // Left Shift 1

}

break;

case 'b': //Decimal to Binary Sixteen bit

for ( int i= LOOPSIZE16; i >= ZERO; i--) {

if(num & MASKONESIX) {

printf("1");

}

else {

printf("0");

}

num = num << ONE ; // Left Shift 1

}

break;

case 'c': //Decimal to Binary Twenty-Four bit

for ( int i= LOOPSIZE24; i >= ZERO; i--) {

if(num & MASKTWOFOUR ) {

printf("1");

}

else {

printf("0");

}

num = num << ONE ; // Left Shift 1

}

break;

default:

{printf(" Number is out of range ");}

}

}

void printbanner() //print banner

{

printf("\t __---__ " );

printf("\t _- _--______ " );

printf("\t __--( )XXXXXXXXXXXXX_ " );

printf("\t --XXX( O O )XXXXXXXXXXXXXXX- " );

printf("\t XXX( U ) XXXXXXX " );

printf("\t XXXXX( )--_ XXXXXXXXXXX " );

printf("\t XXXXX O XXXXXX XXXXX " );

printf("\t XXXXX XXXXXX __ XXXXX " );

printf("\t XXXXXXXX__ XXXXXX __---- " );

printf("\t __ XXXX__ XXXXXX __ -- " );

printf("\t -- --__ ___ XXXXXX ___---= " );

printf("\t -_ XXXXXX '--- XXXXX " );

printf("\t XXXXXXX XXXXXX XXXXX " );

printf("\t XXXXXXXXX XXXXXX " );

printf("\t XXXXXX _XXXXXX " );

printf("\t XXXXX--__ __-- XXXXX " );

printf("\t --XXXXXXX--------------- XXXXX-- " );

printf("\t XXXXXXXXXXXXXXXXXXXXXXXX- " );

printf("\t --XXXXXXXXXXXXXXXXXX " );

printf("\t \t \t Written By: " );

printf("\t \t \t Michael Neria " );

printf(" ");

}

void prompt() // prompt user

{

// Print to screen Prompt

printf(" \t Values Must Range Between 0 and 4000 ");

printf(" \t Enter Values Below \t \t Enter x to end program ");

printf(" \t First is = \t ");

scanf(" %u", &first); //Take in value of first number

if ( first > USERMAX || first < ZERO ) { //Qualify Values

printf(" \t Number out of range ");

}

else {

printf(" \t Second is = \t ");

scanf(" %u", &second); //Take in value of second number

if ( second > USERMAX || second < ZERO ) { //Qualify Values

printf(" \t Number out of range ");

}

else { // Resume Program

printf(" ");

}

}

}

void multiply () //Russian multiply

{

while ( first >= ONE ) { // Run program until first is greater or equal to one

if ( first % TWO == ONE) { // Check if number is even or odd

ans = ans + second; // if odd add second value to answer

}

first = first >> ONE; // Shift right one bit, Divide by 2

second = second << ONE; // Shift right by one bit, Multiply by 2

}

printf("%d", ans); // Return value of answer after condition is met

}

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

Show that B is a convex set with a nonempty interior.

Answered: 1 week ago

Question

Define Java and HTML5 and explain why they are important.

Answered: 1 week ago