0% found this document useful (0 votes)
10 views20 pages

C Lesson#6 (Fin)

This document provides an overview of arrays in C programming language. It discusses that an array is a collection of data that holds a fixed number of values of the same type. It describes one-dimensional and multi-dimensional arrays, and how to declare, initialize, access elements, insert and print values of arrays. Examples are provided to demonstrate summing two matrices using two-dimensional arrays and passing one-dimensional and entire arrays to functions in C. Key points about array bounds and memory allocation of arrays are also summarized.

Uploaded by

devora ave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

C Lesson#6 (Fin)

This document provides an overview of arrays in C programming language. It discusses that an array is a collection of data that holds a fixed number of values of the same type. It describes one-dimensional and multi-dimensional arrays, and how to declare, initialize, access elements, insert and print values of arrays. Examples are provided to demonstrate summing two matrices using two-dimensional arrays and passing one-dimensional and entire arrays to functions in C. Key points about array bounds and memory allocation of arrays are also summarized.

Uploaded by

devora ave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

C Language Programming

Lesson 6:
Arrays

Prepared by:
Prof. Marienel N. Velasco

Unit
Pg. 1
I’m gonna talk about:

Unit
Pg. 2
Arrays

• An array is a collection of data that holds fixed number of


values of same type. For example: if you want to store
marks of 100 students, you can create an array for it.

float marks[100];

• The size and type of arrays cannot be changed after its


declaration.
Unit
Pg. 3
Arrays

2 Types of
1. One-dimensional arrays
Arrays:
2. Multidimensional arrays

How to declare an array in C?


data_type array_name[array_size];

For example,
float mark[5];
Here, we declared an array, mark of floating-point type and
size 5. Meaning, it can hold 5 floating-point values.

Unit
Pg. 4
Elements of an Array and How to access them?

• You can access elements of an array by indices.


• Suppose you declared an array mark as above, The first
element is mark[0], second element is mark[1] and so on.

Few key notes:


•Arrays have 0 as the first index not 1. In this
example,mark[0]
•If the size of an array is n, to access the last element, (n-1)
index is used. In this example, mark[4]
•Suppose the starting address of mark[0] is 2120d. Then, the
next address, a[1] will be 2124d, address of a[2] will be 2128d
and so on. It's because the size of a float is 4 bytes.
Unit
Pg. 5
How to initialize an array in C programming?

• It's possible to initialize an array during declaration. For


example,
int mark[5] = {19, 10, 8, 17, 9};
• Another method to initialize array during declaration:
int mark[] = {19, 10, 8, 17, 9};

Here,
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
Unit
Pg. 6
How to insert and print array elements?

int mark[5] = {19, 10, 8, 17, 9}


// insert different value to third element
mark[3] = 9;
// take input from the user and insert in third
element
scanf("%d", &mark[2]);
// take input from the user and insert in (i+1)th
element
scanf("%d", &mark[i]);
// print first element of an array
printf("%d", mark[0]);
// print ith element of an array
printf("%d", mark[i-1]);

Unit
Pg. 7
Example: C Arrays

// Program to find the average of n (n < 10) numbers using arrays


#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
}

Unit
Pg. 8
Important thing to remember when working with C arrays

• Suppose you declared an array of 10 elements. Let's say,


int testArray[10];

• You can use the array members from testArray[0] to


testArray[9]

• If you try to access array elements outside of its bound, let's


say testArray[12], the compiler may not show any error.
However, this may cause unexpected output (undefined
behavior).

Unit
Pg. 9
C Programming Multidimensional Arrays

• In C programming, you can create an array of arrays known


as multidimensional array. For example,

float x[3][4];
• Here, x is a two-dimensional (2d) array. The array can hold 12
elements. You can think the array as table with 3 row and each
row has 4 column.

Unit
Pg. 10
C Programming Multidimensional Arrays

• Similarly, you can declare a three-dimensional (3d) array. For


example,
float y[2][4][3];
• Here, The array y can hold 24 elements.

• You can think this example as: Each 2 elements have 4


