0% found this document useful (0 votes)
14 views24 pages

C Arrays: Basics and Examples

Uploaded by

lhumayra204
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)
14 views24 pages

C Arrays: Basics and Examples

Uploaded by

lhumayra204
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

Programming in C

Array
Ashis Talukder, PhD
Associate Professor
Department of MIS, DU
Array
• An array is defined as the collection of similar type of data items
stored at contiguous memory locations.
• Arrays are the derived data type in C programming language
which can store the primitive type of data such as int, char,
double, float, etc.
• It also has the capability to store the collection of derived data
types, such as pointers, structure, etc.
• The array is the simplest data structure where each data element
can be randomly accessed by using its index number.
Dr. Ashis Talukder, Associate Professor, MIS, DU
Array
• C array is beneficial if you have to store similar elements.
• For example,
• if we want to store the marks of a student in 6 subjects, then
we don't need to define different variables for the marks in the
different subject.
• Instead of that, we can define an array which can store the
marks in each subject at the contiguous memory locations.
• By using the array, we can access the elements easily. Only a few
lines of code are required to access the elements of the array.
Dr. Ashis Talukder, Associate Professor, MIS, DU
Properties of Array
0 1 2 3 4
• Each element of an array is of
same data type and carries the
a[5] same size, i.e., int = 2 bytes.
1135 1137 1139 1141 1143 • Elements of the array are stored
at contiguous memory locations
• a[3] = 85 where the first element is stored
𝑙𝑙𝑙𝑙𝑙𝑙 𝑎𝑎 𝑖𝑖 = 𝑏𝑏𝑏𝑏𝑏𝑏𝑏𝑏 𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎 𝑎𝑎 + 𝑖𝑖 − 𝑙𝑙𝑙𝑙 ∗ 𝑤𝑤𝑤𝑤𝑤𝑤𝑤𝑤 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠 at the smallest memory location.
• Elements of the array can be
• Here, randomly accessed since we can
• base address(a) = 1135, word size = 2 bytes (int data type) calculate the address of each
element of the array with the
given base address and the size of
𝑙𝑙𝑙𝑙𝑙𝑙 𝑎𝑎 3 = 1135 + 3 − 0 ∗ 2 the data element.
= 1141

Dr. Ashis Talukder, Associate Professor, MIS, DU


Adv & Dis-adv of Array
• Advantage of C Array
• Code Optimization: Less code to the access the data.
• Ease of traversing: By using the for loop, we can retrieve the elements of
an array easily.
• Ease of sorting: To sort the elements of the array, we need a few lines of
code only.
• Random Access: We can access any element randomly using the array.
• Disadvantage of C Array
• Fixed Size: Whatever size, we define at the time of declaration of the
array, we can't exceed the limit. So, it doesn't grow the size dynamically
like Linked List which we will learn later.
Dr. Ashis Talukder, Associate Professor, MIS, DU
Declaration Syntax
• The syntax of Array in C language is given below:

float marks[10],num[5] = {1.3, 3.5, 7.9, 6.8};


int a[10],data[1000];
char string[10] = "abcdefghi";

null character = ‘\0’

Dr. Ashis Talukder, Associate Professor, MIS, DU


Programs of Array
1. Write a program in C to input and out an array.
2. Write a program in C to find the average of N items stored in an array.
3. Write a program in C to find the SD of N items stored in an array.
4. Write a program in C to find the Binary of a Decimal number.

5. Copy content of one array to another array.


6. Reverse the content of an array.

Dr. Ashis Talukder, Associate Professor, MIS, DU


main()
{ int a[10],n,i;

printf(“\nInput Size = “);


scanf(“%d”,&n); Input Size = 6
printf(“\nInput Array: “); Input Array: 6 7 8 9 10
for(i=0;i<n;i++)
scanf(“%d”,&a[i]); Output Array: 6 7 8 9 10

printf(“\n\nOutput Array: “);


for(i=0;i<n;i++)
printf(“%3d”,a[i]);
}

Dr. Ashis Talukder, Associate Professor, MIS, DU


Programs of Array
1. Write a program in C to input and out an array.
2. Write a program in C to find the average of N items stored in an array.
3. Write a program in C to find the SD of N items stored in an array.
4. Write a program in C to find the Binary of a Decimal number.

5. Copy content of one array to another array.


6. Reverse the content of an array.

Dr. Ashis Talukder, Associate Professor, MIS, DU


main()
{ int a[10],n,i;
float sum, avg;

sum = 0.0;
Input Size = 5
for(i=0;i<n;i++)
Input Array: 6 7 8 9 10
sum = sum + a[i];
avg = sum / n;
Output Array: 6 7 8 9 10
Sum = 40.00
printf(“\n\nOutput Array: “);
for(i=0;i<n;i++) Average = 8.00
printf(“%3d”,a[i]);
printf(“\nSum = %.2f”,sum);
printf(“\nAverage = %.2f”,avg);
}

Dr. Ashis Talukder, Associate Professor, MIS, DU


Programs of Array
1. Write a program in C to input and out an array.
2. Write a program in C to find the average of N items stored in an array.
3. Write a program in C to find the SD of N items stored in an array.
4. Write a program in C to find the Binary of a Decimal number.

5. Copy content of one array to another array.


6. Reverse the content of an array.

Dr. Ashis Talukder, Associate Professor, MIS, DU


