Page 1 of 1

An operating system is a computer program that allows a user to perform a variety of tasks on a computer. Billy is curre

Posted: Thu May 05, 2022 12:43 pm
by answerhappygod
An Operating System Is A Computer Program That Allows A User To Perform A Variety Of Tasks On A Computer Billy Is Curre 1
An Operating System Is A Computer Program That Allows A User To Perform A Variety Of Tasks On A Computer Billy Is Curre 1 (66.52 KiB) Viewed 42 times
Provided code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_WIDTH 16
#define BUFFER_HEIGHT 5
#define MAX_STRING_LEN 100
struct screen_cell {
char character;
int start_marker;
};
// Your write_text_to_screen code here!
void write_text_to_screen(struct screen_cell
screen[BUFFER_HEIGHT][BUFFER_WIDTH], char *text) {
}
///////////// PROVIDED CODE ///////////////
// DO NOT MODIFY THESE FUNCTIONS
static void init_screen(struct screen_cell
screen[BUFFER_HEIGHT][BUFFER_WIDTH], int starting_row, int
starting_col);
static void print_screen(struct screen_cell
screen[BUFFER_HEIGHT][BUFFER_WIDTH]);
static void trim_newline(char *string);
// we may use a different main function for marking
// please ensure your write_text_to_screen function is
implemented.
// DO NOT MODIFY THIS MAIN FUNCTION
int main(int argc, char *argv[])
{
if ( argc < 3 ) {
fprintf(stderr, "ERROR: Not enough
arguments!\n");
fprintf(stderr, "Usage ./exam_q5
start_row start_col\n");
fprintf(stderr, "You do not have to
handle this case\n");
exit(1);
return 1;
}
int start_row = atoi(argv[1]);
int start_col = atoi(argv[2]);
if (
start_row >= BUFFER_HEIGHT ||
start_row < 0 ||
start_col >= BUFFER_WIDTH ||
start_row < 0
) {
fprintf(stderr, "ERROR: Start row
and column are too big or too small!\n");
fprintf(stderr, "The max row is 4,
and the max column is 15\n");
fprintf(stderr, "You do not have to
handle this case\n");
exit(1);
return 1;
}
struct screen_cell
screen[BUFFER_HEIGHT][BUFFER_WIDTH];
init_screen(screen, start_row, start_col);
printf("Enter Text: ");
char text[MAX_STRING_LEN], *result;
if ((result = fgets(text, MAX_STRING_LEN, stdin))
!= NULL) {
trim_newline(text);
write_text_to_screen(screen,
text);
print_screen(screen);
} else {
fprintf(stderr, "ERROR: No text
provided!\n");
fprintf(stderr, "You do not have to
handle this case\n");
exit(1);
return 1;
}
return 0;
}
void trim_newline(char *str) {
int len = strlen(str);
if (str[len - 1] == '\n') {
str[len - 1] = '\0';
}
}
void init_screen (
struct screen_cell screen[BUFFER_HEIGHT][BUFFER_WIDTH],
int starting_row, int starting_col
)
{
for (int row = 0; row < BUFFER_HEIGHT; row++)
{
for (int col = 0; col <
BUFFER_WIDTH; col++) {
screen[row][col].character = ' ';
screen[row][col].start_marker = 0;
if (row ==
starting_row && col == starting_col) {
screen[row][col].start_marker = 1;
}
}
}
}
void print_screen(struct screen_cell
screen[BUFFER_HEIGHT][BUFFER_WIDTH]) {
printf("\n");
// top border
for (int i = 0; i < BUFFER_WIDTH + 2; i++)
{
printf("-");
}
printf("\n");
for (int row = 0; row < BUFFER_HEIGHT; row++)
{
// left border
printf("|");
for (int col = 0; col <
BUFFER_WIDTH; col++) {
printf("%c",
screen[row][col].character);
}
// right border
printf("|");
printf("\n");
}
// bottom border
for (int i = 0; i < BUFFER_WIDTH + 2; i++)
{
printf("-");
}
printf("\n");
}
An operating system is a computer program that allows a user to perform a variety of tasks on a computer. Billy is currently working on writing his own operating system, but needs some way of displaying output to the screen. The output screen is a rectangular grid, with each cell containing some text. To model this, he has created a two dimensional array of struct screen_cell. This array is called screen. One of the cells in the strcture will have the start_marker as 1. struct screen_cell { char character; int start_marker; }; Your job is to complete the given write_text_to_screen function in the starter code: // Your write_text_to_screen code here! void write_text_to_screen(struct screen_cell screen [BUFFER_HEIGHT] [BUFFER_WIDTH], char *text) { } To do this, you will need to loop through every struct screen_cell in the screen array, until you have found the cell where the start_marker field is 1. This is where you should starting writing your text from. By text, we mean the text string passed into the write_text_to_screen function. The text should overflow to the next row in the screen array if it is longer than the screen width (this is #defined as BUFFER_WIDTH for you).
If there is too much text to fit on the screen, the program should write as much as it can fit, then stop. I.e - your program should not try and write past the last row and the last column. You will need to go through every character in the text_string, and set the corresponding cell's character field to that character. NOTE: For example - if you are given the text "Hi" - and you have looped through the array and found that the struct at position 1 1 has start_marker as 1. Then, you should set the character field in the struct at 1 1 (since, that is where we need to start writing text) to 'H', and the character field in the struct at 1 2 (the next column) to 'i'.
Examples $ ./exam_q5 2 2 Enter Text: Shrey Rocks | Shrey Rocks $ ./exam_q5 00 Enter Text: Hello world this is a very long string that should overflow | Hello world this| | is a very long | Istring that shoul |ld overflow | | |