Hi following is my code i need help in the check_boundary function as how can i retrieve my initialized 2d (seating) array so that i can compare it with the new row and column number, currently i am getting 0 0 row and column in that function if i print that. the seating array is instantiated in the auditorium_seating_init function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct guest {
char last_name[30];
char first_name[30];
};
struct auditorium_seating {
struct guest **seating;
};
void guest_init_default(struct guest *g)
{
strcpy_s(g->first_name,sizeof(g->first_name), "???");
strcpy_s(g->last_name, sizeof(g->last_name), "???");
}
void guest_init(struct guest *g, char *info)
{
char *token,*token1;
token1 = strtok_s(info, "/",&token);
strcpy_s(g->first_name,sizeof(g->first_name), token1);
token=strtok_s(NULL, "/", &token);
strcpy_s(g->last_name, sizeof(g->last_name), token);
}
void guest_to_string(struct guest *g)
{
printf("%c", g->first_name[0]);
printf(".%c", g->last_name[0]);
}
void auditorium_seating_init(int rowNum, int columnNum, struct auditorium_seating *a)
{
a->seating = malloc(rowNum * sizeof(a->seating));
for (int i = 0; i < rowNum; i++) {
a->seating[i] = malloc(columnNum * sizeof(**a->seating));
for (int j = 0; j < columnNum; j++) {
guest_init_default(&a->seating[i][j]);
}
}
}
int assign_guest_at(int row, int col, struct auditorium_seating *a, struct guest* g)
{
if ((strcmp(a->seating[row][col].first_name , "???") == 1 ) && (strcmp(a->seating[row][col].last_name, "???") == 1))
{
a->seating[row][col] = *g;
return 1;
}
else
{
return 0;
}
}
This function
int check_boundaries(int row, int col, struct auditorium_seating *a)
{
int total = sizeof(a->seating);
//printf("total %d", sizeof(a->seating));
//printf("col %d", sizeof(a->seating) / sizeof(a->seating[0]));
//printf("row %d", sizeof(a->seating[0]) / sizeof(a->seating[0][0]));
if (row >= 0 && col >= 0 && (col <= sizeof(a->seating[0])) && row <= (total / sizeof(a->seating[0]))) {
//printf("col %p",sizeof(a->seating[0]));
//printf("row %p", total/sizeof(a->seating[0]));
return 1;
}
else
{
return 0;
}
}
This is my main
void main() {
struct auditorium_seating auditorium_seating;
struct guest temp_guest;
int row, col, rowNum, columnNum;
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_s("%d", &rowNum);
// 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_s("%d", &columnNum);
/*** reading a guest's information ***/
// auditorium_seating
//scanf_s("%c", guest_info);
auditorium_seating_init(rowNum, columnNum, &auditorium_seating);
printf("Please enter a guest information or enter \"Q\" to quit.");
/*** reading a guest's information ***/
scanf_s("%s", guest_info, sizeof(guest_info));
//scanf("%s", guest_info);
/* we will read line by line **/
while ( strcmp(guest_info,"Q")==1) {
printf("\nA guest information is read.");
// printing information.
printf("%s", guest_info);
// guest
guest_init(&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_s("%d", &row);
printf("Please enter a column number where the guest wants to sit.");
scanf_s("%d", &col);
// Checking if the row number and column number are valid
// (exist in the theatre that we created.)
if (check_boundaries(row, col, &auditorium_seating) == 0) {
printf("\nrow 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("\nThe seat at row %d and column %d is assigned to the guest", row, col);
guest_to_string(&temp_guest);
auditorium_seating_to_string(&auditorium_seating);
}
else {
printf("\nThe 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("%s", guest_info, sizeof(guest_info));
}
}