Lesson 1
ARRAYS
Lesson objectives:
1. To review and implement array concepts in programming
2. To review and implement the appropriate array type given a problem.
3. To review and implement array concepts in sorting and searching data.
4. To learn and implement multi-dimensional concepts in programming.
1.1 Definition of an Array
Collection or set of homogenous elements.
Array usage:
This data structure is used for temporarily storing a finite number of elements for processing.
Types of array
1. One-dimensional or a List of elements
2. Two-dimensional or a Table of elements
3. Multi-dimensional or Table/s within a table of elements
Parts of an Array
a. Array variable name
b. Array data type
c. Number of elements of the array
ONE-DIMENSIONAL ARRAY
Declaring a ONE-DIMENSIONAL array
Format:
<data type> <variable name> [array size];
Example:
int x[5]; // array x has 5 integer elements
[0] [1] [2] [3] [4]
*** Array elements can be accessed thru the array index.
*** An array index starts at index 0.
Sample program:
#include<stdio.h>
#include<conio.h>
int x[5]; // array declaration
int i; // declaring an iteration variable
main()
{ // enter array values using the for loop
for (i=0; i<5;i++)
{ printf("Enter a value for array x[%d] ",i);
scanf("%d", &x[i]);
}
// printing the array values
printf("\n\nThe array values are\n\n");
for (i=0; i<5;i++)
printf("x[%d] = %d\n",i,x[i]);
return 0;
}
TWO-DIMENSIONAL ARRAY
A two-dimensional array is like a table of elements.
Declaring a TWO-DIMENSIONAL array
Format:
<data type> <variable name> [array size][array size];
Example:
int x[3][3]; // A 3 x 3 array having 9 elements
COLUMNS
ROWS
3 x 3 Array index combinations
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
Sample Program:
#include<stdio.h>
#include<conio.h>
int x[3][3];
int i,j;
main()
{ // clrscr();
// Enter integer values in the 3x3 array
for (i=0; i<3; i++) // row iteration variable
for (j=0; j<3; j++) // column iteration variable
{ printf("Enter an integer value: ");
scanf("%d", &x[i][j]); // input numbers are entered row-wise
}
printf("\nThe array values are\n\n");
// Printing the array values
for (i=0; i<3; i++)
{ for (j=0; j<3; j++)
printf("%5d", x[i][j]);
printf("\n");
}
getch();
return 0;
}
Output:
THREE-DIMENSIONAL ARRAY
A Three-dimensional array is like a table within a table.
Declaring a THREE-DIMENSIONAL array
Format:
<data type> <variable name> [array size][array size][array size];
Example:
int x[2][3][3]; // 2 tables with 3 x 3 array each
TABLE [0] TABLE[1]
3 x 3 Array 3 x 3 Array
Sample Program:
#include<stdio.h>
#include<conio.h>
int x[2][3][3];
int i,j,k;
main()
{ clrscr();
// Enter integer values in the 2x3x3 array
for (i=0; i<2; i++)
for (j=0; j<3; j++)
for (k=0;k<3;k++)
{ printf("Enter an integer value: ");
scanf("%d", &x[i][j][k]);
}
printf("\nThe array values are\n");
// Printing the array values
for (i=0; i<2; i++)
{ printf("\nTable %d values:\n\n ", i);
for (j=0; j<3; j++)
{ for (k=0;k<3;k++)
printf("%5d",x[i][j][k]);
printf("\n");
}
}
getch();
return 0;
}
Output:
1.1.2 SORTING
An implementation of the array data structure is SORTING
SORT is a technique to arrange the elements in the array in ascending or descending
order. There are a number of sorting techniques but the most commonly used
techniques are discussed below:
SORTING TECHNIQUES
BUBBLE SORT
#include<stdio.h>
#include<conio.h>
int x[5],i,noex,temp;
main()
{ clrscr();
for(i=0;i<5;i++)
{ printf("Enter a value for x: ");
scanf("%d", &x[i]);
}
printf("The original values are: ");
for (i=0; i<5; i++)
printf("%3d", x[i]);
printf("\n\n");
do
{ noex =0;
for (i=0;i<4;i++)
{ if (x[i] > x[i+1])
{ temp = x[i];
x[i] = x[i+1];
x[i+1] = temp;
noex = 1;
}//if
}//for
} while (noex);//while
// printing sorted array
printf("The sorted values are: ");
for (i =0; i<5; i++)
printf("%3d", x[i]);
getch(); return 0;
}
SHELL SORT
#include<stdio.h>
#include<conio.h>
int x[5],i,noex,temp,gap;
main()
{ clrscr();
for(i=0;i<5;i++)
{ printf("Enter a value for x: ");
scanf("%d", &x[i]);
}
printf("The original values are: ");
for (i=0; i<5; i++)
printf("%3d", x[i]);
printf("\n\n");
gap = 5/2;
while (gap!=0)
{
do
{ noex =0;
for (i=0;i<5-gap;i++)
{ if (x[i] > x[i+gap])
{ temp = x[i];
x[i] = x[i+gap];
x[i+gap] = temp;
noex = 1;
}//if
}//for
} while (noex);//while
gap = gap/2;
}
// printing sorted array
printf("The sorted values are: ");
for (i =0; i<5; i++)
printf("%3d", x[i]);
getch(); return 0;
}
BALLOON SORT
#include<stdio.h>
#include<conio.h>
int x[5],I,j,temp;
main()
{ clrscr();
for(i=0;i<5;i++)
{ printf(“Enter a value for x: “);
scanf(“%d”, &x[i]);
}
printf(“The original values are: “);
for (i=0; i<5; i++)
printf(“%3d”, x[i]);
printf(“\n\n”);
// Sorting
for (i=0; i<4;i++)
for(j=i+1; j<5;j++)
if(x[i] > x[j])
{ temp = x[i];
x[i] = x[j];
x[j] = temp;
}
printf(“The sorted values are: “);
for (i=0; i<5; i++)
printf(“%3d”, x[i]);
printf(“\n\n”);
getch(); return 0;
}
TWO-DIMENSIONAL SORTING
Solution #1:
#include<stdio.h>
#include<conio.h>
// ipg – April 10, 2022
// Ascending Order
int x[3][3];
int i, j,k,m,temp;
main()
{ //input elements in the 3x3 array
for (i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf("enter a value for x[%d][%d]: ",i,j);
scanf("%d",&x[i][j]);
}
//print the elements
printf("\n\nThe Table values are:\n\n");
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d", x[i][j]);
printf("\n");
}
//sorting 2-dim
k=0;m=0;
while (k<3)
{ m=0;
while (m <3)
{
for (i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ if (x[k][m] < x[i][j])
{ temp = x[k][m];
x[k][m] = x[i][j];
x[i][j] = temp;
} // end if
}// end j
} // end i
m++;
} // end m
k++;
} // end k
//print the sorted table of elements
printf("\n\nThe Sorted Table values are:\n\n");
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%5d", x[i][j]);
printf("\n");
}
return 0;
}
Solution #2
#include <stdio.h>
#define ROW 10
#define COL 10
// Balloon Sort
//Ascending Order
int main() {
int mat[ROW][COL];
int i, j, k, l, n, tmp;
/* get the order of the matrix from the user */
printf("Enter the order of the matrix:");
scanf("%d", &n);
/* get the matrix entries from the user */
printf("Enter your entries for the input mat:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
/* sort the contents of the two dimensional array */
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
tmp = mat[i][j];
l = j + 1;
for (k = i; k < n; k++) {
while (l < n) {
/* swapping the data */
if (tmp > mat[k][l]) {
tmp = mat[k][l];
mat[k][l] = mat[i][j];
mat[i][j] = tmp;
}
l++;
}
l = 0;
}
}
}
/* print the result */
printf("\n");
printf("Resultant Matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
1.1.3 Searching uses an array implementation in order to look for a certain item in a list
1.1.3.1 Linear Search Program
This program will search if an input number is in the list.
#include<stdio.h>
#include<conio.h>
int i,num,found =0;
int x[5];
main()
{ clrscr();
// Enter elements in the array
for (i=0;i< 5;i++)
{ printf("Enter a value: ");
scanf("%d",&x[i]);
}
0 5
1 9
2 34
3 78
4 1
// Enter a number to be searched
printf("enter a number to be searched: ");
scanf("%d", &num);
// Go thru the array elements to find the number
for (i=0;i<5;i++)
if (num == x[i])
{ found =1; // Set found to 1 if the number is in the list found = 1
break; // get out of the loop
}
if (found == 1)
printf("The number %d is in the list\n", num);
else
printf("The number %d is not in the list\n", num);
getch(); return 0;
}
1.1.3.2 Binary Search Program
#include<stdio.h>
#include<conio.h>
int i,j, first, last, mid, n, num,temp;
int arr[20];
char ans = 'Y';
main()
{ clrscr();
printf("How many array integer values to be entered? ");
scanf("%d", &n);
printf("Enter array values ....\n");
for (i=0; i<n; i++)
scanf("%d", &arr[i]);
// Sort array values in ascending order using Balloon sort
for(i=0; i<n-1; i++)
for (j = i+1; j<n; j++)
if (arr[i] > arr[j])
{ temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// print sorted values
printf ("Sorted values\n");
for (i=0; i<n; i++)
printf("%3d", arr[i]);
printf ("\n");
getch();
// Binary search process
clrscr();
while (ans == 'Y' || ans == 'y')
{ printf("Enter a number to be searched: ");
scanf("%d", &num); num=
0 1
first =0; first =
1 5
last = n-1; last =
mid = (first+last)/2; mid = 2 9
while (first <=last) 3 34
{ if (arr[mid] < num) 4 78
first = mid +1;
else if ( arr[mid] == num)
{ printf("%d is in the list!\n", num);
break;
}
else
last = mid - 1;
mid = (first+last)/2;
}
if (first > last)
printf("Not found!\n");
printf("\nWould you like to search another number? ");
scanf("\n");
scanf("%c", &ans);
}
printf("End of program!");
getch();
return 0;
}