Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: GENERAL PROGRAMING CONCEPTS
Description: THI NOTES IS ABOUT BASICS OF COMPUTER PROGRAMING THIS NOTES CAN TEACH YOU WHAT IS COMPUTER PROGRAMING AND ALSO GUIDE YOU WHERE TO START COMPUTER PROGRAMING IS BEST THINGS YOU CAN LEARN IN THE WORLD BECAUSE IT TEACH YOU HOW TO THINK . AND OPEN YOUR MIND AND THINK WHAT THINKS YOU CAN DO TO CHANGE THE WORLD THE BEST WAYS TO DO SOMETHING NEW IS TO LEAN PROGRAMING

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


CHAP1: GENERAL PROGRAMMING CONCEPTS
1
...
Once you have defined a new thing that
your computer can do
...




A computer programming therefore, can be defined as the process of writing and maintaining step-bystep instructions which can be interpreted by a computing system to perform a meaningful task
...
2 PROGRAMMING ANALYSIS
 To process or manipulate a particular set of data therefore the computer must be given an appropriate set
of instructions called programs
...

So the stored computer program can be executed at any time
...




The input data will be processed to produce certain desired results called the output
...
3 PROGRAMMING MODEL:


A computer program consists of two parts: code and data where the code is the set of instructions for
performing a task
...
(After running a c program,
the executable result is stored in computer memory called register)
...
Meaning that once you have get a
program working, its code won‟t change, but every time you run it, it will typically be working with
different data
...


Instructor: HODARI Audace

Page 1

1
...
Also known
as Low-Level language
...

In that case, every different CPU(computer) has its own unique machine language
...

Every different type of computer has its own unique instruction set
...



Assembly languages : This lying between machine languages and high-level languages
...
because
they allow programmer to substitute names for numbers whereas machine language consists of only
numbers
...
g :
A-for addition
 L-for load

C-for compare
M-for multiply

 High-Level Language: These are programming languages such as C, FORTRAN or PASCAL that
enables a programmer to write programs that are more or less independent of a particular type of
computer
...

Today, almost all computer programs are written in high-level language, whose instruction set is more
compatible with human languages
...
4 COMPILATION TERMINOLOGY
A program that is written in high-level language, must however be translated into machine language
before it can be executed
...



Compilers translate the entire program (written in high-level language) into machine language before
executing any of the instructions
...

It provides errors not of one line but errors of the entire program
...
It consumes little time for converting a source program to an object program
...




Interpreters: An interpreter reads only one line of a source code at a time and converts it to object
codes
...
The program written with an interpreter
can easily be read and understood by the other users as well
...
Anyone can
modify the source code
...
But the disadvantage is that it consumes more
time for converting a source program to an object program
...

Input

Interpreter/Compiler

Final output

COMPARISON BETWEEN A COMPILER AND AN INTERPRETER:

Interpreter

Compiler

1
...


It reads the entire program and converts it
to the object code
...
It is easier
...


3
...


Instructor: HODARI Audace

Page 3

object program
...
5
...
Define or understand the problem: First of all, you must understand a problem, if you don‟t know the
problem, you can‟t find a solution as desired output
...
Analyze the problem: Once you know what the problem is, you can analyze it and make a plan to
resolve it by thinking about the input data
...
Develop an algorithm and flowchart: This is a process whereby a set of instructions are used to
produce a solution to a given problem
...
Writing the code: This is the next step where you write the codes for the program to make it work
...
Compiling and debugging the code: Once the program coding is completed, you compile your
program means you translate the source code to object code and if there are errors, you debug them
...

7
...

1
...
DEBUGGING THE COMPUTER PROGRAM


Note that errors are unavoidable part of program development
...
So the process of tracking them down and correcting them if they
are in a computer program is therefore, called Debugging
...

 Run-time error
...

Errors
Errors are mistakes which we the programmers make
...
Syntax (compile) error:
Errors in the syntax, or word structure of a program are caught before you run it, at compilation time by
the compiler program
...
However, a compiler will usually not stop at the first error it
encounters but will attempt to continue checking the syntax of a program right to the last line before
Instructor: HODARI Audace

