PROGRAMMING IN C
Module III
CONTENTS
1. One-Dimensional Arrays
2. Two-Dimensional Arrays
3. Strings
COURSE OUTCOME
CO3: Write readable C programs with arrays, structure or
union for storing the data to be processed
Arrays
• An array is a collection of data that holds
fixed number of values of same type.
• The size and type of arrays cannot be
changed after its declaration.
• For Example : If you want to store marks of
100 students you can create an array for it.
float marks[100];
Types of Array
1. One-dimensional arrays.
2. Two-dimensional arrays.
3. Multidimensional arrays.
One Dimensional Array
• A list of item can be given one variable name
using only one subscript and such a variable is called
a single subscripted variable or a one-dimensional
array.
• Example:
float height[50];
int mark[20];
char name[10];
Array Declaration
Syntax:
data_type array_name[array_ size];
The datatype specifies the type of the element that
will be contained in the array, such as int, float, or
char and the size indicates the maximum number of
elements that can be stored inside the array.
int mark[5];
• Here, we declared an array, with name ‘mark’,
type of array elements is ‘int’, for storing 5 values.
Accessing elements of an array
• Array elements are accessed by indices.
• int mark[5];
• The first element is mark[0], second element is mark[1]
and so on.
• Arrays have 0 as the first index not 1. In this example,
mark[0].
• If the size of an array is n, to access the last element, (n-1)
index is used. In this example, mark[4].
• If you try to access array elements outside of its bound,
lets say mark[10], the compiler may not show any error.
However, this may cause unexpected output (undefined
behavior).
Array Initialization
After an array is declared, its elements must be
initialized.
In C programming an array can be initialized either:
• At compile time
• At run time
Compile Time Initialization
Method 1
int mark[5] ={78,45,12,89,56};
Method 2
int mark[ ] ={78,45,12,89,56};
Here,
• mark[0] is equal to 78
mark[1] is equal to 45
mark[2] is equal to 12
mark[3] is equal to 89
mark[4] is equal to 56
Run Time Initialization
• An array can also be explicitly initialized at run
time.
• For example
for(i=0;i<10;i++)
{
scanf(“%d”, &x[i]);
}
• Looping statements are used to initialize the
values of the arrays one by one using
assignment operator or through the keyboard by
the user.
Reading and Displaying elements of the array
#include<stdio.h>
int main()
{
int array[5],i;
//Reading elements of the array
printf("Enter 5 numbers to store them in array \n");
for(i=0;i<5;i++)
{
scanf("%d", &array[i]);
}
//Displaying the elements of the array
printf("Element in the array are: \n");
for(i=0;i<5;i++)
{
printf("Element stored at a[%d]=%d \n", i, array[i]);
}
return 0;
}
Problem
Write a program to read the elements of a 1D array
from user and add 10 to all elements in the array.
#include<stdio.h>
int main()
{
int array[50],i,n;
printf("Enter the number of elements to be stored in the array");
scanf("%d",&n);
printf("Enter the numbers to store them in array \n");
for(i=0;i<n;i++)
{
scanf("%d", &array[i]);
array[i]=array[i]+10;
}
printf("Element in the array after adding 10 \n");
for(i=0;i<n;i++)
{
printf("%d \n", array[i]);
}
return 0;
}
Homework
Write a program to read the elements of a 1D array
from user and add 10 to all odd numbers in the
array.
• Write a program to read an array and find the
sum of elements of the array
s=0;
for(i=0;i<n;i++)
s=s+a[i];
printf (“Sum of elements of the array=%”,sum);
Linear Search
• A search traverses the collection until
– The desired element is found
– Or the collection is exhausted
• If the collection is not ordered, we would have to look at all
elements and can stop looking when all the elements in the
collection are examined.
• The linear (or sequential) search algorithm on an array is:
– Sequentially scan the array, comparing each array item with the searched value.
– If a match is found; return the index of the matched element.
Linear Search Algorithm
Step 1: Start
Step 2: Read the array elements and store it inA[] and the element to be
searched as x
Step 3: Set i to 1
Step 4: if i > n then go to step 9
Step 5: if A[i] = x then go to step 8
Step 6: Set i to i + 1
Step 7: Go to Step 4
Step 8: Print Element x Found at index i and go to step 10
Step 9: Print element not found
Step 10: Stop
Linear Search - Program
#include<stdio.h>
int main()
{
int a[20], i, x, n;
printf("Enter number of elements in the array: ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter the element to search:");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
printf("Element found at index %d",i);
break;
}
}
if(i>=n)
printf("Element not found");
return 0;
}
Sorting
• Sorting takes an unordered collection
and makes it an ordered one.
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
Bubble Sort
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1 2 3 4 5 6
77 42 35 12 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1 2 3 4 5 6
42Swap42
77 77 35 12 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1 2 3 4 5 6
42 35Swap35
77 77 12 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1 2 3 4 5 6
42 35 12Swap12
77 77 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 Swap101
101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 101
Largest value correctly placed
Items of Interest
• Notice that only the largest value is
correctly placed
• All other values are still out of order
• So we need to repeat this process
1 2 3 4 5 6
42 35 12 77 5 101
Largest value correctly placed
“Bubbling” All the Elements
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Repeat “Bubble Up” How Many Times?
• If we have N elements…
• And if each time we bubble an
element, we place it in its correct
location…
• Then we repeat the “bubble up”
process N – 1 times.
• This guarantees we’ll correctly
place all N elements.
Number of Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
Code Snippet
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n - i - 1; j++)
{ // last i elements are sorted already
if (a[j] > a[j + 1])
{ // swap
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
Inserting an element to an array
100
Inserting an element in an array
#include<stdio.h>
int main()
{
int array[50],i,n,ele,pos;
printf("Enter the number of elements to be stored in the array");
scanf("%d",&n);
printf("Enter the numbers to store them in array \n");
for(i=0;i<n;i++)
{
scanf("%d", &array[i]);
}
printf("Enter the element to be inserted");
scanf("%d",&ele);
printf("Enter the position to be inserted");
scanf("%d",&pos);
Inserting an element in an array cont’d
//Shift all elements from last element one position to right till
the position to be inserted is reached
for(i=n;i>=pos-1;i--)
{
array[i]=array[i-1];
}
array[pos-1]=ele;
n++;
//Printing the elements of after insertion
for(i=0;i<n;i++)
{
printf("%d \n", array[i]);
}
}
Deleting an element from an array
Deleting an element from an array
#include<stdio.h>
int main()
{
int array[50],i,j,n,ele;
printf("Enter the number of elements to be stored in the array");
scanf("%d",&n);
printf("Enter the numbers to store them in array \n");
for(i=0;i<n;i++)
{
scanf("%d", &array[i]);
}
printf("Enter the element to be deleted");
scanf("%d",&ele);
Deleting an element from an array cont’d
// Search for the element and shift all elements after that one position to left
for(i=0;i<n-1;i++)
{
if (array[i]==ele)
{
for (j=i;j<n-1;j++)
{
array[j]=array[j+1];
}
}
}
n--;
for(i=0;i<n;i++)
{
printf("%d \n", array[i]);
}
}
THANK YOU