Sie sind auf Seite 1von 35

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

Array Declaration by Specifying Size


// Array declaration by specifying size
int arr1[10];

// With recent C/C++ versions, we can also


// declare an array of user specified size
int n = 10;
int arr2[n];
Malaysian Institute of Aviation Technology

Array Declaration by Initializing Elements


// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }

// Compiler creates an array of size 4.


// above is same as
int arr[4] = {10, 20, 30, 40}
Malaysian Institute of Aviation Technology

Array Declaration by Specifying size and


initializing elements
// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 }

// Compiler creates an array of size 6, initializes first


// 4 elements as specified by user and rest two elements as 0.
// above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"
Malaysian Institute of Aviation Technology

Defining and Initializing One Dimensional Array

• 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];

• Q) Input an integer into 6th element


A) scanf("%d ", &a[5]);

• Q) Output the content of 6th element


A) printf("a[5] is %d.\n ", a[5]);
Malaysian Institute of Aviation Technology

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];

• Q) Input a string into the array


A) scanf("%s", string1);

• Q) Output the array’s content


A) printf("The content is %s.\n", string1);
Malaysian Institute of Aviation Technology

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

Reading and Writing a line of Text


#include <stdio.h>
int main()
{
char line[80];
printf("enter name :");
scanf("%s", &line);
printf("the entered name is %s",line);
return 0;
}
Malaysian Institute of Aviation Technology

Example
Malaysian Institute of Aviation Technology

Passing Arrays to Functions


• Modify the elements in function body is modifying
the actual elements of the array
void main(){
{
int array[30]; //array declaration
func(array) //call function
}
void func(int array[]) {
… //function prototype
}
Malaysian Institute of Aviation Technology

Passing Elements to Functions


• Passing an array’s element is like passing a
variable.
• The actual value is not modified
Malaysian Institute of Aviation Technology

Passing Elements to Functions


Void main()
{
int array[30];
func(array[5]) the passed element
}

void func(int e) { variable e copies the value of array element


}
Malaysian Institute of Aviation Technology

Two Dimensional Array


• To represent table of values consisting of information
arranged in rows and columns
• int a[2][3];
• int a[2][3] = {{1, 2, 3}, {2, 3, 4}};

1st Row 2nd Row


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

for(i=0; i<r; ++i)


for(j=0; j<c; ++j) {
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]); }
// Adding Two matrices
for(i=0;i<r;++i) for(j=0;j<c;++j) {
sum[i][j]=a[i][j]+b[i][j]; }
// Displaying the result
printf("\nSum of two matrix is: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j) {
printf("%d ",sum[i][j]);
if(j==c-1) {
printf("\n\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

// Storing elements of first matrix.


printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j) {
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Storing elements of second matrix.
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j) {
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
Malaysian Institute of Aviation Technology

// Initializing all elements of result matrix to 0


for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
result[i][j] = 0;
}
// Multiplying matrices a and b and storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k) {
result[i][j]+=a[i][k]*b[k][j];
}
Malaysian Institute of Aviation Technology

// Displaying the result


printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
printf("%d ", result[i][j]);
if(j == c2-1)
printf("\n\n");
}
return 0;
}

Das könnte Ihnen auch gefallen