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

Part 2 Structs and Arrays (65 points). In this assignment, we will be making a p

ID: 3881070 • Letter: P

Question

Part 2 Structs and Arrays (65 points).

In this assignment, we will be making a program that reads in student’s information, and create a

classroom seating arrangement with a number of rows and columns specified by a user. Then it

will attempt to assign each student to a seat in an classroom.

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 student. It should contain two variables, last_name (char

[30]) and first_name (char [30]). In addition, the following functions should be defined.

Function Description

void student_init_default

(struct student *s)

Assign the default string " ??? " to both variables,

last_name and first_name.

void student_init

(struct student *s, char *info)

Use the strtok function to extract first name and

last name from the variable student, then assign

them to each instance variable of the student

structure. An example of the input string is:

David/Johnson

void student_to_string (struct student *s) 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 student

David Johnson is:

D.J.

Step 2.

You will be creating a structure called classroom_seating in the same code file. The

structure classroom_seating will contain a 2-dimensional array called "seating" of student type.

Define the following functions:

Function Description

void classroom_seating_init

(int rowNum, int columnNum,

struct classroom_seating *a )

It instantiates a two-dimensional array of the size

"rowNum" by "columnNum" specified by the

parameters inside the struct a. Then it initializes each

student element of this array using the

student_init_default function. So, each student will

have default values for its instance variables.

int assign_student_at

(int row, int col,

struct classroom_seating *a,

struct student* s)

The function attempts to assign the "s" to the seat at

"row" and "col" (specified by the parameters of this

function). If the seat has a default student, i.e., a student

with the last name "???" and the first name "???", then

we can assign the new student "s" 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 student and return 0 (false).

int check_boundaries

