0% found this document useful (0 votes)
17 views

Lecture-06 (Array Lecture Notes)

C programing on array

Uploaded by

touhid.cse.bou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture-06 (Array Lecture Notes)

C programing on array

Uploaded by

touhid.cse.bou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Computer Programming

C Array

Lecture Outline:

 Array
 One Dimensional Array
 Two Dimensional Array

An array is a variable that can store multiple values of the same type. An array is used to store
a collection of data, but it is often more useful to think of an array as a collection of variables
of the same type.

For example, if you want to store 100 integers, you can create an array for it.

int data[100];

Declaration of an array (Syntax):

dataType arrayName[arraySize];

For example: float mark[5]

Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can
hold 5 floating-point values.

Note that, the size and type of an array cannot be changed once it is declared.

Figure: C Array

Access Array Elements:

You can access elements of an array by indices. Suppose you declared an array mark as above.
The first element is mark[0], the second element is mark[1] and so on.

1| CSE 1113 K I Masud, Assistant Professor, Department of CSE, DUET


Remember:

o Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
o If the size of an array is n, to access the last element, the n-1 index is used. In this
example, mark[4]

Initialization of an array:

It is possible to initialize an array during declaration. For example,

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

You can also initialize an array like this:

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

Here, we haven't specified the size. However, the compiler knows its size is 5 as we are
initializing it with 5 elements.

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

Change the Value of an array elements:

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

// make the value of the third element to -1


mark[2] = -1;

// make the value of the fifth element to 0


mark[4] = 0;

2| CSE 1113 K I Masud, Assistant Professor, Department of CSE, DUET


Input and Output Array Elements:

Here's how you can take input from the user and store it in an array element.

// take input and store it in the 3rd element


scanf("%d", &mark[2]);

Here's how you can print an individual element of an array.

// print the first element of the array


printf("%d", mark[0]);

// print the third element of the array


printf("%d", mark[2]);

Example 1: Array Input/Output

Write a C Program to take 5 values from the keyboard and store them in an array then Print
the elements stored in the array.

#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; i++)
{
scanf("%d", &values[i]);
}

printf("Displaying integers: ");


for(int i = 0; i < 5; i++)
{
printf("%d\n", values[i]);
}
return 0;
}

3| CSE 1113 K I Masud, Assistant Professor, Department of CSE, DUET


Example 2: Calculate Average

Write a C Program to find the average of n numbers using arrays.

#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0,
float average;

printf("Enter number of elements: ");


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 = %f", average);

return 0;
}

4| CSE 1113 K I Masud, Assistant Professor, Department of CSE, DUET


Example 3: Sorting Array

Write a C Program to sort the array in ascending order.

#include<stdio.h>
int main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}

return 0;
}

5| CSE 1113 K I Masud, Assistant Professor, Department of CSE, DUET


Multidimensional Array:

In C programming, you can create an array of arrays. These arrays are known as
multidimensional arrays.

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 a table with 3 rows and each row has 4 columns.

Initializing a multidimensional array:

There are 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}};

6| CSE 1113 K I Masud, Assistant Professor, Department of CSE, DUET


Example 4: Two Dimensional Array Input/Output

Write a C Program to store Marks of n students of n subject and display it.

#include <stdio.h>
int main()
{
int STD, SUBJ;
printf("No of Student: ");
scanf("%d", &STD);

printf("No of Subject: ");


scanf("%d", &SUBJ);

int MARKS[STD][SUBJ];
for (int i = 0; i < STD; i++)
{
for (int j = 0; j < SUBJ; j++)
{
printf("MARKS [%d] [%d]: " ,i, j);
scanf("%d", &MARKS[i][j]);
}
}

printf("\nDisplaying values: \n\n");

for (int i = 0; i < STD; i++)


{
for (int j = 0; j < SUBJ; j++)
{
printf("MARKS [%d] [%d]: %d\n", i, j, MARKS[i][j]);
}
}

return 0;

7| CSE 1113 K I Masud, Assistant Professor, Department of CSE, DUET


Example 5: Write a C Program to Add Two Matrices Using Multi-Dimensional Arrays.

#include <stdio.h>
int main()
{
int arr1[2][3] = {{2,2,2}, {3,3,3}};
int arr2[2][3] = {{1,1,1}, {4,4,4}};
int arr3 [2][3];

for(int i=0; i<2; i++)


{
for(int j=0; j<3; j++)
{
arr3[i][j]= arr1[i][j] + arr2[i][j];
printf("%d ", arr3[i][j]);
}
}
return 0;
}

8| CSE 1113 K I Masud, Assistant Professor, Department of CSE, DUET

You might also like