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