Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im not sure where my programming is going wrong but I am close. Please refer to the question/source code below and then look at my

Im not sure where my programming is going wrong but I am close. Please refer to the question/source code below and then look at my code. This is all in C. Not C++. Thanks!

. Part 2 Structs and Arrays (65 points). In this assignment, we will be making a program that reads in guests' information, and create a graduation ceremony seating with a number of rows and columns specified by a user. Then it will attempt to assign each guest to a seat in an auditorium. Use the file homework_part_2.c (attached at the end of this document). Complete the file and include all the following requested code in the file homework_part_2.c

Step 1. First, you need to create a structure guest. It should contain two variables, last_name (char [40]) and first_name (char [40]). In addition, the following functions should be defined. void guest_init_default (struct guest *g) Assign the default string " -- " to both variables, last_name and first_name.

void guest_init_name (struct guest *g, char *info)

Use the strtok function to extract first name and last name from the variable guest, then assign them to each instance variable of the guest structure. An example of the input string is: David-Johnson

void guest_string (struct guest *g) It prints the initial character of the first name, a period, the initial character of the last name, and a period. An example of such string for the guest David Johnson is: D.J.

Step 2. You will be creating a structure called auditorium_seating in the same code file. The structure auditorium_seating will contain a 2-dimensional array called "seating" of guest type. Define the following functions: void auditorium_seating_init (int rowNumb, int columnNumb, struct auditorium_seating *a ) It instantiates a two-dimensional array of the size "rowNumb" by "columnNumb" specified by the parameters inside the struct a. Then it initializes each guest element of this array using the guest_init_default function. So, each guest will have default values for its instance variables.

int assign_guest_at (int row, int col, struct auditorium_seating *a, The function attempts to assign the "g" to the seat at "row" and "col" (specified by the parameters of this function). If the seat has a default guest, i.e., a guest with the last name struct guest* g) "--" and the first name "--", then we can assign the new guest "g" to that seat and the method returns true. Otherwise, this seat is considered to be taken by someone else, the method does not assign the guest and return 0 (false).

int check_edges (int row, int col, struct auditorium_seating *a) The function checks if the parameters row and col are valid. If at least one of the parameters "row" or "col" is less than 0 or larger than the last index of the array (note that the number of rows and columns can be different), then it return 0 (false). Otherwise it returns 1 (true).

void auditorium_seating_to_string (struct auditorium_seating *a ) It prints information of the "seating". It should show the list of guests assigned to the seating using the guest_string function (it shows initials of each guest) and the following format: The current seating -------------------- M.G.-.-.J.C. -.-.-.-.M.M. D.D.C.C.-.-. Please see the sample output listed below. After compiling the homework_part_2.c file, you need to execute it.

******* Source Code****************

#include

struct guest //It creates a structure to store fisrt and last name of the user { char first_name[40]; char last_name[40]; };

struct auditorium_seating//It creates a structure which contains a structure guest type pointer to pointer named seating { struct guest **seating; };

void guest_init_default(struct guest *g) {} void guest_init_name (struct guest *g, char *info) {} void guest_string (struct guest *g ) {} void auditorium_seating_init (int rowNumb, int columnNumb, struct auditorium_seating *a ) {} int assign_guest_at (int row, int col, struct auditorium_seating *a, struct guest* g) {} int check_edges (int row, int col, struct auditorium_seating *a) {} void auditorium_seating_to_string (struct auditorium_seating *a ) {}

void main() {

struct auditorium_seating auditorium_seating;

struct guest temp_guest;

int row, col, rowNumb, columnNumb;

char guest_info[30]; // Ask a user to enter a number of rows for an auditorium seating

printf ("Please enter a number of rows for an auditorium seating. ");

scanf ("%d", &rowNumb); // Ask a user to enter a number of columns for an auditorium seating

printf ("Please enter a number of columns for an auditorium seating. ");

scanf ("%d", &columnNumb);

// auditorium_seating

auditorium_seating_init(rowNumb, columnNumb, &auditorium_seating);

printf("Please enter a guest information or enter \"Q\" to quit. ");/*** reading a guest's information ***/

scanf ("%s", guest_info); /* we will read line by line **/

while (1 /* change this condition*/)

{

printf(" A guest information is read. "); // printing information.

printf ("%s", guest_info); // guest

guest_init_name (&temp_guest, guest_info); // Ask a user to decide where to seat a guest by asking // for row and column of a seat

printf(" Please enter a row number where the guest wants to sit. ");

scanf("%d", &row);

printf(" Please enter a column number where the guest wants to sit. "); scanf("%d", &col); // Checking if the row number and column number are valid // (exist in the theatre that we created.)

if (check_edges(row, col, &auditorium_seating) == 0)

{

printf(" row or column number is not valid.");

printf("A guest %s %s is not assigned a seat.", temp_guest.first_name, temp_guest.last_name);

}

else

{ // Assigning a seat for a guest

if (assign_guest_at(row, col, &auditorium_seating, &temp_guest) == 1)

{

printf(" The seat at row %d and column %d is assigned to the guest ",row, col);

guest_string(&temp_guest);

auditorium_seating_to_string(&auditorium_seating);

}

else

{

printf(" The seat at row %d and column %d is taken. ", row, col);

}

} // Read the next guestInfo

printf ("Please enter a guest information or enter \"Q\" to quit. "); /*** reading a guest's information ***/

scanf("%s", guest_info); } }