(int row, int col,

struct classroom_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 classroom_seating_to_string

(struct classroom_seating *a )

It prints information of the "seating". It should show the

list of students assigned to the seating using the

student_to_string function (it shows initials of each

student) and the following format:

The current seating

--------------------

D.J. ?.?. E.T..

?.?. ?.?. S.W.

T.C. A.T. ?.?.

Please see the sample output listed below.

After compiling the homework_part_2.c file, you need to execute it.

#include <stdio.h>
struct student {
char last_name[30] ;
char first_name[30];
};
struct classroom_seating {
struct student **seating;
};
void student_init_default (struct student *g ) {}
void student_init (struct student *g, char *info) {}
void student_to_string (struct student *g ) {}
void classroom_seating_init (int rowNum, int columnNum, struct classroom_seating *a ) {}
int assign_student_at (int row, int col, struct classroom_seating *a, struct student* g) {}
int check_boundaries (int row, int col, struct classroom_seating *a) {}
void classroom_seating_to_string (struct classroom_seating *a ) {}
void main() {
struct classroom_seating classroom_seating;
struct student temp_student;
int row, col, rowNum, columnNum;
char student_info[30];
// Ask a user to enter a number of rows for an classroom seating
printf ("Please enter a number of rows for an classroom seating.");
scanf ("%d", &rowNum);
// Ask a user to enter a number of columns for an classroom seating
printf ("Please enter a number of columns for an classroom seating.");
scanf ("%d", &columnNum);
// classroom_seating
classroom_seating_init(rowNum, columnNum, &classroom_seating);
printf("Please enter a student information or enter "Q" to quit.");
/*** reading a student's information ***/
scanf ("%s", student_info);
/* we will read line by line **/
while (1 /* change this condition*/ ){
printf (" A student information is read.");
// printing information.
printf ("%s", student_info);
// student
student_init (&temp_student, student_info);
// Ask a user to decide where to seat a student by asking
// for row and column of a seat
printf ("Please enter a row number where the student wants to sit.");
scanf("%d", &row);
printf("Please 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 theatre that we created.)
if (check_boundaries(row, col, &classroom_seating) == 0) {
printf(" row or column number is not valid.");
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, &classroom_seating, &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(&classroom_seating);
} else {
printf(" The seat at row %d and column %d is taken.", row, col);
}
}
// Read the next studentInfo
printf ("Please enter a student information or enter "Q" to quit.");
/*** reading a student's information ***/
scanf("%s", student_info);
}
}

4. Part 2 Structs and Arrays (65 points). In this assignment, we will be making a program that reads in student's information, and create a classroom seating arrangement with a number of rows and columns specified by a user. Then it will attempt to assign each student to a seat in an classroom. 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 student. It should contain two variables, last name (char [30] and first name (char [30]). In addition, the following functions should be defined Function void student init default (struct student *s) Description Assign the default string"??" to both variables, last name and first name. void student init (struct student *s, char *info) Use the strtok function to extract first name and last name from the variable student, then assign them to each instance variable of the student structure. An example of the input string is: David/Johnson void student tostring (struct student s) 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 student David Johnson is: D.J Step 2 You will be creating a structure called classroom seating in the same code file. The structure classroom_seating will contain a 2-dimensional array called "seating" of student type Define the following functions: Function Description It instantiates a two-dimensional array of the size "rowNum" by " parameters inside the struct a. Then it initializes each student element of this array using the student init default function. So, each student will have default values for its instance variables columnNum" specified by the void classroom_seating init (int rowNum, int columnNum, struct classroom_seating *a)

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


#include <stdio.h>
#include <string.h>
#include <stdlib.h>


struct student {
char last_name[30] ;
char first_name[30];
};
struct classroom_seating {
struct student **seating;
int rows;
int cols;
};
void student_init_default (struct student *g ){
strcpy(g->first_name, "???");
strcpy(g->last_name, "???");
  
}
void student_init (struct student *g, char *info) {
char *s = strtok(info, "/");
strcpy(g->first_name, s);
s = strtok(NULL, "");
strcpy(g->last_name, s);
}
void student_to_string (struct student *g ) {
printf("%c.%c", g->first_name[0], g->last_name[0]);
}
void classroom_seating_init (int rowNum, int columnNum, struct classroom_seating *a ) {
int i,j ;
a->seating = (struct student**) malloc(sizeof(struct student*) * rowNum);
for(i = 0; i < rowNum; i++)
{
a->seating[i] = (struct student*) malloc(sizeof(struct student) * columnNum);
for(j = 0; j < columnNum; j++)
{
student_init_default(&a->seating[i][j]);
}
}
a->rows = rowNum;
a->cols = columnNum;

}
int assign_student_at (int row, int col, struct classroom_seating *a, struct student* g) {
struct student 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_boundaries (int row, int col, struct classroom_seating *a) {
if(row < 0 || col < 0 || row >= a->rows || col >= a->cols)
return 0;
else
return 1;
  
}

void classroom_seating_to_string (struct classroom_seating *a ) {
int i, j;
printf("The current seating ");
printf("-------------------- ");
for(i = 0; i < a->rows; i++)
{
for(j = 0; j < a->cols; j++)
{
student_to_string(&a->seating[i][j]);
printf(" ");
}
printf(" ");
}
}
void main() {
struct classroom_seating classroom_seating;
struct student temp_student;
int row, col, rowNum, columnNum;
char student_info[30];
// Ask a user to enter a number of rows for an classroom seating
printf ("Please enter a number of rows for an classroom seating.");
scanf ("%d", &rowNum);
// Ask a user to enter a number of columns for an classroom seating
printf ("Please enter a number of columns for an classroom seating.");
scanf ("%d", &columnNum);
// classroom_seating
classroom_seating_init(rowNum, columnNum, &classroom_seating);
printf("Please enter a student information or enter "Q" to quit.");
/*** reading a student's information ***/
scanf ("%s", student_info);
/* we will read line by line **/
while (strcmp(student_info, "Q") != 0 ){
printf (" A student information is read.");
// printing information.
printf ("%s ", student_info);
// student
student_init (&temp_student, student_info);
// Ask a user to decide where to seat a student by asking
// for row and column of a seat
printf ("Please enter a row number where the student wants to sit.");
scanf("%d", &row);
printf("Please 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 theatre that we created.)
if (check_boundaries(row, col, &classroom_seating) == 0) {
printf(" row or column number is not valid.");
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, &classroom_seating, &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(&classroom_seating);
} else {
printf(" The seat at row %d and column %d is taken.", row, col);
}
}
// Read the next studentInfo
printf ("Please enter a student information or enter "Q" to quit.");
/*** reading a student's information ***/
scanf("%s", student_info);
}
}


output
======

Please enter a number of rows for an classroom seating.3
Please enter a number of columns for an classroom seating.3
Please enter a student information or enter "Q" to quit.David/Johnson

A student information is read.David/Johnson
Please enter a row number where the student wants to sit.0
Please enter a column number where the student wants to sit.1

The seat at row 0 and column 1 is assigned to the studentD.JThe current seating
--------------------
?.? D.J ?.?
?.? ?.? ?.?
?.? ?.? ?.?
Please enter a student information or enter "Q" to quit.Alice/Thomas

A student information is read.Alice/Thomas
Please enter a row number where the student wants to sit.0
Please enter a column number where the student wants to sit.1

The seat at row 0 and column 1 is taken.Please enter a student information or enter "Q" to quit.Alice/Thomas

A student information is read.Alice/Thomas
Please enter a row number where the student wants to sit.1
Please enter a column number where the student wants to sit.1

The seat at row 1 and column 1 is assigned to the studentA.TThe current seating
--------------------
?.? D.J ?.?
?.? A.T ?.?
?.? ?.? ?.?
Please enter a student information or enter "Q" to quit.Q

A student information is read.Q