Arrays: Programming & Data Structures
Arrays: Programming & Data Structures
Arrays: Programming & Data Structures
Pabitra Mitra
Dept. of Computer Sc. & Engg.,
Indian Institute of Technology Kharagpur
1
Dept. of CSE, IIT KGP
Array
2
Dept. of CSE, IIT KGP
Example: Finding Minima of Numbers
3 numbers 4 numbers
if ((a <= b) && (a <= c)) if ((a <= b) && (a <= c) && (a <= d))
min = a; min = a;
else else
if (b <= c) if ((b <= c) && (b <= d))
min = b; min = b;
else else
min = c; if (c <= d)
min = c;
else
min = d;
3
Dept. of CSE, IIT KGP
The Problem
• Solution:
– Use arrays.
4
Dept. of CSE, IIT KGP
Using Arrays
X is a 10-element one
dimensional array
5
Dept. of CSE, IIT KGP
Declaring Arrays
int marks[5];
– marks is an array containing a maximum of 5 integers.
6
Dept. of CSE, IIT KGP
• Examples:
int x[10];
char line[80];
float points[150];
char name[35];
• If we are not sure of the exact size of the array, we can define an
array of a large size.
int marks[50];
though in a particular run we may only be using, say, 10 elements.
7
Dept. of CSE, IIT KGP
How an array is stored in memory?
Array a
8
Dept. of CSE, IIT KGP
Accessing Array Elements
9
Dept. of CSE, IIT KGP
Contd.
10
Dept. of CSE, IIT KGP
A Warning
• Example:
int marks[5];
:
:
marks[8] = 75;
– The above assignment would not necessarily cause an error.
– Rather, it may result in unpredictable program results.
11
Dept. of CSE, IIT KGP
Initialization of Arrays
• General form:
type array_name[size] = { list of values };
• Examples:
int marks[5] = {72, 83, 65, 80, 76};
char name[4] = {‘A’, ‘m’, ‘i’, ‘t’};
• Some special cases:
– If the number of values in the list is less than the
number of elements, the remaining elements are
automatically set to zero.
float total[5] = {24.2, -12.5, 35.1};
Î total[0]=24.2, total[1]=-12.5, total[2]=35.1, total[3]=0,
total[4]=0
12
Dept. of CSE, IIT KGP
Contd.
13
Dept. of CSE, IIT KGP
Character Arrays and Strings
char C[8] = { 'a', 'b', 'h', 'i', 'j', 'i', 't', '\0' };
• C[0] gets the value 'a', C[1] the value 'b', and so on. The last (7th) location
receives the null character ‘\0’.
• Note also that for individual characters, C uses single quotes, whereas for
strings, it uses double quotes.
14
Dept. of CSE, IIT KGP
Example 1: Find the minimum of a set of 10 numbers
#include <stdio.h>
main()
{
int a[10], i, min;
min = 99999;
for (i=0; i<10; i++)
{
if (a[i] < min)
min = a[i];
}
printf (“\n Minimum is %d”, min);
}
15
Dept. of CSE, IIT KGP
Alternate Version 1 #include <stdio.h>
#define size 10
main()
{
int a[size], i, min;
17
Dept. of CSE, IIT KGP
Example 2: #include <stdio.h>
Computing gpa #define nsub 6
main()
{
int grade_pt[nsub], cred[nsub], i,
gp_sum=0, cred_sum=0, gpa;
• You cannot
– use = to assign one array variable to another
a = b; /* a and b are arrays */
– use == to directly compare array variables
if (a = = b) ………..
– directly scanf or printf arrays
printf (“……”, a);
19
Dept. of CSE, IIT KGP
How to copy the elements of one array to another?
20
Dept. of CSE, IIT KGP
How to read the elements of an array?
21
Dept. of CSE, IIT KGP
How to print the elements of an array?
22
Dept. of CSE, IIT KGP
Two Dimensional Arrays
Student 1 75 82 90 65 76
Student 2 68 75 80 70 72
Student 3 88 74 85 76 80
Student 4 50 65 68 40 70
23
Dept. of CSE, IIT KGP
Contd.
24
Dept. of CSE, IIT KGP
Declaring 2-D Arrays
• General form:
type array_name [row_size][column_size];
• Examples:
int marks[4][5];
float sales[12][25];
double matrix[100][100];
25
Dept. of CSE, IIT KGP
Accessing Elements of a 2-D Array
26
Dept. of CSE, IIT KGP
How is a 2-D array is stored in memory?
a[0]0] a[0][1] a[0]2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
28
Dept. of CSE, IIT KGP
How to print the elements of a 2-D array?
29
Dept. of CSE, IIT KGP
Contd.
30
Dept. of CSE, IIT KGP
Example: Matrix Addition
32
Dept. of CSE, IIT KGP
Passing Arrays to Function
33
Dept. of CSE, IIT KGP
Passing Entire Array to a Function
34
Dept. of CSE, IIT KGP
Whole array as Parameters
#define ASIZE 5
float average (int a[]) {
int i, total=0;
for (i=0; i<ASIZE; i++)
total = total + a[i];
return ((float) total / (float) ASIZE);
}
main ( ) {
int x[ASIZE] ; float x_avg;
x = {10, 20, 30, 40, 50}
x_avg = average (x) ;
}
35
Dept. of CSE, IIT KGP
Contd.
main()
{
int n;
We don’t need to write float list[100], avg;
the array size. It works :
with arrays of any size. avg = average (n, list);
:
}
36
Dept. of CSE, IIT KGP
Arrays as Output Parameters
void VectorSum (int a[], int b[], int vsum[], int length) {
int i;
for (i=0; i<length; i=i+1)
vsum[i] = a[i] + b[i] ;
}
int main (void) {
int x[3] = {1,2,3}, y[3] = {4,5,6}, z[3];
VectorSum (x, y, z, 3) ;
PrintVector (z, 3) ;
}
void PrintVector (int a[], int length) {
int i;
for (i=0; i<length; i++) printf (“%d “, a[i]);
}
37
Dept. of CSE, IIT KGP
The Actual Mechanism
38
Dept. of CSE, IIT KGP
Contd.
39
Dept. of CSE, IIT KGP
Passing 2-D Arrays
40
Dept. of CSE, IIT KGP
Example Usage
41
Dept. of CSE, IIT KGP