Page 4

aborting
...

Example: spelling printf as prinntf or printif
You can also receive a compiler error, if accidentally use the wrong punctuation or place punctuation in the
wrong place
...

c) Logic error:
Your application runs but produces incorrect result
...

Note that errors are unavoidable part of program development
...
Using this information,
you can edit your source code to correct the error
...


1
...
ALGORITHMS & FLOWCHART


An algorithm is a detailed sequence of steps that are needed to resolve a given problem
...


BASIC SYMBOLS OF A FLOWCHART

Start

Process,
Instructi

Instructor: HODARI Audace

Page 5

Que

Input
/Out

Comments, Explanations,

Definitions

FLOWCHART EXAMPLES:
1
...
0
...




This means that you can use C to create lists of instructions for a computer to follow
...

2
...
FEATURES OF C:

Instructor: HODARI Audace

Page 6



C programs are highly portable means that a program has capacity to run in different environments
...




C programming language is well suited for structured programming
...
2
...

2
...

4
...

6
...


Include header file section
...

Function main
...

Executable part
...

Comments
...
Each
header file by default is extended with
...
The file should be included using # include directive as
given below
...
h> is to allow the use of the printf()
statement to provide program output
...


Example:
#include ...
h> file is included i
...
all definitions and prototypes of function defined in this
file are available in the current program
...
These
variables are known as global variables
...

Function main: Every program written in C language must contain main () function
...
The function main () is a starting point of every „C‟ program
...

The program execution starts from the opening brace ({ ) and ends with the closing brace ( })
...

Declaration part: The declaration part declares the entire variables that are used in executable part
...
The initialization means providing initial
values to the variables
...
This part
contains a set of statements or a single statement
...
Eg
...
”), scanf(“…
...
These
functions are generally defined after the main () function
...

Comments: Comments are not necessary in the program
...
Comments are used for documentation
...

Instructor: HODARI Audace

Page 8

The compiler does not execute comments
...

2
...
1
...
h>
main()
{
Clrscr();
printf("Hello!");
getch();
}
Program

Compile

Run

Output

DESCRIPTION


When executed, this program instructs the computer to print out the line "Hello!"



This C program starts with #include ...




The clrscr(); this statement is used for clearing the screen for the new output display
...




The printf statement in C allows you to send output to standard out (for us, the screen)
...




The portion in quotes “Hello!” is called the format string and describes how the data is to be formatted
when printed
...

/* Program to calculate the area of a circle*/
Instructor: HODARI Audace

/* Title Comment */

Page 9

#include ...
14*radius*radius;

/* Assignment Statement */

printf(“Area of circle= %f”,area);

/* output Statement */

getch();
return 0;
}
2
...
PROGRAMMING RULES:


A programmer while writing a program should follow the following rules:

1) All statements should be written in lower case letters
...

2)
3) Blank spaces may be inserted between words
...

4) It is not necessary to fix the position of statement in the program i
...
the programmer can write the
statement anywhere between the two braces following the declaration part
...

The following statements are valid
...
The opening and closing braces should be balanced i
...
for example, if opening braces are four; then
closing braces should also be four
...
Every C statement should be ended with Semi-colon (;)

THE „C‟ KEYWORDS
The C keywords are reserved words by the compiler
...

The keywords can not be used as variables names because they have been assigned fixed jobs
...
It is
suggested not to mix up keywords with variable names
...


Auto

Double

Int

Struct

Break

Else

Long

Switch

Case

Enum

Register

Typedef

Char

Extern

Return

Union

Const

Float

Short

Unsigned

#include ...
\n" );
getchar();
return 0;
}
Instructor: HODARI Audace

Page 11

" Let's look at the elements of the program
...
h into our program before actually creating the executable
...
h
...
It tells the compiler that you're at the end of a
command
...

The next imporant line is int main()
...
The "curly braces" ({ and }) signal the beginning and end of functions
and other code blocks
...
Even if
you haven't programmed in Pascal, this is a good way to think about their meaning
...
The quotes tell the compiler that
you want to output the literal string as-is (almost)
...
The actual effect of '\n' is to move the cursor on your screen to the next line
...

The next command is getchar()
...
This line is included because many compiler environments will
open a new console window, run the program, and then close the window before you can see the output
...
Including that line gives you time to see the program run
...
This return value is important as it can be used to tell the operating system whether our program
succeeded or not
...

The final brace closes off the function
...
You can cut and
paste the code into a file, save it as a
...


Instructor: HODARI Audace

Page 12

CHAP3: VARIABLES AND BASIC DATA TYPES:
This chapter is concerned with the basic elements used to construct c statements
...


3
...
The characters in C are classified in the following categories:
1)
2)
3)
4)

Letters(Both uppercase letters & lowercase letters)
Digits
White spaces
Special characters

The whole character set is listed below:

1) Letters

2) Digits

3) White spaces

Capital A to Z

All decimal digits 0 to 9

Blank space(\)
Horizontal tab

Small a to z
Vertical tab
New line(\n)
Form feed

4) Special Character
,

Comma

&

Ampersand


...
2 Delimiters

Language pattern of C uses special kind of symbols, which are called as delimiters
...
3 Identifiers and Keywords:
A
...

Identifies can consist of letters, digits and underscope in any order except that the first character must be
a letter or an underscope and never a digit
...
The upper case and lower
case letters are not interchangeable
...

“x”
Illegal character (“)
...

Error flag
Illegal character (blank space)
...
They are user-defined names, consisting of sequence
of letters and digits, with the letter as the first character
...
However, the upper
case letters are also permitted
...
In general under score
is used as a link between two words in long identifiers
...


a) #define N 10
b) #define a 15
Here, „N‟ and „a‟ are user-defined identifiers
...
4 The „C‟ keywords:
The C keywords are reserved words by the compiler
...

The keywords can not be used as variables names because they have been assigned fixed jobs
...
It is
suggested not to mix up keywords with variable names
...


auto

Double

Int

Struct

break

Else

Long

Switch

Case

Enum

Register

Typedef

char

Extern

Return

Union

const

Float

Short

Unsigned

continue

For

Signed

Void

default

Goto

Sizeof

Volatile

Do

If

Static

While

Keywords are standard (reversed) identifiers that have some specific, predefined meanings (purposes) in C
...
This means that they can be used only for their intended purpose
...

Note that keywords must be written in lower case
...
5 Constants:

Instructor: HODARI Audace

Page 16

The constants in C are applicable to the values, which do not change during the execution of a program
...

C constants

Numeric Constants

Integer Constants

Real constants

Character Constants

Single Character Constants

String Constants

A
...

It requires minimum two bytes and maximum four bytes
...
The number without a sign is assumed as
positive
...
Integer constants are unfit to represent many
quantities
...
For example,
length, height, prize etc
...

Example 2
...
521 etc

B
...
They are also represented with a single digit or a single special
symbol or white space enclosed within a pair of single quote marks
...


Character constants have integer values known as ASCII (American Standard Code for Information
Interchange) values
...


2) String constants
String constants are sequence of characters enclosed within a double quote marks
...

Example “Hello”, “a”,”UNILAK”
2
...
The data types are integers, real or
character constants
...


A variable is a data name used for storing a data value
...

The variables value keeps on changing during the execution of a program
...

A variable name may be declared based on the meaning of the operation
...

Example height, average, sum etc
...


Instructor: HODARI Audace

Page 18

2) The length of the variable varies from compiler to compiler
...
However, the ANSI standard recognizes the maximum length of a variable
up to 31 characters
...
For example suM and sum are not
the same
...

2
...
This enables the programmer to select the appropriate data type
as per the need of the application
...
The numbers may
be integers or real
...


1
...
They are short and long
...

The short integer requires two bytes and the long integers four bytes
...


b) Integers, signed and unsigned
Difference between signed and unsigned integers
Signed integer

Unsigned integer

Occupies 2 bytes in memory

Occupies 2 bytes in memory

Range: -32 768 to 32 767

Range: 0 to 65 535

Control string is %d or %I

Control string %u

By default signed int is short-signed int
...

There are also long signed integer
having range from

