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

Write a program, blockhead.cpp, that uses a struct called block which has three

ID: 671389 • Letter: W

Question

Write a program, blockhead.cpp, that uses a struct called block which has three fields:

1. An integer rows

2. An integer cols

3. An character letter

A block will be declared and initialized in main. The program should repeatedly read one of the following commands until a q is entered:

r: Change the number of rows in the block

c: Change the number of columns in the block

l: Change the letter in the block

a: Change the letter in the block based on an ASCII value

p: Print the block in two dimensional format


If an illegal command is entered, the program should print an appropriate error message.

You are required to write the following six functions:

// initializes the block to 4 rows, 4 columns and the

// letter *

void init_block (block& b);

// prompts for and reads a number of rows from the user

// and assigns that number of rows to the block

void change_rows (block& b);

// prompts for and reads a number of columns from

// the user and assigns that number of columns to the

// block

void change_cols (block& b);

// prompts for and reads the ASCII value of a character

// from the user and sets the letter in the block to

// that character

void change_ascii (block& b);

// prompts for and reads a letter from

// the user and assigns that letter to the

// block

void change_letter (block& b);

// prints the block in two dimensional format with a

// label

void print (block b);

Here is a sample run of the program (user input in bold). Your program should use the same output formatting.

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): p

The block is:

****

****

****

****

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): r

Enter the number of rows: 3

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): c

Enter the number of columns: 6

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): l

Enter the letter: W

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): p

The block is:

WWWWWW

WWWWWW

WWWWWW

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): k

Illegal command, just enter one of r, c, l, a, p, or q!

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): r

Enter the number of rows: 5

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): a

Enter ASCII letter number: 3

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): p

The block is:

Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): q

Thanks

Explanation / Answer

#include <iostream>
using namespace std;

struct block
{
int rows;
int cols;
char letter;
};
void init_block(struct block *b)
{
b->rows = 4;
b->cols = 4;
b->letter = '*';
}

void change_rows(struct block *b)
{
cout<<"Enter the number of rows: ";
cin>>b->rows;
}

void change_cols(struct block *b)
{
cout<<"Enter the number of cols: ";
cin>>b->cols;
}

void change_ascii(struct block *b)
{
int num;
cout<<"Enter ascii letter number: ";
cin>>num;
b->letter = num;
}

void change_letter(struct block *b)
{
cout<<"Enter the letter: ";
cin>>b->letter;
}

void print(struct block b)
{
int i,j;
for(i = 0; i < b.rows; i++)
{
for(j = 0; j < b.cols; j++)
cout<<b.letter<<" ";
cout<<endl;
}
}

int main()
{
struct block b;
char ch;
init_block(&b);
while(1)
{
cout<<"Enter r (rows), c (columns), l (letter), a (ascii), p (print) or q (quit): ";
cin>>ch;
switch(ch)
{
case 'r':
            change_rows(&b);
            break;
case 'c':
            change_cols(&b);
            break;
case 'l':
            change_letter(&b);
            break;
case 'a':
            change_ascii(&b);
            break;
case 'p':
               print(b);
               break;
   case 'q':
              exit(1);
   default:
              cout<<"Invalid choice.";
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote