Beruflich Dokumente
Kultur Dokumente
What Is A Computer?
A computer is an electronic device, operating under the control of
instructions (software) stored in its own memory unit, that can accept
data (input), manipulate data (process), and produce information
(output) from the processing.
Generally, the term is used to describe a collection of devices that
function together as a system.
Monitor Speaker
(output) (output) System unit
(Processor, memory)
Printer
Mouse
(input)
Scanner Keyboard
(input) (input)
Storage devices
(CD-RW, Floppy,
Hard disk, zip,)
What Does A Computer Do?
Computers can perform four general operations, which comprise the
information processing cycle.
Input
Process
Output
Storage
speed.
Reliability (low failure rate).
Accuracy.
Ability to store huge amounts of data and information.
Ability to communicate with other computers.
How Does a Computer know what to do?
It must be given a detailed list of instructions, called a compute program
Keyboard.
Mouse.
The Keyboard
The most commonly used input device is the keyboard on which data
is entered by manually keying in or typing certain keys. A keyboard
typically has 101 or 105 keys.
The Mouse
Memory
Output Devices
Output devices make the information resulting from the processing available
for use. The two output devices more commonly used are the printer and
the computer screen.
The printer produces a hard copy of your output, and the computer screen
produces a soft copy of your output.
Storage Devices
Auxiliary storage devices are used to store data when they are not
being used in memory. The most common types of auxiliary storage used on
personal computers are floppy disks, hard disks and CD-ROM drives.
.
Hard Disks
Computer Software
Computer software is the key to productive use of computers. Software can
be categorized into two types:
Operating system software
Application software.
Word processing
Electronic spreadsheet
Database
Presentation graphics
Word Processing
Word Processing software is used to create and print documents. A key
advantage of word processing software is that users easily can make
changes in documents.
Electronic Spreadsheets
Electronic spreadsheet software allows the user to add, subtract, and
perform user-defined calculations on rows and columns of numbers.
These numbers can be changed and the spreadsheet quickly recalculates
the new results.
Database Software
Allows the user to enter, retrieve, and update data in an organized and
efficient manner, with flexible inquiry and reporting capabilities.
Presentation Graphics
Presentation graphic software allows the user to create documents called
slides to be used in making the presentations. Using special projection
devices, the slides display as they appear on the computer screen.
Compliers and interpreters
High level languages are called either compiled languages or
interpreted languages.
In a compiled language, a translation program is run to convert the
programmers entire high level program, which is called the source
code, into a machine language code. This translation process is
called compilation.
Some of the most widely used compiled languages are c, c++, cobol ,
fortran.
In an interpreted language, a translation program converts each
program statement into machine code just before the program
statement is to be executed.
The most frequently used interpreted language is BASIC.
The compilation process
Source file --compiler ----object file ---linker ---executable
program
Machine Language
EG:
Opcode (Operation code) Operand (Address/Location)
Advantages and limitations of Machine Language
Programs written in machine language can be executed very fast by the computer. This
is because machine instructions are directly understood by the computer, and no
translation of the program.
Disadvantages
Machine dependent because the internal design of every type of computer is
different from every other type of computer, the machine language also differs
from computer to computer.
Difficult to program
It is necessary for the programmer to memorize the dozens of operation code
numbers for the commands.
Error prone(level)
For writing programs in machine language, since a programmer has to remember
the opcodes, and must keep track of the storage locations of data and instructions,
it becomes very difficult for him/her to concentrate fully on logic of the problem.
Difficult to modify: its difficult to correct or modify machine language
programs. Checking machine instructions to locate errors is very difficult and
time consuming.
In Short, writing a program in machine language is so difficult and time
consuming that it is rarely used today.
Assembly language
Assembly language programming, which was introduced in 1952, helped in
overcoming the above listed limitations of machine language programming.
A language, which allows instructions and storage locations to be represented
by letters and symbols, instead of numbers is called an assembly language or
symbolic language.
Assembler
An assembly language program must be converted into its equivalent machine
language program, before it can be executed on the computer. This translation is done
with the help of a translator program, which is known as assembler.
EG:
Mnemonic Opcode
ADD 14
SUB 15
Assembly language program------ Assembler ----------Machine language
program
Advantages
Easier to understand and use
Due to the use of mnemonics, instead of numeric op codes, and symbolic names
and locations, assembly language programs are much easier to understand.
Easier to locate and correct errors
Easier to modify
No worry about address
Easily relocatable
Algorithm Example
BEGIN
Step 1 : Accept 3 marks say Marks1, Marks2, Marks3 scored by the
student
Step 2 : Add Marks1, Marks2, Marks3 and store the result in Total
Step 3 : Divide Total by 3 and find the Average
Step 4 : Display Average
END
To find the average marks scored by a student in three subjects.
Read the problem statement and find out
What is the input required to solve the problem ( marks scored in 3 subjects in
case of the example above)
What is the processing needed on the input that will generate the desired
Output (the marks are added and the total is divided by 3 to get average)
What is the output (the result we get is average which is displayed back to the
user)
The above algorithm can be written in precise steps.
Algorithm-Example
Find the average marks scored by a student in 3 subjects:
BEGIN
Step 1 : Read Marks1, Marks2, Marks3
Step 2 : Sum = Marks1 + Marks2 + Marks3
Step 3 : Average = Sum / 3
Step 4 : Display Average
END
Use of Algorithms
Facilitates easy development of programs
Iterative refinement
Easy to convert it to a program
Review is easier
Write a program to find the average marks of a student. Also check whether the student
has passed or failed. For a student to pass, average marks should not be less than 65.
BEGIN
Step 1 : Read Marks1, Marks2, Marks3
Step 2 : Total = Marks1 + Marks2 + Marks3
Step 3 : Average = Total / 3
Step 4 : Set Output = Student Passed
Step 5 : if Average < 65 then Set Output = Student Failed
Step 6 : Display Output
END
Example Iterational
BEGIN
Step 1 : Read NumberOfStudents
Step 2 : Counter = 1
Step 3 : Read Marks1, Marks2, Marks3
Step 4 : Total = Marks1 + Marks2 + Marks3
Step 5 : Average = Total / 3
Step 6 : Set Output = Student Passed
Step 7 : If (Average < 65) then Set Output = Student Failed
Step 8 : Display Output
Step 9 : Counter = Counter + 1
Step 10 : If (Counter <= NumberOfStudents ) then goto step 3
END
Programming Methodologies
o Classification based on behavior
Sequential Approach
Event Driven Approach
o Classification based on structure
Structured Programming
Object Oriented Programming
Comments
Comments are used to document programs and improve readability
In C a comment will start with /* and ends with */
Syntax: /* Comments */
/*This is a single line comment */
/* This is a multiline
* comment in C */
/****************************************************
* This style of commenting is used for functions
****************************************************/
Variables
o Hold the data in your program
o Is a location (or set of locations) in memory where a value can be stored
for use by a program
o A quantity that can change during program execution
Variable Names
o A variable name can either begin with an alphabet or underscore
o No other special character except underscore is allowed
o Maximum characters recognized is 31 (compiler dependent)
o Variable names are case sensitive
o Keywords cannot be used as variable names
Data Types
o Data types determine the following:
Type of data stored
Number of bytes it occupies in memory
Range of data
Operations that can be performed on the data
o C supports the following data types:
int for storing whole numbers
char for storing character values, represents a single character
float
double
o Note: float and double data types are used to store fractional values.
o float can store up to 6 digits of precision
o double can store up to 12 digits of precision
Data types specify various formats that data can appear in.
The fundamental data types are the basic building blocks for all the other data
types.
Primitive data types:
o Basic/Primitive data types supported by C are:
o int for storing integer values. Size will vary from 2 bytes 4 bytes
o char for storing character values Size is 1 byte
o float for storing decimal values single precision values are stored
where number of digits of precision after the decimal point is 6.
o double used to store decimal values of 12-digit precision
o char data types in C are represented using ASCII coding format. ASCII
stands for American Standard Code for Information Interchange.
True and false values in C:
C does not have a boolean data type.
In C, a true value is any non zero value including negative numbers.
A false value is zero.
Declaration of variables
Syntax:
data-type [variable-name list]
data-type variable-name1 , variable-name2, ;
Example:
int iNum;
Declares a variable of int data type
The variables will contain some garbage value when they are declared.
A variable can be initialized when it is declared
int iNum = 10;
float fData = 2.3F ;
char cChoice = Y;
A complete C program:
Preprocessor Directives
C provides many built-in functions, also called as library functions.
These functions are declared in header files.
To use them, weve to include proper header files in our program using #include
statement.
e.g.:
#include <stdio.h>
More Header files in C:
math.h
string.h
limits.h
types.h
Each standard library has a corresponding header file containing the function prototypes
for all the functions in that library and definitions of various data types and constants
needed by those functions.
Operators In C
Assignment operator =
Arithmetic operators +, -, *, /, %
Relational operators >, >=, <, <=, == , !=
Logical operators !, &&, ||
Address operator &
Increment and Decrement operators ++, --
Compound Assignment Operators =, +=, -=, /=, *=, %=
sizeof operator
Arithmetic operators
The % (modulo operator) is used to find the reminder of a divide operation
o eg.int x=5,y=2;
o int z=5/2; will store 2 in z
o int z=5%2 will store 1 in z
o % operator cannot be applied to float data type. Hence the instruction 5.2 %
2.5 will result in error.
For comparing if 2 values are equal or not remember to use == and not a single =.
== indicates comparisons and = indicates assignment operation.
o eg. int x=5,y=3
o x==y will be considered as an conditional expression that will be evaluated to
true(non-zero) for false(zero)
o x=y will be considered as an assignment expression which will assign 3 (value of
y) to x. x after the instruction will have value 3.
Logical operators
! (NOT), && (AND), || (OR)
eg. consider 3 variables x,y,x containing values 1,2,3 respectively
x=1, y=2, z=3
The condition x==1 && y == 3 will evaluate to false
The condition x==1 && z==3 will evaluate to true
The condition x==1 || y==3 will evaluate to true
The condition x==1 || z==3 will evaluate to true
The condition !(x <2 && y==2) will evaluate to false
Address of Operator
Ampersand (&) is the address of operator
It is used to fetch the memory address of a variable
Precedence of Arithmetic Operators
Operator Priority
, / and % Highest
+ and - Lowest
The expression that is written within parenthesis is given highest priority
Fundamental Algorithms
Requirement :
At the end of Running your logic, the values in the variables should be exchanged, that is
A = 20
B = 10
Temp = A
A= B
B = Temp
Program Logic :
int a , b;
scanf(%d%d, &a,&b);
temp = a;
a = b;
b = temp;
EXCHANGING VALUES OF TWO VARIABLES
a = a + b;
b = a b;
a = a b;
Program Logic :
int a , b;
scanf(%d%d, &a,&b);
a = a + b;
b = a b;
a = a b;
Requirement :
User goes on entering the numbers as inputs, once the user is done with entering
giving the inputs, the logic should say the COUNT of Numbers he has inputted. We can
use arrays to store the numbers the user is inputting
Algorithm
int numberSet[100000]
char Choice;
int Count = 0, i,
for ( i = 0 ; i <= (100000 1) ; i++)
{
scanf(%d,&numberSet[i]);
Count++;
printf(Do you want to input more ?);
scanf(%c,&Choice);
if ((Choice == Y) || (Choice = = y))
continue;
else
break;
}
printf( Count of Numbers in the Array is %d , Count);
Requirement :
Have to get Set of Numbers from the user and Display the Summed up value of
the Set of Numbers.
int Sum = 0;
int Count, i, Value
printf( How many Numbers are in the set);
scanf(%d, &count);
for( i = 1; i <= Count ; count++)
{
Scanf(%d, &Value);
Sum = Sum + Value ;
}
FACTORIAL COMPUTATION
Requirement :
Have to get an integer value from the user and display its Factorial Value
Algorithm
1) Get the value to be computed from the user and store it in Val
2) Declare Variable for storing the result say Factorial
3) Store value 1 in the Factorial variable
4) Run a Loop that runs for i value from 1 to Val number of times
4.1) Multiply value in Factorial variable with i and store it back in
Factorial
5) Display the value in Factorial Variable
Program Sequence for the Logic :
int Val ;
int Factorial = 1;
int i;
printf(Enter the Value to compute Factorial for);
scanf(%d, &Val);
for( i = 1; i <= Val ; i++)
{
Factorial = Factorial * i ;
}
Printf(The Factorial of %d is %d , Val, Factorial);
#include<stdio.h>
#include<conio.h>
main()
{
int a=10;
int b=50;
int t;
clrscr();
printf("before swapping");
printf("%d%d",a,b);
t=a;
a=b;
b=t;
printf("after swapping");
printf("%d%d",a,b);
getch();
}
Output :
50,10
#include<stdio.h>
#include<conio.h>
main()
{
int a=10;
int b=50;
int t;
clrscr();
printf("before swapping");
printf("%d%d",a,b);
t=a;
a=b;
b=t;
printf("after swapping");
printf("%d%d",a,b);
getch();
}
Output :
50,10
Output:
108342
4283
************************************************************************
Counting
Problem
Given set of n students examination marks make a count of the number of students that
passed the examination. A pass awarded for all marks of 20 and above.
Example
Set of marls ={20,40,50,30,10,20}
No.of.passed =5
Algorithm development
I. Prompt the read the number of marks to be processed.
II. Initialize count to zero;
III. While there are still marks to be processed repeatedly do
a. Read next mark
b. If it is pass(ie >=20) then add one to count.
IV. Write out total number of passes
Count how many numbers are greater than or equal to 20(Using 2 step)
#include<stdio.h>
#include<conio.h>
main()
{
int sub[5]={10,20,30,40,50};
int count,ccount=0;
clrscr();
for(int i=0; i<5;i++)
{
if(sub[i]>=20)
{
count=1;
ccount=ccount+count;
}
}
printf("%d",ccount);
getch();
}
************************************************************************
Summation of a set of numbers
Problem
Given set of n numbers design an algorithm that adds these numbers and returns the
resultant sum.
Example
Set of marls ={20,40,50,30,10,20}
Sum=170
Algorithm description
I. Prompt and read in the number of numbers to be summed.
II. Initialize sum for zero numbers.
III. While less than n numbers have been summed repeatedly do
a. Read in next number
b. Compute current sum by adding the number read to the most recent sum.
IV. Write sum of n numbers.
Program
#include<stdio.h>
#include<conio.h>
main()
{
int mark[5]={23,43,56,12,40};
int sum=0,i;
clrscr();
for(i=0;i<5;i++)
{
sum=sum+mark[i];
}
printf("%d",sum);
getch();}
Factorial Computation
Problem :
Given a number n,compute n factorial where n>0
Example
N!=N*(N-1)!
4!=4*3!
3!=3*2!
2!=2*1!
1!=1*0!
4!=4*3*2*1=24.
Program
Method 1
#include<stdio.h>
#include<conio.h>
main()
{
int fn=1;
clrscr();
for(int i=5;i>0;i--)
{
fn=fn*i;
}
printf("%d",fn);
int n=6;
int fact=1;
clrscr();
while(n>0)
{
fact=fact*n;
n--;
}
Method 2
int n=5;
int fact=1;
clrscr();
do
{
fact=fact*n;
n--;
}while(n>0);
printf("%d",fact);
getch();
}
Program
Method 1
#include<stdio.h>
#include<conio.h>
main()
{
int a=0;
int b=1;
int c;
int i=2;
int n=10;
clrscr();
printf("%d%d",a,b);
while(i<=n)
{
c=a+b;
a=b;
b=c;
i--;
printf("%d" ,c);
}
}
Method 2:
#include<stdio.h>
#include<conio.h>
void main()
{
int x=0, y=1;
clrscr();
printf("%d, %d, ",x,y);
for(i=0;i<n;i++)
{
f=x+y;
x=y;
y=f;
printf("%d, ",y);
}// end of for loop
}//end of main
************************************************************************
Reversing the Digits of an integer
Problem:
Design an algorithm that accepts a positive integer and reverse the order of its digits.
Example
Intput 27953
Output 35972
Algorithm Description
I. Establish n, the positive integer to be reversed.
II. Set the initial condition for reversed integer q.
III. While the integer being reversed is greater than zero do
a. Use the remainder function to extract the right most digit of the number
being reversed.
b. Use integer division by 10 to remove the last rightmost digit from the
number being reversed.
program
#include<stdio.h>
#include<conio.h>
main()
{
int n=12356;
clrscr();
int q;
while(n>0)
{
q=n%10;
n=n/10;
printf("%d" ,q);}}
Method 2:
#include<conio.h>
void main()
{
int n,rev=0,r;
clrscr();
printf("enter the number : ");
scanf("%d",&n);
while (n>0)
{
r=n%10;
n=n/10;
rev=rev*10+r;
}// end of while loop
printf("%d",rev);
}
Base Conversion
Problem:
Convert a decimal integer to its corresponding octal representation
43%8=3
43/8=5
53 decimal equivalent is 43
3*80 =3
5*81 =40
program
void main()
{
int i=0,j=0,rem=0,a[10],b[10];
long int num;
clrscr();
printf("\nEnter a number :");
scanf("%ld",&num);
while(num)
{
if(num<8)
{
a[j++]=num;
break;
}
else
{
a[j++]=num%8;
num=num/8;
}
}
for(i=j-1;i>=0;i--)
b[rem++]=a[i];
printf("\nOctal equivalent :");
for(j=0;j<rem;j++)
printf("%d",b[j]);
getch();
}
Sample 2:
4.Base Conversion
________________
i) decimal to binary
#include<stdio.h>
#include<conio.h>
void main()
{
int i,k,num,arr[10],n=0;
scanf("%d",&num);
while(num>1)
{arr[i++]=num%2;
num=num/2;
}
arr[i]=num;
}
while(k=i;k>=0;k--)
n=n*10+arr[k];
prinrf("%d",n);
}//end of main
#include<stdio.h>
#include<conio.h>
void main()
{
int i,k,num,arr[10],n=0;
scanf("%d",&num);
while(num>1)
{arr[i++]=num%8;
num=num/8;
}
arr[i]=num;
}
while(k=i;k>=0;k--)
n=n*10+arr[k];
prinrf("%d",n);
}//end of main
#include<stdio.h>
#include<conio.h>
void main()
{
int i,k,num,n=0;
char arr[10];
scanf("%d",&num);
while(num>1)
{ arr[i++]=num%16;
num=num/16;
}
arr[i]=num;
}
while(k=i;k>=0;k--)
{
switch(arr[k])
{
case 10 : printf("A");break;
case 11 : printf("B");break;
case 12 : printf("C");break;
case 13 : printf("D");break;
case 14 : printf("E");break;
case 15 : printf("F");break;
default :
printf("%d",arr[k]);
}
}//end of main
Character to Number conversion
Problem : convert character in to number (ASCII)
ASCII:
ASCII stands for American Standard Code for Information Interchange.
Computers can only understand numbers, so an ASCII code is the numerical
representation of a character such as 'a' or '@' or an action of some sort. ASCII
was developed a long time ago and now the non-printing characters are rarely
used for their original purpose. Below is the ASCII character table and this
includes descriptions of the first 32 non-printing characters. ASCII was actually
designed for use with teletypes and so the descriptions are somewhat obscure. If
someone says they want your CV however in ASCII format, all this means is they
want 'plain' text with no formatting such as tabs, bold or underscoring - the raw
format that any computer can understand
Sample Program
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
do
{