1. An Array as an ADT:
An array is a fixed size sequence of elements of the same type .
An Array is fundamental abstract data type.
Ex:
int A[100]
Here 100 is the fixed size of the array. Specifies the capacity of
the array. And it specifies the elements type like.
Ex:
int A[100]
The basic operation include direct access to each element in the
array by specifying its position so that values can be retrieved
from or stored in that position.
Array have different functionality across various
implementations of programming languages.
2. • Matrix Addition Algorithm:( A,B,M,N,X,Y)
• A is a Two Dimensional Array with M Rows and N columns
and B is a Two Dimensional Array with X Rows and Y
Columns. This algorithm adds these two arrays
• 1. if (M ≠ X) or (N ≠ Y) Then
• 2. print : Addition is not possible.
3. Exit
[ End of if ]
4.Repeat For I = 1 to M
5. Repeat For J = 1 to N
6. set C [i][j] =A [i][j] + B [i][j]
[End of step 4 for loop]
[End of step 5 for loop]
7. Exit
3. • Matrix Subtraction Algorithm:( A,B,M,N,X,Y)
• A is a Two Dimensional Array with M Rows and N columns
and B is a Two Dimensional Array with X Rows and Y
Columns. This algorithm subtracts these two arrays.
• 1. if (M ≠ X) or (N ≠ Y) Then
• 2. print : Subtraction is not possible.
3. Exit
[ End of if ]
4.Repeat For I = 1 to M
5. Repeat For J = 1 to N
6. set C [i][j] =A [i][j] - B [i][j]
[End of step 4 for loop]
[End of step 5 for loop]
7. Exit
4. • Matrix Multiplication Algorithm: ( A,B,M,N,X,Y)
• A is a Two Dimensional Array with M Rows and N columns and B is a
Two Dimensional Array with X Rows and Y Columns. This algorithm
multiplies these two arrays.
• 1. if (M ≠ Y) or (N ≠ X) Then
• 2. print : Multiplication is not possible.
• 3. Else
4.Repeat For I = 1 to N
5. Repeat For J = 1 to X
6. Set c[i] [j] = 0
7. Repeat For K = 1 to Y
8. Set c [i][j] = C [i][j] + A[i][k] * B[k][j]
[ End of step 7 for loop ]
[ End of step 5 for loop ]
[ End of step 4 for loop ]
[End of if ]
9. Exit
5. Sparse Matrices.
• Matrix with maximum zero entries is termed as sparse
matrix. It can be represented as:
•
• Lower triangular matrix: It has non-zero entries on or
below diagonal.
•
• Upper Triangular matrix: It has non-zero entries on or
above diagonal.
•
• Tri-diagonal matrix: It has non-zero entries on diagonal
and at the places immediately above or below
diagonal.
6. Control Statements:
• If else
• Nested if else
• If else if
• Loops :
• While loop
• do while loop
• for loop
7. • In C programs, Statements are executed sequentially in the
order in which they appear in the program.
• But some times we want to use a condition for executing only
a part of program.
• Also many situations arise we want to execute some
statements some statements several times.
If –else:
• This statement is used to test a condition and take one of the
two possible actions.
• If the condition is true then block of statements is executed.
• Otherwise another block of statements is executed.
8. • Syntax:
• if(condition)
statement1;
else
statement2;
Nested if else:
we can have another if….else statement in the if block or the else
block. This is called nesting of if….else statements.
if(condition)
{
if(condition)
statementA1;
else
statementA2;
}
else
{
if(condition)
statementB1;
else
statementB2;
}
9. If else if or else if ladder:
• This is a type of nesting in which there is an if …..else statement in every
else part except the last else part.
• Here each condition is checked and when a condition is found to be true,
the statements corresponding to that are executed, and the control comes
out of the nested structure without checking remaining conditions.
• If none of the condition is true then last else part is executed.
• Syntax:
• if (condition)
statement 1;
• else if (condition)
statement 2;
• .
• .
• .
• else
Statement ;
10. Loops :
Loops are used when we want to execute block of statements several times.
There are 3 loops:
i) while
ii) do while
iii) for
While loop :
This while statements can be written as:
while(condition)
{
Statements; ---- body of the loop
}
It is an entry-controlled loop. If a condition is true then and only then the
body of a loop is executed. After the body of a loop is executed then
control again goes back at the beginning, and the condition is checked if it
is true, the same process is executed until the condition becomes false.
Once the condition becomes false, the control goes out of the loop.
11. • Do-While Loop
• A do…while loop in C is similar to the while loop except that the condition
is always executed after the body of a loop. It is also called an exit-
controlled loop.
• Syntax of Do-While Loop in C:
do
{
statements ;
} while (expression);
• As we saw in a while loop, the body of the loop is executed only if the
condition is true. In some cases, we have to execute a body of the loop at
least once even if the condition is false. This type of operation can be
achieved by using a do-while loop.
• In the do-while loop, the body of a loop is always executed at least once.
After the body is executed, then it checks the condition. If the condition is
true, then it will again execute the body of a loop otherwise control is
transferred out of the loop.
12. For loop in C
• A for loop is a more efficient loop structure in ‘C’
programming. The general structure of for loop syntax in C is
as follows:
• Syntax of For Loop in C:
for (initial value; condition; incrementation or decrementation )
{
statements;
}
• The initial value of the for loop is performed only once.
• The condition is a Boolean expression that tests and compares
the counter to a fixed value after each iteration, stopping the
for loop when false is returned.
• The incrementation/decrementation means (increases or
decreases) the counter by a set value.