Arrays PDF
Arrays PDF
Arrays PDF
Adding Comments
• Why is it important to write comments?
Some programmers are not very smart and write ugly
codes!!
1
ONE
MONTH
LATER
…
ONE
MONTH
LATER
…
2
Two types of Comments
• Single line comment
• Multi-line comments
/*
Addition Of Two Numbers
By Bill Gates
© Microsoft Corporation
*/
int a=(b*c + b*d)/b; int a = c + d;
However…..
3
Arrays
One variable many data
Problem:
Read 10 numbers from the keyboard and store them
4
100 Problem:
Read 10 numbers from the keyboard and store them
// solution #1
int a, b, c, d, e, f, g, h, i, j;
Arrays
• An array is an ordered list of data values of similar kind
0 1 2 3 4 5 6 7 8 9
a 79 87 94 82 67 98 87 81 74 91
5
An array with 8 elements of type double
Arrays
• The values held in an array are called array
elements
6
Declaring Arrays
data_type array_name[size];
For example:
int a[10];
a is an array of 10 integers.
float prices[3];
char c[6];
c is an array of 6 characters.
7
How to assign values?
First way
8
How to assign values?
Second way:
int a[6];
a[0]=3;
a[1]=6;
scanf(“%d”, &a[0]);
scanf(“%d”, &a[5]); }
9
How to print values?
Use printf to show values in the array:
int a[6]={1,5,3,9,8,3};
int a[6]={1,5,3,9,8,3};
printf(“%d”,a);
printf(“%d”, a[0]);
printf(“%d”, a[1]);
int a[6];
……..
for(i= 0; i<6; i++){
…..
printf(“%d”, a[i]);
printf(“%d”, a[5]);
}
10
Arrays: Some examples
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
Example 4:
Take N students marks
as input and store them
in an array. Find
highest/lowest mark.
11
Finding maximum
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 40 30 70 60 70 88 99 10 87 91 65
Large
r?
Initially assume first element is the maximum
max = a[0]
find_maximum.c
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 14, 21, 36, 14, 12, 9, 8, 22, 7, 81, 77, 10};
int i;
12
Problem:
Find the minimum number in an array
of unsorted integers
find_minimum.c
find_minimum.c
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 14, 21, 36, 14, 12, 9, 8, 22, 7, 81, 77, 10};
int i;
13
Problem:
Find the minimum number (and its index)
in an array of unsorted integers
find_minimum_and_index.c
find_minimum_and_index.c
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 14, 21, 36, 14, 12, 9, 8, 22, 7, 81, 77, 10};
int i, min;
14
Example 5: Take N numbers as input and store them
in an array. Shift all elements of the array one place
towards left. The first element will go to the last place.
4 6 6 4 7 8 10 7 8 10 4 9
t = a[0]
Desired output:
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
6 6 4 7 8 10 7 8 10 4 9 4
15
Code snippet
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 4, 6, 6, 4, 7, 8, 10, 7, 8, 10, 4, 9};
int i, t;
t = a[0];
for(i=0; i < N-1; i++)
a[i] = a[i+1];
a[N-1] = t;
printf(" Array elements after left rotation……..\n");
Searching
16
Search
Linear Search
• The most basic
17
Example: Example:
Successful Successful
Linear Linear
Search Search
Example:
Failed
Linear
Search
18
Problem:
Find the index of a number in an
unsorted array of integers
linear_search.c
Linear_Search.c
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 4, 21, 36, 14, 62, 91, 8, 22, 7, 81, 77, 10};
int i;
int target, i, idx;
printf("What do you want to search?");
scanf("%d", &target);
idx = -1;
for(i=0; i< N; i++)
{
if(a[i] == target)
{
idx=i;
break;
}
}
if(idx == -1)
printf("Target not found.\n\n");
else
printf("Target found at index: %d \n\n", idx);
}
19
Linear Search in a Sorted Array
Target 19
21 > 19
Problem:
Find the index of a number in a
sorted array of integers
LinearSearch_InSortedArray.c
20
LinearSearch_InSortedArray.c
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N]= { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91};
int target, i, idx;
printf("What do you want to search?");
scanf("%d", &target);
idx = -1;
for(i=0; i< N; i++)
{
if(a[i] == target)
{
idx=i;
break;
}
else if(a[i]>target)
break; // we can stop here
}
if(idx == -1)
printf("Target not found.\n\n");
else
printf("Target found at index: %d. \n\n", idx);
}
Analysis
21
Collecting statistics in an array
Marks Grade
===== =====
90 – 100 A
80 – 89 B
70 – 79 C
60 – 69 D
<60 F
}
printf(“Number of students getting A %d \n“, aCount);
printf(“Number of students getting B %d \n“, bCount);
printf(“Number of students getting C %d \n“, cCount);
printf(“Number of students getting D %d \n“, dCount);
printf(“Number of students getting F %d \n“, fCount);
}
22
Some Harder Examples
4 4 6 7 7 7 8 9 10 10 10 10
t = a[0]
t == a[1] t != a[1]
23
Code snippet
#include <stdio.h>
#include <stdlib.h>
#define N 12
int main()
{
int a[N] = { 4, 4, 6, 6, 7, 7, 7, 8, 9, 10, 10, 10, 10};
int i, counter;
counter = N;
for(i=0; i< N-1; i++){
if(a[i] == a[i+1])
counter--;
}
printf("The number of distinct elements in the array is
%d.\n\n", counter);
}
24
Largest and Second largest
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 21 36 14 62 91 8 22 7 81 77 10
secL = 4 largest = 21
Third element
t = a[2]
Code Snippet
if(a[0] > a[1]){
largest = a[0];
secL = a[1];
}
else{
largest = a[1];
secL = a[0];
}
25
Questions?
26