Skip to content

Commit

Permalink
11 | 12 - 17 | sharafat
Browse files Browse the repository at this point in the history
  • Loading branch information
SharafatKarim committed Aug 12, 2023
1 parent c9afffa commit 6412bf9
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 0 deletions.
102 changes: 102 additions & 0 deletions solutions/sharafat/11/12.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <stdio.h>
#include <string.h>

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;
}
100 changes: 100 additions & 0 deletions solutions/sharafat/11/13.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <stdio.h>

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);
}
62 changes: 62 additions & 0 deletions solutions/sharafat/11/14.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <string.h>

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;
}
98 changes: 98 additions & 0 deletions solutions/sharafat/11/15.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <stdio.h>
#include <stdlib.h>

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]);
}
}
14 changes: 14 additions & 0 deletions solutions/sharafat/11/16.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

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;
}
Loading

0 comments on commit 6412bf9

Please sign in to comment.