There are also long unsigned int with range
0 to 4 294 967 295

- 2 147 483 648 to 2 147 483 647
Example:

Example:

int a=2;

unsigned long b;

short int b=2;

unsigned long int c;

When a variable is declared as unsigned, the
negative range of the data type is transferred to
positive i
...
doubles the largest size of the
possible value
...


2
...
Floats and Doubles

Difference between float and double

Float

Double

Occupies 4 bytes in memory

Occupies 8 bytes in memory

Range: -3
...
4e10+38

Range: -1
...
7e10+308

Control string is %f

Control string :%lf

Example:

Example:

Float a;

Double y;
There also exist long double having ranged
-1
...
7e10+4 932 and occupies 10
bytes in memory
...
Entire Data Types in C

The entire data types supported by the „C‟
...
4e10-38 to +3
...
7e10-308 to +1
...
7e10-4 932 to +1
...
An operator
indicates an operation to be performed on data that yields a value
...
An operand is a data item on which operators performs an operation
...
1 Arithmetic operators
There are two types of arithmetic operators:
1
...
Unary operator

Binary operators:
These operators: are commonly used in most of the computer languages
...
They are so called Binary Arithmetic Operators
...

 Minus (-)
Unary minus is used to indicate or change the algebraic sign of a value
...

There is no unary plus (+) in C, even though, a value assigned with plus sign is valid
...

 Increment (++) & Decrement (--) Operators
The operator ++ adds one to its operand whereas the operator -- subtracts one from its operand
...
That is, x=x+1; can be represented as x++ or
++x
...
2 Relational operators:
These operators: are used to distinguish between two variables depending on their relations
...
If the relation is true then it returns a value 1 otherwise 0
for false relation
...
h>
main()
{
clrscr();
printf(“\n Condition : Return Values\n”);
Instructor: HODARI Audace

Page 24

printf(“\n 10!=10 : %d”,10!=10);
printf(“\n 10==10 : %d”,10= =10);
printf(“\n 10>=10 : %d”,10>=10);
printf(“\n 10<=100 : %d”,10<=10);
printf(“\n 10!=9 : %d”,10!=9)
}
Output:
Condition: Return Values
10! =10

:0

10==10

:1

10>=10

:1

10<=100

:1

10! =9

:1

4
...
After checking the conditions, logical true (1) or false (0)
is provided
...


Operator

Description or Action

Example

Return Value

&&

Logical AND

5>3 && 5<10

1

||

Logical OR

8>5 || 8<2

1

!

Logical Not

!(8= =8)

0

1
...

2
...

3
...

Instructor: HODARI Audace

Page 25

Write a program to illustrate the use of logical operators
#include ...
4 Conditional operator (? : )
The conditional operator contains a condition followed by two statements or values
...

The conditional operator ( ? ) and ( : ) are sometimes called ternary operators because they take three
arguments
...
If the condition is true expression1 gets evaluated otherwise
expression2
...

Write a program to use the conditional operator with two values
...
h>
#include ...
Hence, 5 is printed
...


#include ...
h>

main()
{
clrscr();
3>2?printf(“True”):printf(“False);
}
Instructor: HODARI Audace

Page 27

Output:
True

4
...
For example,
x = 1 sets x to 1, and
a=b
sets a to whatever b's value is
...
(C provides several ``shortcut'' operators for
modifying variables in this and similar ways, which we'll meet later
...
C does not have ``assignment statements''; instead, an assignment like a = b is an
expression and can be used wherever any expression can appear
...
This value can then be used in a larger expression; for
example, we might write
c=a=b
which is equivalent to
c = (a = b)
and assigns b's value to both a and c
...
) Later we'll
see other circumstances in which it can be useful to use the value of an assignment expression
...
It operates on them
...

Operand :
Each thing which is operated upon by an operator is called an operand
...
Some of them may already be familiar:

+ - * / = & ==
Most operators can be thought of as belonging to one of three groups, divided up arbitrarily according to what
they do with their operands
...
They make a result from their operands
...
g
...

Operators which make comparisons
...
g
...

