Unit Ii
Unit Ii
Unit Ii
ARRAYS
UNIT II
CONTENTS
return 0;
}
void func( void )
{
static int i = 5;
i++;
printf("i is %d and count is %d\n", i, count);
}
THE EXTERN STORAGE CLASS
¡ The extern storage class is used to give a reference of a global variable that is visible to ALL the
program files. When you use 'extern', the variable cannot be initialized however, it points the variable
name at a storage location that has been previously defined.
¡ When you have multiple files and you define a global variable or function, which will also be used in
other files, then extern will be used in another file to provide the reference of defined variable or
function. Just for understanding, extern is used to declare a global variable or function in another file.
¡ The extern modifier is most commonly used when there are two or more files sharing the same global
variables or functions.
¡ External variables are also known as global variables. These variables are defined outside the function.
These variables are available globally throughout the function execution. The value of global variables can
be modified by the functions. “extern” keyword is used to declare and define the external variables.
EXAMPLE
First File: main.c
Second File: support.c
#include <stdio.h>
#include “support.c” #include <stdio.h>
a) Write a C program to find sum of digits, Decimal to Binary conversion, reversal of numbers using
functions.
b) Write a C program to find Factorial, Greatest Common Divisor, and Fibonacci using recursion
RETURN A FLOAT VALUE
#include<stdio.h>
float sum(float a,float b);
int main()
{
float a=1.2,b=1.3;
printf("the sum is %.2f",sum(a,b));
return 0;
}
float sum(float a,float b)
{
return a+b;
}
RETURN A CHARACTER
#include<stdio.h>
char greet(char c );
int main()
{
char c="W";
printf("the string is %c",greet(c));
return 0;
}
const greet(char c)
{
return c;
}
RETURN A STRING
#include<stdio.h>
const char* greet(char c[10]);
int main()
{
char c[10]="Welcome";
printf("the string is %s",greet(c));
return 0;
}
const char* greet(char c[10])
{
return c;
}
PART 2
ARRAYS
DEFINING AN ARRAY
PROCESSING AN ARRAY
ONE DIMENSIONAL ARRAYS
TWO DIMENSIONAL ARRAYS
PASSING ARRAY AS AN ARGUMENT TO FUNCTION
DEFINING AN ARRAY
¡ Arrays a kind of data structure that can store a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often
more useful to think of an array as a collection of variables of the same type.
¡ All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
DECLARING ARRAYS
For example,
¡ float mark[5];
¡ Suppose you declared an array mark as above. The first element is mark[0], the second element
is mark[1] and so on.
HOW TO INITIALIZE AN ARRAY?(ONE DIMENSIONAL ARRAYS)
12. Write a program in C to find the second largest and second smallest element in an array.
MULTI DIMENSIONAL ARRAYS(TWO DIMENSIONAL ARRAYS)
¡ In C programming, you can create an array of arrays. These arrays are known as
multidimensional arrays. For example,
¡ int x[3][4];
¡ Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can think
the array as a table with 3 rows and each row has 4 columns.
¡
INITIALIZATION OF A 2D ARRAY
1. Write a program in C for a 2D array of size 3x3 and print the matrix.
2. Write a program in C for addition of two Matrices of same size.
3. Write a program in C for subtraction of two Matrices.
4. Write a program in C for multiplication of two square Matrices.
5. Write a program in C to find transpose of a given matrix.
6. Write a program in C to find sum of rows and columns of a Matrix.
7. Write a program in C to accept two matrices and check whether they are equal.
LAB SYLLABUS PROGRAMS ON ARRAYS
Your program should take as input: dimension of a square matrix N, two
matrices of size N x N with integer values, and one operator symbol (+, -
,*). It must perform the corresponding operation given below;
¡ Matrix Addition b) Matrix Subtraction c) Matrix Multiplication
PASSING ARRAY AS AN ARGUMENT TO FUNCTION
Method 1:
¡ Formal parameters as a sized array −
void myFunction(int param[10])
{...}
Method 2:
¡ Formal parameters as an unsized array −
void myFunction(int param[])
{...}
EXAMPLE
#include<stdio.h>
int sum(int a[],int n);
int main()
{ int n,i;
printf("enter the size");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{printf("enter the value");
scanf("%d",&a[i]);}
sum(a,n);
return 0;}
int sum(int a[],int n)
{ int i;
for(i=0;i<n;i++){
printf("%d\n",a[i]);}
}
SORTING TECHNIQUES
1. Bubble Sort
2. Insertion Sort
3. selection sort
BUBBLE SORT
¡ Bubble sort is the simplest sorting algorithm. In this technique we follow given step to short
given elements in increasing order.
Steps to Sort data
1. First compare First (previous) element with its next elements.
2. If next element is grater than previous element just ignore it.
3. If next element is smaller than previous element then interchange their position.
Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element
otherwise do nothing. The process goes on until the last element.
Ø After each iteration, minimum is placed in the front of the unsorted list.
For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated until all the elements are placed at
their correct positions.
LAB PROGRAMS
¡Linear search
¡Binary search.
LINEAR SEARCH
¡ This searching technique is applicable only for sorted array, but this searching technique is faster than linear
search.
¡ Steps for binary search
¡ You need to first sort elements of array if it is not in sorted order, because binary search is only application
on sorted element.
STEPS