Arrays
Arrays
Definition
• An array is a fixed-size sequenced collection of
elements of the same data type.
• An Array is a collection of similar datatype of
elements addressed by a common variable name.
Syntax :
type arrayname [ size ];
Where the type specifies the type of element that will be
contained in the array, such as int, float, or char.
The size indicates the maximum number of elements that can
be stored inside the array. Size must be an integer constant
greater than zero.
Declaration Example
void main()
{
int x [ 10 ] ;
}
size
Data type Array Name
scores
scores: : 85
85 79
79 92
92 57
57 68
68 80
80 . .. .. .
0 1 2 3 4 5 98 99
7
Array Declaration
// array of 10 uninitialized
integers
int Ar[10];
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --
Initialization of One-Dimensional Array
First Last
X[0] X[1] X[2] X[3] X[4]
Element Element
• void main()
• {
• int x[5] = { 35,40,20,57};
• X[0]= 35;
• X[1] = 40;
• X[2] = 20;
• X[3] = 57;
• }
The values to the array elements can be
assigned as,
X[0] = 35;
X[1]= 40;
X[2]= 20;
X[3]= 57;
X[4]= 19;
35 40 20 57 19
void main()
{
int x [10], i;
for (i = 0; i<10; i++)
{
x[i] = 1;
}
}
1
• Void main()
• {
• int x [10],i;
• for(i=0; i<10; i++)
• {
• scanf(“%d”, &x[i];
• }
• }
13 50 25
Reading Data into an Array
Using scanf() function.
void main()
{
int x [3];
scanf(“%d%d%d”, &x[0], &x[1], &x[2]);
}
Reading Data into an Array
Using scanf() function.
void main()
{
int x [10], i;
for (i = 0; i<10; i++)
{
scanf(“%d”, &x[i]);
}
}
• void main()
• int x[] ={1, 2, 3, 4, 5,6,7,8,9,10};
• for (i=0; i<10; i++)
• {
• printf(“%d”, x[i];
• }
• }
Printing the contents of an Array
void main()
{
int x [5] = {1,2,3,4,5};
for (i = 0; i<10; i++)
{
printf(“%d”, x[i]);
}
}
1 2 3 4 5
void main()
{
int x[10],i;
printf(“Enter 10 integers”);
for(i=0; i<5; i++)
{
scanf(“%d”, &x[i]);
}
for (i=0; i<10; i++)
{
printf(“%d”, x[i]);
}
}
Write a program to read data from keyboard into an
array and display the array content.
void main()
{
int x [10],i;
for (i = 0; i<10; i++)
{
scanf(“%d”, &x[i]);
}
for (i = 0; i<10; i++)
{
printf(“%d”, x[i]);
}
}
One Dimensional Array
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
"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
42 Swap4277
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
1 2 3 4 5 6
42 35 12 77 5 101
• If we have N elements…
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
Program to sort the elements of an array (Using Bubble Sort)
#include <stdio.h>
void main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n – 1 ; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For increasing order use < */
{ swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
}
2- Dimensional Array
2- Dimensional Array
Row 0 8 16 9 52
Row 1 3 15 27 6
Row 2 14 25 2 10
val Col 0 Col 1 Col 2 Col 3
Row 0 8 16 9 52
Row 1 3 15 27 6
Row 2 14 25 2 10
This will initialize the first row to zero and the second row
to one
Initializing 2D arrays
int table[][3] = {
{0, 0, 0},
{ 1, 1, 1 },
{2 , 2, 2 }
};
This is permitted.
This will initialize the first row to zero and the second row to one
Initializing 2D arrays
int table[2][3] = {
{ 1, 1 },
{2}
};
This is permitted.
This will initialize the first two elements of first row to 1 , the first
element of the second row to 2, and all other elements to 0.
A
1 1 0 A[0][0] A[0][1] A[0][2]
2 0 0
A[1][0] A[1][1] A[1][2]
void main()
{
int x[3][3] row, col;
for(row = 0; row<3; row++)
{
for(col = 0; col<3; col++)
{
X[0][0] x[0][1] x[0][2]
scanf(“%d”, &x[row][col]; X[1][0] x[1][1] x[1][2]
} X[2][0] x[2][1] x[2][2]
}
For(row = 0; row<3; row++)
{
for(col = 0; col<3; col++)
{
printf(“%d”, x[row][col];
}
Printf(“\n”);
}
}
}
Reading data into an 2-Dimensional Array
void main()
{
int a[3][3], i, j;
for(i=0; i < 3; i++)
{
for(j=0; j < 3; j++)
{
scanf(“%d”, &a[i][j]);
}
}
}
Displaying the contents of an
2-Dimensional Array
void main()
{
int a[3][3], i, j;
for(i=0; i < 3; i++)
{
for(j=0; j < 3; j++)
{
scanf(“%d”, &a[i][j]);
}
}
for(i=0; i < 3; i++)
{
for(j=0; j < 3; j++)
{
printf(“%d”, &a[i][j]);
}
printf(“\n”);
}
}
Exercise
j
j 0 1 2 3
0 1 2 3
0 3 4 0 22 29 45 35
2 3 7 1
1 5 2 x = 1 18 40 47 21
4 5 6 8 2
2
1 6 26 33 43 49
i i
j=0
c[i][j] =
2
i=0 3 4 x k = a[i][k=0]*b[k=0][j] +
4 a[i][k=1]*b[k=1][j]
k
62
Matrix Multiplication
Logic of Matrix Multiplication
#define N 3
#define M 2
#define L 4
void matrix_mul(a[N][M], int b[M][L], int c[N][L])
{
int i, j, k;
for(i=0; i < N; i++) {
for(j=0; j < L; j++) {
c[i][j] = 0;
for(k=0; k < M; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return;
}
63
Passing One-Dimensional Array to Function
Program to find the largest value in an
array of elements. void FindLargest(int a[], int n)
{
#include<stdio.h>
int I;
void main() int max;
{ max = a[0];
void FindLargest(int a[], int n) for(i= 1; i < n; i++)
int value[5] = { 12, 27, 32, 15,95 }; {
FindLargest (value, 4); if (max < a[i])
max = a[i];
}
}
printf(“%d”, max);
}
Passing One-Dimensional Array to Function
Program to find the largest value in an
array of elements. void FindLargest(int a[4])
{
#include<stdio.h>
int I;
void main() int max;
{ max = a[0];
void FindLargest(int a[4]) for(i= 1; i < 4; i++)
int value[5] = { 12, 27, 32, 15,95 }; {
FindLargest (value); if (max < a[i])
max = a[i];
}
}
printf(“%d”, max);
}
Passing Two-Dimensional Array to Function
Program to find the average of the values void FindLargest(int a[][2], int r, int c )
in a two dimensional matrix. {
#include<stdio.h> int i;
float avg;
void main()
int sum=0;
{ for(i= 0; i < r; i++)
void FindLargest(int a[][2], int r, int c) {
int value[2][2] = { 1,2,3,4 }; for(j=0; j<c; j++)
FindAverage (value, 2,2); {
} sum = sum + a[i][j];
}
}
avg = sum /4.0;
Printf(“%f”, avg);
}
Passing Two-Dimensional Array to Function
Program to find the average of the values void FindLargest(int a[2][2])
in a two dimensional matrix. {
#include<stdio.h> int i;
float avg;
void main()
int sum=0;
{ for(i= 0; i < 2; i++)
void FindLargest(int a[2][2]) {
int value[2][2] = { 1,2,3,4 }; for(j=0; j<2; j++)
FindAverage (value); {
} sum = sum + a[i][j];
}
}
avg = sum /4.0;
Printf(“%f”, avg);
}