Introduction To Arrays: ESC101 September 6
Introduction To Arrays: ESC101 September 6
ESC101
September 6th
Announcements
• Extra class on Friday, 8th September
– 1700-1750 in L20
– We will discuss major quiz 1
• Lab mid sem on Saturday, 16th September
– At NCL, rooms to be allotted at the time
– Sections B1-B6 10am-1pm
– Sections B7-B12 1200pm-4pm
• Remedial labs every week Fridays 2pm-5pm
Today
• Wrapping up last lecture
– Static variables
– Gentle intro to memory management
• Introduction to arrays
– Syntax
– Basic I/O
– Simple usage
Static Variables
• We have seen two kinds of variables: local variables and
global variables.
• There are static variables too.
int f () { = 0;
ncalls GOAL: count number of calls to f()
int
int f ncalls
static
() int
{ = 0;
ncalls = 0; SOLUTION: define ncalls as a static
ncalls = ncalls + 1; variable inside f().
/* track the number of It is created as an integer box the
first time f() is called.
times f() is called */
Once created, it never gets
… body of f() … destroyed, and retains its value
} across invocations of f().
Multiple initializations have no effect
• Use a local variable? within the active scope
• gets destroyed every It is like a global variable, but visible
time f returns only within f().
• Use a global variable? Static variables are not allocated on
stack. So they are not destroyed
• other functions can when f() returns.
Aug-19 change it! (dangerous) 4 ESC101, Functions
Gentle brush with pointers
• Pointers are special variables that store
memory addresses
• We will cover pointers in much greater depth
soon
Why use arrays
Example : Maximum of 3 numbers
int main(){ int max(int a, int b){
int a, b, c, m; if (a>b)
return a; How would
/* code to read else you scale
* a, b, c */ return b; this code to
} handle
if (a>b){ large
if (a>c) m = a; int main() { number
else m = c; int a, b, c, m; of inputs
} (e.g.: max
else{ /* code to read of 100
if (b>c) m = b; * a, b, c */ numbers!)
else m = c;
} m = max(a, b);
m = max(m, c);
/* print or use m */ /* print or use m */
return 0; return 0;
} }
Aug-19 6 ESC101, Functions
Operations on a list
• Take this list
• Do something to every element on this list
• Return the output for every element on the
list
These addresses contain entries from ONE list
int a[5];
return 0;
}
include <stdio.h> Let us trace the
int main () { execution of the program.
int a[5];
int i;
1 2 3 4 5
Overall design
int main() {
char s[100]; /* to hold the input */
/* read_into_array */
/* print_reverse */
return 0;
}
Let us design the program fragment read_into_array.
Keep the following variables:
1. int count: count the number of characters read so far.
2. int ch: to read the next character using getchar().
int i;
set i to the index of last character read.
while (i >= 0) {
print s[i]
i = i-1; /* shift array index one to left */
}
The
array `H’ `E’ `L’ `P’
char
s[100] s[0] s[1] s[2] s[3] s[99]
i
count 4
index i runs backwards in array
int i; int i;
set i to index of the last character read. i = count-1;
read_into_array code.
print_reverse code
int count = 0;
int ch; int i;
ch = getchar(); i = count-1;
while ( ch != EOF && count < 100) { while (i >=0) {
s[count] = ch; putchar(s[i]);
count = count + 1; i=i-1;
ch = getchar(); }
}
#include <stdio.h>
int main() {
char s[100]; /* the array of 100 char */
int count = 0; /* counts number of input chars read */
int ch; /* current character read */
int i; /* index for printing array backwards */
ch = getchar();
while ( ch != EOF && count < 100) {
s[count] = ch; /*read_into_array */
count = count + 1;
ch = getchar();
}
i = count-1;
while (i >=0) {
putchar(s[i]);
/*print_in_reverse */
i=i-1;
}
return 0;
}
#include <stdio.h>
int main() {
char s[100];
int count = 0;
int ch;
int i; /*read_into_array */
while ( (ch=getchar()) != EOF &&
count < 100 )
{ s[count] = ch;
count = count + 1;
}
i = count-1;
while (i >=0) {
putchar(s[i]); /*print_in_reverse */
i=i-1;
}
return 0;
}
Next class
• More about arrays