Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Arrays: A Block of Many Variables of The Same Type Array Can Be Declared For Any Type Examples

The document discusses arrays in C programming. It explains that arrays allow storing multiple variables of the same type in contiguous memory locations. The document provides examples of declaring and initializing arrays, accessing array elements, and using arrays in a sorting algorithm. It also discusses using #define to avoid "magic numbers" and make code more readable and maintainable.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Arrays: A Block of Many Variables of The Same Type Array Can Be Declared For Any Type Examples

The document discusses arrays in C programming. It explains that arrays allow storing multiple variables of the same type in contiguous memory locations. The document provides examples of declaring and initializing arrays, accessing array elements, and using arrays in a sorting algorithm. It also discusses using #define to avoid "magic numbers" and make code more readable and maintainable.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Arrays

 A block of many variables of the same type


 Array can be declared for any type
 E.g. int A[10] is an array of 10 integers.
 Examples:
 list of students’ marks
 series of numbers entered by user
 vectors
 matrices
Arrays in Memory
 Sequence of variables of specified type
 The array variable itself holds the address in
memory of beginning of sequence
 Example:
double S[10]; … 0 1 2 3 4 5 6 7 8 9 …

 The k-th element of array A is specified by


A[k-1] (0 based)
Example - reverse
#include <stdio.h>

int main(void)
{
int i, A[10];

printf("please enter 10 numbers:\n");


for(i=0; i<10; i++)
scanf("%d",&A[i]);

printf("numbers in reversed order:\n");


for(i=9; i>=0; i--)
printf("%d\n",A[i]);
}
Define
 Magic Numbers (like 10 in the last
example) in the program convey little
information to the reader
 Hard to change in a systematic way
 #define defines a symbolic name
 During preprocessing phase, symbolic
names are replaced by the replacement
text
Reverse with #define
/* get 10 integers from the user and printing them in reversed order*/
#include <stdio.h>

#define NUM 10

int main(void)
{
int i;
int A[NUM];

printf(“Please enter %d numbers:\n",NUM);


for(i=0; i<NUM; i++)
scanf("%d",&A[i]);

printf("numbers in reversed order:\n");


for(i=NUM-1; i>=0; i--)
printf("%d\n",A[i]);
}
Initialization
 Like in the case of regular variables, we can initialize the array
during declaration.
 the number of initializers cannot be more than the number of
elements in the array
 but it can be less

 in which case, the remaining elements are initialized to 0

 if you like, the array size can be inferred from the number of
initializers
 by leaving the square brackets empty

 so these are identical declarations :

int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16};


int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};
Example

Sort.c
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


----

i j tmp
#include <stdio.h>
---- ---- ----

#define SIZE 3

int main(void)
{
int A[SIZE] = {4,8,2};
int min_index;
int i,j,tmp;
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


----

i j tmp
for(i=0; i<SIZE-1; i++) 0 ---- ----
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


0

i j tmp
for(i=0; i<SIZE-1; i++) 0 ---- ----
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


0

i j tmp
for(i=0; i<SIZE-1; i++) 0 1 ----
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


0

i j tmp
for(i=0; i<SIZE-1; i++) 0 1 ----
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


0

i j tmp
for(i=0; i<SIZE-1; i++) 0 2 ----
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 0 2 ----
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 0 3 ----
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
4 8 2

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 0 3 4
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 8 2

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 0 3 4
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 8 4

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 0 3 4
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 8 4

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 1 3 4
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 8 4

Sort – step by step min_index


1

i j tmp
for(i=0; i<SIZE-1; i++) 1 3 4
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 8 4

Sort – step by step min_index


1

i j tmp
for(i=0; i<SIZE-1; i++) 1 2 4
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 8 4

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 1 2 4
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 8 4

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 1 3 4
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 8 4

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 1 3 8
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 4 4

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 1 3 8
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 4 8

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 1 3 8
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
A[0] A[1] A[2]
2 4 8

Sort – step by step min_index


2

i j tmp
for(i=0; i<SIZE-1; i++) 2 3 8
{
min_index=i;
for(j=i+1; j<SIZE; j++)
min_index=(A[min_index]>A[j] ? j : min_index);

tmp=A[i];
A[i]=A[min_index];
A[min_index]=tmp;
}
Exercise
Write a program that gets an input line from
the user (ends with ‘\n’) and displays the
number of times each letter appears in it.
Example:
For the input line - hello, world!
The output should be:
d -1
e–1
h–1
l–2
o–2
w-1

Assume that the input is all in lower-case.


Solution

letter_count.c
Passing arrays to functions
 Functions can accept arrays as
arguments
 Usually the array’s size also needs to be
passed (why?)
 For example -
int CalcSum(int arr[], int size);
 Within the function, arr is accessed in
the usual way
 Example – calc_sum.c
Passing arrays to functions
 Arrays can be passed to functions and
have their values changed from within
the function!
 Unlike regular variables
 This is possible because an array
variable is actually an address.
 Example – vec_mul.c
Exercise
 Implement a function that accepts two
integer arrays and returns 1 if they are
equal, 0 otherwise
 Write a program that accepts two
arrays of integers from the user and
checks for equality
 (assume their size is 10)
Solution
 compare_arrays.c
Two Dimensional Arrays
 We sometimes want to keep an
inherent Two-Dimensional structure of
data
 Example: We can define a two-
dimensional 3x3 matrix by
double A[3][3];
Two Dimensional Arrays
 Array of arrays:
int A[2][3] = { {1, 2, 3},
{4, 5, 6} };
 Means an array of 2 integer arrays,
each of length 3.
 Access: j-th element of the i-th array is
A[i][j]
Two Dimensional Arrays
 When passing a two-dimensional array to a
function, it is necessary to indicate the size
of the second dimension in the function
header
 But this does not mean the array’s size needn’t
be passed as well
 Same is true when initializing the array

int func(int[][4] arr);


double B[][2] = {{1,2}, {2,3}, {3,4}};
Example

mat_add.c
Exercise
 Write a program that defines 3 matrices
A,B,C of size 3x3 with float elements;
initialize the first two matrices (A and B)
 Compute the matrix multiplication of A and B
and store it in C (i.e. C = A*B)
 Matrix Multiplication:
n 1
C  i  j    A i  k  * B k  j 
k 0

 Print all the matrices on the screen


Solution

mat_mul.c

You might also like