Lab16 A
Lab16 A
md 2024-11-10
CSE115L Week:08(Structure)
Name:
ID:
Section:
Example 01 Define a structure named Time with members hours, minutes, and seconds. Write a
C program to input two times, add them, and display the result in proper time format.
#include <stdio.h>
// Define the structure "Time"
struct Time {
int hours;
int minutes;
int seconds;
};
int main() {
// Declare variables to store two times and the result
struct Time time1, time2, result;
// Input time1
printf("Input the first time (hours minutes seconds): ");
scanf("%d %d %d", &time1.hours, &time1.minutes, &time1.seconds);
// Input time2
printf("Input the second time (hours minutes seconds): ");
scanf("%d %d %d", &time2.hours, &time2.minutes, &time2.seconds);
// Add the two times
result.seconds = time1.seconds + time2.seconds;
result.minutes = time1.minutes + time2.minutes + result.seconds / 60;
result.hours = time1.hours + time2.hours + result.minutes / 60;
// Adjust minutes and seconds
result.minutes %= 60;
result.seconds %= 60;
// Display the result
printf("\nResultant Time: %02d:%02d:%02d\n", result.hours,
result.minutes, result.seconds);
return 0;
}
1/4
lab16_A.md 2024-11-10
Example 02: C Program that reads two distances (in feet+inches) and prints their sum:
#include <stdio.h>
int main()
{
D t1, t2, result;
2/4
lab16_A.md 2024-11-10
Example 03: Write a C program to store N students information & display top scorar inforamtion.
#include <stdio.h>
#include <string.h>
struct student
{
char name[50];
int id;
float marks;
};
3/4
lab16_A.md 2024-11-10
Try yourself 01: Create a struct called Student (see below) and read the records of two students
using it. Then print the name and id of the student who has higher CGPA than the other.
struct Student{
char name[50];
int id;
float CGPA;
};
Try yourself 02: Create a struct called “Employee” with the following members:
Name
Date of Birth (D.O.B., in short)
Starting Date
Salary Create an array of 4 employee variables; then read user input to fill up this array. Then display
the info of the employee who gets the highest salary.
Hint: You can use scanf("%d/%d/%d", &d, &m, &y); command to read dates in the above format.
Sample input/output:
Salary: 10000
Info of employee with highest salary:
Name: katty Harley
D.O.B: 12/4/1993
Starting date: 2/6/2016
Salary: 30000
4/4