You are on page 1of 27

AMIE(I) STUDY CIRCLE(REGD.

)
A Focused Approach
Dated: 20-08-2010
Before taking printout of this chapter, please ensure that this material has not been covered
in the printed course material provided to you (by us).

Additional Course Material


Computer Basics, Hardware and Software
Additional Topic

Interrupt
In computing, an interrupt is an asynchronous signal indicating the need for attention or a
synchronous event in software indicating the need for a change in execution.
A hardware interrupt causes the processor to save its state of execution and begin execution
of an interrupt handler.
Software interrupts are usually implemented as instructions in the instruction set, which cause
a context switch to an interrupt handler similar to a hardware interrupt.
Interrupts are a commonly used technique for computer multitasking, especially in real-time
computing. Such a system is said to be interrupt-driven.
An act of interrupting is referred to as an interrupt request (IRQ).
Hardware interrupts were introduced as a way to avoid wasting the processor's valuable time
in polling loops, waiting for external events. They may be implemented in hardware as a
distinct system with control lines, or they may be integrated into the memory subsystem.
Interrupts can be categorized into: maskable interrupt, non-maskable interrupt (NMI), inter-
processor interrupt (IPI), software interrupt, and spurious interrupt.
Maskable interrupt (IRQ) is a hardware interrupt that may be ignored by setting a
bit in an interrupt mask register's (IMR) bit-mask.
Non-maskable interrupt (NMI) is a hardware interrupt that lacks an associated bit-
mask, so that it can never be ignored. NMIs are often used for timers, especially
watchdog timers.
Inter-processor interrupt (IPI) is a special case of interrupt that is generated by one
processor to interrupt another processor in a multiprocessor system.
Software interrupt is an interrupt generated within a processor by executing an
instruction. Software interrupts are often used to implement system calls because they
implement a subroutine call with a CPU ring level change.

POST BOX NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 1
TOTAL PAGES: 27
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
Spurious interrupt is a hardware interrupt that is unwanted. They are typically
generated by system conditions such as electrical interference on an interrupt line or
through incorrectly designed hardware.
Processors typically have an internal interrupt mask which allows software to ignore all
external hardware interrupts while it is set. This mask may offer faster access than accessing
an interrupt mask register (IMR) in a PIC, or disabling interrupts in the device itself. In some
cases, such as the x86 architecture, disabling and enabling interrupts on the processor itself
act as a memory barrier, however it may actually be slower.
An interrupt that leaves the machine in a well-defined state is called a precise interrupt.
Such an interrupt has four properties:
The Program Counter (PC) is saved in a known place.
All instructions before the one pointed to by the PC have fully executed.
No instruction beyond the one pointed to by the PC has been executed (that is no
prohibition on instruction beyond that in PC, it is just that any changes they make to
registers or memory must be undone before the interrupt happens).
The execution state of the instruction pointed to by the PC is known.

Primary (Main) Memory


Primary memory has direct link with input unit and output unit. It stores the input data,
calculation results. This primary memory is classified into two types of memory as follows :
Random Access Memory (RAM): As the name indicates, this memory is used to read and
write at any part of the memory. But it stores the data temporarily till the steady flow of
electricity. The data in RAM is lost, as soon as the power supply is off. So, RAM is also
called volatile memory.
Additional topic
Computer Architectures also impose an architectural constraint on the maximum allowable
RAM. This constraint is normally equal to 2WordSize memory locations.
RAM (Random Access Memory) is the most common form of Main Memory. RAM is
normally located on the motherboard and so is typically less than 300 mm from the CPU.
ROM (Read Only Memory) is like RAM except that its contents cannot be overwritten. ROM
memory is often used to store the boot or start-up program that a computer executes when
powered on.
Although slower than register memory, the contents of any location in RAM can still be
read or written very quickly. The time to read or write is referred to as the access time
and is the same for all RAM locations.
In contrast to register memory, RAM is used to hold both program code (instructions) and
data (numbers, strings etc). Programs that are running are normally loaded into RAM from
a disk prior to execution by the CPU.

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 2
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
Locations in RAM are identified by an addressing scheme e.g. numbering the bytes in RAM
from 0 onwards. The contents of RAM are lost if the power is turned off.
We can imagine Main Memory to be organised as a matrix of bits. Each row represents a
memory location, typically this is equal to the word size of the architecture, although it can be
a word multiple (e.g. 2 x Wordsize) or a partial word (e.g. half the wordsize). For simplicity
we will assume that data within Main memory can only be read or written a single row
(memory location) at a time.
For a 96-bit memory we could organise the memory as 12 8 bits, or 8 12 bits or 6 16
bits, or even as 96 1 bits or 1 96 bits. Each row also has a natural number Address which
is used for selecting the row:

