Module 3
Module 3
Module 3
Course Outcome:-
An array is a collection of elements all of the same data type.
Elements of array can be of any data type in C.
Array has a unique name.
An array element is referred to by specifying the array name followed
by the subscript enclosd in square brackets.
subscript must be expressed as a nonnegative integer.
Arrays are used when we need to store a group of data of same type
and do different processing on it.
Why do we need arrays?
• We can use normal variables (v1, v2, v3) when we have a small
number of objects, but if we want to store a large number of
instances, it becomes difficult to manage them with normal
variables.
Data type of A[i] is int.
A[i] is treated as a simple variable.
So it can be used in printf(), scanf() or in an expression.
A[2]=10; A[0]=20; A[5]=25;
A[7]=A[2]+A[0];
scanf(“%d”, &A[1]); printf(“%d”,A[0]);
One Dimensional Arrays in C
main()
{
int a[10], n, i;
scanf(“%d”,&n);
for (i=0; i<n; i++) // Reading n numbers
scanf(“%d”,&a[i]);
• 1st approach
int Mark[5] = {25,35,30,45,40};
• 2nd approach
int Mark[ ] = {25,35,30,45,40};
• 3rd approach
int Mark[5] = {25,35,30};
• 4th approach
for(int i=0;i<5;i++)
{
scanf(“%d”,&Mark[i]);
}
One Dimensional Arrays in C
Note :-
Array name is treated as a constant pointer.
So it can not be used on the LHS of an assignment expression.
Array name without square brackets represents the address of the first
element of the array.
&array[0] is same as array.
Accessing Array Elements
• We can access the elements of an array using index.
#include<stdio.h>
void main()
{
int n,A[20],i;
printf("Enter the size of Array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number");
scanf("%d",&A[i]);
}
for(i=0;i<n;i++)
{
printf("%d\t",A[i]);
}
}
Write a program to read and display an array of size n in reverse order.
#include<stdio.h>
void main()
{
int n,A[20],i;
printf("Enter the size of Array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number");
scanf("%d",&A[i]);
}
for(i=n-1;i>=0;i--)
{
printf("%d\t",A[i]);
}
}
1) Increment every element by 1.
2) Linear Search.
3) Bubble sort.
4) Exchange sort
5) Selection sort
6) Decimal number to Binary number
7) Binary search
Write a program to read and display even numbers in an array of size n.
#include<stdio.h>
void main()
{
int n,A[20],i;
printf("Enter the size of Array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number");
scanf("%d",&A[i]);
}
for(i=0;i<n;i++)
{
if(A[i]%2 == 0)
{
printf("%d\t",A[i]);
}
}
}
Write a program to find largest element in array of size n.
#include<stdio.h>
void main()
{
int n,A[20],i,large;
printf("Enter the size of Array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number");
scanf("%d",&A[i]);
}
large=A[0];
for(i=0;i<n;i++)
{
if(large < A[i])
{
large = A[i]
}
}
printf("Large =%d",large);
}
Deleting an element from array
To delete an element at index k, shift all elements from k+1, one position
to left
i = k;
while(i<n-1)
{
array[i] = array [i+1];
i++;
}
n=n-1;
Inserting an element to array
To insert an element at index k, shift all elements from position k, one
position to right.
i = n;
while(i>k)
{
array[i] = array [i-1];
i--;
}
array[k]=value;
n=n+1;
Two Dimensional Arrays in C
• We can access the record using both the row index and column
index.
Two Dimensional Arrays in C
Two dimensional array is a table of rows and columns.
Syntax of array definition of a 2-D array is :
data_type array_name[No.of rows][No.of columns];
Normarlly No.of rows and No.of columns are expressed as unsigned integers.
0 1 2 3 Column no
0 3 6 1 7
Examples :-
1 5 4 2
int a[3][4]; 1
2 6 10 17 0 a[1][2]
Row No
An element is accessed by
array_name[row_no][col_no]
Two Dimensional Arrays in C
int A[10][10];
Data type of A[i][j] is int.
A[i][j] is treated as a simple variable.
So it can be used in printf(), scanf() or in an expression.
A[2][1]=10; A[0][0]=20;
A[0][2]=A[1][2]+A[1][1];
scanf(“%d”, &A[1][2]); printf(“%d”,A[0][2]);
Two Dimensional Arrays in C
main()
{ int a[10][10], n, i;
scanf(“%d%d”,&m,&n);
• This is done so that programs can tell when the end of a string has
been reached.
• For example,
• char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Strings in C
• If you follow the rule of array initialization then you can
write the previous statement as follows −
• char str[] = "Hello";
Declaration of String
• String can be declared in the same way we declare an array.
• We just need to use char data type followed by string name and its
size.
• Syntax:
• String_Size: Number of characters required for this string plus one (\0)
Initialization of String
char name[30];
name = “Mahatma Gandhi”;
/* Error. Array name is a constant pointer.
A constant can not be used on the LHS of
assignment expression */
Reading/Displaying Strings
scanf() :
char name[30];
scanf (“%s”, name); // note that no & before name.
Reads upto the first white space character (space, tab, newline)
Null character is stored as the end of the string.
If you are giving the input as GRACE HOPPER it will store only
GRACE
printf() :
printf() is used for displaying strings using %s
printf(“Name : %s\n“, name)
Reading/Displaying Strings
scanf() :
The follwing scanf() can be used for reading string containg
spaces.
scanf (“%[^\n]”, name); // note that no & before name.
Reads upto the newline character(\n)
Newline character is not stored in string.
Null character is stored as the end of the string.
printf() :
printf(“Name : %s\n“, name)
Reading/Displaying Strings
gets() :
Usage is
gets(string_var);
Reads upto the newline character(\n), skips the newline
character.
The gets() allows the user to enter the space-separated
strings
Newline character is not stored in string.
Null character is stored as the end of the string.
puts() :
Usage is
puts(string_item);
puts() displays a newline after the string_item.
• Example program
#include<stdio.h>
int main()
{
char str[50]="An Engineering Student";
int x=0;
while(str[x]!='\0')
{
printf("%c",str[x]);
x++;
}
return 0;
}
String Handling Functions
• With every C compiler a large set of useful string handling library
functions are provided.
strlen()
strcpy()
strcat()
strcmp()
• The header file that we will use for these functions is string.h
strlen() function
Syntax
strlen(string_name)
strlen() function
#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
int l;
printf("Enter a string: ");
scanf("%s",str);
l=strlen(str);
printf("Length of the entered string is %d",l);
return 0;
}
strcpy() function
• If they are not, it returns the numerical difference between the ASCII values
of the first non-matching pairs of characters.
For example a character array cities[4][10] may be used
to store a list of 4 cities, each of length not more than
10 characters.
Sort a list of names in alphabetical order
for (i=0; i < N-1 ; i++) {
if(strcmp(name[j],name[j+1]) > 0) {
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);