Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please convert to pseudo code. /Program needs to accept character input from keyboard and store in packCharacters //Output should be the characters in their bit

Please convert to pseudo code.

/Program needs to accept character input from keyboard and store in packCharacters

//Output should be the characters in their bit format before and after they are packed in to

//the unsigned int to prove they are packed correctly.

#include

unsigned packCharacters(unsigned c1, char c2);

void display(unsigned val);

int main(void)

{

//Define variables

char a;

char b;

char d;

char e;

unsigned result;

unsigned result1;

unsigned result2;

//Prompt user to enter 4 characters

printf("Enter any four characters:");

//Read 4 characters

scanf("%c%c%c%c",&a, &b, &d, &e);

//display 1st char in bits

printf("Representation of '%c' in bits as an unsigned integer is: ", a);

display(a);

//2nd char in bits

printf(" Representation of '%c' in bits as an unsigned integer is: ", b);

display(b);

//3rd char in bits

printf(" Representation of '%c' in bits as an unsigned integer is: ", d);

display(d);

//4th char in bits

printf(" Representation of '%c' in bits as an unsigned integer is: ", e);

display(e);

unsigned ch = a;

// Call function "packCharacters()" and display resutls

result = packCharacters(ch, b);

result1 = packCharacters(result, d);

result2 = packCharacters(result1, e);

printf(" Representation of '%c\''%c\''%c\' and '%c\' packed in an unsigned integer is: ", a, b, d, e);

//call the function

display(result2);

return 0;

}

// function to pack 4 characters in an unsigned integer

unsigned packCharacters(unsigned c1, char c2)

{

unsigned pack = c1;

//shift 8 bits to the left

pack <<= 8;

//using or operator pack c2

pack |= c2;

return pack;

}

void display(unsigned val)

{

//bit counter

unsigned c;

unsigned mask = 1<<31;

printf("%7u = ", val);

//loop through bits

for (c = 1; c <= 32; c++)

{

//shift 1 bit to the left

val & mask ? putchar('1') : putchar('0');

val <<= 1;

if (c % 8 == 0)

{

//print blank space

printf("");

}

}

//print new line character

putchar(' ');

}

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 Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

Compare and contrast the housing patterns of different cultures

Answered: 1 week ago

Question

Compare and contrast high- and low-load environments

Answered: 1 week ago

Question

Describe why intercultural communication competence is a necessity

Answered: 1 week ago