Additional Topic

Cache Memory
In computer science, a cache (called kashe) is a component that improves performance by
transparently storing data such that future requests for that data can be served faster. The data
that is stored within a cache might be values that have been computed earlier or duplicates of
original values that are stored elsewhere. If requested data is contained in the cache (cache
hit), this request can be served by simply reading the cache, which is comparably faster.
Otherwise (cache miss), the data has to be recomputed or fetched from its original storage
location, which is comparably slower. Hence, the more requests can be served from the cache
the better the overall system performance is.
As opposed to a buffer, which is managed explicitly by a client, a cache stores data
transparently: This means that a client who is requesting data from a system is not aware that
the cache exists, which is the origin of the name cache (from French "cacher", to conceal).

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 3
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
To be cost efficient and to enable an efficient lookup of data, caches are comparably small.
Nevertheless, caches have proven extremely effective in many areas of computing because
access patterns in typical computer applications have locality of reference.
Main cache memories are
CPU cache. A CPU cache is a cache used by the central processing unit of a
computer to reduce the average time to access memory. The cache is a smaller, faster
memory which stores copies of the data from the most frequently used main memory
locations. As long as most memory accesses are cached memory locations, the
average latency of memory accesses will be closer to the cache latency than to the
latency of main memory. When the processor needs to read from or write to a location
in main memory, it first checks whether a copy of that data is in the cache. If so, the
processor immediately reads from or writes to the cache, which is much faster than
reading from or writing to main memory.
Disk cache. While CPU caches are generally managed entirely by hardware, a variety
of software manages other caches. The page cache in main memory, which is an
example of disk cache, is managed by the operating system kernel. While the hard
drive's hardware disk buffer is sometimes misleadingly referred to as "disk cache", its
main functions are write sequencing and read prefetching. Repeated cache hits are
relatively rare, due to the small size of the buffer in comparison to HDD's capacity.
However, high-end disk controllers often have their own on-board cache. In turn, fast
local hard disk can cache information held on even slower data storage devices, such
as remote servers (web cache) or local tape drives or optical jukeboxes. Such a
scheme is the main concept of hierarchical storage management. Nowadays, most
people use semiconductor memory like DRAM for disk cache. However, in the
future, MRAM is proposed as a replacement memory for disk cache use.
Web cache. Web browsers and web proxy servers employ web caches to store
previous responses from web servers, such as web pages. Web caches reduce the
amount of information that needs to be transmitted across the network, as information
previously stored in the cache can often be re-used. This reduces bandwidth and
processing requirements of the web server, and helps to improve responsiveness for
users of the web. Web browsers employ a built-in web cache, but some internet
service providers or organizations also use a caching proxy server, which is a web
cache that is shared among all users of that network.
Additional Topic
How data is stored in Hard Disk: A key concept in data storage is that it is linear, logical and
systematic in nature. When you 'save' data to the hard disk, the disk will follow a logical
system.
A hard disk can be compared to a large, square piece of graphing paper composed of squares
a thousand long and a thousand wide. Each individual square can accommodate one kilobyte

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 4
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
of data and thus a 'strip' having a thousand squares can store one megabyte of data. In such a
case, the disk head will 'fill' in the upper leftmost square first then continue down the line
one square at a time until all the appropriate squares are filled.
A second key concept to remember is that data storage follows a simple rule data is stored
(or 'written') on the first available space, wherever this may be.
Imagine that you are saving a 1-Mb Word document on the hard disk. Saving a 1 MB file
means the disk will systematically 'fill in' a thousand squares.
Additional Topic

