Title: CS101 Lecture 15
1Lecture 15
2What will I learn in this lecture?
- Know how to declare arrays, access elements of
an array. - Understand how character arrays can represent
strings. - Use compact C notation such as assignment
operators or the increment and decrement
operators. - Two dimensional arrays
-
- Related Chapters FER Chapter 10.1-10.3 , 13.1 -
13.3 and Chapter 17
3What is an Array?
A one dimensional array is a list of data values,
with all values having the same data type(the
base type), such as
- integer
- float
- double
- char
Technically, an array is a uniform data
structure. Individual array elements are
referenced using the array name and a subscript
that identifies the element position in the array.
4Declaring an Array
For a one dimensional array, we specify the array
name, its base data type, and the number of
storage locations required using declarations
such as
int a25 float x100, y100
which specifies 25 integer locations for a and
100 floating-point locations for arrays x and y.
Storage for array elements are in contiguous
locations in memory, referenced by subscript(or
index) values starting at 0. Thus for array a
above, the storage is
RAM
. . .
a0 a1 a24
5Declaring and initializing an Array
The array size could also be specified using a
symbolic constant
define ARRAY_SIZE 25 int aARRAY_SIZE
. . .
We can initialize array elements in declaration
statements e.g.,
int counts5 1, 2, 3, 4, 5 int evens
2, 4, 6, 8
/ evens has 4 elements /
We cannot specify more initial values than there
are array elements, but we can specify fewer
initial values, and the remaining elements will
be initialized to 0. e.g.,
int b10 2 double x250 0
6Arrays - subscripting
Given that the array x is declared as int
x250 To initialize a large array to a nonzero
value - say 10, we can use a loop. e.g.,
for (k 0 k lt 249 k k1) xk 10
k is a subscript
Note when referencing a particular element of an
array use square brackets, not parenthesis or
curly braces.
7Arrays - subscripting
A subscript value for an array element can be
specified as any integer expression. For
example, given the following declarations double
y4 -1.0, 12.0, 3.5, 3.2e-2 int c 2 the
following statement would assign the value of 5.5
to y1
y2 c - 3 5.5
8Example - Fibonacci Numbers
1. Problem Definition Write a program fib that
prompts the user for an integer n. The program
prints the first n Fibonacci numbers. 2.
Refine, Generalize, Decompose the problem
definition (i.e., identify sub-problems, I/O,
etc.) Input n - the count of Fibonacci Numbers
to be printed Output Fib Numbers printed to
screen. 3. Develop Algorithm Use the formula
fib0 1, fib1 1 and for k gt 1 fibk fibk-1
fibk-2
Given an integer value n, we can use the
following code to compute and store the first n
Fibonacci numbers in an array
9include ltstdio.hgt / C program to print the
first n Fibonacci Numbers. /void main(void)
int k,n int fib100 1,1 / note n must
be lt 100 , why ? / printf("How many Fibonacci
number do you want listed? ") scanf("i",n)
for (k 2 k lt n k) / the is the
increment operator / fibk fibk-1
fibk-2 / print the results, four per line
/ for (k 0 k lt nk) if (k 4
0) printf(\n) printf("8i", fibk) /
thats 8 ints / / end of for loop /
printf("\n") / end of main /
(For large values of parameter n, we would need
more space for each printed column. Also, the
calculated values may become too large for the
allocated storage space for a datatype int.)
10Increment and Decrement Operators
k
Since incrementing and decrementing (negative
increments) are common programming operations,
the C language provides shortcut Increment /
Decrement operators.
k Increment k by 1 and use incremented
value in expression containing
k. k Use the current value of k then
increment by 1. --k Decrement k by 1 and
use decremented value in expression
containing --k. k-- Use current value of
k then decrement by 1.
.
11Increment and Decrement Operators - Examples
k /C statement k k 1 / int x,zint y
1int a 2x y a/ now x 3 , y 1
, a 3 /z x --a/ now a 2, z 5, x
3, y 1 / z --x y/ now x 2, y 2,
z 4, a 2 /x a / --y/ now y 1, x
2, a 3, z 4 /
Note The increment/decrement operators can be
applied only to single variables. They cannot be
applied to expressions.
12Example - Average and Difference List
1. Problem Definition Write a program that
inputs a list of numbers into an array. The
program then calculates the average value, and
then prints a list of differences. The
differences are computed by taking the original
values in the list minus the average. 2.
Refine, Generalize, Decompose the problem
definition (i.e., identify sub-problems, I/O,
etc.) Input list of reals in a file
input.dat . We will use Unix redirection to
read values from this file. Output The average
and the list of differences.
Given an integer value n, we can use the
following code to compute and store the first n
Fibonacci numbers in an array
13Example - Average and Difference List
include ltstdio.hgtvoid main(void) int
counter,k double datAve, sum 0.0,
datVal500 / 500 element max / / read the
file and compute the average / counter 0
while (EOF ! scanf("lf", datValcounter))
sum datValcounter / is an
assignment operator / counter /
compute and print the average / datAve sum
/counter printf("The average islf \n",
datAve) / compute and print the diff list
/ for(k0kltcounterk) printf("lf\n",
datValk-datAve)
14Assignment Operators
sum datValcounter
- a shortcut method for writing assignment
statements of the form
var1 var1 op expression
Using an "assignment operator", we can rewrite
the above as
var1 op expression
where op can be any one of the arithmetic binary
operators
- /
15Assignment Operators - Examples
sum datValcounter or sum sum
datValcounter k 5 or k k 5 n -
4 or n n -4 a 2.0 or a a2.0 x /
b or x x/b remainder 6 or remainder
remainder 6 newValue (a-b)/(2c) or
newValue newValue (a-b)/(2c)
16Character Arrays
Unlike many other languages, there is no data
type for character strings in C. We will
implement strings in C by using a one dimensional
array of characters where the last character is
the NULL character \0.
17Declaring a Character Array
Declaring a string array with character constants
char aString 'Z', 'i', 'p', '!, '\0'
Declaring character arrays with a string constant
char aString5 "Zip!" char atomic
"hydrogen"
IMPORTANT!!! In C, you must always terminate a
character array with the NULL character, \0 .
Therefore, the array size of your character array
should be one plus the maximum length of the
string you want to store. Example In the
declarationchar atomic "hydrogen" atomic
is an array of nine elements, the last being \0
18Declaring a Character Array
You may declare and initialize a character arrays
with a string constant as in,
char atomic "hydrogen"
but you cannot assign an array in the same
manner, e.g. ,
not legal in C!
atomic "carbon"
however you may use the strcpy function (found in
the string.h library).
strcpy(atomic, "carbon")
19Input and Ouput of Strings
Use the s conversion specifier to read in a
string of characters. Blanks in the input are
considered delimiters. That is, any blank
character input such as in the string Hello
World, would indicate two strings Hello and
World. For security reasons its good to use
9s rather than s in the following example for
scanf.
char strArray 10 printf("Input a string with
at most nine characters ) printf("with no
blanks in the string \n") scanf("s",
strArray) / Notice No is used for an array
argument! / printf("\ns", strArray)
20Outputing a String
You can use the c conversion specifier to output
the string character by character.
k 0while (strArrayk ! \0) printf("c",
strArrayk)
21string.h Library Functions
include ltstring.hgt Arguments str1 and str2 in
the following functions can be character array
names, string pointers(discussed in a future
lecture), or string constants.
- strlen(str) - string length, not counting the
\0 char. - strcpy(str1, str2) - copy string2 to string1.
- strcmp(str1, str2) - returns a negative , zero
or positive int depending on whether str1 is
lexicographically less than, equal or greater
than str2 respectively. - strstr(str1,str2) if str2 is NOT a substring
of str1 this function returns the NULL value.
See Chapter 13.3 for examples. There are many
other string functions (see Appendix G p. 1036
for a complete list).
22Example Hand Calculator
1. Problem Definition Write a hand calculator
program using the operators , -, , / for a
simple calculator. The input from the calculator
will be in the Reverse Polish Notation format.
For example, 2 3 is equivalent to
23 and 3 5 2 is equivalent to
(35)2 Note that RPN doesnt require
parenthesis. 2. Refine, Generalize, Decompose the
problem definition (i.e., identify sub-problems,
I/O, etc.) Input list of reals and operators
in a file input.dat . We will use Unix
redirection to read values from this
file. Output The result of the calculation as a
real number.
Given an integer value n, we can use the
following code to compute and store the first n
Fibonacci numbers in an array
23Example Hand Calculator
3. Develop Algorithm A LIFO (last in first
out) Stack is like a spring loaded plate
dispenser found in a cafeteria. The last (clean)
plate that goes into the dispenser is the first
one to be selected by a customer. We will use an
array named 'stack' to implement the stack in our
program. Here is how our calculator works with a
Stack to compute (3 5)21) Push the 3 on the
stack.stack0 3 top of
stack 2) Next push the 5 on the top of the
stack. stack1 5 top of
stack stack0 3
24Example Hand Calculator
3. Develop Algorithm 3)Pop the two values off of
the stack and push the result (35) back on the
stack. stack0 8 top of stack
4) Next, push the 2 on the stack. stack1
2 top of stack stack0 8 5)
Now pop the two values and them (8 2) and put
the result on the stack. stack0 16
top of stack
25 include ltstdio.hgt include ltstring.hgt / for
strcmp function / include ltstdlib.hgt / for
atof function / void main(void) char
input32 double stack64 int top -1
/ shouldnt this be 0 ? / while (EOF !
scanf("s", input)) if (
strcmp(input,"") 0) stacktop-1
stacktop top-- else if (
strcmp(input,"-") 0) stacktop-1 -
stacktop top-- else if (
strcmp(input,"") 0) stacktop-1
stacktop top-- else if (
strcmp(input,"/") 0) stacktop-1 /
stacktop top-- else
top stacktop atof(input)
printf("result lf \n", stack0)
26stdlib.h Library Functions
include ltstdlib.hgt
- atof(str) converts alpha (string) to float
(double) - qsort(array, num_elements, bytes_per_element,
aux_function)
See Lecture 21 slides 7-16 for an example of the
use of qsort. There are many other functions in
stdlib.h, (see Appendix G p. 1034 for a complete
list).
27Two-Dimensional Arrays
- Two dimensional arrays are declared similarly
to one dimensional arrays
char t2010
(and referred to as a matrix, or as a "table")
The first subscript gives the row number, and the
second subscript specifies column number.
28Declaration and Initialization
We can also initialize a two-dimensional array
in a declaration statement E.g.,
int m23 1, 2, 3, 4, 5, 6
which specifies the elements in each row of the
array (the interior braces around each row of
values could be omitted). The matrix for this
example is
29Declaration and Initialization
Specification of the number of rows could be
omitted. But other subscript specifications can
not be omitted. So, we could also write the
initialization as
int m 3 1, 2, 3, 4, 5, 6
30Memory Storage for a Two Dimensional Array
int a23 1,2,3,4,5,6
specifies 6 integer locations. Storage for
array elements are in contiguous locations in
memory in row major order (unlike column major
order in Fortran), referenced by subscripts(or
index) values starting at 0 for both rows and
columns.
RAM
1
2
3
4
5
6
a00 a01 a02 a10 a11
a12
31Example - Maximum Value
- 1. Problem Definition
- Assume we have a file in.dat (located in the
pwd) consisting of 10 rows of 10 integers per row
(100 integers total). Write a C program which
reads from the file and stores the numbers in a
two dimensional array named mat, computes the
largest of all of these numbers and prints the
largest value to the screen. - Â
- 2. Refine, Generalize, Decompose the problem
definition - (i.e., identify sub-problems, I/O, etc.)
- Input from file in.dat
- Output Largest of the 100 integers printed to
screen. - 3. Develop Algorithm
- Use Unix redirection to read file in.dat.
- Use nested for loop to go through two
dimensional array to - find the largest value.
32include ltstdio.hgt void main(void) int row,
col, maximum / Declare a 2-D array called
mat which can hold a 10-by-10 / / matrix
of integers / int mat1010 / Write code
here to read from the file in.dat /
for(row0rowlt10row) for(col0collt10col
) scanf("i",matrowcol) / Write code
to find the largest value in the array mat /
maximum mat00 / why ??? / Â
for(row0rowlt10row) / How does this
work??? / for(col0collt10col) if
(matrowcol gt maximum) maximum
matrowcol  / Print the largest value to
the screen / Â printf(" max value in the array
i\n",maximum) Â
33typedef
The typedef mechanism in C allows us to define
our own data types. For example, typedef double
matrix1020 In this example we create a new
data-type named matrix. We can declare a variable
of data-type matrix by matrix a and this
declaration is equivalent to double a1020
34typedef
Other examples of the use of typedef are,
typedef int blah / dont do this !!!!!
/ typedef char string5 In the first example
we give another name or alias to the data type
int . In the second example we create a new
data-type named string.
35typedef
To declare a variable of data-type blah , blah x
this declaration is equivalent to int x To
declare a variable of data-type string , string
var "init" / declare and initialize
/scanf("s",var) / read in a string of chars
/ strcpy(var , "next") The declaration string
morse26 declares morse as an array of 26
elements. Each element has data-type string.
36typedef
The declaration and initialization string
morse26 ".-", "-...", "-.-.", "-..",
".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-",
".--", "-..-", "-.--", "--.. " declares
morse as an array of 26 elements with
morse0 ".-" morse1 "-..." and so on...
37Higher Dimensional Arrays
We can also use 3, 4, dimensional arrays, e.g.,
threeDArray ijk
Standard (ANSI) C allows up to 12 dimension.
But more computation is required by the system
to locate elements in multi-subscripted arrays.
Therefore, in problems involving intensive
numerical calculations, we should use arrays with
fewer dimension.