Sei sulla pagina 1di 9

K. J.

Somaiya College of Engineering, Mumbai-77


(Autonomous College Affiliated to University of Mumbai)

Batch: J2 Roll No.: 1915103

Experiment / assignment/ tutorial No. 12

Grade: AA / AB / BB / BC / CC / CD /DD

TITLE: To store and print the information for Time using structure.

AIM:
Define a structure to store the following information for Time PEriod

● Hours

● Minutes

● Seconds

Accept and store the information for 2 time period. Write a C program to add these 2
time periods, also display the difference between these 2 time periods.

Expected OUTCOME of Experiment:


The user will provide the two time inputs that is hours, minutes and seconds.
The program will compile and provide the sum and difference of two input time periods.

Books/ Journals/ Websites referred:

1. Programming in C, second edition, Pradeep Dey and Manas Ghosh, Oxford


University Press.
2. Programming in ANSI C, fifth edition, E Balagurusamy, Tata McGraw Hill.
3. Introduction to programming and problem solving, G. Michael Schneider ,Wiley
India edition.
4. Let’s C by Yashwant Kanetkar
5. http://cse.iitkgp.ac.in/~rkumar/pds-vlab/

Problem Definition:
Define a structure to store the following information for Time PEriod

● Hours

● Minutes

Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019


K. J. Somaiya College of Engineering, Mumbai-77
(Autonomous College Affiliated to University of Mumbai)

● Seconds

Accept and store the information for 2 time period. Write a C program to add these 2
time periods, also display the difference between these 2 time periods.

Algorithm:

1. Start.

2. Define a structure and members like hours,minutes,seconds etc.

3. Create instances of the structure according to our need.

4. store two time periods in two instances separately.

5. Usinglogic add and subtract time periods aand store them in different n distinct
instances.

6.After addition make sure the values of minutes and seconds are under 60 if not
using logic make proper changes.

7. Forsubtraction if seconds of the time period from which another timeperiod is to


be subtracted is lesser then use the borrow method of subtraction and accordingly
decrement minutes.similar procedure for minutes subtraction is to be followed.

8. Display the sum and difference of time periods.

9.Stop.

Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019


K. J. Somaiya College of Engineering, Mumbai-77
(Autonomous College Affiliated to University of Mumbai)

Implementation details:

#include<stdio.h>
#include<stdlib.h>
struct time
{

int hours; int min; int sec;


}t1,t2,t3,t4;
int main()
{
printf("Name:Megh Patel\nRoll No:1915103\nBatch:J2\n\n");
printf("enter the hours, minutes and seconds of time period t1\n");
scanf("%d",&t1.hours);
scanf("%d",&t1.min);
scanf("%d",&t1.sec);
printf("\nenter the hours, minutes and seconds of time period t2\n");
scanf("%d",&t2.hours);
scanf("%d",&t2.min);
scanf("%d",&t2.sec);
t3.hours=t1.hours + t2.hours; t3.min=t1.min +t2.min; t3.sec=t1.sec +t2.sec;

while(t3.sec>60)

{
t3.sec=t3.sec-60; t3.min=t3.min+1;
}
Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019


K. J. Somaiya College of Engineering, Mumbai-77
(Autonomous College Affiliated to University of Mumbai)
while(t3.min>60)
{
t3.min=t3.min-60; t3.hours=t3.hours+1;
}
printf("\nthe sum of two time periods is %d hours: %d min: %d sec\n",t3.hours,t3.min,t3.sec);
if(t2.sec>t1.sec)
{
t1.sec=t1.sec+60; t1.min=t1.min-1;
}
t4.sec=t1.sec-t2.sec; if(t2.min >t1.min)
{
t1.min=t1.min+60; t1.hours=t1.hours-1;
}
t4.min=t1.min-t2.min; t4.hours=t1.hours-t2.hours;
printf("\ndifference of two time periods is %d hours: %d min: %d sec\n",t4.hours,t4.min,t4.sec);
return 0;
}

Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019