Operators which produce new variable types: like the cast operator
...
In fact the second group is a subset of the first, in which the
result of the operation is a boolean value of either true of false
...
This is more than, say, Pascal and BASIC put together! The
operators serve a variety of purposes and they can be used very freely
...
The more abstruse operators are looked at in another chapter
...
In C these are the following:
+
plus (unary)
minus (force value to be negative)
+
addition
subtraction
*
multiplication
Instructor: HODARI Audace

Page 29

/
floating point division
/
integer division "div"
%
integer remainder "mod"
These operators would not be useful without a partner operator which could attach the values which they
produce to variables
...
For example:
double x,y;
x = 2
...
As usual there is some standard jargon for this, which is useful to know
because compilers tend to use this when handing out error messages
...
An expression is
simply the name for any string of operators, variables and numbers
...

The name comes from `left values' meaning anything which can legally be written on the left hand side of an
assignment
...
h>
/**************************************/
main ()
{ int i;
printf ("Arithmetic Operators\n\n");
i = 6;
printf ("i = 6, -i is : %d\n", -i);
printf ("int 1 + 2 = %d\n", 1 + 2);
printf ("int 5 - 1 = %d\n", 5 - 1);
printf ("int 5 * 2 = %d\n", 5 * 2);
printf ("\n9 div 4 = 2 remainder 1:\n");
printf ("int 9 / 4 = %d\n", 9 / 4);
printf ("int 9 % 4 = %d\n", 9 % 4);
printf ("double 9 / 4 = %f\n", 9
...
0);
}

Instructor: HODARI Audace

Page 31

Output
Arithmetic Operators
i = 6, -i is : -6
int 1 + 2 = 3
int 5 - 1 = 4
int 5 * 2 = 10
9 div 4 = 2 remainder 1:
int 9 / 4 = 2
int 9 4 = 1
double 9 / 4 = 2
...
They have a value
in the sense that they assume the value of whatever expression is inside them
...
If an expression is written out in an ambiguous way, such as:
a+b/4*2

it is not clear what is meant by this
...
By using parentheses, any doubt about what the expression means is removed
...
Putting parentheses in may remove the ambiguity of expressions, but it does not alter than fact that
a+b/4*2

Instructor: HODARI Audace

Page 32

is ambiguous
...
The convention is that some operators are
stronger than others and that the stronger ones will always be evaluated first
...
Use parentheses to be sure
...

Unary Operator Precedence
Unary operators are operators which have only a single operand: that is, they operate on only one object
...

Special Assignment Operators ++ and -C has some special operators which cut down on the amount of typing involved in a program
...
The simplest of these perhaps are the
increment and decrement operators:

++
increment: add one to
-decrement: subtract one from
These attach to any variable of integer or floating point type
...
) They are used to
simply add or subtract 1 from a variable
...
Similarly:
variable = variable - 1;

is equivalent to:
variable--;

or
--variable;

Notice particularly that these two operators can be placed in front or after the name of the variable
...


CHAP5: THE STATEMENTS AND CONTROL FLOW STRUCTURES:
The terms statement and control flow are somewhat circular
...

Most of the statements in a C program are expression statements
...
The lines
i = 0;
i = i + 1;
and
printf("Hello, world!\n");

Instructor: HODARI Audace

Page 34

are all expression statements
...

 Selection
...


1
...
And many program examples have seen before
...
Selection: The selection means choosing among two or more possible cases of actions
...


3
...
And it is more
commonly referred to as Looping
...

For loop statement
...

While loop statement
...

Do-while loop statement
...
0 Selection: if-else condition:

2
...
In case the condition is false the compiler skips the line
within the if block
...
one of the important functions of the if statement is that it allows the program to select an action based
upon the user's input
...
Use if statement
...
h>
Void main()
{
clrscr();
int m,n,o;
printf("\n Enter two numbers :");
scanf("%d %d",&m,&n);
if(m-n= =0)
printf("\n Two numbers are equal");
getch();
}
Output
Enter two numbers: 8 8
Two numbers are equal

2
...
else statement takes care of true as well as false conditions
...
One block is for if and it is
executed when the condition is true and the other block of else is executed when the condition is false
...
If you do this, you never have to remember to put them in
when you want more than one statement to be executed, and you make the body of the if statement more visually
clear
...
If the condition is true, the
statements following the if are executed
...

Instructor: HODARI Audace

Page 38

The statement is executed only when the condition is true
...


Example: Find the greatest among two numbers:
#include ...
Use if…else statement
...
h>
#include ...
3 Nested if-else statement
In the case of many logical conditions, nested if-else function is used
...
You can use
an "else if" statement following an if statement and its body; that way, if the first statement is true, the "else if" will be
ignored, but if the if statement is false, it will then check the condition for the else if statement
...
It is possible to use numerous else if statements to ensure that only one
block of code is executed
...
Read the starting and ending meter reading
...
50

100-200

2
...
50

#include ...
50;
else if(consumed>=100&&consumed<=200)
total=consumed*2
...
50;
printf("\n Total bill for %d units is %f",consumed,total);
getch();
}
Output:
Enter the initial and final readings: 800 850
Total bill for 50 units is 75
...
h>

int main()

/* Most important part of the program!

Instructor: HODARI Audace

Page 42

*/
{
int age;

/* Need a variable
...
*/
}
else if ( age == 100 ) {

/* I use else just to show an example */

printf( "You are old\n" );
}
else {
printf( "You are really old\n" );

/* Executed if no other statement is

*/
}
return 0;
}

Example3: Write a program to find largest number out of three numbers
...


#include ...
0 Iteration:
Iteration simply means repeating or reiterating the same sequence of commands
...


What is a loop?
A loop is defined as a block of statements which are repeatedly executed for a certain number of times
...

Each time the updated value is checked by the loop itself
...


3
...
Condition may be predefined or
open-ended
...
This statement is executed only once
...
The for loop continues to execute as long as conditional test is satisfied
...


3) The re-evaluation parameter decides how to make changes in the loop (quite often increase or decrease operations
are to be used)
...

In case there is only one statement after the for loop, braces may not be necessary
...
It is a good practice to use braces even for single statement following the
for loop
...


#include ...
2 The while loop

Syntax
While(test condition)

Instructor: HODARI Audace

Page 46

{
Body of the loop
}

The test condition may be any expression
...
i
...
When the condition becomes
false the execution will be out of the loop
...


#include ...
3 The do-while Loop
The structure (syntax) for Do-while Loop is shown bellow as follows:

Instructor: HODARI Audace

Page 47

do
{
Statement/s;
}
while(condition);

The difference between the while and do-while loop is in the place where the condition is to be tested
...
Whereas as in do-while,
the condition is checked at the end of the loop
...

Example:
Write a program to display even numbers from 0 to 14 using do while loop
...
h>
void main()
{
clrscr();
int i=0;
do
{
printf("%5d",i);
i+=2;
}
while(i<=15);
getch();
Instructor: HODARI Audace

Page 48

}
Output:
0 2 4 6 8 10 12 14

Example2:

Write a program to count numbers between 1 to 100 not divisible by 2,3 and 5 using for loop
...
h>
main()
{
clrscr();
int x,c=0;
printf("Numbers from 1 to 100 not divisible by 2,3&5: \n");
for(x=0;x<=100;x++)
{
if(x%2!=0&&x%3!=0&&x%5!=0)
{
printf("%5d",x);
c++;
}
}
printf("\n Total Numbers:%d",c);

Instructor: HODARI Audace

Page 49

getch();
}
Output:
Numbers from 1 to 100 not divisible by 2,3&5:
1 7 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83 89 91 97
...


*
**
***
****
*****

#include ...


1
1 2
1 2 3

3 2 1

Instructor: HODARI Audace

Page 51

2 1
1

#include ...

An array in C language is a collection of similar data-type, means an array can hold value of a particular data
type for which it has been declared
...
So
an integer array can only hold integer values and cannot hold values other than integer
...
So this is the most important difference between a variable and an array
...
These similar quantities could be
percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25
students
...
These similar elements could be all integers or all
floats or all characters etc
...
Note: All elements of any given array must be of the same data type
...

Syntax: data_type array_name[width];
Definition of an array

Instructor: HODARI Audace

Page 53

Array is a collection of similar data types in which each element is unique one and located in separate memory
locations
...

2) All the elements of an array share the same name and they are distinguished from one another with the help
of an element number
...

4) Any particular element of an array can be modified separately without disturbing other elements
...
To
carry out this task, the statement a[4]=10 can be used
...

5) Any element of an array a[] can be assigned/equated to another ordinary variable or array of its type
...

b) In the statement a[2]=a[3] or vice versa, value of a[3] is assigned to a[2] where both statements are of
the same array
...
The amount of storage required for holding
elements of the array depends on its type and size
...

Total bytes=sizeof(data type) X size of array

Types of Arrays:
1
...

2
...

3
...

Declaration of an Array

Instructor: HODARI Audace

Page 54

Arrays must be declared before they can be used in the program
...

Variable name= Array name
...

double height[10];
float width[20];
int min[9];
char name[20];
Description: Here type specifies the variable type of the element which is going to be stored in the array such
as int, float or char
...
In C programming language we can declare the array of any basic standard type which C language
supports
...
Any subscripts 0 to 49 are valid
...
So height [0] refers to the first element of the array
...


For example2:

int values[9];

The declaration int values[9]; would reserve enough space for an array called values that could hold up to 9
integers
...

values[0] values[1] values[2] values[3] values[4] values[5] values[6] values[7] values[8]

The array values stored in the memory
...
The elements of the array occupy adjacent locations in memory
...

#include ...
The initializing values are enclosed within the curly
braces in the declaration and placed following an equal sign after the array name
...
Array can also be initialized after declaration
...

We can initialize the elements in the array in the same way as the ordinary variables when they are declared
...
The remaining elements
will be set to zero automatically
...
Here the
array has two subscripts
...

The declaration of two dimension arrays is as follows:

Instructor: HODARI Audace

Page 56

data_type array_name[row_size][column_size];
Example: int m[10][20];
Here m is declared as a matrix having 10 rows( numbered from 0 to 9) and 20 columns(numbered 0 through
19)
...


Declaration of an array is done as follows

7
...
The array elements are stored sequentially in separate locations
...

Reading of array elements begins from ‘0’
...


Calling Array Elements
a[0] refers to 1st element i
...
e 2
a[2] refers to 3rd element i
...
e 4
a[4] refers to 5th element i
...


Characteristic of an array

6) The declaration int a[5] is nothing but creation of 5 variables of integer types in the memory
...

8) The element number in an array plays major role for calling each element
...

int a[5]={1,2,3,4,8};

If a programmer needs to replace 8 with 10, he/she doesn’t require to change all other numbers expect 8
...
Here all other three elements are not disturbed
...


For example
b=a[2];
a[2]=a[3];

c) In the statement b=a[2] or vice versa, value of a[2] is assigned to ‘b’ where b is an integer
...


6)The array elements are stored in continuous memory locations
...
The total size in bytes for a single dimensional array is computed as shown
below
...
2 One Dimensional Array

The elements of an integer array a[5] are stored in continuous memory locations
...
Each integer element requires 2 bytes; hence subsequent element appears after gap
of 2 locations
...
The only difference is
that numbers of locations are different for different data types
...


Write a program to print bytes reserved for various types of data & spaces required for storing them in memory using
arrays
...
h>
#include ...

Data type

Memory Requirement

Character

1 byte

Integer

2 bytes

Float

4 bytes

Long

4 bytes

Double

8 bytes

Character arrays are called strings
...

The NULL character acts as an end of the character array
...
When a compiler reads the NULL character ‘\0’ it ends a character array
...


#include ...
h>

Instructor: HODARI Audace

Page 60

main()
{
clrscr();
char name[6]={'A','R','R','A','Y'};
int i=0;
printf("\n Character\t\tMemory Location \n");
while(name[i]!='\0')

{
printf("\n [%c]\t\t\t[%u]",name[i],&name[i]);
i++;
}
getch();
}
Output:

Write a program to compute and display addition of even numbers and product of odd numbers for an array of 5
numbers
...
h>
#include ...

#include ...
h>
main()
{
int n,m[25],i,a=0,s=1;
clrscr();
printf("How many numbers:");
scanf("%d",&n);
for(i=0;i{
printf("\n Number %d is:",(i+1));
scanf("%d",&m[i]);
}
for(i=0;i{
if(m[i]%2==0)
{
printf("\n Even number:%d",m[i]);

Instructor: HODARI Audace

Page 63

a=a+m[i];
}
else
{
printf("\n Odd number :%d",m[i]);
s=s*m[i];
}
}
printf("\n\n\n Addition of even numbers:%d",a);
printf("\n Product of odd numbers:%d",s);

}

Output:

Write a program that detects the occurrence of a character in a given string
...
h>
#include ...
",f,s,c);
getch();
}
Output:

Instructor: HODARI Audace

Page 65

7
...


Example int x [3] [3]

Col1

Col2

Col3

Row1

x[0][0]

x[0][1]

x[0][2]

Row2

x[1][0]

x[1][1]

x[1][2]

Row3

x[2][0]

x[2][1]

x[2][2]

Conceptually the elements are shown in matrix form
...


The two dimensional array is a collection of a number of one dimensional arrays which are placed one after another
...


Write a program to display two dimensional array elements together with their addresses
...
h>
#include ...


#include ...
h>main()
{
int i,j,r1,c1,a[10][10],b[10][10];
printf("Enter order of Matrix A&B up to 10X10:");
scanf("%d %d",&r1,&c1);
printf("\n Enter Elements of Matrix A:\n");
for(i=0;i{
for(j=0;jscanf("%d",&a[i][j]);
}
Instructor: HODARI Audace

Page 67

printf("\n Enter Elements of Matrix B:\n");
for(i=0;i{
for(j=0;jscanf("%d",&b[i][j]);
}
printf("\n Matrix Addition\n");
for(i=0;i{
for(j=0;jprintf("%5d",a[i][j]+b[i][j]);
printf("\n");
}

printf("\n Matrix Subtraction \n");
for(i=0;i{
for(j=0;jprintf("%5d",a[i][j]-b[i][j]);
printf("\n");
}
}
Output:

Instructor: HODARI Audace

Page 68

7
...
The compiler determines the restriction on it
...


Four dimensional array can be initialized as follows
...


#include ...
h>
Instructor: HODARI Audace

Page 69

main()
{
int i,j,sum=0,a[10],n;
printf("\n Enter how many numbers:");
scanf("%d",&n);
for(i=0;i{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("\n Numbers in ascending order:");
for(i=0;i<=sum;i++)
{
for(j=0;j{
if(i==a[j])
printf("%3d",a[j]);
}
}

printf("\n Numbers in descending order:");
for(i=sum;i>=0;i--)
{
for(j=0;j{

Instructor: HODARI Audace

Page 70

if(i==a[j])
printf("%3d",a[j]);
}
}
}

Output:

Write a program to sort the numbers in ascending order by comparison method

#include ...
h>
main()
{
int i,j,temp,a[10],n;
printf("\n Enter how many numbers to sort:");
scanf("%d",&n);
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{

Instructor: HODARI Audace

Page 71

for(j=i+1;j{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

printf("\n The numbers in ascending order:");
for(i=0;iprintf("%4d",a[i]);

Output:

Instructor: HODARI Audace

Page 72

Instructor: HODARI Audace

Page 73


Title: GENERAL PROGRAMING CONCEPTS
Description: THI NOTES IS ABOUT BASICS OF COMPUTER PROGRAMING THIS NOTES CAN TEACH YOU WHAT IS COMPUTER PROGRAMING AND ALSO GUIDE YOU WHERE TO START COMPUTER PROGRAMING IS BEST THINGS YOU CAN LEARN IN THE WORLD BECAUSE IT TEACH YOU HOW TO THINK . AND OPEN YOUR MIND AND THINK WHAT THINKS YOU CAN DO TO CHANGE THE WORLD THE BEST WAYS TO DO SOMETHING NEW IS TO LEAN PROGRAMING