main()
{ int a[10],n,i;
float sum, avg,sd_sum,var,sd;

sd_sum = 0.0;
for(i=0;i<n;i++) Input Size = 5
sd_sum = sd_sum + (a[i] – avg)*(a[i] – avg); Input Array: 6 7 8 9 10
var = sd_sum / n;
sd = sqrt(var);
Output Array: 6 7 8 9 10
printf(“\n\nOutput Array: “); Sum = 40.00
for(i=0;i<n;i++) Average = 8.00
printf(“%3d”,a[i]);
Variance = 2.00
printf(“\nSum = %.2f”,sum);
Standard Deviation = 1.41
printf(“\nAverage = %.2f”,avg);
printf(“\nVriance = %.2f”,var);
printf(“\nStandard Deviation = %.2f”,sd);
}
Dr. Ashis Talukder, Associate Professor, MIS, DU
Programs of Array
1. Write a program in C to input and out an array.
2. Write a program in C to find the average of N items stored in an array.
3. Write a program in C to find the SD of N items stored in an array.
4. Write a program in C to find the Binary of a Decimal number.

5. Copy content of one array to another array.


6. Reverse the content of an array.

Dr. Ashis Talukder, Associate Professor, MIS, DU


main()
{ int i,n,m,binary[100],bn;

printf("\nInput Decimal Number = ");


scanf("%d",&n);
Input Decimal Number = 10
m = n; Binary Number = 1010
bn = 0;
while (m > 0)
Input Decimal Number = 1024
{ binary[bn++] = m % 2;
m = m / 2; Binary Number = 10000000000
}

printf("\nBinary Number = ");


for(i=bn-1;i>=0;i--)
printf("%d",binary[i]);
}

Dr. Ashis Talukder, Associate Professor, MIS, DU


2D Array
• The two-dimensional array can be defined as an array of arrays.
• The 2D array is organized as matrices which can be represented as the
collection of rows and columns.
• However, 2D arrays are created to implement a relational database
lookalike data structure.
• It provides ease of holding the bulk of data at once which can be passed to
any number of functions wherever required.

Dr. Ashis Talukder, Associate Professor, MIS, DU


2D Array
• Initialization of 2D Array in C
• In the 1D array, we don't need to specify the size of the array if the
declaration and initialization are being done simultaneously.
• However, this will not work with 2D arrays.
• We will have to define at least the second dimension of the array.
• The two-dimensional array can be declared and defined in the following
way.

Dr. Ashis Talukder, Associate Professor, MIS, DU


2D Array
• Input and output a matrix amn
• Add two matrices: cmn = amn + bmn
• Multiply two matrices: cmn = amn X bmn

Dr. Ashis Talukder, Associate Professor, MIS, DU


2D Array: Matrix Addison
1 2 3 1 2 3
𝑎𝑎33 = 4 5 6 𝑏𝑏33 = 4 5 6
7 8 9 7 8 9

1+1 2+2 3+3 2 4 6


𝑐𝑐33 = 𝑎𝑎33 + 𝑏𝑏33 = 4 + 4 5 + 5 6 + 6 = 8 10 12
7+7 8+8 9+9 14 16 18

Dr. Ashis Talukder, Associate Professor, MIS, DU


2D Array: Matrix Multiplication
1 2
1 2 3
𝑎𝑎32 = 3 4 𝑏𝑏23 =
4 5 6
5 6
a[0][0] =1 a[1][1] = 4 b[1][2] = 6

1.1 + 2.4 1.2 + 2.5 1.3 + 2.6 9 12 15


𝑐𝑐33 = 𝑎𝑎32 × 𝑏𝑏23 = 3.1 + 4.4 3.2 + 4.5 3.3 + 4.6 = 19 26 33
5.1 + 6.4 5.2 + 6.5 5.3 + 6.6 29 40 51

Dr. Ashis Talukder, Associate Professor, MIS, DU


2D Array: Matrix Multiplication
1 2
1 2 3
𝑏𝑏23 = 𝑎𝑎32 = 3 4
4 5 6
5 6

1.1 + 2.3 + 3.5 1.2 + 2.4 + 3.6


𝑐𝑐22 = 𝑏𝑏23 × 𝑎𝑎32 =
4.1 + 5.3 + 6.5 4.2 + 5.4 + 6.6

22 28
=
49 64
𝑎𝑎32 × 𝑏𝑏23 ≠ 𝑏𝑏23 × 𝑎𝑎32

Dr. Ashis Talukder, Associate Professor, MIS, DU


Programs for Array: Singly Array
1. Write a program in C to input and out an array.
2. Find sum of all array elements.
3. Find the average of N items stored in an array.
4. Find the variance and standard deviation of N items stored in an
array.
5. Find max/min in an array.
6. Copy all elements from an array to another array.
7. Reverse an array.
8. Find binary from a decimal number
9. Merge two array to third array.
10. Count frequency of each element and make a frequency
distribution
Dr. Ashis Talukder, Associate Professor, MIS, DU
Programs for Array: Singly Array
11. Decimal into Octal conversion

Dr. Ashis Talukder, Associate Professor, MIS, DU


Programs for Array
2D Array
1. Add two matrices
2. Multiply two matrices
3. Transpose a matrices
4. Sum/Product of diagonal elements
5. Sum of lower triangular elements
6. 30. Sum of upper triangular elements
7. Find transpose of matrix

Dr. Ashis Talukder, Associate Professor, MIS, DU


Thank You

Dr. Ashis Talukder, Associate Professor, MIS, DU

You might also like