K. J. Somaiya College of Engineering, Mumbai-77
(Autonomous College Affiliated to University of Mumbai)

Output(s):

Conclusion:
The program has successfully provide the output of sum of two timeperiods and
difference of two timeperiods.

Post Lab Descriptive Questions:


1. Write down the difference between the structure and union

Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019


K. J. Somaiya College of Engineering, Mumbai-77
(Autonomous College Affiliated to University of Mumbai)

BASIS OF
COMPARISON STRUCTURE UNION

Basic The separate memory location All members of the


is allotted to each member of 'union' share the same
the 'structure'. memory location.

Declaration struct struct_name{ union u_name{


type element1; type element1;
type element2; type element2;
. .
. .
} variable1, variable2, ...; } variable1, variable2,
...;

keyword 'struct' 'union'

Size Size of Structure= sum of size Size of Union=size of


of all the data members. the largest members.

Store Value Stores distinct values for all Stores same value for
the members. all the members.

At a Time A 'structure' stores multiple A 'union' stores a single


values, of the different value at a time for all
members, of the 'structure'. members.

Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019


K. J. Somaiya College of Engineering, Mumbai-77
(Autonomous College Affiliated to University of Mumbai)

BASIS OF
COMPARISON STRUCTURE UNION

Way of Viewing Provide single way to view Provide multiple way to to


each memory location. view same memory
location.

Anonymous No Anonymous feature. Anonymous union can be


feature declared.

Explain the meaning and purpose of following:


typedef keyword
it is used to give a specific variable name to a structure.
For eg instead os writing struct student () every where we can first use typedef and assign a
different specifc data type to our structure for our convenience and ease to not writng the big
structure syntax again n again.
sizeof operator
it is used to to get sizes of various datatypes. For eg: x=sizeof(int)
Here x will be given the value 4 which is size of integer datatype

Bit fields
In C, we can specify size (in bits) of structure and union members. The idea is to
use memory efficiently when we know that the value of a field or group of fields
will never exceed a limit or is withing a small range

Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019


K. J. Somaiya College of Engineering, Mumbai-77
(Autonomous College Affiliated to University of Mumbai)
For eg:
#include <stdio.h>

Space optimized representation of the date struct date {


d has value between 1 and 31, so 5 bits
are sufficient unsigned int d : 5;

m has value between 1 and 12, so 4 bits


are sufficient unsigned int m : 4;

unsigned int y;
};

int main()
{
printf("Size of date is %lu bytes\n", sizeof(struct date)); struct date dt = { 31, 12, 2014 };
printf("Date is %d/%d/%d", dt.d, dt.m, dt.y); return 0;
}
The above representation of ‘date’ takes 12 bytes on a compiler where an
unsigned int takes 4 bytes. Since we know that the value of d is always from 1 to
31, the value of m is from 1 to 12, we can optimize the space using bit fields.

Write the output of the given code with justification


#include<stdio.h> int main()
{
struct org
{
int employees; char comp[5];

struct founder
{
char ceo[10];
}p;
};
struct org x = {4000, "Ford", "Henry"};
printf("%s %d %s", x.comp, x.employees, x.p.ceo); return 0;
}

ANS:

Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019


K. J. Somaiya College of Engineering, Mumbai-77
(Autonomous College Affiliated to University of Mumbai)
Output: Ford4000Henry
Here the instance x of structure is created which is then initialized with some values.
As we can see here, 4000 is stored in employee member of structure instance x ,ford is stored in
company name of same instance and Henry is stored as ceo name of the same instance of structure.
And when the output instruction is given as written in program the program accesses the values
stored inside the instance x of structure org and we get the above output.

Date:02/12/2019
Signature of faculty in-charge

Department of Science and Humanities

Page No PIC Sem II/Jan-May 2019

Potrebbero piacerti anche