Course Update: Flow Charts

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

AMIE(I) STUDY CIRCLE(REGD.

)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 

Course Update
Flow Charts
Benefits of Algorithms
The use of algorithms provides a number of benefits. One of these benefits is in the
development of the procedure itself, which involves identification of the processes, major
decision points, and variables necessary to solve the problem. Developing an algorithm
allows and even forces examination of the solution process in a rational manner.
Identification of the processes and decision points reduces the task into |a series of smaller
steps of more manageable size. Problems that would be difficult or impossible to solve in
entirety can be approached as a series of small, solvable sub-problems.
By using an algorithm, decision making becomes a more rational process. In addition to
making the process more rational, use of algorithm will make the process more efficient and
more consistent. Efficiency is an inherent result of the analysis and specification process.
Consistency comes from both the use of the same specified process and increased skill in
applying the process. An algorithm serves as a mnemonic device and helps ensure that
variables or parts of the problem are not ignored. Presenting the solution process as an
algorithm allows more precise communication. Finally, separation of the procedure steps
facilitates division of labour and development of expertise.
A final benefit of the use of an algorithm comes from the improvement it makes possible. If
the problem solver does not know what was done, he or she will not know what was done
wrong. As time goes by and results are compared with goals, the existence of a specified
solution process allows identification of weaknesses and errors in the process. Reduction of a
task to a specified set of steps or algorithm is an important part of analysis, control and
evaluation.

ADVANTAGES OF USING FLOWCHARTS


The benefits of flowcharts are as follows:
 Communication: Flowcharts are better way of communicating the logic of a system
to all concerned.
 Effective analysis: With the help of flowchart, problem can be analysed in more
effective way.
 Proper documentation: Program flowcharts serve as a good program documentation,
which is needed for various purposes.
 Efficient Coding: The flowcharts act as a guide or blueprint during the systems
analysis and program development phase.
 Proper Debugging: The flowchart helps in debugging process.

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 1/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
 Efficient Program Maintenance: The maintenance of operating program becomes
easy with the help of flowchart. It helps the programmer to put efforts more
efficiently on that part.

LIMITATIONS OF USING FLOWCHARTS


Although a flowchart is a very useful tool, there are a few limitations in using flowcharts
which are listed below:
 Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
 Alterations and Modifications: If alterations are required the flowchart may require
re-drawing completely.
 Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart
becomes a problem.
 The essentials of what is done can easily be lost in the technical details of how it is
done.

LIMITATIONS OF USING FLOWCHARTS


Although a flowchart is a very useful tool, there are a few limitations in using flowcharts
which are listed below:
 Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
 Alterations and Modifications: If alterations are required the flowchart may require
re-drawing completely.
 Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart
becomes a problem.
 The essentials of what is done can easily be lost in the technical details of how it is
done.

Example (AMIE W12, 6 marks)

Draw a flow chart for finding maximum and minimum elements in a set of n elements.

Solution

See following flow diagram:

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 2/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 

Example (AMIE S16, 5 marks)

Design a flow chart to determine whether a given input string is a palindrome or not.

Solution

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 3/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 

Informatics
WLAN
A wireless local area network (WLAN) is a wireless computer network that links two or more
devices using a wireless distribution method (often spread-spectrum or OFDM radio) within a
limited area such as a home, school, computer laboratory, or office building. This gives users
the ability to move around within a local coverage area and yet still be connected to the
network. A WLAN can also provide a connection to the wider Internet.
Most modern WLANs are based on IEEE 802.11 standards and are marketed under the Wi-Fi
brand name.
Wireless LANs have become popular for use in the home, due to their ease of installation and
use. They are also popular in commercial complexes that offer wireless access to their
customers (often without charge).

RESPONSIBILITIES OF DATABASE ADMINISTRATOR


 Maintaining all databases required for development, testing, training and production
usage
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 4/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
 Installation and configuration of DBMS server software and related products.
 Upgrading and patching/hot-fixing of DBMS server software and related products.
 Migrate database to another server.
 Evaluate DBMS features and DBMS related products.
 Establish and maintain sound backup and recovery policies and procedures.
 Take care of the Database design and implementation.
 Implement and maintain database security (create and maintain logins, users and
roles, assign privileges).
 Performance tuning and health monitoring on DBMS, OS and application.
 Setup and maintain documentation and standards.
 Plan growth and changes (capacity planning).
 Do general technical troubleshooting and give consultation to development teams.
 Documentation of any implementation and changes (database changes, reference data
changes and application UI changes etc)
 Be able to provide a strategic database direction for the organization.
 Expert level knowledge of DBMS Architecture, all features in all versions and
troubleshooting skill

Responsibilities of Database Designer


 Determine purpose of database.
 Gather information that will be recorded in database.
 Divide information into tables.
 Turn information into columns.
 Specify primary keys.
 Set up table relationships.
 Refine and enhance design.
 Apply normalization rules to ensure tables are structured correctly.
 Make adjustments as necessary.
 Follow W3C programming standards to address accessibility guidelines.
 Develop structural design of various systems, applications, and databases for custom
database-driven website.
 Coordinate information systems with program objectives.
 Code, document, and test programs.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 5/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
 Create data migration/conversion techniques for system conversions or upgrades.
 Assist organizational members and clients with database-driven websites, network,
database, and application training and technical support.
 Assist staff with internal systems, applications, and databases, including developing
procedures, forms, and other organizational tools.

CLIENT SERVER TECHNOLOGY


Additional material

2-Tier Architecture
2-tier architecture is used to describe client/server systems where the client requests resources
and the server responds directly to the request, using its own resources. This means that the
server does not call on another application in order to provide part of the service.

3-Tier Architecture
In 3-tier architecture, there is an intermediary level, meaning the architecture is generally
split up between:
A client, i.e. the computer, which requests the resources, equipped with a user interface
(usually a web browser) for presentation purposes
The application server (also called middleware), whose task it is to provide the requested
resources, but by calling on another server
The data server, which provides the application server with the data it requires

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 6/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 

Comparing both types of architecture


2-tier architecture is therefore a client-server architecture where the server is versatile, i.e. it
is capable of directly responding to all of the client's resource requests.
In 3-tier architecture however, the server-level applications are remote from one another, i.e.
each server is specialized with a certain task (for example: web server/database server). 3-tier
architecture provides:
 A greater degree of flexibility
 Increased security, as security can be defined for each service, and at each level
 Increased performance, as tasks are shared between servers

Operating Systems
CPU SCHEDULING
 CPU or processor is one of the primary computer resources. All computer resources
like I/O, memory, and CPU ate scheduled for use.
 CPU scheduling is important for the operating system. In a multiprogramming and
time sharing system, the processor executes multiple processes by switching the CPU
among the processes, so that no user has to wait for long for a program to execute. To
enable running of several concurrent processes, the processor time has to be
distributed amongst all the processes efficiently.
 Scheduler is a component of the operating system that is responsible for scheduling
transition of processes. At any one time, only one process can be in running state and
the rest arc in ready or waiting state. The scheduler assigns the processor to different
processes in a manner so that no one process is kept waiting for long.
 Scheduling can be non-pre-emptive scheduling or pre-emptive scheduling. In non-pre-
emptive scheduling, the processor executes a process till termination without any
interruption. Hence the system resources are not used efficiently. In pre-emptive
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 7/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
scheduling, a running process may be interrupted by another process that needs to
execute. Pre-emption allows the operating system to interrupt the executing task and
handle any important task that requires immediate action. In pre-emptive scheduling,
the system resources arc used efficiently.
 There are many different CPU scheduling algorithms that are used to schedule the
processes. Some of the common CPU scheduling algorithms are as follows:
o First Come First Served (FCFS) Scheduling: As the name says, the process
that requests for the CPU first, gets the CPU first. A queue is maintained for
the processes requesting the CPU. The process first in the queue is allocated
the CPU first. FCFS scheduling is non-pre-emptive. The drawback of this
scheduling algorithm is that the process that is assigned to the CPU may take
long time to complete, keeping all other processes waiting in the queue, even
if they require less CPU time.
o Shortest Job First (SJF) Scheduling: The process that requires the least CPU
time is allocated the CPU first. SJF scheduling is non-pre-emptive. The
drawback of this scheduling is that a process that requires more CPU time may
have to wait for long time, since processes requiring less CPU time will be
assigned the CPU first.
o Round Robin (RR) Scheduling: It is designed for time-sharing systems. RR
scheduling is pre-emptive. In this scheduling, a small quantum of time (10-100
ms) is defined, and each process in the queue is assigned the CPU for this
quantum of time circularly. New processes arc added at the tail of the queue
and the process that has finished execution is removed from the queue. RR
scheduling overcomes the disadvantage of FCFS and SJF scheduling. A
process docs not have to wait for long, if it is not the first one in the queue, or,
if it requires CPU for a long period of time.

OPERATING STRUCTURE
The Operating System is a complicated piece of software. It consists of a number of routines
catering to IM, MM and PM as discussed earlier. Obviously, the size of the Operating System
is very large and it is not very wise to keep the full Operating System in the memory all the
time because very little space would be left for other application programs, due to the limited
size of the memory.
Therefore, the Operating System is divided into two parts. One consists of the very essential
routines which are required very often and almost all the time and the other consists of
routines which are required sometimes, but not always. In this sense, they are not vital. The
vital portion is called the Kernel of the Operating System. This is the innermost layer of the
Operating System close to the hardware, and controlling the actual hardware. It is the heart of
the Operating System. If you want to find out the memory overhead the Operating System
puts on the system, you should determine the size of the kernel. All the other routines are

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 8/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
loaded from the disk to the memory, as and when needed. This scheme saves the usage of
memory but then you lose the time it takes to load the required routines when necessary. This
is the trade-off which decides the size of the kernel and also the design of the Operating
System.
Depending upon the size and functionality included in the kernel and the organization of
different components within the Operating System, there are four broad approaches for
designing an Operating System:
 Monolithic (Simple) Operating System
 Layered Operating System
 Microkernel Operating System
 Exokernel Operating System

Monolithic (Simple) Operating System


This is the oldest of all the approaches used to design an Operating System. The modules of
the Operating System are not organized in a well-defined manner and a module can call any
other module without any major restriction. However, the Operating System does distinguish
between the system mode and user mode while executing an application programs. An
application program is run in the user mode. When it makes a system call, the Operating
System traps the system call, and executes it in the system mode. Once the execution of the
system call is complete, the execution of the application program resumes in the user mode.

Layered Operating System


The problems encountered while maintaining or enhancing Monolithic Operating Systems
prompted the designers of the Operating Systems to follow a much more systematic
approach. Layered Operating Systems proved to be the next logical step. As the name
suggests, the modules of the Operating Systems are divided into several layers stacked one
above the other, thus forming a hierarchical structure. The lowest layer (Layer 0) interacts
with the underlying hardware and the topmost layer (Layer N) provides an interface to the
application programs/users.

Microkernel Operating System


Layered Operating Systems are better organized and more systematic in terms of their design
and operation. However, generally they have proven to be less efficient. Moreover, as the
designers of the Operating Systems started adding more and more features, the kernel became
'too heavy ' and unmanageable. These obstacles gave birth to a totally new approach for
designing Operating Systems - the Microkernel approach. Microkernel has become a real
buzzword in the world of Operating Systems today with more and more Operating Systems
subscribing to this approach.
The basic concept of a Microkernel Operating Systems is very easy to understand. In this
approach, the kernel provides only the most essential Operating System functions like

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 9/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
process management, communication primitives and low-level memory management- System
programs or user level programs, implemented outside the kernel, provide the remaining
Operating System services. These programs are known as servers. As a result, the size of the
kernel reduces dramatically, making it a Microkernel. The application programs and various
servers communicate with each other using messages that pass through the Microkernel. The
Microkernel validates the messages, passes them between the various modules of the
Operating System and permits access to the hardware.

Programming in C
STRUCTURE
Structure is a collection of variables of different types under a single name.
For example: You want to store some information about a person: his/her name, citizenship
number and salary. You can easily create different variables name, citNo, salary to store these
information separately.
However, in the future, you would want to store information about multiple persons. Now,
you'd need to create different variables for each information per person: name1, citNo1,
salary1, name2, citNo2, salary2
You can easily visualize how big and messy the code would look. Also, since no relation
between the variables (information) would exist, it's going to be a daunting task.