DEVICE DRIVERS
In computing, a device driver or software driver is a computer program allowing higher-level
computer programs to interact with a hardware device.
A driver typically communicates with the device through the computer bus or
communications subsystem to which the hardware connects. When a calling program invokes
a routine in the driver, the driver issues commands to the device. Once the device sends data
back to the driver, the driver may invoke routines in the original calling program. Drivers are
hardware-dependent and operating-system-specific. They usually provide the interrupt
handling required for any necessary asynchronous time-dependent hardware interface.
A device driver simplifies programming by acting as a translator between a hardware device
and the applications or operating systems that use it. Programmers can write the higher-level
application code independently of whatever specific hardware device it will ultimately
control, because code and device can interface in a standard way, regardless of the software
superstructure or of underlying hardware. Every version of a device, such as a printer,
requires its own hardware-specific specialized commands. In contrast, most applications
utilize devices (such as a file to a printer) by means of high-level device-generic commands
such as PRINTLN (print a line). The device-driver accepts these generic high-level
commands and breaks them into a series of low-level device-specific commands as required
by the device being driven. Furthermore, drivers can provide a level of security as they can
run in kernel-mode, thereby protecting the operating system from applications running in
user-mode.

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 5
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach

Flow Charts
Example (AMIE Summer 2008, 7 marks)

Develop the flowchart representation for a program that would read an integer from the user
representing the number of terms in a Fibonacci series and then print the terms in the series.

Solution

In mathematics, the Fibonacci numbers are the numbers in the following sequence:
0, 1, 1, 2, 3, 5, 8, 13........
By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent number is the
sum of the previous two.
Flow chart is given below:

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 6
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
Example (AMIE Summer 2009)

A class has 30 students. Each student has a name (upto 30 characters) and roll number
(integer). Each student appears in an examination of 100 marks. The name of the students
who have scored more than class average need to be printed.

Solution

Example (AMIE Winter 2009, 8 marks)

Draw a flow chart of the given problem. Read marks of four subjects and print grade of the
student according to total marks obtained.
Total Marks Grade
Above 800 A
601 800 B
401 600 C
201 400 D
Below 200 F

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 7
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
Solution

Example (AMIE Summer 2010, 7 marks)

Write the flow chart representation of an algorithm names binary search, that takes a sorted
array, size of the array, and an element to search as argument and performs a binary search,
for the element in the array. If the element is found in the array, it should return the position
at which it occurs, otherwise it should return -1.

Solution

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 8
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach

Informatics
DATABASE MANAGEMENT SYSTEM
Additional topics

Advantages of DBMS
The advantages of DBMS are as follows:
Controlling redundancy
Providing storage structure for efficient query processing. A DBMS allows large
amounts of data to be stored in the most efficient manner possible. The data is stored
in lists of independent information. Additional data sets can be added at any time.
Relationships between the data sets are defined in the DBMS, and can be altered
without affecting the data.
Restricting unauthorized users. It is very common to have different access levels built
into a database system, and a DBMS makes this very easy to manage. Access can be
defined at the data table level. If the user does not have access to a specific table, they
cannot run any query that uses data from that table. Control at this level is easy to
maintain, and preferable to setting the access at the individual reporting level.
High processing speed. The processing speed and responsiveness of a computer
system is much better with a DBMS than with a linear computer program. Common
functions, such as count, sum, and calculate are programmed at the DBMS level. In a
DBMS, the programs to update, report, and store the information are set at the system
level.
Providing concurrency.
Providing backup and recovery. DBMS provide a separate architecture for the data
and the programming logic. This separation allows the database manager to back up
just the data to one hard drive and the programming onto another. Should the
hardware fail for either system, it is possible to redirect the program server to utilize
the data from the back-up, and vice versa.
Enforcing integrity constraints.

Disadvantages of DBMS
The disadvantages are as follows:
Centralization: That is use of the same program at a time by many user sometimes
lead to loss of some data.
High cost of software.
Technical expertise is required
power dependency
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 9
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
security issues

Normalization of Relational Database


In the field of relational database design, normalization is a systematic way of ensuring that a
database structure is suitable for general-purpose querying and free of certain undesirable
characteristicsinsertion, update, and deletion anomaliesthat could lead to a loss of data
integrity. Informally, a relational database table (the computerized representation of a
relation) is often described as "normalized" if it is in the Third Normal Form. Most 3NF
tables are free of insertion, update, and deletion anomalies. A standard piece of database
design guidance is that the designer should create a fully normalized design.
Objectives of normalization are
To free the collection of relations from undesirable insertion, update and deletion
dependencies;
To reduce the need for restructuring the collection of relations as new types of data
are introduced, and thus increase the life span of application programs;
To make the relational model more informative to users;
To make the collection of relations neutral to the query statistics, where these
statistics are liable to change as time goes by.