********MYCODE***************

#include #include #include

//It creates a structure to store first and last name of the user struct guest { char first_name[40]; char last_name[40]; };

// It creates a structure which contains a structure guest type pointer to pointer named seating struct auditorium_seating { struct guest **seating; int row; int col; };

void guest_init_default(struct guest *g) { strcpy(g->first_name, "--"); strcpy(g->first_name, "--"); }

void guest_init_name(struct guest *g, char *info) { char *scan = strtok(info, "-"); strcpy(g->first_name, scan); scan = strtok(NULL, "\0"); strcpy(g->last_name, scan); }

void guest_string(struct guest *g) { printf("%c.%c.", g->first_name[0], g->last_name[0]); }

void auditorium_seating_init(int rowNumb, int columnNumb, struct auditorium_seating *a) { int i, j; a->seating = (struct guest**) malloc(sizeof(struct guest*) * rowNumb); for (i = 0; i < rowNumb; i++) { a->seating[i] = (struct guest*) malloc(sizeof(struct guest) * columnNumb); for (j = 0; j < columnNumb; j++) { guest_init_default(&a->seating[i][j]); } } a->row = rowNumb; a->col = columnNumb; }

int assign_guest_at(int row, int col, struct auditorium_seating *a, struct guest* g) { struct guest s = a->seating[row][col]; if (strcmp(s.first_name, "--") == 0 && strcmp(s.last_name, "--") == 0) { a->seating[row][col] = *g; return 1; } else return 0; }

int check_edges(int row, int col, struct auditorium_seating *a) { if (row < 0 || col < 0 || row >= a->row || col >= a->col) return 0; else return 1; }

void auditorium_seating_to_string(struct auditorium_seating *a) { int i, j; printf("The current seating "); printf("-------------------- "); for (i = 0; i < a->row; i++) { for (j = 0; j < a->col; j++) { guest_string(&a->seating[i][j]); printf(" "); } printf(" "); } }

void main() { struct auditorium_seating auditorium_seating; struct guest temp_guest; int row, col, rowNumb, columnNumb; char guest_info[30]; // Ask a user to enter a number of rows for an auditorium seating printf("Please enter a number of rows for an auditorium seating. "); scanf("%d", &rowNumb); // Ask a user to enter a number of columns for an auditorium seating printf("Please enter a number of columns for an auditorium seating. "); scanf("%d", &columnNumb);

// auditorium_seating auditorium_seating_init(rowNumb, columnNumb, &auditorium_seating); printf("Please enter a guest information or enter \"Q\" to quit. ");/*** reading a guest's information ***/ scanf("%s", guest_info); /* we will read line by line **/ while (strcmp(guest_info, "Q") != 0) { printf(" A guest information is read. "); // printing information. printf("%s", guest_info); // guest guest_init_name(&temp_guest, guest_info); // Ask a user to decide where to seat a guest by asking for row and column of a seat printf(" Please enter a row number where the guest wants to sit. "); scanf("%d", &row); printf(" Please enter a column number where the guest wants to sit. "); scanf("%d", &col); // Checking if the row number and column number are valid // (exist in the theatre that we created.) if (check_edges(row, col, &auditorium_seating) == 0) { printf(" row or column number is not valid."); printf("A guest %s %s is not assigned a seat.", temp_guest.first_name, temp_guest.last_name); } else { // Assigning a seat for a guest if (assign_guest_at(row, col, &auditorium_seating, &temp_guest) == 1) { printf(" The seat at row %d and column %d is assigned to the guest ", row, col); guest_string(&temp_guest); auditorium_seating_to_string(&auditorium_seating); } else { printf(" The seat at row %d and column %d is taken. ", row, col); } } // Read the next guestInfo printf("Please enter a guest information or enter \"Q\" to quit. "); /*** reading a guest's information ***/ scanf("%s", guest_info); } }

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 For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions