View
219
Download
0
Category
Preview:
Citation preview
7/28/2019 C & Ds Tutorial First 2 Units
1/29
C & Data Structures 2 Units Course File
I UNIT:
AIM OF THIS UNIT :
This unit creates awareness among students about the basic concepts of computer,Itsorganization,different environments where it is made highly applicable for reaching eminent
technologies today.
Introduction to computer
The term computer is derived from the later word computare which means to compute or
calculate.so it is considered as calculating device.
Definition:-
It is an electronic device which converts raw data into the meaning information i.e
It is an electronic device which accepts the data,process it and produce it into desired output.
Basic operations of computer:-
i)Input:-It is the process of acquiring the information from the input devices like
keyboard,mouse etc..
ii)Process:-It is the transformation process to convert input data into output.
iii)Output:-It is the result which comes from the process
iv)Storing:-It is the process of saving the data or information so that they can be retained or
required whenever required.
v)Controlling:-It is the process of directing the manner and sequence in which all the operations
are to be performed.
7/28/2019 C & Ds Tutorial First 2 Units
2/29
Computer Block diagram
Central Processing Unit
Computer Software
It is divided into two parts.One is system software and second is application software.
Software system manages computer resources and it provides and interface between hardware
and users but it does nothing to directly serve the users need.
On the otherhand application Software is responsible for helping the user to solve their
problems.
Control unit
Memory UnitINPUT OUTPUT
Arithmatic & Logic
Unit
Secondary Memory
7/28/2019 C & Ds Tutorial First 2 Units
3/29
Computing Environments:-
There are four types of environments
i). Personal computing environments:-
In this we only have one single system,all of the computer hardware components are tied
together.in this environment ,if we give any task the entire process will be done in this system
only and we will give the appropriate outputs.
ii) Time sharing System:-
In this environment ,many users are connected to one or more computers,these may be mini
computers or central mainframe computers.In this environment the output devices such as
printers,auxillary storage devices,shared by all the users.This should be done by the centralsystem(Server System).
Disadvantages:1)All of these works tends to keep the computer busy
2)Because of busy the response is very slow.
iii) Client Server Environment:-
It splits the computing function b/w a central computer and users terminals.In this environment
,the user terminals are called clients.Advantages:-
Here response time and moniter display are faster than fast sharing computers and users are more
productive.
iv) Distributed computing environment:-
This system produces an integration of computing functions b/w different servers and clients.Theinternet provides the connectivity to different servers throughout the world.
Advantages:-This environment provides a reliable,scalable and highly available network.
7/28/2019 C & Ds Tutorial First 2 Units
4/29
Program Development Steps
To develop the program in high level language and translate into machine level language we are
having following steps.
1) Writing and editing the program2) Compiling the program3) Linking the program with the required library modules4) Executing the program
Writing and editing the program:-
The software used to write the program is known as text editor.It helps us enter,change,store the
character data.After completion of program we save the file to disk.This file is input to thecompiler,it is source file
Compiling the program:-
The code in a source file stored on a disk must be translated to machine lan.This is the job of
compiler.The C compiler is actually two separate programs one is pre.processor second one is
translator.The preprocessor reads the source code and prepares it for the translator.whilepreparing the code it scancs for special instructions known as preprocessor commands.These
commands tell the preprocessor to look for a special code libraries,make substitutions in the code
and in other way prepare the code for translation into machine language .The result of pre-processing is called the translation unit.
Linking the program:-
Since C program is made up of many functions we write some functions and they process our
source code,how ever there are other functions such as input,output ,library functions.The linkerassembles all of these functions in to executable file.
Executing the program:-
After completion of linking program,it is ready for execution.To execute the program,load the
program into primary numbering and execute it by command run.Getting the program into main
memory is the function of an operating system program known as loader.It locates the executablefile and reads into main memory,then execution starts.After the program process the data it
prepares the outputon the moniter.
7/28/2019 C & Ds Tutorial First 2 Units
5/29
Computer Languages:-
Computer language is defined as instructing a computer what to do with a special set of
instructions.which are then interpreted by the computer to perform particular task.
Computer language evolution is as follows1) . 1940s Machine language
2) . 1950s Symbolic language3) . 1960s High level language
Machine level language:-
In the earlier phase of computers, the only programming languages available were machine
language,which is made of streams of 0s and 1s.The instructions must be in 0s and 1s because the internal circuits of a computer are made up of
switches,transistors and other electronic devices that can be in two states on or off.
Advantages
1. High speed execution2. The computer can understand instructions directly.
3. No translation is required
Disadvantages
1). Machine dependant
2). Programming is very difficult3). Difficult to write error free programs
4). Difficult to understand and identify errors.
Symbolic languages:-
In early days of 1950s a special computer program was developed by Hopper that would
convert the programs in to machine language uses symbols or mneumonics to represent the
various machine language instructions.Because computer cannot understand symbolic language.
A special program calls Assembler that translates Symbolic code to machine languages codebecause symbolic languages had to be assembled into machine language,thats why they arecalled as Assembly Language
7/28/2019 C & Ds Tutorial First 2 Units
6/29
Advantages
1. High efficiency2. Easy to modify and isolate errors
3. Easy to understand and use
4.More control on hardware
Disadvantages
1). Machine dependant
2). Requires translators
3). Difficult to learn and write the program4). Slow development kind .
High level Language:-
These languages looks like a normal English.so programming using these languages are very
easy.It also requires languages are very easyIt also requires the translators to convert the programs into machine understandable form even
though symbolic languages greatly improved programming efficiency,working with symbolic
languwas very difficult because each machine instruction had to be individually coded.High level languages are codable to many different computers they must be converted to
machine language,this process of converting into machine language is known as compilation
The most widely used language FORTRAN,COBOL,C,C++,JAVA
Advantages
1. Easy to modify and isolate errors2. Machine independant and easy to understand and use
3.Low development cost and better readability
Disadvantages1). Needs translator
2). Requires high execution time
3).poor control on hardware.
7/28/2019 C & Ds Tutorial First 2 Units
7/29
7/28/2019 C & Ds Tutorial First 2 Units
8/29
format
Example : Draw a flow chart for average of three numbers
start
sto
Display c
c=a+b
Read a,b
7/28/2019 C & Ds Tutorial First 2 Units
9/29
Pseudocode:-.
An algorithm is a procedure for solving a problem in terms of the actions to be executed and the
order in which those actions are to be executed. An algorithm is merely the sequence of steps
taken to solve a problem. The steps are normally "sequence," "selection, " "iteration," and a case-
type statement.
In C, "sequence statements" are imperatives. The "selection" is the "if then else" statement, andthe iteration is satisfied by a number of statements, such as the "while," " do," and the "for,"
while the case-type statement is satisfied by the "switch" statement.
Pseudocode is an artificial and informal language that helps programmers develop algorithms.
Pseudocode is a "text-based" detail (algorithmic) design tool.
The rules of Pseudocode are reasonably straightforward. All statements showing "dependency"are to be indented. These include while, do, for, if, switch. Examples below will illustrate this
notion.
Examples:
1.
If student's grade is greater than or equal to 60Print "passed"
else
Print "failed"
2.
Set total to zero
Set grade counter to oneWhile grade counter is less than or equal to ten
Input the next grade
Add the grade into the totalSet the class average to the total divided by ten
Print the class average.
7/28/2019 C & Ds Tutorial First 2 Units
10/29
3.
Initialize total to zero
Initialize counter to zero
Input the first gradewhile the user has not as yet entered the sentinel
add this grade into the running total
add one to the grade counterinput the next grade (possibly the sentinel)
if the counter is not equal to zero
set the average to the total divided by the counterprint the average
else print 'no grades were entered'
4.
initialize passes to zero
initialize failures to zeroinitialize student to one
while student counter is less than or equal to ten
input the next exam resultif the student passed
add one to passes
elseadd one to failures
add one to student counter
print the number of passes
print the number of failuresif eight or more students passed
print "raise tuition"
7/28/2019 C & Ds Tutorial First 2 Units
11/29
II UNIT
AIM OF THIS UNIT :
After compilation of this unit, student will understand the basic concepts of the programming
language and can also able to write and execute the simple programs and get some tasks.
ABOUT C LANGUAGE :
C is a programming language developed at AT&Ts BELL Laboratory of USA in 1972. Dennis
Ritchie designed it. Because of its reliability. C is very popular. C stands between high-levellanguage (HLL) and Low Level Language (LLL). Cs compactness and coherence is mainly due
to the fact that it is a one-man language.
C is highly portable & it is well suited for structured programming. C program consists ofcollection of functions.
PROGRAM DEVELOPMENT STEPS :
Programming is nothing but Communicating with a computer that involves speaking the
language that computer understands. However this is very close to learn English language.
ALPHABETSWORDSSENTENCESPARAGRAPHS
ALPHABETS CONSTANTSDIGITS VARIABLES INSTRUCTIONS PROGRAM
SYMBOLS KEYWORDS
STRUCTURE OF C PROGRAM :
Each instruction in a C program is written as a separate statement. The program starts with a
main function followed by the opening braces which indicates the start of the function then thevariable and constant declarations followed by the statements, which includes input and output
statements.
C program may contain one or more sections as shown below.
DOCUMENTATION SECTION
LINK SECTION
DEFINITION SECTION
GLOBAL DECLARATION SECTION
Main() Function section
{
Declaration part
Executable part
}
SUBPROGRAM SECTION
User defined functions
7/28/2019 C & Ds Tutorial First 2 Units
12/29
EXAMPLE :
main(){
int p, n;
float r, si;p=100;
n=3;
r=8.5si=p*n*r/100;
printf(%f, si);
}
EXECUTION OF C PROGRAM :
Steps to be followed in writing and running a C program are
a) Creation of the source program
b) Compilation of the programc) Program Execution
OPERATOR:
An operator is a symbol that tells the computer to perform certain mathematical or logicalManipulations.
C has four types of operators1. Arithmetic Operators2. Relational Operators3. Logical Operators4. Bit-wise Operators
C Arithmetic Operators
Arithmetic operators include the familiar addition (+), subtraction (-), multiplication (*) and
division (/) operations. In addition there is the modulus operator (%) which gives the
remainder left over from a division operation. Let us look at some examples:
Logical Operators
The logical operators perform logical-AND (&&) and logical-OR ( || ) operations.
7/28/2019 C & Ds Tutorial First 2 Units
13/29
operator Description
&& The logical-AND operator produces the value 1 if both operands have nonzero
values. If either operand is equal to 0, the result is 0. If the first operand of a logical-
AND operation is equal to 0, the second operand is not evaluated.
|| The logical-OR operator performs an inclusive-OR operation on its operands. The
result is 0 if both operands have 0 values. If either operand has a nonzero value, the
result is 1. If the first operand of a logical-OR operation has a nonzero value, the
second operand is not evaluated.
The operands of logical-AND and logical-OR expressions are evaluated from left to right. If the value of
the first operand is sufficient to determine the result of the operation, the second operand is not
evaluated. This is called "short-circuit evaluation." There is a sequence point after the first operand.
See Sequence Pointsfor more information.
Examples
The following examples illustrate the logical operators:
otherint w, x, y, z;
if ( x < y && y < z )printf( "x is less than z\n" );
In this example, the printf function is called to print a message ifx is less than y and y is less than z.
Ifx is greater than y, the second operand (y < z) is not evaluated and nothing is printed. Note that this
could cause problems in cases where the second operand has side effects that are being relied on for
some other reason.
otherprintf( "%d" , (x == w || x == y || x == z) );
In this example, ifx is equal to either w, y, or z, the second argument to the printf function evaluates to
true and the value 1 is printed. Otherwise, it evaluates to false and the value 0 is printed. As soon as one
of the conditions evaluates to true, evaluation ceases.
http://msdn.microsoft.com/en-us/library/azk8zbxd%28v=vs.80%29.aspxhttp://msdn.microsoft.com/en-us/library/azk8zbxd%28v=vs.80%29.aspxhttp://void%280%29/http://void%280%29/http://void%280%29/http://msdn.microsoft.com/en-us/library/azk8zbxd%28v=vs.80%29.aspx7/28/2019 C & Ds Tutorial First 2 Units
14/29
C Relational Operators
Relational operators compare operands and return 1 for true or 0 for false. The following
relational operators are available:
Symbol Meaning
< Less than
> Greater than
= Greater than or equal to
== Equal to
!= Not equal to
C Bitwise Operators
The bitwise operators perform bitwise-AND (&), bitwise-exclusive-OR (^), and bitwise-inclusive-OR (|)
operations.
Syntax
AND-expression:
equality-expression
AND-expression & equality-expression
exclusive-OR-expression:
AND-expression
exclusive-OR-expression ^AND-expression
inclusive-OR-expression:
exclusive-OR-expression
inclusive-OR-expression | exclusive-OR-expression
The operands of bitwise operators must have integral types, but their types can be different. These
operators perform the usual arithmetic conversions; the type of the result is the type of the operands afterconversion.
7/28/2019 C & Ds Tutorial First 2 Units
15/29
The C bitwise operators are described below:
Operator Description
& The bitwise-AND operator compares each bit of its first operand to the
corresponding bit of its second operand. If both bits are 1, the corresponding resultbit is set to 1. Otherwise, the corresponding result bit is set to 0.
^ The bitwise-exclusive-OR operator compares each bit of its first operand to the
corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the
corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
| The bitwise-inclusive-OR operator compares each bit of its first operand to the
corresponding bit of its second operand. If either bit is 1, the corresponding result bit
is set to 1. Otherwise, the corresponding result bit is set to 0.
Examples
These declarations are used for the following three examples:
othershort i = 0xAB00;short j = 0xABCD;short n;
n = i & j;
The result assigned to n in this first example is the same as i (0xAB00 hexadecimal).
othern = i | j;
n = i ^ j;
The bitwise-inclusive OR in the second example results in the value 0xABCD (hexadecimal), while the
bitwise-exclusive OR in the third example produces 0xCD (hexadecimal).
Microsoft Specific
The results of bitwise operation on signed integers is implementation-defined according to the ANSI C
standard. For the Microsoft C compiler, bitwise operations on signed integers work the same as bitwise
operations on unsigned integers. For example, -16 & 99 can be expressed in binary as
other11111111 11110000
& 00000000 01100011_________________
http://void%280%29/http://void%280%29/http://void%280%29/7/28/2019 C & Ds Tutorial First 2 Units
16/29
00000000 01100000
The result of the bitwise AND is 96 decimal.
Special Operators likea) Increment & Decrement Operators
b) Conditional Operators
c) Assignment Operators
Increment Operators
Prefix: the value is incremented/decremented first and then applied.
Postfix: the value is applied and the value is incremented/decremented.
#include
main( ){
int i = 3,j = 4,k;k = i++ + --j;printf("i = %d, j = %d, k = %d",i,j,k);
}
i = 4, j = 3, k = 6
Decrement Operators
include #include
int main(){
char weight[4];int w;
printf("Enter your weight:");gets(weight);w=atoi(weight);
printf("Here is what you weigh now: %i\n",w);w--;printf("w++: %i\n",w);w--;
printf("w++: %i\n",w);
return(0);}
7/28/2019 C & Ds Tutorial First 2 Units
17/29
Enter your weight:123Here is what you weigh now: 123w++: 122w++: 121
Conditional Expressionconditional-expression:
logical-or-expression
logical-or-expression?expression:conditional-expression
The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator
works as follows:
The first operand is implicitly converted to bool. It is evaluated and all side effects are completedbefore continuing.
If the first operand evaluates to true (1), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated.
Assighnment operators
assignment operators are used to assigned the value of a variable or expression to a
variable. The syntax of assignment operators is:1var = expression;
2var = var;
Beside = operator, C programming language supports other short hand format which
acts the same assignment operator with additional operator such as +=, -=, *=, /=, %=.1var +=expression; //means
2var = var + expression;
Each assignment operator has a priority and they are evaluated from right to left based
on its priority. Here is assignment operator and its priority: =, +=, -=, *=, /=, %=.
Data types : The variety of data types available allow the programmer to select the type
appropriate to the needs to the application as will as the machine.
Ansi C supports four classes of data types:1). Primary data type s
2). User defined data types.
3). Derived data types.4).empty data types.
C has a concept of 'data types' which are used to define a variable before its use.
http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/glossary.html#definitionhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/glossary.html#definition7/28/2019 C & Ds Tutorial First 2 Units
18/29
7/28/2019 C & Ds Tutorial First 2 Units
19/29
short long signed unsigned
The modifiers define the amount of storage allocated to the variable. The amount ofstorage allocated is not cast in stone. ANSI has the following rules:
short int +2,147,483,647 (2Gb)
long int 4 32 -2,147,483,648 -> +2,147,483,647 (2Gb)
signed char 1 8 -128 -> +127unsigned char 1 8 0 -> +255
float 4 32double 8 64
long double 12 96
These figures only apply to todays generation of PCs. Mainframes and midrange
machines could use different figures, but would still comply with the rule above.
You can find out how much storage is allocated to a data type by using
the sizeofoperator.
Qualifiers
const volatile
http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/sizeof.htmlhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/sizeof.htmlhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/const.htmlhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/const.htmlhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/volatile.htmlhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/volatile.htmlhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/volatile.htmlhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/const.htmlhttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/sizeof.html7/28/2019 C & Ds Tutorial First 2 Units
20/29
7/28/2019 C & Ds Tutorial First 2 Units
21/29
7/28/2019 C & Ds Tutorial First 2 Units
22/29
IF STATEMENT :
If statement is also called as conditional statement. This will execute the statement only if
the condition is satisfied else the statement is not executed.Syntax or general form:
IF (condition is true)
Execute the statements;The operators used in the conditional expressions are = =,! =, < , > , =
EXAMPLE OF IF STATEMENT :
main ()
{int a, b;
printf( enter two numbers);
scanf(%d%d,&a,&b);if (a>b)
printf ( A is big);if (b>a)
printf( B is big);}
IF ELSE STATEMENT :
Syntax or general form :if (condition is true)
{Execute the statements;}
else
{Execute the statements;}
for example
main()
{int a,b;
printf( enter two numbers);
scanf(%d%d,&a,&b);if (a>b)
printf( A is big)
elseprintf( B is big);
}
7/28/2019 C & Ds Tutorial First 2 Units
23/29
SWITCH STATEMENTS : The switch statement is the execution of the IF-ELSE
statement. The switch statement makes one selection when there are several choices to be
made.
The general form of the switch statement is
switch (int expression)
{case cons1: statements;
case cons2: statements;
:
:
:default : statement;}
Example of the switch statement:
# include main ()
{
int i=1;printf (1. Choice 1);
printf(2. Choice 2);
printf(3. Choice 3);printf( select your option between 1 and 4);
scanf(%d, &i);
switch (i){
case 1:
printf( u have selected first choice);
printf(\n);break;
case 2:
printf( u have selected first choice);printf(\n);
break;
case 3:printf ( u have selected first choice);
printf (\n);
break;}}
7/28/2019 C & Ds Tutorial First 2 Units
24/29
Decision making and looping :
The C language provides three loop constructs for performing loop operations .
The while statement , the do statement ,the for statement .
WHILE LOOP :The while loop is best suited to repeat a statements as long as some condition is satisfied.General form of the while loop is
while (expression)
{
Statement;}
Where the statement may be a single statement or a compound statement.
EXAMPLE OF WHILE LOOP:
# include
main()
{
int counter = 1;while (counter
7/28/2019 C & Ds Tutorial First 2 Units
25/29
EXAMPLE OF DO-WHILE :
# includemain()
{
int counter=1;do
{
printf( now the counter value is %d\n,counter);counter++;
}
while (counter
7/28/2019 C & Ds Tutorial First 2 Units
26/29
OBJECTIVE TYPE QUESTIONS:
1. What is program?2. What is Algorithm?3. What is flow chart?4. What do you mean by problem solving?5. What is an operator?6. List out all types of Operators in C?7. What is Type Declarations Instruction?
8. What is Input/Output Instruction?
9. What is Arithmetic Instruction?10.What is Control Instruction?11.
What are decision-making instructions?12.What are Increment & Decrement Operators?
13.Distinguish between top-down and Bottom-up designs.14.Define Modular programming.15.Define debugging.16.what is the documentation.17.What do you mean by High Level Language (HLL) & Low Level Language (LLL)?18. What is Compiler and Compilation?19.List out all control statements in C?20.Write the general form of all Control statements?21.What is Break statement?22.What is Continue Statement?23.What is a Header file?24.What is a pre processor command?25.define symbolic constants?26.what are the commonly used input/output functions in C? how are they accessed ?27.what is meant by precision of an output data item?.28.what is the null statement.
PREVIOUS QUESTION PAPAERS:
1. a) What is a Flow Chart? Explain the different symbols used in the flow chart.b) Write a flow chart to find the maximum & minimum of given numbers.
(APR-2003)
2. a) What are Constants?b) Name the different data types that C supports and explain them in detail.
(APR-2003)
3. a) Explain the following and illustrate it with an example each:i) Increment & Decrement of the dataii) Conditional Operator
7/28/2019 C & Ds Tutorial First 2 Units
27/29
iii) Bit-wise Operatoriv) Assignment Operator
b) State the rules that applied while evaluating expression in automatic type conversion.(APR-2003)
4. a) What is an algorithm? Write the various criteria used for judging an algorithm.b) Write an algorithm to find the roots of a Quadratic equation for all the cases.
5.a) What are the basic characteristics of an algorithm? Explain them with an example.
b) Give the syntax of the following C construct and explain them. (i) Input-output statement(ii) Case statement (iii) function declaration (iv) Array Declaration.
(Jan 2003)
6.a) Explain the control statements in C-language.b) Discuss the operations on Strings and how they are implemented.
7a) Distinguish between Algorithm and program? Write flow chart to read 20 numbers andfor printing the maximum and minimum out of them.
b) Explain various Data types in C-Language. (Jan 2003)
8.a) Explain the different conditional statements in C-Language with examples.b) Write a C-program that reads a positive integer n and prints the factorial of n.
(Jan 2003)
9.a) What is token? Explain tokens in C-Language.b) What is an operator? Explain different operators in C-Language.
10).explain different control structures in C with program examples.
11). Distinguish between switch and nested if statements in C.12) Explain different iterative statements in C-Language with examples.
(Jan 2003)
Exercises programs :1). Write a C program to print the sum of digits of a given integer number as a single digit.2).write a C program to print out all Armstrong numbers between 1 and 500 . Armstrongnumber is a number whose sum of cubes of each digit is equal to the number itself.(ex 153).
3). write a C program to test whether a given integer is palindrome or not .
4).Write a C program to generate and print prime numbers upto a given integer number n.5).write a C program to convert and print a given decimal number to a binary number.
(Ex.. 25 = 0001 1001).
6).write a C program to generate and print Fibonacci numbers upto a given integer.
7).write a program to read the price of an item in Decimal form(like 15.95) and print the outputin paisa (like 1595 paise).
8 ).Given the string WORDPROCESSING ,write a program to read the string from theterminal and display the same in the following formats :a). WORD PROCESSING
b). WORD
PROCESSINGc).W.P.
9).write an algorithm to find the roots of quadratic equation for all the cases.
7/28/2019 C & Ds Tutorial First 2 Units
28/29
7/28/2019 C & Ds Tutorial First 2 Units
29/29
Recommended