Number System, Boolean Algebra


Additional topic

HEXADECIMAL NUMBERS.
In principal, Hexadecimal numbers work in the exact same way as decimal numbers do.
Nearly all the same rules apply. The main difference is that there are more symbols. In the
case of Hexadecem, Latin for 16, there are 16. The first ten are old faithful 0, 1, 2, 3, 4, 5, 6,
7, 8 and 9. The six new ones are A, B, C, D, E, F. Just like we made a little counting table for
Decimal, we can make one for Hexadecimal as well:
0 1 2 3 4 5 6 7 8 9 A B C D E F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
Apart from the difference in the symbols and the different number of them, the Math rules in
Hexadecimal, which we will from now on simply call Hex, are the same as the rules in
Decimal.In Hexadecimal math, 4+1=5 and 6 + 3 = 9 and 9 + 3 = C. As in Decimal math, you
add an MSD to the left in Hex when you run out of Symbols, example in Hex: C +5 = 11 and
Hex 11 + 3C = 4D. Although this sounds simple and logical, our brain has trouble with it
since we are trained in decimal!

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 10
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
Hexadecimal Arithmetic. Covert hexadecimal number to binary number and then apply
arithmetic operations.
Additional Topic

COMBINATIONS CIRCUITS VS. SEQUENTIAL CIRCUITS


The main difference between sequential circuits and combinational circuits is that sequential
circuits compute their output based on input and state, and that the state is updated based on a
clock. Combinational logic circuits implement Boolean functions, so they are functions only
of their inputs, and are not based on clocks.
A sequential circuit uses flip flops. Unlike combinational logic, sequential circuits have state,
which means basically, sequential circuits have memory.

Here are some of the key things to notice:


Like combinational logic circuits, a sequential logic circuit has inputs (labelled with x
with subscripts) and outputs (labelled with z with subscripts).
Unike combinational logic circuits, a sequential logic circuit uses a clock.
Also, there is a box inside the circuit called State.
This box contains flip flops. Assume it has k flip flops. The flip flops basically store a
k-bit number representing the current state.
The output z is computed based on the inputs (x with subscripts) and the state coming
out of the state box (q with subscripts).
The state may be updated at each positive clock edge. When there's not a positive
clock edge, the state remains unchanged.
The information needed to update to the state (called the next state) comes from the
current state (the current value of q) and the input, which is fed through

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 11
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
combinational logic, and fed back into the state box, telling the state box how to
update itself.
For example, suppose you are currently in state 00, and see an input of 1. This may produce
an output of, say, 10, and then produce feedback that tells the state box to update to state 01
by the next clock edge.

Operating Systems
Time Sharing System
Additional lines
The labelled task state transition diagram for a time shared OS is shown in following figure.

Additional sub heading under heading WINDOWS

Windows vs. DOS


MS-DOS is the predecessor of Windows operating system software for PC. Basic difference
is that MS-DOS was a command line software and all commands had to be entered in text
format on a command line while in Windows mouse is widely used to click and execute
commands. Windows have had many versions since DOS and have been becoming more
powerful along with progress in PC hardware technologies. In Windows many programs can
run at the same time wile in DOS only one program was run at one time. Now also in
Windows commands can be executed through text command line interface.

Windows Vista
Windows Vista is an operating system expressed in several variations developed by
Microsoft for use on personal computers, including home and business desktops, laptops,
tablet PCs, and media center PCs. The release of Windows Vista came more than five years
after the introduction of its predecessor, Windows XP, the longest time span between
successive releases of Microsoft Windows desktop operating systems. It was succeeded by
Windows 7 which was released to manufacturing on July 22, 2009, and for the general public
on October 22, 2009.
Windows Vista contains many changes and new features, including an updated graphical user
interface and visual style dubbed Aero, a redesigned search function, multimedia tools
including Windows DVD Maker, and redesigned networking, audio, print, and display sub-

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 12
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
systems. Vista aims to increase the level of communication between machines on a home
network, using peer-to-peer technology to simplify sharing files and media between
computers and devices. Windows Vista includes version 3.0 of the .NET Framework,
allowing software developers to write applications without traditional Windows APIs.
While these new features and security improvements have garnered positive reviews, Vista
has also been the target of much criticism and negative press. Criticism of Windows Vista has
targeted its high system requirements, its more restrictive licensing terms, the inclusion of a
number of new digital rights management technologies aimed at restricting the copying of
protected digital media, lack of compatibility with some pre-Vista hardware and software,
and the number of authorization prompts for User Account Control. As a result of these and
other issues, Windows Vista had seen initial adoption and satisfaction rates lower than
Windows XP.