A better approach will be to have a collection of all related information under a single name
Person, and use it for every person. Now, the code looks much cleaner, readable and efficient
as well.
This collection of all related information under a single name Person is a structure.
Structure Definition in C
Keyword struct is used for creating a structure.
Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 10/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
Note: Don't forget the semicolon }; in the ending line.
We can create the structure for a person as mentioned above as:
struct person
{
char name[50];
int citNo;
float salary;
};
This declaration above creates the derived data type struct person.
Structure variable declaration
When a structure is defined, it creates a user-defined type but, no storage or memory is
allocated.
For the above structure of a person, variable can be declared as:
struct person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct person person1, person2, person3[20];
return 0;
}
Another way of creating a structure variable is:
struct person
{
char name[50];
int citNo;
float salary;
} person1, person2, person3[20];
In both cases, two variables person1, person2 and an array person3 having 20 elements of
type struct person are created.
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 11/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
Accessing members of a structure
There are two types of operators used for accessing members of a structure.
Member operator(.)
Any member of a structure can be accessed as:
structure_variable_name.member_name
Suppose, we want to access salary for variable person2. Then, it can be accessed as:
person2.salary

Example of structure

Write a C program to add two distances entered by user. Measurement of distance should be
in inch and feet. (Note: 12 inches = 1 foot)
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} dist1, dist2, sum;

int main()
{
printf("1st distance\n");

// Input of feet for structure variable dist1


printf("Enter feet: ");
scanf("%d", &dist1.feet);

// Input of inch for structure variable dist1


printf("Enter inch: ");
scanf("%f", &dist1.inch);

printf("2nd distance\n");

// Input of feet for structure variable dist2


SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 12/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
printf("Enter feet: ");
scanf("%d", &dist2.feet);

// Input of feet for structure variable dist2


printf("Enter inch: ");
scanf("%f", &dist2.inch);

sum.feet = dist1.feet + dist2.feet;


sum.inch = dist1.inch + dist2.inch;

if (sum.inch > 12)


{
//If inch is greater than 12, changing it to feet.
++sum.feet;
sum.inch = sum.inch - 12;
}

// printing sum of distance dist1 and dist2


printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
return 0;
}
Output
1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances = 15'-5.7"

UNIONS
Unions are quite similar to structures in C. Like structures, unions are also derived types.
union car

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 13/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
{
char name[50];
int price;
};
Defining a union is as easy as replacing the keyword struct with the keyword union.
How to create union variables?
Union variables can be created in similar manner as structure variables.
union car
{
char name[50];
int price;
} car1, car2, *car3;
OR
union car
{
char name[50];
int price;
};

int main()
{
union car car1, car2, *car3;
return 0;
}
In both cases, union variables car1, car2 and union pointer variable car3 of type union car is
created.
Accessing members of a union
Again, the member of unions can be accessed in similar manner as structures.
In the above example, suppose you want to access price for union variable car1, it can be
accessed as:
car1.price
Likewise, if you want to access price for the union pointer variable car3, it can be accessed
as:
SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 14/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
(*car3).price
or;
car3->price
Difference between union and structure
Though unions are similar to structure in so many ways, the difference between them is
crucial to understand.
The primary difference can be demonstrated by this example:
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;

struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;

int main()
{
printf("size of union = %d", sizeof(uJob));
printf("\nsize of structure = %d", sizeof(sJob));
return 0;
}
Output
size of union = 32
size of structure = 40

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 15/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
ENUMERATION (ENUM)
An enumeration is a user-defined data type that consists of integral constants. To define an
enumeration, keyword enum is used.
enum flag { const1, const2, ..., constN };
Here, name of the enumeration is flag.
And, const1, const2,...., constN are values of type flag.
By default, const1 is 0, const2 is 1 and so on. You can change default values of enum
elements during declaration (if necessary).
// Changing default values of enum
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3,
};
Enumerated Type Declaration
When you create an enumerated type, only blueprint for the variable is created. Here's how
you can create variables of enum type.
enum boolean { false, true };
enum boolean check;
Here, a variable check of type enum boolean is created.
Here is another way to declare same check variable using different syntax.
enum boolean
{
false, true
} check;

Example: Enumeration Type

#include <stdio.h>
enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };
int main()
{
enum week today;

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 16/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
today = wednesday;
printf("Day %d",today+1);
return 0;
}
Output
Day 4

Q.1. (AMIE W14, 8 marks)

Explain "call by value" and "call by reference" in C language with the help of examples.

Answer

