Exercise 1.1 (40 points) Define a structure of student information including stu
ID: 3708419 • Letter: E
Question
Exercise 1.1 (40 points) Define a structure of student information including student ID, name, age, and score; input the information of N (N=3) students; sort the student information in descending order in terms of score and print the student information accordingly. Hint: use a structure array to store the information of N students; use a sort method, such as bubble sort and selection sort, to sort the array. . 3.2 Definition and usage of pointers Exercise 1.2 (20 points) Design a function which can output student information in descending order in terms of score. Structure variables shall be defined as pointer type. Hint: use the malloc function to allocate memory for structure pointer variable; 3.3 Relationship between pointers and arrays Exercise1.3 (20 points) Define a one-dimensional integer array, and a one-dimensional array of student structure; assign the value of integer array element to the score member of the student structure array. Structure pointer variables shall be used to perform the assignment of the structure variables. Hint: use malloc function to allocate memory for the pointer variables of the student structure. The pointer variable of the structure is pointed to the first element of the student structure array, and use “-->” to realize the assignment of structure variables. Exercise 1.4 (20 points) Define a pointer array to char, where each element of the array is a string, and define a char type double pointer variable. Output the string in the char pointer array in sequential order by using a double pointer. Attention? Char *[] is different from char (*)[], the former is a pointer array to char, while the latter is a one-dimensional array pointer to n elements Exercise 1.1 (40 points) Define a structure of student information including student ID, name, age, and score; input the information of N (N=3) students; sort the student information in descending order in terms of score and print the student information accordingly. Hint: use a structure array to store the information of N students; use a sort method, such as bubble sort and selection sort, to sort the array. . 3.2 Definition and usage of pointers Exercise 1.2 (20 points) Design a function which can output student information in descending order in terms of score. Structure variables shall be defined as pointer type. Hint: use the malloc function to allocate memory for structure pointer variable; 3.3 Relationship between pointers and arrays Exercise1.3 (20 points) Define a one-dimensional integer array, and a one-dimensional array of student structure; assign the value of integer array element to the score member of the student structure array. Structure pointer variables shall be used to perform the assignment of the structure variables. Hint: use malloc function to allocate memory for the pointer variables of the student structure. The pointer variable of the structure is pointed to the first element of the student structure array, and use “-->” to realize the assignment of structure variables. Exercise 1.4 (20 points) Define a pointer array to char, where each element of the array is a string, and define a char type double pointer variable. Output the string in the char pointer array in sequential order by using a double pointer. Attention? Char *[] is different from char (*)[], the former is a pointer array to char, while the latter is a one-dimensional array pointer to n elements Exercise 1.1 (40 points) Define a structure of student information including student ID, name, age, and score; input the information of N (N=3) students; sort the student information in descending order in terms of score and print the student information accordingly. Hint: use a structure array to store the information of N students; use a sort method, such as bubble sort and selection sort, to sort the array. . 3.2 Definition and usage of pointers Exercise 1.2 (20 points) Design a function which can output student information in descending order in terms of score. Structure variables shall be defined as pointer type. Hint: use the malloc function to allocate memory for structure pointer variable; 3.3 Relationship between pointers and arrays Exercise1.3 (20 points) Define a one-dimensional integer array, and a one-dimensional array of student structure; assign the value of integer array element to the score member of the student structure array. Structure pointer variables shall be used to perform the assignment of the structure variables. Hint: use malloc function to allocate memory for the pointer variables of the student structure. The pointer variable of the structure is pointed to the first element of the student structure array, and use “-->” to realize the assignment of structure variables. Exercise 1.4 (20 points) Define a pointer array to char, where each element of the array is a string, and define a char type double pointer variable. Output the string in the char pointer array in sequential order by using a double pointer. Attention? Char *[] is different from char (*)[], the former is a pointer array to char, while the latter is a one-dimensional array pointer to n elementsExplanation / Answer
Exercise 1.1:
#include <stdio.h>
#define N 3
struct student {
int stu_id;
char name[20];
int age;
int score;
};
int main()
{
struct student info[N] = {
{1, "alice", 23, 80},
{2, "bob", 24, 70},
{3, "john", 24, 90}
};
struct student temp = {0};
int i = 0;
int j = 0;
/* Sort the array using bubble sort */
for (i = 0; i < N - 1; i++) {
for (j = 0; j < N - i - 1; j++) {
if (info[j].score < info[j + 1].score) {
temp = info[j];
info[j] = info[j + 1];
info[j + 1] = temp;
}
}
}
printf("Student Id Name Age Score ");
printf("---------- ---- --- ----- ");
for (i = 0; i < N; i++) {
printf("%d %s %d %d ", info[i].stu_id, info[i].name,
info[i].age, info[i].score);
}
return 0;
}
Exercise 1.2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
struct student {
int *stu_id;
char *name;
int *age;
int *score;
};
void sort_structure(struct student *info)
{
struct student temp;
int i = 0;
int j = 0;
/* Sort the array using bubble sort */
for (i = 0; i < N - 1; i++) {
for (j = 0; j < N - i - 1; j++) {
if (*info[j].score < *info[j + 1].score) {
temp = info[j];
info[j] = info[j + 1];
info[j + 1] = temp;
}
}
}
return;
}
void clean_up(struct student *info)
{
int i = 0;
for (i = 0; i < N; i++) {
/* free structure parameters */
free(info[i].stu_id);
free(info[i].name);
free(info[i].age);
free(info[i].score);
}
}
int main()
{
struct student info[N];
int i = 0;
info[0].stu_id = malloc(sizeof(int));
*info[0].stu_id = 1;
/* +1 is to store '' at the end of the string */
info[0].name = malloc(strlen("alice") + 1);
strcpy(info[0].name, "alice");
info[0].age = malloc(sizeof(int));
*info[0].age = 23;
info[0].score = malloc(sizeof(int));
*info[0].score = 80;
info[1].stu_id = malloc(sizeof(int));
*info[1].stu_id = 2;
/* +1 is to store '' at the end of the string */
info[1].name = malloc(strlen("bob") + 1);
strcpy(info[1].name, "bob");
info[1].age = malloc(sizeof(int));
*info[1].age = 24;
info[1].score = malloc(sizeof(int));
*info[1].score = 70;
info[2].stu_id = malloc(sizeof(int));
*info[2].stu_id = 3;
/* +1 is to store '' at the end of the string */
info[2].name = malloc(strlen("john") + 1);
strcpy(info[2].name, "john");
info[2].age = malloc(sizeof(int));
*info[2].age = 24;
info[2].score = malloc(sizeof(int));
*info[2].score = 90;
/* Sort the structure */
sort_structure(info);
printf("Student Id Name Age Score ");
printf("---------- ---- --- ----- ");
for (i = 0; i < N; i++) {
printf("%d %s %d %d ", *info[i].stu_id, info[i].name,
*info[i].age, *info[i].score);
}
/* Free all the allocated memory */
clean_up(info);
return 0;
}
Exercise 1.3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
struct student {
int *stu_id;
char *name;
int *age;
int *score;
};
void sort_structure(struct student *info)
{
struct student temp;
int i = 0;
int j = 0;
/* Sort the array using bubble sort */
for (i = 0; i < N - 1; i++) {
for (j = 0; j < N - i - 1; j++) {
if (*info[j].score < *info[j + 1].score) {
temp = info[j];
info[j] = info[j + 1];
info[j + 1] = temp;
}
}
}
return;
}
void clean_up(struct student *info)
{
int i = 0;
for (i = 0; i < N; i++) {
/* free structure parameters */
free(info[i].stu_id);
free(info[i].name);
free(info[i].age);
}
}
int main()
{
int array[N] = {80, 70, 90};
struct student info[N];
int i = 0;
info[0].stu_id = malloc(sizeof(int));
*info[0].stu_id = 1;
/* +1 is to store '' at the end of the string */
info[0].name = malloc(strlen("alice") + 1);
strcpy(info[0].name, "alice");
info[0].age = malloc(sizeof(int));
*info[0].age = 23;
/* Assign the first value of integer array */
info[0].score = &array[0];
info[1].stu_id = malloc(sizeof(int));
*info[1].stu_id = 2;
/* +1 is to store '' at the end of the string */
info[1].name = malloc(strlen("bob") + 1);
strcpy(info[1].name, "bob");
info[1].age = malloc(sizeof(int));
*info[1].age = 24;
/* Assign the second value of integer array */
info[1].score = &array[1];
info[2].stu_id = malloc(sizeof(int));
*info[2].stu_id = 3;
/* +1 is to store '' at the end of the string */
info[2].name = malloc(strlen("john") + 1);
strcpy(info[2].name, "john");
info[2].age = malloc(sizeof(int));
*info[2].age = 24;
/* Assign the third value of integer array */
info[2].score = &array[2];
/* Sort the structure */
sort_structure(info);
printf("Student Id Name Age Score ");
printf("---------- ---- --- ----- ");
for (i = 0; i < N; i++) {
printf("%d %s %d %d ", *info[i].stu_id, info[i].name,
*info[i].age, *info[i].score);
}
/* Free all the allocated memory */
clean_up(info);
return 0;
}
Exercise 1.4
#include <stdio.h>
#define N 3
int main()
{
/* Pointer array to char */
char *array[N] = {"alice", "bob", "john"};
/* Char type double pointer variable */
char **double_array = NULL;
int i = 0;
/*
* Assign first element's address in array[] to double_array
* for accessing array[] elements using double_array pointer
*/
double_array = &array[0];
printf("Array Contents: ");
for (i = 0; i < N; i++)
printf("%s ", double_array[i]);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.