Windows 7
Windows 7 is a version of Microsoft Windows, a series of operating systems produced by
Microsoft for use on personal computers, including home and business desktops, laptops, net
books, tablet PCs, and media center PCs. Windows 7 was released to manufacturing on July
22, 2009, and reached general retail availability on October 22, 2009, less than three years
after the release of its predecessor, Windows Vista.
Unlike its predecessor, Windows Vista, which introduced a large number of new features,
Windows 7 was intended to be a more focused, incremental upgrade to the Windows line,
with the goal of being compatible with applications and hardware with which Windows Vista
is already compatible.

C Programming
Additional Programs

Example (AMIE Winter 2008, 6 marks)

The following program was written to interchange the values assigned to two integer
numbers:
# include <stdio.h>
void swap (int x, int y)
{
int p, q ; p = x ;
q = y ; x=q;

y = p;

}
void main ( ) {
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 13
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
int a = 5 ; int b = 3 ; swap (a, b) ;
printf <<"%d %d\n", a, b) ;
}

What will be the output of the program ? Explain why the above program will not give the
desired output. How will you modify the program so that the values are interchanged in the
function swap () and are displayed in function main ().

Solution

Output will be 5 3 i.e. a = 5. b = 3. No swapping is done.


Here 'a' and 'b' are local variables in the function main. When we call swap (a, b) only the
values of a and b are passed to the function swap. Inside swap, the local variables x and y get
values 5 and 3. These are exchanged with the help of local variables p and q. Thus the value
of x and y becomes 3 and 5. But the value of variable 'a' and 'b' in the function main is still 5
and 3 respectively. Thus when we print a and b, ~e see the values are not exchanged.
The modified program for swapping is shown belo # include <stdio.h>
void swap (int * x, int * y)
{
int p, q ;
p=*x
q=*y
*x=q
*y=p
}
void main ( )
int a = 5, b = 3 ;
swap (& a, & b) ; /* pass address of a and b */
printf ("% d, % d\n", a, b) ;
}
If the above program is run it will produce the output 3 and 5.

Example (AMIE Winter 2008, 12 marks)

Write a function to receive, as arguments, the number of elements in an array of integer


numbers and the pointer to the array. Evaluate average of the numbers in the array and
return the same to calling function.
Write a program to read 10 integer numbers and find their average. Use the function defined
above.
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 14
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
Solution

(i) float array-avg (int * a, int n) -


{

int sum = 0, i ;
float average ;
for (i = 0.; i < n ; + + i)
sum = sum + a [i] ;
average = sum/n ;
return (average) ;
}
(ii) # include <stdio.h> void main ( )
{

int x [10], i, n = 10 ;
float avg ;
for (i = 0 ; i < n ; + + i)
scanf ("% d", & x [i]) ;
avg = array-avg (x, 10) ;
printf ("\n the average is %f of \n", avg) ;
}

Example (AMIE W 2009, 6 marks)

Write a program to print four digit positive integer number in reverse order.

Solution

