From 6412bf99c5abe69576f96b9e950d1ee9c7e941e9 Mon Sep 17 00:00:00 2001 From: SharafatKarim Date: Sat, 12 Aug 2023 15:12:42 +0600 Subject: [PATCH] 11 | 12 - 17 | sharafat --- solutions/sharafat/11/12.c | 102 +++++++++++++++++++++++++++++++++++++ solutions/sharafat/11/13.c | 100 ++++++++++++++++++++++++++++++++++++ solutions/sharafat/11/14.c | 62 ++++++++++++++++++++++ solutions/sharafat/11/15.c | 98 +++++++++++++++++++++++++++++++++++ solutions/sharafat/11/16.c | 14 +++++ solutions/sharafat/11/17.c | 24 +++++++++ 6 files changed, 400 insertions(+) create mode 100644 solutions/sharafat/11/12.c create mode 100644 solutions/sharafat/11/13.c create mode 100644 solutions/sharafat/11/14.c create mode 100644 solutions/sharafat/11/15.c create mode 100644 solutions/sharafat/11/16.c create mode 100644 solutions/sharafat/11/17.c diff --git a/solutions/sharafat/11/12.c b/solutions/sharafat/11/12.c new file mode 100644 index 0000000..ce8a47a --- /dev/null +++ b/solutions/sharafat/11/12.c @@ -0,0 +1,102 @@ +#include +#include + +struct census +{ + char city[20]; + long int population; + float literacy; +}; + +void display_city(struct census city[]) +{ + int i; + printf("The details of the cities are:\n"); + printf(" City\t\tPopulation\tLiteracy rate\n"); + for (i = 0; i < 5; i++) + { + printf(" %s\t%ld\t\t%f\n", city[i].city, city[i].population, city[i].literacy); + } +} + +void sort_list_alphabetically(struct census city[]) +{ + int i, j; + struct census temp; + for (i = 0; i < 5; i++) + { + for (j = i; j < 5; j++) + { + if (strcmp(city[i].city, city[j].city) > 0) + { + temp = city[i]; + city[i] = city[j]; + city[j] = temp; + } + } + } + display_city(city); +} + +void sort_list_by_population(struct census city[]) +{ + int i, j; + struct census temp; + for (i = 0; i < 5; i++) + { + for (j = i; j < 5; j++) + { + if (city[i].population > city[j].population) + { + temp = city[i]; + city[i] = city[j]; + city[j] = temp; + } + } + } + display_city(city); +} + +void sort_list_by_literacy(struct census city[]) +{ + int i, j; + struct census temp; + for (i = 0; i < 5; i++) + { + for (j = i; j < 5; j++) + { + if (city[i].literacy > city[j].literacy) + { + temp = city[i]; + city[i] = city[j]; + city[j] = temp; + } + } + } + display_city(city); +} + +int main() +{ + struct census city[5]; + int i; + for (i = 0; i < 5; i++) + { + printf("Enter the name of the city: "); + scanf("%s", city[i].city); + printf("Enter the population of the city: "); + scanf("%ld", &city[i].population); + printf("Enter the literacy rate of the city: "); + scanf("%f", &city[i].literacy); + } + printf("\n"); + sort_list_alphabetically(city); + + printf("\n"); + sort_list_by_literacy(city); + + printf("\n"); + sort_list_by_population(city); + + return 0; +} \ No newline at end of file diff --git a/solutions/sharafat/11/13.c b/solutions/sharafat/11/13.c new file mode 100644 index 0000000..7e5cfd2 --- /dev/null +++ b/solutions/sharafat/11/13.c @@ -0,0 +1,100 @@ +#include + +struct hotel +{ + char name[20]; + int address; + int grade; + int average_room_charge; + int number_of_rooms; +}; + +void input_hotel(struct hotel *h, int number) +{ + int i; + for (i = 0; i < number; i++) + { + printf("Enter the name of the hotel: "); + scanf("%s", h[i].name); + printf("Enter the address of the hotel: "); + scanf("%d", &h[i].address); + printf("Enter the grade of the hotel: "); + scanf("%d", &h[i].grade); + printf("Enter the average room charge of the hotel: "); + scanf("%d", &h[i].average_room_charge); + printf("Enter the number of rooms in the hotel: "); + scanf("%d", &h[i].number_of_rooms); + printf("\n"); + } +} + +void sort_by_average_room_charge(struct hotel *h, int number) +{ + int i, j; + struct hotel temp; + for (i = 0; i < number; i++) + { + for (j = i; j < number; j++) + { + if (h[i].average_room_charge > h[j].average_room_charge) + { + temp = h[i]; + h[i] = h[j]; + h[j] = temp; + } + } + } +} + +void print_specific_grade(struct hotel *h, int number) +{ + int grade; + printf("Enter the grade: "); + scanf("%d", &grade); + int i; + for (i = 0; i < number; i++) + { + if (h[i].grade == grade) + { + printf("\tName: %s\n", h[i].name); + printf("\tAddress: %d\n", h[i].address); + printf("\tGrade: %d\n", h[i].grade); + printf("\tAverage room charge: %d\n", h[i].average_room_charge); + printf("\tNumber of rooms: %d\n", h[i].number_of_rooms); + printf("\n"); + } + } +} + +void print_charge_less_than(struct hotel *h, int number) +{ + int charge; + printf("Enter the max charge: "); + scanf("%d", &charge); + int i; + for (i = 0; i < number; i++) + { + if (h[i].average_room_charge < charge) + { + printf("\tName: %s\n", h[i].name); + printf("\tAddress: %d\n", h[i].address); + printf("\tGrade: %d\n", h[i].grade); + printf("\tAverage room charge: %d\n", h[i].average_room_charge); + printf("\tNumber of rooms: %d\n", h[i].number_of_rooms); + } + } +} + +int main() +{ + int n; + printf("Enter the number of hotels: "); + scanf("%d", &n); + struct hotel h[n]; + input_hotel(h, n); + + sort_by_average_room_charge(h, n); + print_specific_grade(h, n); + + print_charge_less_than(h, n); +} \ No newline at end of file diff --git a/solutions/sharafat/11/14.c b/solutions/sharafat/11/14.c new file mode 100644 index 0000000..cd6f20b --- /dev/null +++ b/solutions/sharafat/11/14.c @@ -0,0 +1,62 @@ +#include +#include + +struct cricket +{ + char player_name[20]; + char team_name[20]; + int batting_average; +}; + +void input_player(struct cricket *p, int number) +{ + int i; + for (i = 0; i < number; i++) + { + printf("Enter the name of the player: "); + scanf("%s", p[i].player_name); + printf("Enter the name of the team: "); + scanf("%s", p[i].team_name); + printf("Enter the batting average of the player: "); + scanf("%d", &p[i].batting_average); + printf("\n"); + } +} + +void sort_by_team_name(struct cricket *p, int number) +{ + int i, j; + struct cricket temp; + for (i = 0; i < number; i++) + { + for (j = i; j < number; j++) + { + if (strcmp(p[i].team_name, p[j].team_name) > 0) + { + temp = p[i]; + p[i] = p[j]; + p[j] = temp; + } + } + } +} + +void print_all_players(struct cricket *p, int number) +{ + int i; + printf("All players:\n"); + printf("Player name\tTeam name\tBatting average\n"); + for (i = 0; i < number; i++) + { + printf("%s\t\t%s\t\t%d\n", p[i].player_name, p[i].team_name, p[i].batting_average); + } +} + +int main() +{ + struct cricket player[50]; // make it a lower value to test + input_player(player, 50); + sort_by_team_name(player, 50); + print_all_players(player, 50); + return 0; +} \ No newline at end of file diff --git a/solutions/sharafat/11/15.c b/solutions/sharafat/11/15.c new file mode 100644 index 0000000..1db5bd1 --- /dev/null +++ b/solutions/sharafat/11/15.c @@ -0,0 +1,98 @@ +#include +#include + +struct date +{ + int day; + int month; + int year; +}; + +struct student_record +{ + char name[20]; + struct date date_of_birth; + int total_marks; +}; + +void input_date(struct date *date) +{ + printf("Enter date in DD/MM/YYYY format: "); + scanf("%d/%d/%d", &date->day, &date->month, &date->year); +} + +void validate_data(struct date *date) +{ + if (date->year < 1) + { + printf("Invalid year\n"); + exit(1); + } + if (date->month < 1 || date->month > 12) + { + printf("Invalid month\n"); + exit(1); + } + if ((date->day < 1 || date->day > 31) || (date-> day > 30 && (date->month == 4 || date->month == 6 || date->month == 9 || date->month == 11)) || (date->day > 28 && date->month == 2) || (date->day > 29 && date->month == 2 && (date->year % 4 != 0 || (date->year % 100 == 0 && date->year % 400 != 0)))) + { + printf("Invalid day\n"); + exit(1); + } +} + + +void print_date(struct date *date) +{ + printf("Date: %d/%d/%d\n", date->day, date->month, date->year); +} + +void input_student_record(struct student_record *record) +{ + printf("Enter the name of the student: "); + scanf("%s", record->name); + input_date(&record->date_of_birth); + validate_data(&record->date_of_birth); + printf("Enter the total marks of the student: "); + scanf("%d", &record->total_marks); +} + +void print_student_record(struct student_record *record) +{ + printf("Name: %s\n", record->name); + print_date(&record->date_of_birth); + printf("Total marks: %d\n", record->total_marks); +} + +void sort_by_total_marks(struct student_record *record, int number) +{ + int i, j; + struct student_record temp; + for (i = 0; i < number; i++) + { + for (j = i; j < number; j++) + { + if (record[i].total_marks > record[j].total_marks) + { + temp = record[i]; + record[i] = record[j]; + record[j] = temp; + } + } + } +} + +int main() +{ + int num = 5; + struct student_record record[num]; + int i; + for (i = 0; i < num; i++) + { + input_student_record(&record[i]); + } + sort_by_total_marks(record, num); + for (i = 0; i < num; i++) + { + print_student_record(&record[i]); + } +} \ No newline at end of file diff --git a/solutions/sharafat/11/16.c b/solutions/sharafat/11/16.c new file mode 100644 index 0000000..3040d9d --- /dev/null +++ b/solutions/sharafat/11/16.c @@ -0,0 +1,14 @@ +#include + +struct vector +{ + int *elements; + int size; +}; + +int main() +{ + struct vector v; + printf("The size of the vector is %lu bytes\n", sizeof(v)); + return 0; +} \ No newline at end of file diff --git a/solutions/sharafat/11/17.c b/solutions/sharafat/11/17.c new file mode 100644 index 0000000..bf7f62d --- /dev/null +++ b/solutions/sharafat/11/17.c @@ -0,0 +1,24 @@ +#include + +struct vector +{ + int *elements; + char size; + double double_size; +}; + +union union_vector +{ + int *elements; + char size; + double doule_size; +}; + +int main() +{ + struct vector v; + union union_vector u; + printf("The size of the struct is %lu bytes\n", sizeof(v)); + printf("The size of the union is %lu bytes\n", sizeof(u)); + return 0; +} \ No newline at end of file