elements, which makes 8 elements and each 8 elements
can have 3 elements. Hence, the total number of elements
is 24.

Unit
Pg. 11
How to initialize a multidimensional array?

• There is more than one way to initialize a multidimensional


array.

Initialization of a two dimensional array


// Different ways to initialize two dimensional array
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[2][3] = {1, 3, 0, -1, 5, 9};
• Above code are three different ways to initialize a two
dimensional arrays.

Unit
Pg. 12
How to initialize a multidimensional array?

Initialization of a three dimensional array.


• You can initialize a three dimensional array in a similar way
like a two dimensional array. Here's an example,

int test[2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
};

Unit
Pg. 13
Example1: Two Dimensional Array to store
and display values

Unit
Pg. 14
Example2: Sum of two matrices using Two
dimensional arrays

C program to find the sum of two matrices of order 2*2 using


multidimensional arrays.
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], c[2][2];
int i, j; // Taking input using nested for loop
printf("Enter elements of 1st matrix\n");
for(i=0; i<2; ++i) for(j=0; j<2; ++j)
{
printf("Enter a%d%d: ", i+1, j+1);
scanf("%f", &a[i][j]); } // Taking input using nested for loop
printf("Enter elements of 2nd matrix\n");
for(i=0; i<2; ++i) for(j=0; j<2; ++j) {
printf("Enter b%d%d: ", i+1, j+1);
scanf("%f", &b[i][j]);
} // adding corresponding elements of two arrays
for(i=0; i<2; ++i) for(j=0; j<2; ++j) { c[i][j] = a[i][j] +
b[i][j]; } // Displaying the sum
printf("\nSum Of Matrix:");
for(i=0; i<2; ++i) for(j=0; j<2; ++j)
{ printf("%.1f\t", c[i][j]);
if(j==1) printf("\n"); }
return 0;
}
Unit
Pg. 15
How to pass arrays to a function in C
Programming?

• In C programming, a single array element or an


entire array can be passed to a function.
• This can be done for both one-dimensional array or a
multi-dimensional array.

Unit
Pg. 16
Passing One-dimensional Array In Function

• Single element of an array can be passed in similar manner


as passing variable to a function.
C program to pass a single element of an array to function

#include <stdio.h>
void display(int age)
{
printf("%d", age);
}
int main()
{
int ageArray[] = { 2, 3, 4 };
display(ageArray[2]); //Passing array element
ageArray[2] only.
return 0; }

Unit
Pg. 17
Passing an entire one-dimensional array to a
function

• While passing arrays as arguments to the function, only


the name of the array is passed (,i.e, starting address of
memory area is passed as argument).

C program to pass an array containing age of person to a function. This function


should find average age and display the average age in main function.
#include <stdio.h>
float average(float age[]);
int main()
{
float avg, age[] = { 23.4, 55, 22.6, 3, 40.5, 18};
avg = average(age); // Only name of an array is passed as an argument
printf("Average age = %.2f", avg);
return 0;
} float average(float age[]) {
int i;
float avg, sum = 0.0;
for (i = 0; i < 6; ++i) { sum += age[i]; }
avg = (sum / 6);
return avg;
}

Unit
Pg. 18
Passing Multi-dimensional Arrays to Function

#Example: Pass two-


dimensional arrays to a To pass two-
function dimensional array to a
function as an
#include <stdio.h> argument, starting
void displayNumbers
(int num[2][2]); address of memory area
int main() reserved is passed as in
one dimensional array
{
int num[2][2], i, j; printf("Enter 4
numbers:\n"); for (i = 0; i < 2; ++I
for (j = 0; j < 2; ++j) scanf("%d",
&num[i][j]); // passing multi-
dimensional array to displayNumbers
function
displayNumbers(num);
return 0; }
void displayNumbers(int num[2][2]) {
// Instead of the above line, // void
displayNumbers(int num[][2]) is also
valid
int i, j; printf("Displaying:\n");
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j)
printf("%d\n", num[i][j]);
} Unit
Pg. 19
That’s all folks!

Thank you for listening!

Unit
Pg. 20

You might also like