- What This Assignment Is About:
- Structures
- Functions
- Arrays of Primitive Values
- Arrays of Structs
- Recursion
- for and if Statements
- Selection Sort
- Use the following Guidelines
- Give identifiers semantic meaning and make them easy to read (examples num_students, gross_pay, etc).
- Use lower case word for all identifiers (variables, functions, objects). Separate words with underscore character
- Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes structures, functions, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
- Use white space to make your program more readable.
- You should only submit your .c files
. homework_part_1.c
. homework_part_2.c
For each file in your assignment, provide a heading (in comments) which includes:
- The assignment number.
- Its author (your name).
- A description of what this program is doing.
- Part 1. Primitive Types, Searching, Recursion (35 points).
While there could be diverse ideas to solve b and c, it is mandatory to use pointers, since the goal is for you to practice them
- Create a file c
- Create a function initialize_array that receives two parameters: an array of integers and the array size. (Use pointers to pass an array of integers as parameter) Use a for loop
and an if statement to put 5s in the odd positions of the array and 0s in the even positions. Hint: review pointers as parameters.
- Create a function print_array that receives as parameters an array of integers and the array size. (Use pointers to pass an array of integers as parameter) Use a for statements to print all the elements in the array. Hint: review pointers as parameters.
- Create a function selection_sort that receives as parameters an array of integers and the array size, and order the array element in descending order. (Use pointers to pass an array of integers as parameter) Implement Selection Sort algorithm. It should be Selection Sort, not Bubble Sort, not Quick Sort, etc. If you do not remember selection sort, this link could be useful:
https://www.geeksforgeeks.org/c–program–for–selection–sort/
- Create a recursive function that calculate and returns the factorial of a number. The function receives the number (integer number) as parameter.
- Copy the following main function in your class,
int main() {
int a [10] = {3, 5, 6, 8, 12, 13, 16, 17, 18, 20}; int b [6]= {18, 16, 19, 3 ,14, 6}; int c [5]= {5, 2, 4, 3, 1};
// testing initialize_array
print_array(a, 10); // print: 3, 5, 6, 8, 12, 13, 16, 17, 18, 20 selection_sort(a, 10);
print_array(a, 10); // print: 0, 5, 0, 5, 0, 5, 0, 5, 0, 5
// testing initialize_array
print_array(b, 6); // print: 18, 16, 19, 3 ,14, 6 selection_sort (b, 6);
print_array(b, 6); // print: 19, 18, 16, 14, 6, 3
// testing factorial
printf(“Factorail of 5 – %d
”, factorial (5)); //print: 120
c[0] = factorial (c[0]); c[1] = factorial (c[2]);
print_array(c, 5); // print: 120, 24, 4, 3, 1
return 0;
}
Grading Criteria for the part 1
01 pts: file contains header information
01 pts: adequate comment to explain every function
01 pts: consistent indentation and spacing
08 pts: selectionSort
08 pts: printArray
08 pts: initializeArray
08 pts: factorial
- Part 2 Structs and Arrays (65 points).
In this assignment, we will be making a program that reads in students’ information and create an examination seating with a number of rows and columns specified by a user. Then it will attempt to assign each student to a seat in an examination.
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 *p) | Assign the default string ” ### ” to both variables, last_name and first_name. |
void student_init(struct student *p, 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 *p) | 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 examination_seating in the same code file. The structure examination_seating will contain a 2-dimensional array called “seating” of student type. (Note: student is a structure you created above). You will also need to create variables to check rows and columns for the seating (If your code does not contain any of the following functions, points will be deducted.) Define the following functions:
Function | Description |
void examination_seating_init (int rowNum, int columnNum, struct examination_seating *t ) | It instantiates a two-dimensional array of the size “rowNum” by “columnNum” specified by the parameters inside the struct t. 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 examination_seating *t, struct student* p) | The function attempts to assign the “p” 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 “p” 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 examination_seating *t) | 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 examination_seating_to_string (struct examination_seating *t ) | 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.
Sample Output: (the inputs entered by a user are shown in bold)
Make sure that your program works at least with this scenario.
Please enter a number of rows for a examination seating.
3
Please enter a number of columns for a examination seating.
3
Please enter a student information or enter “Q” to quit.
Mickey/Mouse
A student information is read.
Mickey/Mouse
Please enter a row number where the student wants to sit.
1
Please enter a column number where the student wants to sit.
2
The seat at row 1 and column 2 is assigned to the student M.M.
The current seating
——————–
#.#. #.#. #.#.
#.#. #.#. M.M.
#.#. #.#. #.#.
Please enter a student information or enter “Q” to quit. | |
Daisy/Duck |
A student information is read.
Daisy/Duck
Please enter a row number where the student wants to sit.
2
Please enter a column number where the student wants to sit. 0
The seat at row 2 and column 0 is assigned to the student D.D.
The current seating
——————–
#.#. #.#. #.#.
#.#. #.#. M.M.
D.D. #.#. #.#.
Please enter a student information or enter “Q” to quit. | |
Clarabelle/Cow |
A student information is read. | |
Clarabelle/Cow |
Please enter a row number where the student wants to sit.
2
Please enter a column number where the student wants to sit. 1
The seat at row 2 and column 1 is assigned to the student C.C.
The current seating
——————–
#.#. #.#. #.#.
#.#. #.#. M.M.
D.D. C.C. #.#.
Please enter a student information or enter “Q” to quit.
Max/Goof
A student information is read.
Max/Goof
Please enter a row number where the student wants to sit.
0
Please enter a column number where the student wants to sit. 0
The seat at row 0 and column 0 is assigned to the student M.G.
The current seating
——————– M.G. #.#. #.#.
#.#. #.#. M.M.
D.D. C.C. #.#.
Please enter a student information or enter “Q” to quit.
Horace/Horsecollar
A student information is read.
Horace/Horsecollar
Please enter a row number where the student wants to sit.
5
Please enter a column number where the student wants to sit.
1 row or column number is not valid.
A student Horace Horsecollar is not assigned a seat.
Please enter a student information or enter “Q” to quit. | ||
Sylvester/Shyster | ||
A student information is read. | ||
Sylvester/Shyster |
Please enter a row number where the student wants to sit.
2
Please enter a column number where the student wants to sit.
0
The seat at row 2 and column 0 is taken.
Please enter a student information or enter “Q” to quit.
Snow/White
A student information is read.
Snow/White
Please enter a row number where the student wants to sit.
-1
Please enter a column number where the student wants to sit. 0
row or column number is not valid.
A student Snow White is not assigned a seat.
Please enter a student information or enter “Q” to quit.
Jiminy/Criket
A student information is read. | ||
Jiminy/Criket | ||
Please enter a row number where the student wants to sit. | ||
The seat at row 0 and column 2 is assigned to the student J.C.
Grading Criteria for the part 2
05 pts: Every file contains header information
05 pts: adequate comment to explain every function
05 pts: consistent indentation and spacing
05 pts: it compiles
10 pts: The functions student_init are correct
05 pts: The function student_to_string method is correct
10 pts: The function examination_seating_init is correct
10 pts: The function assign_student_at (int, int, p) method is correct
05 pts: The function check_boundaries(int, int) method is correct
05 pts: The function examination_seating_to_string method is correct
#include <stdio.h> struct student { char last_name[30] ; char first_name[30];
};
struct examination_seating { struct student **seating;
};
void student_init_default (struct student *p ) {} void student_init (struct student *p, char *info) {} void student_to_string (struct student *p ) {}
void examination_seating_init (int rowNum, int columnNum, struct examination_seating *t ) {} int assign_student_at (int row, int col, struct examination_seating *t, struct student* p) {} int check_boundaries (int row, int col, struct examination_seating *t) {} void examination_seating_to_string (struct examination_seating *t ) {}
void main() {
struct examination_seating examination_seating; struct student temp_student;
int row, col, rowNum, columnNum; char student_info[30];
// Ask a user to enter a number of rows for a examination seating printf (“Please enter a number of rows for a examination seating.”); scanf (“%d”, &rowNum);
// Ask a user to enter a number of columns for a examination seating printf (“Please enter a number of columns for a examination seating.”); scanf (“%d”, &columnNum);
// examination_seating
examination_seating_init(rowNum, columnNum, &examination_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 examination that we created.) if (check_boundaries(row, col, &examination_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, &examination_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);
examination_seating_to_string(&examination_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);
}
}
Reviews
There are no reviews yet.