There are two ways to pass arguments/parameters to function calls - call by value and call by
reference. The major difference between call by value and call by reference is that in call by
value a copy of actual arguments is passed to respective formal arguments. While, in call by
reference the location (address) of actual arguments is passed to formal arguments, hence any
change made to formal arguments will also reflect in actual arguments.
In C, all function arguments are passed "by value" because C does not support references like
C++ and Java do. In C, the calling and called functions do not share any memory - they have
their own copy and the called function cannot directly alter a variable in the calling function;
it can only alter its private, temporary copy.
The call by value scheme is an asset, however, not a liability. It usually leads to more
compact programs with fewer extraneous variables, because parameters can be treated as
conveniently initialized local variables in the called routine. Yet, there are some cases where
we need call by reference:
The called function communicates to the calling function only through return statement and
return statement can only send only one value back to the calling function. If there are more
than one value we want to alter, call by reference is required
If the size of data is large , copying actual arguments to formal arguments could be a time
consuming operation and occupies more memory.
The call by value does not address above cases, hence we need call by reference. To achieve
call by reference functionality in C language the calling function provides the address of the
variable to be set (technically a pointer to the variable), and the called function declares the
parameter to be a pointer and access the variable indirectly through it. Since the address of
the argument is passed to the function, code within the called function can change the value
of the actual arguments.
Example using Call by Value

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 17/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
The classic example of wanting to modify the caller's memory is a swapByValue() function
which exchanges two values. For C uses call by value, the following version of swap
swapByValue() will not work...
#include <stdio.h>
void swapByValue(int, int); /* Prototype */
int main() /* Main function */
{
int n1 = 10, n2 = 20;
/* actual arguments will be as it is */
swapByValue(n1, n2);
printf("n1: %d, n2: %d\n", n1, n2);
}
void swapByValue(int a, int b)
{
int t;
t = a; a = b; b = t;
}
OUTPUT
n1: 10, n2: 20
The swapByValue() does not affect the arguments n1 and n2 in the calling function it only
operates on a and b local to swapByValue() itself. This is a good example of how local
variables behave.
Example using Call by Reference
In call by reference, to pass a variable n as a reference parameter, the programmer must pass
a pointer to n instead of n itself. The formal parameter will be a pointer to the value of
interest. The calling function will need to use & to compute the pointer of actual parameter.
The called function will need to dereference the pointer with * where appropriate to access
the value of interest. Here is an example of a correct swap swapByReference() function. So,
now you got the difference between call by value and call by reference!
#include <stdio.h>
void swapByReference(int*, int*); /* Prototype */
int main() /* Main function */
{
int n1 = 10, n2 = 20;

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 18/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
/* actual arguments will be altered */
swapByReference(&n1, &n2);
printf("n1: %d, n2: %d\n", n1, n2);
}
void swapByReference(int *a, int *b)
{
int t;
t = *a; *a = *b; *b = t;
}
OUTPUT
n1: 20, n2: 10

Example Q.2. (AMIE W15, 6 marks)

How is the structure in C different from union? Explain with examples.

Answer

Both are user defined data types to store data of different types as a single unit.
Now the differences goes as follows.
 The keyword struct is used to define a structure while keyword union is used to
define a union.
 When a variable is associated with a structure, the compiler allocates the memory for
each member. The size of structure is greater than or equal to the sum of sizes of
its members. The smaller members may end with unused slack bytes. While in case
of Union when a variable is associated with a union, the compiler allocates the
memory by considering the size of the largest memory. So, size of union is equal to
the size of largest member.
 Each member within a structure is assigned unique storage area of location while in
case of union memory allocated is shared by individual members of union.
 In Structure, the address of each member will be in ascending order This indicates
that memory for each member will start at different offset values while in unions the
address is same for all the members of a union. This indicates that every member
begins at the same offset value.
 In Structure, altering the value of a member will not affect other members of the
structure while in union altering the value of any of the member will alter other
member values.

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 19/20
AMIE(I) STUDY CIRCLE(REGD.)
COURSE UPDATE
COMPUTING & INFORMATICS A  Focused  Approach 
 In structure, individual member can be accessed at a time while in unions only one
member can be accessed at a time.
 Several members of a structure can initialize at once while in unions only the first
member can be initialized.

SECOND FLOOR, SULTAN TOWER, ROORKEE – 247667 UTTARAKHAND PH: (01332) 266328 Web: www.amiestudycircle.com 20/20

You might also like