EST 102-Module 3.Docx
EST 102-Module 3.Docx
An array is a collection of similar elements. The first element in the array is in index 0, so the
last element index is 1 less than the size of the array. An array is also known as a subscripted
variable. The element numbers in [ ] are called index or subscript. Before using an array its
type and dimension must be declared. However big an array, its elements are always stored in
contiguous memory locations.
Array initialization
➢ Compile Time Initialization:
Array declaration by initializing elements
Eg: int A[] = { 10, 20, 30, 40 } ;
Array declaration by specifying size and initializing elements
Eg: int A[6] = { 10, 20, 30, 40 };
Characteristics of Array
1. Declaration int mark[5] is the creation of 5 integer types in memory.
2. All elements of an array share the same name and they are distinguished with the help
of element number.
3. Any particular element in an array can be modified separately without disturbing
other elements.
4. Any element of an array a[] can be assigned to another ordinary variable or array
variable of its type.
a. Eg: b=a[2];
b. a[2]=a[3];
5. Single operation which involves entire array is not allowed in c.
Write a program to read and display an array of size n.
#include<stdio.h>
int main()
{
int n,A[20],i;
printf("Enter the size of Array : ");
scanf("%d",&n);
printf("Enter the elements of array : ");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
printf(“The array elements are \n");
for(i=0;i<n;i++)
{
printf("A[%d] = %d\n",i,A[i]);
}
}
OUTPUT
LINEAR SEARCH
Search is a process of finding a value in a list of values. This search process starts comparing
search element with the first element in the list. If both are matched then result is element found
otherwise search element is compared with the next element in the list. Repeat the same until
search element is compared with the last element in the list, if that last element also doesn't
match, then the result is "Element not found in the list". That means, the search element is
compared with element by element in the list.
Algorithm – Linear search
Step 1:Declare array a, size n, search element item, flag=0,i
Step 2: Read the size of the array n
Step3 : Read the array elements
Step4 : Read the search element item
Step 5: Repeat steps 6 -7 until i < n
Step 6: if a[i] == item then
Step 6.1: Set flag = 1
Step 6.2: Print Element found at position i+1
Step 7:i=i+1
Step 8: if flag=0
Step 8.1 : Print element not found
Step 9: Exit
Program – Linear Search
#include <stdio.h> //header files
int main () //main function
{
int a[20],n,item,i,flag=0;
printf("Enter size of the array ");
scanf("%d",&n);
printf("Enter array elements");
for(i = 0; i <n; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be Search: ");
scanf("%d",&item);
for (i = 0; i<n; i++) //looping logic
{
if(a[i] == item)
{
flag = 1;
printf("\nItem found at location %d\n",i+1);
break;
}
}
if(flag == 0)
printf("\nItem not found\n");
}
Write a program to copy content of one array to another array.
#include <stdio.h>
void main()
{
int a[20], b[20],i,n;
printf("Enter the size of array :");
scanf("%d",&n);
Datatype arrayname[rowsize][columnsize];
Intializing 2D array
1.) Eg: int A[2][3]={4,5,6,3,2,1};
Here array named A consist of 6 elements . Size of the array and values are assigned directly.
2.)Eg: int A[2][3]={{4,5,6},{3,2,1}};
3) Eg: int arr[ ][3] = { 52, 30, 23, 55, 56, 85,20,25,40 } ;
It is important to remember that while initializing a 2-D array it is necessary to mention
the second (column) dimension, whereas the first dimension (row) is optional.
4.)Eg: int a[2][3]={{4},{3,2}};
Row elements are kept in ordered set. Since the column size is 3 , remaining fields
will be filled with 0
Matrix Multiplication:
Multiplication result is
Size of A is r1xc1
Size of B is r2xc2
Size of C is r1xc2
Number of multiplication needed to find single element in C is c1 or r2
Example:
Write a program to multiply the content of two matrices.
#include<stdio.h>
int main()
{
int r1,c1,r2,c2,A[20][20],B[20][20],C[20][20],i,j,k;
printf("Enter the order of matrices:");
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
if(c1 != r2)
{
printf("Matrices Multiplication Not possible");
}
else
{
printf("Enter the Matrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("Enter the Matrix B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&B[i][j]);
}
}
printf("Matrix C:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
C[i][j]= 0;
for(k=0;k<c1;k++)
{
C[i][j] += A[i][k] * B[k][j];
}
printf("%d\t",C[i][j]);
}
printf("\n");
}
}
}
STRINGS
Strings are defined as an array of characters. String is a 1D character array.The string is
terminated with a special character ‘\0’.
Declaration of strings:
char string_name[size];
Eg: char city [10];
char name[30];
When the compiler assigns a character string to a character array, it automatically supplies a
null character (\0) at the end of string.Therefore
Size of a string = maximum number of characters in string + one
Initialization can be in either of the following two forms.
scanf ( ) is not capable of receiving multi-word strings. Therefore names such as ‘Hello World’
would be unacceptable.
#include <stdio.h>
int main()
{
char str[20];
printf("Enter the String\n");
scanf("%s", str);
printf("String: %s\n", str);
}
Output
Enter the String : Hello World
String: Hello
Issue with scanf: There is a whitespace after Hello. So it read the input till Hello and store it in
str.
#include <stdio.h>
void main()
{
char str[20];
printf("enter the string\n");
scanf("%[^\n]", str);
printf("String: %s\n", str);
}
Solution 2 : using the function gets ( ).
It does not skip white space.
The usage of functions gets( ) and its counterpart puts( ) is shown below.
#include <stdio.h>
int main ( )
{
char name[25] ;
printf ( "Enter full name\n " ) ;
gets ( name ) ;
puts ( "Hello!" ) ;
puts ( name ) ;
}
OUTPUT
Enter full name
Harun Shan
Hello!
Harun Shan
Output
Value=11
2. strcat( )
It concatenates two strings and returns the concatenated string.
Syntax: strcat(string1,string2) – concatenate string1 with string2.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("After concatenation: %s", s1);
}
Output:
After concatenation: HelloWorld
3. strcpy( )
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30];
char s2[30] = "Hello World";
strcpy(s1,s2);
printf("String s1: %s", s1);
}
Output:
String s1 is: Hello World
4. strcmp( )
It compares the two strings and returns an integer value. If both the strings are same (equal)
then this function would return 0 otherwise it may return a negative or positive value based
on the comparison.
Syntax : strcmp(str1,str2)