Array: Malaysian Institute of Aviation Technology
Array: Malaysian Institute of Aviation Technology
ARRAY
Malaysian Institute of Aviation Technology
Introduction
• An array is collection of items stored at continuous
memory locations.
• We can use normal variables (v1, v2, v3, ..) when
we have small number of objects, but if we want to
store large number of instances, it becomes
difficult to manage them with normal variables.
• The idea of array is to represent many instances in
one variable.
Malaysian Institute of Aviation Technology
Introduction
• First element in an array is the
zeroth element, a[0]
• In the example: Second element
is a[1]= 6. Element two is
a[2]=11. Different way of
statement resulting in different
meaning!
Malaysian Institute of Aviation Technology
Array Declaration
• Array can be declared by specifying its type and size
or by initializing it or by both.
– Array declaration by specifying size
– Array declaration by initializing elements
– Array declaration by specifying size and initializing
elements.
Malaysian Institute of Aviation Technology
• int a[5];
• int a[5] = {0}; //all elements equal zero
• int a[5] = {10, 20, 30, 40, 50};
• int a[5] = {10, 20, 30, 40, 50, 60}; //error
• float a[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
• char a[6] = {'h', 'e', 'l', 'l', 'o'};
Note: a[6] carries the value ‘/0’, more on
this later.
Malaysian Institute of Aviation Technology
Example
Malaysian Institute of Aviation Technology
Input / Output
• Q) Declare an integer array named a with 10 elements
A) int a[10];
Character Array
• This type of array Stores characters
• Character arrays are also capable of storing strings
• Revise: %c for characters ( eg. A, b, c, D )
%s for strings ( eg. hi, welcome, good )
Malaysian Institute of Aviation Technology
Character Array
• char string[]="hello";
• Initializes the elements of array string to the
individual characters in the string “hello”
• string "hello" has 5 characters plus a special string
termination character called null character ('\0‘)
Malaysian Institute of Aviation Technology
Character Array
char string[]={'h', 'e', 'l', 'l', 'o', '\0'};
• string[0] = 'h'
• string[1] = 'e'
• string[2] = 'l'
• string[3] = 'l'
• string[4] = 'e‘
• string[5] = '\0'
Malaysian Institute of Aviation Technology
Input / Output
• Q) Declare a char array named string1 with 20 elements
A) char string1[20];
Input / Output
• Function scanf will read characters until a space, tab,
newline or EOF is encountered
• The string should be no longer than 19 characters to
leave room for ‘\0’.
Malaysian Institute of Aviation Technology
Example
Malaysian Institute of Aviation Technology
…
}
Malaysian Institute of Aviation Technology
Example:
• Eg. int b[3][4];
Malaysian Institute of Aviation Technology
Malaysian Institute of Aviation Technology
Exercise 1
• Write a program to find the average of the
entered numbers
Malaysian Institute of Aviation Technology
#include <stdio.h>
int main(){
int avg = 0;
int sum =0;
int x=0;
/* Array- declaration – length 4*/
int num[4];
/* We are using a for loop to traverse through the array
* while storing the entered values in the array
*/
for (x=0; x<4;x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
sum = sum+num[x];
}
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
Malaysian Institute of Aviation Technology
Exercise 2
Write a Program to Find Largest Element of an
Array. Max number of element is 10.
Malaysian Institute of Aviation Technology
#include <stdio.h>
Answer 2
int main() {
int i, n;
float arr[10];
printf("Enter total number of elements(1 to 10): ");
scanf("%d", &n);
printf("\n"); // Stores number entered by the user
for(i = 0; i < n; ++i) {
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]);
}
// Loop to store largest number to arr[0]
for(i = 1; i < n; ++i) {
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i]) arr[0] = arr[i];
}
printf("Largest element = %.2f", arr[0]);
return 0; }
Malaysian Institute of Aviation Technology
Exercise 3
Write a program to Add Two Matrix Using Multi-
dimensional Arrays.
Malaysian Institute of Aviation Technology
Answer 3
#include <stdio.h>
int main(){
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter number of rows (between 1 and 10): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 10): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j) {
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]); }
printf("Enter elements of 2nd matrix:\n");
Malaysian Institute of Aviation Technology
Exercise 4
Write a program to Multiply two Matrix Using
Multi-dimensional Arrays
Malaysian Institute of Aviation Technology
Answer 4
#include <stdio.h>
int main() {
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to row of second matrix
while (c1 != r2) {
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
Malaysian Institute of Aviation Technology