Lab 9 (C Programming) Solution
Mar 6, 2019programs written here are compiled and run in gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
1)Create a structure “student” (name, roll and marks), read all the attributes of the structure and display the information.
Source Code:
#include <stdio.h> struct student{ char name[30]; int roll; float marks; }; int main() { struct student s; printf("Enter student information:\n"); printf("Name:"); scanf("%[^\n]", s.name); printf("Roll:"); scanf("%d", &s.roll); printf("Marks:"); scanf("%f", &s.marks); printf("Information you entered is :\n"); printf("Name : %s\n", s.name); printf("Roll : %d\n", s.roll); printf("Marks : %f\n", s.marks); return 0; }
Output:
Enter student information: Name:Shyam Sundar Bhattarai Roll:34 Marks:78.77 Information you entered is : Name : Shyam Sundar Bhattarai Roll : 34 Marks : 78.769997
2)Write a program to create a structure of Cartesian coordinate point (x and y), read the two points from the user and find the mid-point and the distance between the points.
Source Code:
#include <stdio.h> #include <math.h> struct coordinate{ int x; int y; }; int main() { struct coordinate p1, p2, m; float dist; printf("Enter first point x and y respectively:"); scanf(" %d %d", &p1.x, &p1.y); printf("Enter second point x and y respectively:"); scanf(" %d %d", &p2.x, &p2.y); /* here the point will be in integer */ m.x = (p1.x + p2.x)/2; m.y = (p1.y + p2.y)/2; dist = sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); printf("Mid point(x, y)=(%d, %d)", m.x, m.y); printf("Distance = %f", dist); return 0; }
Output:
Enter first point x and y respectively:23 22 Enter second point x and y respectively:13 11 Mid point(x, y)=(18, 16)Distance = 14.866069
3)Write a program to create the structure Time(minute, hour and second), read two Times from the user, find the sum of the times entered by the user (create functions to read display and find the sum of the time, also create necessary logic for the sum of the time to be in proper format).
Source Code:
#include <stdio.h> struct Time{ int hour; int minute; int second; }; /* function prototypes */ void read(struct Time *t); void display(struct Time t); struct Time sum(struct Time t1, struct Time t2); int main() { struct Time t1, t2, sum_t; printf("Enter first time:\n"); read(&t1); printf("Enter second time:\n"); read(&t2); sum_t = sum(t1, t2); printf("Times are:"); display(t1); display(t2); printf("Sum is :"); display(sum_t); return 0; } /* function definations */ void read(struct Time *t){ /* pointer to member access technique */ printf("Enter hour:"); scanf("%d", &t->hour); printf("Enter minute:"); scanf("%d", &t->minute); printf("Enter second:"); scanf("%d", &t->second); } void display(struct Time t){ printf("\n%d : %d : %d\n", t.hour, t.minute, t.second); } struct Time sum(struct Time t1, struct Time t2){ struct Time temp; temp.hour = t1.hour + t2.hour; temp.minute = t1.minute + t2.minute; temp.second = t1.second + t2.second; if(temp.second >= 60){ temp.minute++; temp.second -= 60; } if(temp.minute >= 60){ temp.hour++; temp.minute-= 60; } if(temp.hour >= 24){ temp.hour -= 24; } return temp; }
Output:
Enter first time: Enter hour:11 Enter minute:46 Enter second:44 Enter second time: Enter hour:4 Enter minute:23 Enter second:45 Times are: 11 : 46 : 44 4 : 23 : 45 Sum is : 16 : 10 : 29
4)Define a structure “Complex” (typedef), read two complex numbers from the user and perform the addition and subtraction of these two Complex numbers and display the result.
Source Code:
#include <stdio.h> typedef struct complex{ float real; float imag; } Complex; /* function prototypes */ void read_complex(Complex *c); void display_complex(Complex c); Complex sum(Complex c1, Complex c2); Complex sub(Complex c1, Complex c2); int main() { Complex com1, com2, com_add, com_sub; printf("Enter first complex number(real, imag):"); read_complex(&com1); printf("Enter second complex number(real, imag):"); read_complex(&com2); com_add = sum(com1, com2); com_sub = sub(com1, com2); printf("Complex numbers are:\n"); display_complex(com1); display_complex(com2); printf("\n\nSUM\n\n"); display_complex(com_add); printf("\n\nDIFFERENCE\n\n"); display_complex(com_sub); return 0; } /* function defination */ void read_complex(Complex *c){ printf("Real part:"); scanf("%f", &c->real); printf("Imaginary part:"); scanf("%f", &c->imag); } void display_complex(Complex c){ printf("\n(real, imag) = (%f, %f)\n", c.real, c.imag); } Complex sum(Complex c1, Complex c2){ Complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag; return temp; } Complex sub(Complex c1, Complex c2){ Complex temp; temp.real = c1.real - c2.real; temp.imag = c1.imag - c2.imag; return temp; }
Output:
Enter first complex number(real, imag):Real part:12 Imaginary part:6 Enter second complex number(real, imag):Real part:6 Imaginary part:3 Complex numbers are: (real, imag) = (12.000000, 6.000000) (real, imag) = (6.000000, 3.000000) SUM (real, imag) = (18.000000, 9.000000) DIFFERENCE (real, imag) = (6.000000, 3.000000)
5)Write a program to create the structure “employee” (name, address and the salary). Create array of 8 employee, read the information from the user, finally sort the employee based on the name of the employee, then display the result.(implement the program using function)
Note: capital letters are considered to be come first
Source Code:
#include <stdio.h> #define N 8 struct employee{ char name[30]; char address[30]; float salary; }; void read(struct employee *e); void display(struct employee e); int find_length(char *str); int compare_string(char *str1, char *str2); void sort(struct employee e[], int size); int main() { struct employee emp[N]; int i; /* reading employee */ for(i = 0; i < N; i++){ printf("\nEmployee : %d\n", i+1); read(&emp[i]); } sort(emp, N); /* displaying sorted employees */ printf("\n SORTED EMPLOYEES:\n\n"); for(i = 0; i < N; i++){ display(emp[i]); } return 0; } void read(struct employee *e){ printf("Enter name:"); scanf(" %[^\n]", e->name); printf("Enter address:"); scanf(" %[^\n]", e->address); printf("Enter salary:"); scanf("%f", &e->salary); } void display(struct employee e){ printf("\nName : %s\n",e.name); printf("Address : %s\n",e.address); printf("Salary : %f\n",e.salary); } int find_length(char *str){ int i; for(i = 0; *(str + i) != '\0'; i++){ } return i; } /* function that copares two strings */ int compare_string(char *str1, char *str2){ int i, l1, l2; l1 = find_length(str1); l2 = find_length(str2); for(i = 0; *(str1+i) != '\0' || *(str2 + i) != '\0'; i++){ if(*(str1+i) > *(str2+i)){ return 1; }else if(*(str1+i) < *(str2+i)){ return -1; } } if(l1 == l2){ return 0; }else{ return -1; } } void sort(struct employee e[], int size){ struct employee temp; int i, j; for(i = 0; i < size - 1; i++){ for(j = i + 1; j < size; j++){ if(compare_string(e[i].name, e[j].name) == 1){ temp = e[i]; e[i] = e[j]; e[j] = temp; } } } }
Output:
Employee : 1 Enter name:shyam sundar bhattarai Enter address:kathmandu, nepal Enter salary:25000 Employee : 2 Enter name:bishnu sharma Enter address:bhainsipati Enter salary:4000 Employee : 3 Enter name:abhinandan sharma Enter address:jorpati, bhaktapur Enter salary:50000 Employee : 4 Enter name:Hariram shrestha Enter address:jorpati Enter salary:60000 Employee : 5 Enter name:rameshor sarki Enter address:kalikasthan Enter salary:40000 Employee : 6 Enter name:rameshor wagle Enter address:gothatar Enter salary:45000 Employee : 7 Enter name:barun kayestha Enter address:thamel kathmandu Enter salary:30000 Employee : 8 Enter name:birman shrestha Enter address:kailali, nepal Enter salary:40000 SORTED EMPLOYEES: Name : Hariram shrestha Address : jorpati Salary : 60000.000000 Name : abhinandan sharma Address : jorpati, bhaktapur Salary : 50000.000000 Name : barun kayestha Address : thamel kathmandu Salary : 30000.000000 Name : birman shrestha Address : kailali, nepal Salary : 40000.000000 Name : bishnu sharma Address : bhainsipati Salary : 4000.000000 Name : rameshor sarki Address : kalikasthan Salary : 40000.000000 Name : rameshor wagle Address : gothatar Salary : 45000.000000 Name : shyam sundar bhattarai Address : kathmandu, nepal Salary : 25000.000000