Method 1
#include<stdio.h>
#include<conio.h>
void main()
{
int abcd;
printf("enter a 4-digit no.");
scanf("%d",&abcd);
a=abcd/1000;
abcd=abcd%1000;
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 15
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
b=abcd/100;
abcd=abcd%100;
c=abcd/1o;
abcd=abcd%10;
d=abcd;
printf("reverse=%d",dcba);
getch();
}
Method 2
#include <stdio.h>
void main ( )
{ int num, i, j, rev;
j = 1000;
rev = 0;
printf ("Enter a 4 digit number\n");
scanf ("% d", & num); .
for (i = 0; i < 4; ++i)
{ rev = rev + (num% 10)* j;
num = num /10;
j = j/10;
}
printf ("The reverse is %d", rev);

Example (AMIE Winter 2009, 6 marks)

Write a program to add first seven terms in the following series:


1/1! + 2/2! + 3/3! + 4/4! + ........

Solution

#include<stdio.h>
void main ( )
{ int fact (int) ;
int i ;
float sum = 0.0;

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 16
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
for (i = 1; i < 7; ++i)
sum = sum + i/fact (i);
printf ("The summation is %f', sum);
}
int fact(i)
{ int prod = 1, p;
for (p = 1; P < = i; ++p)
prod = prod * p;
return (prod);

Example (AMIE Winter 2009, 6 marks)

Write a program to display all the prime numbers from 70 to 100.

Solution

Method 1
void main()
{
int i,num=70;
clrscr();
while(num<=100)
{
i=2;
while(i<=num)
{
if(num%i==0)
break;
i++;
}
if(i==num)
printf("\n%d is Prime",num);
num++;

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 17
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
getch();
}
Method 2
#include <stdio.h>
void main ()
{

int i, j, switch;
printf ("The prime numbers are\n");
for (i = 70; i < = 100; ++i)
{ switch = 0;
for (j = 2; j < i/2; ++j)
{ if (i% j = = 0)
switch = 1;
}

if (switch = = 1)
printf ("%d", i)
}
}

Example

Write a program to evaluate the factorial value of a number.

Solution

main()
{
int i=1,fact=1,n;
printf("enter a no.");
scanf("%d"n);
while("i<=n");
{
fact=i*fact;
i++;
}
printf("factorial=%d",fact);
getch();
}

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 18
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
Example (AMIE Winter 2009, 6 marks)

Write a program to find the length of given string.

Solution

#include <stdio.h>
# include <string.h>
void main ()
{

char S[80];
int length = 0, i;
printf ("Enter a string\n'');
gets (S) /* Read the string into S*/
i=0
while (S[i]! = '\0')
{ ++ length;
++ i;
printf ("The length of string is %d", length);.
}
printf ("The length of string is %d", length);
}

Example (AMIE Winter 2009)

Write a program to reverse a string.

Solution

Method 1
#include
#include
#include
void ReverseStr ( char *buff, int start, int end )
{
char tmp ;
if ( start >= end )
{

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 19
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
printf ( "\n%s\n", buff );
return;
}
tmp = *(buff + start);
*(buff + start) = *(buff + end);
*(buff + end) = tmp ;
ReverseStr (buff, ++start, --end );
}
int main()
{
char buffer[]="This is Test";
ReverseStr(buffer,0,strlen(buffer)-1);
return 0;
}
Method 2
for(int i = 0, j = reversed.Length - 1; i < j; i++, j--)
{
char temp = reversed[i];
reversed[i] = reversed[j];
reversed[j] = temp;
}
return new String(reversed);

Example

Write a C program to check for palindromes.

Solution

A palindrome is a word, phrase, number or other sequence of units that can be read the same
way in either direction
An example of a palidrome is "avon sees nova"
Method1
#include < stdio.h >
#include < string.h >
#include < stdlib.h >
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 20
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
#include < ctype.h >
void isPalindrome(char *string);
int main()
{
isPalindrome("avon sees nova");
isPalindrome("a");
isPalindrome("avon sies nova");
isPalindrome("aa");
isPalindrome("abc");
isPalindrome("aba");
isPalindrome("3a2");
exit(0);
}
void isPalindrome(char *string)
{
char *start, *end;
if(string)
{
start = string;
end = string + strlen(string) - 1;
while((*start == *end) && (start!=end))
{
if(start < end)start++;
if(end > start)end--;
}
if(*start!=*end)
{
printf("\n[%s] - This is not a palidrome!\n", string);
}
else
{
printf("\n[%s] - This is a palidrome!\n", string);

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 21
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
}
}
printf("\n\n");
}
Method2
#include < stdio.h >
#include < string.h >
#include < stdlib.h >
#include < ctype.h >
int isPalindrome(char string[]);
int main()
{
isPalindrome("avon sees nova");
isPalindrome("a");
isPalindrome("avon sies nova");
isPalindrome("aa");
isPalindrome("abc");
isPalindrome("aba");
isPalindrome("3a2");
return(0);
}
int isPalindrome(char string[])
{
int count, countback, end, N;
N = strlen(string);
end = N-1;
for((count=0, countback = end); count <= (end/2); ++count,--countback)
{
if(string[count]!=string[countback])
{
return(1);
}

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 22
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
}
printf("\n[%s] is a palidrome!\n", string);
return(0);
}

Example (AMIE Winter 2009, 6 marks)

Write a program to read names of students in a file and copy the data from that file to
another file.

Solution

#include <stdio.h>
void main ( )
{ FILE * fp I, *fp2;
int i;
charc;
fp 1 = fopen ("stud.txt", "r");
fp2 = fopen ("scopy.txt", "w");
if ((fp I = = '\0' || (fp-2 = = '\0))
{
printf ("copying failure \n");
exit ( );
}
while ((c = fgetc (fp1)! = EOF)
{
fputc (fp2);
}
fclose (fp 1);
fclose (fp2);
printf ("copying successfully done \n");
}

Example (AMIE Winter 2009, 6 marks)

Write a program to display the ASCII value of a given character.

Solution

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 23
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
#include <stdio.h>
void main ()
{
int i ;
char c;
printf ("Enter a character \n");
scanf ("%c", & c)
i=c
printf ("The ASCII code of the character is %d", i);

Example (AMIE Summer 2010, 7 marks)

Write the flow chart representation of an algorithm names binary search, that takes a sorted
array, size of the array, and an element to search as argument and performs a binary search,
for the element in the array. If the element is found in the array, it should return the position
at which it occurs, otherwise it should return -1. Write a program in C.

Solution

For flow chart, see chapter on "flow charts".


The program is
/* function to implement Binary -search */
int binary-search (int A [ ], int low, int high, int data)
{ while (low < = high)
{ int middle = low + (high - low)/2;
if (data> A [middle])
low = middle + 1;
else if (data < A [middle]).
high = middle - 1;
else
return middle;
}
return -1;
}

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 24
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach

C++ Programming
Example (AMIE Summer 2009)

A class has 30 students. Each student has a name (upto 30 characters) and roll number
(integer). Each student appears in an examination of 100 marks. The name of the students
who have scored more than class average need to be printed. Draw flow chart and write C ++
program.

Solution

Flow chart has been drawn in chapter of "Flow Charts".


C++ Program
Class student
{ private:
char name [30];
int rollno;
int marks;
public:
student ( ); / / constructor
void getdata( );
void showdata( );
int getmark( );
char * getname( );
int getrollno( );
}
void student:: getdata( )
{ cout << "Enter Name" ;
cin >> name;
cout >> "Enter rollno";
cin >> rollno;
cout << "Enter marks";
cin >> marks;
return;
}
void student:: showdata( )
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 25
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
{ cout << name;
cout << " " << roll no;
cout << marks << endl;
}
int student :: getmarks( )
{ return( marks)
}
char* st udent :: getname( )
return( name);
}
int student:: getrollno ( )
{ return(roll no);
# include <iostream.h>
int main ( )
float average'
student S[30]
int sum, i, x;
cout << "Enter student data";
for (i = 0; i < 30; ++i)
{ S[i]get data ( )
Sum = Sum + S[i].getmark( )
}
average = sum/ 30.0
cout << "Those scored above average are"
cout << endl ;
for (i = 0; i < 30; ++i)
{ x = S[i].getmarks( );
if (x > = average)
S[i]. showdata( );
}
}

P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 26
AMIE(I) STUDY CIRCLE(REGD.)
A Focused Approach
Example (AMIE
Write a program to check whether the given number is an Armstrong number.
Solution
Armstrong numbers are the sum of their own digits to the power of the number of digits. As
that is a slightly brief wording, let me give an example:
153 13 53 33 = 153
Each digit is raised to the power three because 153 has three digits. They are totalled and we
get the original number again!
C++ Program
# include <iostream.h>
# include <conio.h>
# include <math.h>
void main ()
{ clrscr();
int a,b=0,sum=0;
long int n;
cout<<"ENter the NO. : ";
cin>>n;
for(;n>0;)
//counts the digits
{ a=n%10;
n=n/10;
b++;
}
for(;n>0;)
{ a=n%10;
sum=sum+pow(a,b);
n=n/10;
}
if(sum==n)
{ cout<<"IT IS AN ARMSTRONG NUMBER...";
getch();
}
else
{ cout<<"IT IS NOT AN ARMSTRONG NUMBER...";
getch();
}
}
P.B. NO.77, 2 ND FLOOR, SULTAN TOWERS, ROORKEE 247667 UTTARANCHAL PH: (01332) 266328 Email : pcourses@hotmail.com 27

You might also like