Arrays and Matrices
Arrays and Matrices
MODULE III
C HAPTER-4: ARRAYS
Arrays 1
Programming in C and Data Structures
Array is a data structure which collects related data items that share a common array name and
data type is called an array.
4.4 Properties of an Array
Arrays 2
Programming in C and Data Structures
Num[3]
Arrays 3
Programming in C and Data Structures
Num[4]
The table below shows the values that are stored in the particular numbers.
Num[0]
Num[1]
Num[2]
Num[3]
Num[4]
57
20
56
17
23
Arrays 4
Programming in C and Data Structures
Arrays 5
Programming in C and Data Structures
double median_x;
/* Determine median of
values in the array. */
k = floor(n/2);
if( n % 2 != 0)
median_x = x[k];
else
median_x = (x[k-1]+x[k])/2;
/* Return median value. */
return median_x;
}
Arrays 6
Programming in C and Data Structures
Here, a is an array of two dimension, which is an example of multidimensional array. This array
has 2 rows and 6 columns.
Advantages:
Arrays 7
Programming in C and Data Structures
Arrays 8
Programming in C and Data Structures
Arrays 9
Programming in C and Data Structures
1. Write a program to sort the arrays in ascending order using bubble sort:
#include<stdio.h>
#include<conio.h>
Arrays 10
Programming in C and Data Structures
}
}
Arrays 11
Programming in C and Data Structures
return -1;
}
void main()
{
int n, key, a[20], i, pos;
printf(“enter the value of n”);
scanf(“%d”, &n);
prinf(“enter the elements of array”);
for(i=0; i<n;i++)
scanf(“%d”, &a[i]);
printf(“enter the item to be searched”);
scanf(“%d”, &key);
pos=linear(key, a, n);
if(pos==-1)
printf(“Item not found”);
else
printf(“Item found\n”);
}
Arrays 12
Programming in C and Data Structures
{
int low, high, mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
return mid;
if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}
void main()
{
int n, a[10], key, pos;
printf(“enter the no. of elements”);
scanf(“%d”, &n);
printf(“enter the elements”);
for(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
prinf(“enter the elements to be searched”);
scanf(“%d”, &key);
pos=binary_search(key, a, n);
if(pos==-1)
printf(“Item not found”);
else
printf(“Item found\n”);
}
Arrays 13
Programming in C and Data Structures
Arrays 14
Programming in C and Data Structures
The type specifies the type of 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.
2. int group1[11];
Declares the group1 as an array to contain a maximum of 10 integer constants.
The C language treats character strings simply as arrays of characters. The size in a character
string represents the maximum number of characters that the string can hold.
For example:
char text[10];
Suppose we read the following string constant into the string variable text.
“HOW ARE Y OU”
Each character of the string is treated as an element of the array text and is stored in the
memory as follows.
‘H’
‘O’
‘W’
‘A’
‘R’
‘E’
‘Y ’
‘O’
‘U’
‘\o’
When the compiler sees a character string, it terminates it with an additional null character. Thus,
the element text[11] holds the null character ‘\o’ at the end. When declaring character arrays, we
must always allow one extra element space for the null terminator.
Arrays 15
Programming in C and Data Structures
For example:
static float num1[5]={0.1,2.3,4.5};
Will initialize the first three elements to 0.1,2.3 and 4.5 and the remaining two elements to zero.
The word static used before type declaration declares the variable as a static variable. In some
cases the size may be omitted. In such cases, the compiler allocates enough space for all
initialized elements.
For example, the statement
static int count[ ]= {2,2,2,2};
Will declare the counter array to contain four elements with initial values 2.
Character arrays may be initialized in a similar manner. Thus, the statement
int list[5], i;
for(i=0; i<5; i++){
list[i] = i;
}
OR
for(i=0; i<=4; i++){
list[i] = i;
}
Arrays 16
Programming in C and Data Structures
#define SIZE 39
#define GRADES 5
int main ( void )
{
int score [ SIZE ] ;
int gradeCount [ GRADES ] ;
–
–
–
}
Arrays 17
Programming in C and Data Structures
Arrays 18
Programming in C and Data Structures
printf(“Result = %d”,res);
}
Arrays 19
Programming in C and Data Structures
printf(“%5d”, a[i][j]);
printf(“\n”);
}
Arrays 20
Programming in C and Data Structures
main()
{
int i;
float a[10],value1,total;
printf(“Enter 10 Real numbers\n”);
for(i=0;i<10;i++)
{
scanf(“%f”, &value);
x[i]=value1;
}
total=0.0;
for(i=0;i<10;i++)
total=total+a[i]*a[i];
printf(“\n”);
for(i=0;i<10;i++)
printf(“x[%2d]= %5.2f\n”, i+1, x[i]);
printf(“\ntotal=%.2f\n”, total);
}
Arrays 21
Programming in C and Data Structures
{
row=i+1;
printf(“%2d|”, R1);
for(j=1;j<=C1;j++)
{
col=j;
prod[i][j]=row*col;
printf(“%4d”, prod[i][j]);
}
printf(“\n”); } }
Output
MULTIPLICATION TABLE
1234
---------------------------------------
1 |1 2 3 4
2 |2 4 6 8
3 |3 6 9 12
4 |4 8 12 16
Arrays 22
Programming in C and Data Structures
#define N 10
main()
{
int i,j,k;
float a[N],t;
printf(“Enter the number of items\n”);
scanf(“%d”, &n);
printf(“Input %d values \n”, n);
for(i=1; i<=n ;;i++)
scanf(“%f”, &a[i]);
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i; j++)
{
if)a[j]<=a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
else
continue;
}
}
for(i=1;i<=n;i++)
printf(“%f”, a[i]);
}
Arrays 23
Programming in C and Data Structures
int i,n;
Arrays 24
Programming in C and Data Structures
#include<stdio.h>
main()
{
int i,s, largcount,smcount;
float num[30],lar,small;
printf(“\n size of array (MAX 30): \t”);
scanf(“%d”, &size);
printf(“\n Array elements:\t”);
for(i=0;i<size;i++)
scanf(“%f”, &num[i]);
for(i=0;i<size;i++)
printf(“%f”, &num[i]);
lar=small=num[0];
larcount=smcount=1;
for(i=1;i<size;i++)
Arrays 25
Programming in C and Data Structures
{
if(num[i]>lar)
{
lar=num[i];
larcount=i+1;
}
elseif(num[i]<small)
{
small=num[i];
smcount=i+1;
}
}
printf(“\n Largest value is % f found at %d”, lar,larcount);
printf(“\n Smallest value is %f found at %d “, small, smcount);
}
8. Design a program to find maximum value in data array where the elements in the
array are random number between 0 - 99
#include <stdio.h>
{
int data[10], max, i;
for (i=0; i<5; i++)
data[i] = rand()%100;
max = data[0];
for (i=1; i<5; i++)
{
if (data[i] > max)
max = data[i];
}
printf("Max = %d\n",max);
return 0 ;
}
Output:
Consider 5 random numbers: 35, 4,78,12,45
Max = 78
Arrays 26
Programming in C and Data Structures
10. Write a program to add all the element of single dimensional array.
#include<iostream.h>
#include<conio.h>
int main()
{
int Arr[100],n,sum=0;
cout<<"Enter number of elements you want to insert ";
cin>>n;
Arrays 27
Programming in C and Data Structures
for(int i=0;i<n;i++)
Arrays 28
Programming in C and Data Structures
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];
getch();
return 0;
}
Arrays 29
Programming in C and Data Structures
Arrays 30
Programming in C and Data Structures
}
}
12. Write a c program to find out second largest element of an unsorted array.
#include<stdlib.h>
main()
{
int a[100],i,num,n,max;
printf("enter number of elements to be entered");
scanf("%d",&n);
printf("enter numbers");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
num=a[0];
for(i=0;i<n;i++)
{
if(a[i]>num)
{
if(a[i]>max)
{num=max;
max=a[i];}
if(a[i]<max)
num=a[i];
}
}
printf("second largest no. is %d",num);
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
main()
{
Arrays 31
Programming in C and Data Structures
int a[100],i,n,loc;
printf("enter number of elements to be entered");
scanf("%d",&n);
printf("enter numbers");
for(i=0;i<n;i++)
Arrays 32
Programming in C and Data Structures
scanf("%d",&a[i]);
printf("enter the position at which element has to be deleted");
scanf("%d",&loc);
for(i=loc-1;i<n-1;i++)
a[i]=a[i+1];
for(i=0;i<n-1;i++)
printf("%d\n",a[i]);
system("pause");
}
#include <stdio.h>
void main()
{
int array[10];
int i, j, n, m, temp, key, pos;
Arrays 33
Programming in C and Data Structures
}
}
}
printf("Sorted list is \n");
for (i = 0; i < n; i++)
Arrays 34
Programming in C and Data Structures
{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted \n");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
array[pos] = key;
printf("Final list is \n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
}
15. Write a program to swap two integers with and without using temporary
variables.
#include<stdio.h>
#include<conio.h>
int main()
{
int first, second; // declaring two variables
printf(“Read two values ”);
scanf(“%d%d”, &first, &second);
first=first+second;
second=first-second;
first=first-second;
printf(“After swapping the values are: %d, %d ”, first, second);
getch();
Arrays 35
Programming in C and Data Structures
return 0;
}
Arrays 36
Programming in C and Data Structures
# i nc l ud e < s t d i o . h>
v o i d ma i n( )
{
i nt a r r a y 1 [ 1 0] [ 1 0] , a r r a y 2[ 1 0] [ 1 0] , a r r a y s um[ 1 0] [ 1 0] ,
a r r a y d i f f [ 1 0] [ 1 0] ;
i nt i , j , m, n, o p t i o n;
Arrays 37
Programming in C and Data Structures
}
br e ak;
c a s e 2:
f or (i = 0; i < m; i ++)
{
f or (j = 0; j < n; j ++)
{
Arrays 38
Programming in C and Data Structures
ar r aydi f f [ i ] [ j ] = ar r ay1 [ i ] [ j ] - a r r a y 2[ i ] [ j ] ;
}
}
p r i nt f ( " Di f f e r e nc e ma t r i x i s \ n" ) ;
f or (i = 0; i < m; i ++)
{
f or (j = 0; j < n; j ++)
{
p r i nt f ( " %3d " , ar r aydi f f [ i ] [ j ] ) ;
}
p r i nt f ( " \ n" ) ;
}
br e ak;
}
# i nc l ud e < s t d i o . h>
v o i d ma i n( )
{
s t at i c i nt a r r a y [ 1 0] [ 1 0] ;
i nt i , j , m, n;
Arrays 39
Programming in C and Data Structures
f or (i = 0; i < m; ++i )
{
p r i nt f ( " %d " , ar r ay[ i ] [ j ] ) ;
}
p r i nt f ( " \ n" ) ;
}
4.11 Exercise
1. What is an array? What is the use of using array? How are they declared in C?
what are the rule to be followed while using arrays? (JULY /AUG 2005).
2. Write a C program to multiply two matrices A, B and store the result in C.
(JAN/FEB 2003)
3. Write a note on one dimensional and two dimensional arrays (JULY /AUG 2003)
4. Write a C program to read ‘n’ integer numbers interactively and to print the
biggest and smallest of ‘n’ numbers (JULY /AUG 2004)
5. Given two sets A and B of integers, write a program to read them, determine its
UNION and INTERSECTION and print the resultant sets. (FEB/MAR 2005)
6. Write a program to find the intersection of 2 arrays A and B with size m and n
respectively (JAN2006)
7. Explain initialization and declaration of 2D array (JUNE/JULY 2014)
8. Write a c program for insert an element at desired position in an array.
9. Write a c program for delete an element at desired position in an array.
10. Write a c program to find out second largest element of an unsorted array.
12. Write a program to add all the element of single dimensional array.
16. Write a program to swap two integers with and without using temporary variables
Arrays 40
Programming in C and Data Structures
4.12 Quiz:
Arrays 41
Programming in C and Data Structures
2. Are the expressions arr and &arr same for an array of 10 integers
a) Y ES b) NO
6. Does this mentioning array name gives the base address in all the contexts?
a) Y ES b) NO
Arrays 42
Programming in C and Data Structures
a) 2, 1, 15 b) 1, 2, 5 c) 3, 2, 15 d)2, 3, 20
}
a) The African Queen b) Queen c)Queen d) Null
13. If A[4] is declaration, then the first and last array index will be
(JUNE/JULY 2014)
a) 1,4 b) 0, 3 c) 3, 0 d) none of these
14. Given A[3][2] = { 1, 2, 3, 4, 5, 6}; The element in 3rd row 2nd col is
a) 3 b) 4 c) 6 d) 2
Arrays 43