Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When I execute my code, I am able to enter the size of the classroom 3 X 3. I am able to enter the first

When I execute my code, I am able to enter the size of the classroom 3 X 3. I am able to enter the first student's name "Mickey/Mouse". I am able to type in the row (1) and column (2) I want to place this student. However my code keeps saying that the seat is taken when it's not taken. I am not sure how to fix this.

#include

#include

#include //Define struct student. struct student{ char last_name[30]; char first_name[30]; }; struct classroom_seating { struct student **seating; }; void student_init_default(struct student *s){ strcpy(s->last_name,"???"); strcpy(s->first_name,"???"); } void student_init(struct student *s, char *info){ strcpy(s->first_name, strtok(info, "/")); strcpy(s->last_name, strtok(NULL, "/")); } void student_to_string(struct student *s){ printf("%c.%c. ",s->first_name[0],s->last_name[0]); } void classroom_seating_init(int rowNum, int columnNum, struct classroom_seating *a){ int i; int j; a->seating = (struct student **)malloc(rowNum * sizeof(struct student*)); for(i=0;iseating[i] = (struct student*)malloc(columnNum* sizeof(struct student)); } for(i=0; iseating[i][j]=obj; } } } int assign_student_at(int row, int col, struct classroom_seating *a, struct student *s){ char string[30]; strcpy(string, a->seating[row][col].first_name); if(strcmp(string, "???")){ strcpy(a->seating[row][col].first_name,s->first_name); strcpy(a->seating[row][col].last_name,s->last_name); return 1; }else{ return 0; } } int check_boundaries(int row, int col, struct classroom_seating *a){ if(row<0 || col<0 row>sizeof(a->seating) || col>sizeof(a->seating[0])){ return 0; }else{ return 1; } } void classroom_seating_to_string(struct classroom_seating *a){ int i, j; printf("Current Seating "); for(i=0; i< sizeof(a->seating);i++){ for(j=0;jseating[0]);j++){ student_to_string(&a->seating[i][j]); } printf(" "); } } int main(){ struct student temp_student; struct classroom_seating seat; int row, col, rowNum, columnNum; char student_info[60]; //Ask a user to enter a number of rows for a classroom seating printf("Enter a number of rows for a classroom seating chart."); scanf("%d", &rowNum); //Ask a user to enter a number of columns for a classroom seating chart. printf("Enter a number of columns for a classroom seating chart."); scanf("%d", &columnNum); //classroom_seating classroom_seating_init(rowNum, columnNum, &seat); printf("Enter a student's information (first_name/last_name) or enter \"Q\" to quit."); scanf("%s",&student_info); student_init(&temp_student, student_info); while (strcmp(student_info, "Q") !=0){ printf(" A student information is read. "); student_to_string(&temp_student); printf(" "); //Ask a user to decide where to seat a student by asking //for row and column of a seat. printf("Enter a row number where the student wants to sit."); scanf("%d", &row); printf("Enter a column number where the student wants to sit."); scanf("%d", &col); //Checking if the row number and column number are valid. //(exist in the classroom that we created). if(check_boundaries(row, col, &seat) == 0){ printf("A student %s %s is not assigned a seat.", temp_student.first_name, temp_student.last_name); }else{ //Assigning a seat for a student if(assign_student_at(row, col, &seat, &temp_student) == 1){ printf(" The seat at row %d and column %d is assigned to the student", row, col); student_to_string(&temp_student); classroom_seating_to_string(&seat); }else{ printf(" The seat at row %d and column %d is taken.", row,col); } printf("Please enter a student information or enter \"Q\" to quit."); scanf("%s",student_info); } return 0; } }

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

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

=+90 percent of all oil refineries) into several smaller companies

Answered: 1 week ago