0% found this document useful (0 votes)
6 views4 pages

Lab16 A

The document provides examples of C programs that utilize structures to manage and manipulate data. It includes programs for adding times, summing distances, and storing student information to find the top scorer. Additionally, it presents exercises for creating structures for student and employee records, emphasizing user input and data display.

Uploaded by

fabiha.noor.232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Lab16 A

The document provides examples of C programs that utilize structures to manage and manipulate data. It includes programs for adding times, summing distances, and storing student information to find the top scorer. Additionally, it presents exercises for creating structures for student and employee records, emphasizing user input and data display.

Uploaded by

fabiha.noor.232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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>

typedef struct Distance


{
int feet;
int inches;
} D;

int main()
{
D t1, t2, result;

printf("Enter feet and inches: ");


scanf("%d%d", &t1.feet, &t1.inches);

printf("Enter feet and inches: ");


scanf("%d%d", &t2.feet, &t2.inches);

result.feet = t1.feet + t2.feet;


result.inches = t1.inches + t2.inches;

while (result.inches >= 12)


{
result.feet++;
result.inches = result.inches - 12;
}

printf("Result : %d feet, %d inches\n", result.feet, result.inches);


return 0;
}

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

int topScorar(struct student s[], int N)


{
float max = 0;
int idx = 0;
for (int i = 0; i < N; i++)
{
if (s[i].marks > max)
{
max = s[i].marks;
idx = i;
}
}
return idx;
}
int main()
{
int i, N;
printf("N : ");
scanf("%d", &N);
struct student s[N];
printf("Enter information of students:\n");
// storing information
for (i = 0; i < N; ++i)
{
printf("Enter Student ID : ");
scanf("%d", &s[i].id);
// getchar();
// fflush(stdin);
char c;
while ((c = getchar()) != '\n'){}

printf("Enter Student Name : \n");


gets(s[i].name);
printf("Enter Student Marks : \n");
scanf("%f", &s[i].marks);
}
int index = topScorar(s, N);
printf("top scoar ID : %d, with %.2f, marks\n", s[index].id,
s[index].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:

Name: Bob Marley


D.O.B: 1/4/1993
Starting date: 12/6/2016
Salary: 20000

Name: Rob Harfey


D.O.B: 2/11/1982
Starting date: 16/12/2016
Salary: 20000

Name: katty Harley


D.O.B: 12/4/1993
Starting date: 2/6/2016
Salary: 30000
Name: Betty Simpson
D.O.B: 3/11/1980
Starting date: 25/11/2016

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

You might also like