Recap: Arrays
▪ A collection of elements all of which have the same data type
float marks[500];
marks[0] marks[1] marks[2] marks[499]
▪ Each array element is accessed using the array index (integer-valued)
▪ For the above example, marks[0], marks[2], marks[499], marks[int_expr] where
int_expr is integer-valued expression such that 0 <= int_expr <= 499
1
Recap: Array: Declaration and Initialization
Can be initialized at time of declaration itself
int a[6] = {3,7,6,2,1,0}; a 3 7 6 2 1 0
Can be partly initialized as well
int a[6] = {3,7,6}; a 3 7 6
Over initialization may crash I will figure out how
int a[6] = {1,2,3,4,5,6,7,8,9};
No need to specify much space needed
the array size during
Better way to initialize is the following
declaration
int a[] = {1,2,3,4,5,6,7,8,9};
Warning: uninitialized arrays contain garbage, not zeros
2
Array: Declaration and Initialization
▪ Can declare the array first and initialize its elements later
▪ The later initialization can be done using user-provided values (e.g.,
using scanf), or some expression, or using some fixed values
Can I run the
int i,tmp,a[5]; loop as
Read a for(i=1;i<=5;i++)?
use-provided
for(i=0;i<5;i++){
value scanf(“%d”,&tmp);
Assign the read
value to the ith a[i] = tmp; Yes, if you use
element of the a[i-1] = tmp; in
array } loop’s body 3
Array: Declaration and Initialization
▪ Can declare the array first and initialize its elements later
▪ The later initialization can be done using user-provided values (e.g.,
using scanf), or some expression, or using some fixed value
Note: &a[i] is evaluated
as &(a[i]) since [] has
int i,a[5]; higher precedence than &
Directly read a user
for(i=0;i<5;i++){ provided value into the
ith element of the array
A shortcut for scanf(“%d”,&a[i]); (the tmp variable is not
needed)
reading user
provided
}
values 4
Operator Name Symbol/Sign Associativity
Brackets, array subscript, Post (), [] ++, -- Left
increment/decrement
Unary negation, Pre -, ++, --, ! Right
increment/decrement, NOT
Multiplication/division/ *, /, % Left
remainder
Addition/subtraction +, - Left
Relational <, <=, >, >= Left
Relational ==, != Left
AND && Left
OR || Left
Conditional ?: Right
Assignment, Compound =, +=, -=, *=, /=, Right
assignment %= 5
Array: Declaration and Initialization
▪ Can declare the array first and initialize its elements later
▪ The later initialization can be done using user-provided values (e.g.,
using scanf), or some expression, or using some fixed value
1 2 3 4 5
int i,a[5]; a[0] a[1] a[2] a[3] a[4]
for(i=0;i<5;i++){
a[i] = i+1; Assign a value of
expression i+1 to
} the ith element of
the array
6
Array: Declaration and Initialization
▪ Can declare the array first and initialize its elements later
▪ The later initialization can be done using user-provided values (e.g.,
using scanf), or some expression, or using some fixed value
10 10 10 10 10
int i,a[5]; a[0] a[1] a[2] a[3] a[4]
for(i=0;i<5;i++){ Assign a fixed
(constant) value 10
a[i] = 10; to the ith element of
the array
}
7
Tracing the execution of an array based program
include <stdio.h> Let us trace the
int main () { execution of the program.
int a[5];
int i;
for (i=0; i < 5; i= i+1) {
a[i] = i+1; i
} 5
2
0
1
3
4
return 0;
}
a[0] a[1] a[2] a[3] a[4]
1 2 3 4 5
Statement becomes a[0] =0+1;
Statement becomes a[1] =1+1; Statement becomes a[3] = 3+1;
Statement becomes a[2] =2+1; Statement becomes a[4] = 4+1; 8
Arrays: Some Example Programs
▪ Create an integer array of size 100
▪ Initialize elements with even index as 0
▪ Initialize elements with odd index as 1
int i,a[100];
for(i=0; i<100; i=i+1){
Method 1
if(i%2==0) a[i] = 0;
else a[i] = 1;
} 9
Arrays: Some Example Programs
▪ Create an integer array of size 100
▪ Initialize elements with even index as 0
Incrementing the
▪ Initialize elements with odd index as 1 loop counter by 2
int i,a[100];
Method 2,
for(i=0; i<100; i=i+2){
This for loop will run
without if-else a[i] = 0; 50 times. Each
iteration will assign
a[i+1] = 1; values to 2 elements,
one at odd index, one
} at even index
10
Greek origin word:
palin = again,
Arrays: Some Example Programs dromos = direction
▪ Check whether a sequence of numbers is a palindrome sequence
Let’s specify a maximum flag = 1 assumes that sequence is
int main(){ sequence size palindrome (set 0 if later found otherwise)
Palindrome: Forward and int a[100], temp, len = 0, i, flag = 1;
The while(1) loop keeps reading numbers
Reverse gives the same until user enters -1, store each number as an
while(1){
sequence scanf("%d", &temp);
element of the array named a
if(temp == -1) This line does a[len] = temp;
break; and then increments len
Some palindromes:
a[len++] = temp;
123454321 } After the while(1) loop exits, len is the
123321 size of the array (indices are 0 to len-1)
for(i = 0; i < len; i++)
if(a[i] != a[len-i-1]) Compares a[0] with a[len-1], then a[1]
Some non-palindromes: flag = 0; with a[len-2], and so on. If any pair does
12345 if(flag) printf("YES"); not match, set flag variable to 0
123341 else printf("NO");
90408 return 0;
} a[0] a[1] a[2] a[len-2] a[len-1] 11
Arrays: Some Example Programs
#include <stdio.h>
Read until user has
int main() {
entered 100 chars or char s[100];
the end-of-file (EOF) /* the array of 100 char */
int count = 0; /* counts number of input chars read */
special character int ch; /* current character read */
has been read. int i; /* index for printing array backwards */
ch = getchar();
Now print the characters while ( ch != EOF && count < 100) {
in reverse order s[count] = ch;
count = count + 1; /*read_into_array */
ch = getchar();
}
getchar() returns a
single character i = count-1;
entered by the user while (i >=0) {
putchar(s[i]); /*print_in_reverse */
i=i-1;
}
return 0; putchar() prints a single character
}
12