You are on page 1of 40

CSE-109: Computer Programming

Shahriar Rahman Khan


Lecturer, Department of CSE, MIST
Email: shahriarkhan@cse.mist.ac.bd

1
Lec 10-11: Array in C

PREPARED BY
SHAHRIAR RAHMAN KHAN
Lecturer, Department of CSE, MIST
Array Initialization
• Static array initialization - Initializes all elements of array during its declaration.
• Dynamic array initialization - The declared array is initialized some time later
during execution of program

Static array initialization


int Ar[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
0 1 2 3 4 5 6 7 8 9
Ar 9 8 7 6 5 4 3 2 1 0
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]
Ar[3] = -1;
0 1 2 3 4 5 6 7 8 9
Ar 9 8 7 -1 5 4 3 2 1 0
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]
11/20/2023 LEC SHAHRIAR RAHMAN KHAN 17
Array Initialization

 int A[5] = {2, 4, 8, 16, 32};


Static or automatic

 int B[20] = {2, 4, 8, 16, 32};


Unspecified elements are guaranteed to be zero

 int C[4] = {2, 4, 8, 16, 32};


Error – compiler detects too many initial values, extra values are discarded

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 18


Array Initialization

int var[7];

var[0] = 14;
var[1] = 22;
var[2] = 31;
var[3] = 18;
var[4] = 19;
var[5] = 7;
var[6] = 23;
0 1 2 3 4 5 6
14 22 31 18 19 7 23

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 19


Array Initialization
Dynamic array initialization:

#include<stdio.h>
int main()
{
int ar[10];
int i;
for (i=0; i<10; i++) // for Initializing Array
{
ar[i]=i;
}
for (i=0; i<10; i++) // for displaying Array
{
printf("%d ", ar[i]);
}
}
11/20/2023 LEC SHAHRIAR RAHMAN KHAN 20
Array Initialization
 int var[7] = {14, 22, 31, 18, 19, 7, 23}; #include<stdio.h>
 int var[ ] = {14, 22, 31, 18, 19, 7, 23}; int main()
{
 Size of array is optional when declaring
int i;
and initializing array at once. The C int ar[7] = {14, 22, 31, 18, 19, 7,
compiler automatically determines 23};
array size using number of array //int var[] = {14, 22, 31, 18, 19,
elements 7, 23};
for (i=0; i<7; i++)
0 1 2 3 4 5 6 {
14 22 31 18 19 7 23 printf("%d ", ar[i]);
}
 int var[7] = {14, 22, 31}; }

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 21


Array Initialization
int var[7] = {9, 14, 0};
0 1 2 3 4 5 6
9 14 0 0 0 0 0

int var[7] = {9};


0 1 2 3 4 5
9 0 6 0 0 0 0 0

int var[7] = {0};


0 1 2 3 4 5
0 0 6 0 0 0 0 0

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 22


Array-Bounds Checking

 C, unlike many languages, does NOT check array bounds subscripts during:
• Compilation
• Runtime (bounds are never checked)
 If you access off the ends of any array, it will calculate the address it expects the
data to be at, and then attempts to use it anyways
• may get “something…”
• may get a memory exception (segmentation fault, bus error, core dump error)
 It is the programmer’s responsibility to ensure that their programs are correctly
written and debugged!

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 23


Array-Bounds Checking

 If either the index < 0 or the index > ARRAY_SIZE-1


• then we say that the index is out of bounds
 There is no guard against indices that are out of bounds
• C does not check if the index value is within range

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 24


Array-Bounds Checking
#include <stdio.h>
int main()
{
int values[7]= {14, 22, 31, 18, 19, 7, 23};
int i;
0 1 2 3 4 5 6
for (i = 0; i < 7; i++) 14 22 31 18 19 7 23
{
printf(“%d”, values[i])
}
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 25


Array-Bounds Checking
#include <stdio.h>
int main()
{
int values[7]= {14, 22, 31, 18, 19, 7, 23};
int i;
for (i = 0; i < 10; i++)
{
printf(“%d”, values[i])
}
}

What’s gonna happen?

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 26


Restrictions on Array Processing
int myList[5]= {14, 22, 31, 18, 19};
int yourList[5];
Now, we want to make a copy of the array myList. How can it be done?
yourList = myList;
• Assignment does not work with arrays.
• In order to copy one array into another array we must copy component-wise
int i;
for (i = 0; i < 5; i++)
{
yourList[i] = myList[i];
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 27


Base Address of an Array
• The base address of an array is the address, or memory location of the first
array component
• If list is a one-dimensional array
o Base address of list is the address of the component list[0]
• When we pass an array as a parameter
o Base address of the actual array is passed to the formal parameter

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 28


Base Address of an Array
• The base address of an array is the address, or memory location of the first
array component
• If list is a one-dimensional array
o Base address of list is the address of the component list[0]
• When we pass an array as a parameter
o Base address of the actual array is passed to the formal parameter

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 29


Array Application

 Given a list of test scores, determine the max, min, average, sum
 Array count (Ex: Given the height measurements of students in a class, output
the count of those students who are taller than average.)
 Array reverse (temp array, in-place)
 Frequency of digits in a number
 Read in a list of student names and rearrange them in alphabetical order
(Bubble sorting).
 Searching a particular item from a given list (Linear Search, Binary Search).

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 30


Given a List of Numbers, Determine
the Minimum Score

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 31


Array Application

 Finding the smallest element in the array named num.


 num[]={12.0,41.5,-31.2,-45.0,33.0,-21.2,-24.1,0.7,3.2,0.5};
0 1 2 3 4 5 6 7 8 9
12.0 41.5 -31.2 -45.0 33.0 -21.2 -24.1 0.7 3.2 0.5

• First, it assumes that the smallest value is in num[0] and assigns it to the
variable Small.
• Then it compares Small with the rest of the values in num array, one at a time.
• When an element is smaller than the current value contained in Small, it is
assigned to Small. The process finally places the smallest array element in
Small.

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 32


#include <stdio.h>

int main()
{
int index;
float Small, num[]= {12.0,41.5,-31.2,-45.0,33.0,-21.2, 24.1,0.7,3.2,0.5};
Small = num[0];
for(index=0; index<10; index++)
{
printf("%.2f ",num[index]);
}

for(index=1; index<10; index++)


{
if(Small > num[index])
{
Small = num[index];
}
}
printf("\nThe smallest value in the given array is %.2f\n", Small);
return 0;
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 33


Given a List of Numbers, Determine
the Maximum Score

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 34


#include <stdio.h>

int main()
{
int index;
int Large, num[]= {12, 34, 21, 45, 9};
Large = num[0];
for(index=0; index<5; index++)
{
printf("%d",num[index]);
}

for(index=1; index<10; index++)


{
if(num[index]) > Large)
{
Large = num[index];
}
}
printf("\nThe largest value in the given array is %d\n", Large);
return 0;
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 35


Given a List of Numbers, Determine
the Sum of the Numbers

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 36


#include <stdio.h>
int main()
{
int index, num[5], sum = 0;

for(index=0; index<5; index++)


{
scanf("%d",&num[index]);
}

for(index=0; index<5; index++)


{
sum = sum + num[index];
}
printf("\nThe sum value of the given array is %d\n", sum);
return 0;
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 37


Given a List of Numbers, Determine
the Average Number

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 38


#include <stdio.h>
int main()
{
int index, num[5], sum = 0;
float avg;

for(index=0; index<5; index++)


{
scanf("%d",&num[index]);
}

for(index=0; index<5; index++)


{
sum = sum + num[index];
}
avg = sum/5;
printf("\nThe average value in the given array is %.2f\n", avg);
return 0;
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 39


Given the height measurements of students in a
class, output the count of those students who are
taller than average

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 40


#include <stdio.h>
int main()
{
int index, sum = 0, count=0;
float avg;
int num[5] = {12, 9, 14, 7, 4};
for(index=0; index<5; index++)
{
sum = sum + num[index];
}
avg = (float)sum/5;
printf("\nThe average value in the given array is %.2f\n", avg);
for(index=0; index<5; index++)
{
if(num[index]>avg)
{
count++;
}
}
printf("\nThe count value in the given array is %d\n", count);
return 0;
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 41


Array Reverse (inplace)

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 42


#include <stdio.h>
int main()
{
int i,j, temp, size;
size = 5;
int a[5] = {12, 9, 14, 7, 4};
i = size - 1;
j = 0;
while(i > j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i--;
j++;
}
//Output contents of now-reversed array.
for(i = 0; i < size; i++)
printf("%d ", a[i]);
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 43


Array Reverse (Using Auxiliary Array)

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 44


#include <stdio.h>
int main()
{
int i,j=0, temp, size;
size = 5;
int a[5] = {12, 9, 14, 7, 4};
int b[5];
for(i = size-1;i>=0;i--)
{
b[j]=a[i];
j++;
}
//Output contents of now-reversed array.
for(i = 0; i < size; i++)
printf("%d ", b[i]);
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 45


Frequency of Digits in a Number

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 46


#include <stdio.h>
int main()
{
int num, n;
int i, lastDigit;
int freq[10];
printf("Enter any number: ");
scanf("%d", &num);
for(i=0; i<10; i++)
{
freq[i] = 0;
}
n = num;
while(n != 0)
{
lastDigit = n % 10;
n /= 10;
freq[lastDigit]++;
}
printf("Frequency of each digit in %d is: \n", num);
for(i=0; i<10; i++)
{
printf("Frequency of %d = %d\n", i, freq[i]);
}
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 47


Practice Bubble Sort

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 48


11/20/2023 LEC SHAHRIAR RAHMAN KHAN 49
Linear Searching

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 51


#include <stdio.h>
void main()
{
int Score[6]= {3, 8, 2, 9, 4, 12};
int Index, Lookup, found=0;;
printf("What score do you want to look up? ");
scanf("%d", &Lookup);
for(Index = 0; Index <6; Index++)
{
if(Score[Index] == Lookup)
{
printf("The score of %d was at index %d in the list.\n", Lookup, Index);
found=1;
break;
}
}
if (found==0)
printf("The score of %d was not found in the list.\n", Lookup);
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 52


Practice Binary Searching

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 53


11/20/2023 LEC SHAHRIAR RAHMAN KHAN 54
Motivation

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 55

You might also like