CSci 152: Programming II Fall 2004 - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

CSci 152: Programming II Fall 2004

Description:

Write a quick program that first gets an integer as input ... when you compile your program? ... x and y are two arrays of the same type and size, say 25. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 37
Provided by: facultyTa
Category:

less

Transcript and Presenter's Notes

Title: CSci 152: Programming II Fall 2004


1
CSci 152 Programming IIFall 2004
  • One-Dimensional Arrays
  • Malik, Ch 9, pp 423-442

2
Questions about Arrays
  • Why arrays? What are they used for?
  • How do we define (and use) arrays in C/C.

3
Simple vs. More structured data types.
  • A data type is called simple if variables of
    that type can store only one value at a time.
  • A structured data type is one in which each data
    item is a collection of other data items.

4
  • //Program to read five numbers, find their sum,
    and
  • //print the numbers in reverse order.
  •  
  • include ltiostreamgt
  • using namespace std
  •  
  • int main()
  • int item0, item1, item2, item3, item4
  • int sum
  •  
  • coutltlt"Enter five integers "
  • cingtgtitem0gtgtitem1gtgtitem2gtgtitem3gtgtitem4
  • coutltltendl
  •  
  • sum item0 item1 item2 item3 item4
  •  
  • coutltlt"The sum of the numbers "ltltsumltltendl
  • coutltlt"The numbers in reverse order are "

5
  • We note the following in the above program.
  • 1. Five variables must be declared because the
    numbers are to be printed in reverse order.
  • 2. All variables are of the type intthat is, of
    the same data type.
  • 3. The way in which these variables are declared
    indicates that the variables to store these
    numbers have the same name except the last
    character, which is a number.
  • Statement 1 tells you that you have to declare
    five variables.
  • Statement 3 tells you that it would be convenient
    if you could somehow put the last character,
    which is a number, into a counter variable and
    use one for loop to count from 0 to 4 for reading
    and another for loop to count from 4 to 0 for
    printing.
  • All variables are of the same type. So you should
    be able to specify how many variables must be
    declaredand their data typewith a simpler
    statement than the one we used earlier.

6
Why Arrays?
  • We often have to do the same operation on lots of
    things (a collection).
  • It is impractical to declare hundreds of
    variables (or thousands or millions) to hold each
    item in collection.

7
Arrays (How to declare in C)
  • An array is a collection of a fixed number of
    components wherein all of the components are of
    the same data type.
  • A one-dimensional array is an array in which the
    components are arranged in a list form.
  • The general form of declaring a one-dimensional
    array is
  • dataType arrayNameintExp
  • where intExp is any expression that evaluates to
    a positive integer.

8
  • Example 9-1
  • The statement
  • int num5
  • declares an array num of 5 components. Each
    component is of the type int. The components are
    num0, num1, num2, num3, and num4.

9
Accessing Array Components (How to use em)
  • The general form (syntax) of accessing an array
    component is
  • arrayNameindexExp
  • where indexExp, called index, is any expression
    whose value is a nonnegative integer.
  • Index value specifies the position of the
    component in the array.
  • In C, is an operator, called the array
    subscripting operator.
  • In C array index starts at 0.

10
  • int list10

11
  • list5 34

12
  • If i is an int variable, the assignment statement
  • list3 63
  • is equivalent to the assignment statements
  • i 3
  • listi 63
  • If i is 4, then the assignment statement
  • list2i-3 58
  • stores 58 in list5, because 2i-3 evaluates to
    5.

13
  • list3 10
  • list6 35
  • list5 list3 list6

14
  • Example 9-2
  • You can also declare arrays as follows
  •              
  • const int arraySize 10
  •  
  • int listarraySize
  • When you declare an array, its size must be
    known.
  •                 
  • int arraySize //Line 1
  •  
  • coutltlt"Enter the size of the array " //Line 2
  • cingtgtarraySize //Line 3
  • coutltltendl //Line 4
  •  
  • int listarraySize //Line 5 not allowed

15
Quick Quiz
  • Write a quick program that first gets an integer
    as input from a user, then creates an array of
    integers of that indicated size.
  • What happens when you compile your program?
  • For those of you using Bloodshed Dev C is it
    different from those people using Microsoft .NET?

16
Quick Quiz
  • void main()
  • int size
  • cout ltlt Enter the array size
  • cin gtgt size
  • int iarraysize

17
Quick Quiz
  • Create an array of 10 characters, called carray,
    an initialize all of the characters with the
    value 'a'.
  • Set the character at index 4 in your carray to
    have the value 'z'
  • Try doing this
  • cout ltlt carray
  • Are you surprised by this result? If not
    why not? What happens if you create an array of
    10 integers and try and print it out doing the
    same thing?

18
Quick Quiz
  • void main()
  • char carray10
  • int i
  • for (int i0 ilt10 i)
  • carrayi 'a'
  • carray4 'z'
  • cout ltlt carray

19
Processing One-Dimensional Arrays
  • Some of the basic operations performed on a
    one-dimensional array are initialize, input data,
    output data stored in an array, find the largest
    and/or smallest element.
  • Each of these operations requires the ability to
    step through the elements of the array.
  • Stepping-through the elements of an array is
    easily accomplished by a loop.

20
  • Consider the declaration.
  • int list100 //list is an array of the size 100
  • int i
  • The following for loop steps-through each element
    of the array list starting at the first element
    of list.
  • for(i 0 i lt 100 i) //Line 1
  • process listi //Line 2
  • If processing list requires inputting data into
    list, the statement in Line 2 takes the from of
    an input statement, such as the cin statement.
  • for(i 0 i lt 100 i) //Line 1
  • cingtgtlisti

21
  • Example 9-3
  • double sale10
  • int index
  • double largestSale, sum, average
  • (a) Initializing an array
  • for(index 0 index lt 10 index)
  • saleindex 0.0
  • (b) Reading data in an array
  • for(index 0 index lt 10 index)
  • cingtgtsaleindex
  • (c) Printing an array
  • for(index 0 index lt 10 index)
  • coutltltsaleindexltlt" "

22
  • (d) Finding the sum and average of an array
  • sum 0
  • for(index 0 index lt 10 index )
  • sum sum saleindex
  • average sum / 10
  • (e) Largest element in the array
  • maxIndex 0
  • for(index 1 index lt 10 index )
  • if(salemaxIndex lt saleindex)
  • maxIndex index
  • largestSale salemaxIndex

23
  • maxIndex 0 index 1
  • id mId salemId saleid salemIdltsaleid
  • 1 0 12.50 8.35 12.50 lt 8.35 is false
  • 2 0 12.50 19.60 12.50 lt 19.60 is true
    mId 2
  • 3 2 19.60 25.00 19.60 lt 25.00 is
    true mId 3
  • 4 3 25.00 14.00 25.00 lt 14.00 is false
  • 5 3 25.00 39.43 25.00 lt 39.43 is true
    mId 5
  • 6 5 39.43 35.90 39.43 lt 35.90 is false
  • 7 5 39.43 98.23 39.43 lt 98.23 is true mId
    7
  • 8 7 98.23 66.25 98.23 lt 66.65 is
    false
  • 9 7 98.23 35.64 98.23 lt 35.64 is false
  • In the above table id stands for index and mId
    stands for maxIndex

24
Array Index Out of Bounds (Some gotchas to watch
out for when using arrays)
  • double num10
  • int i
  • The component numi is valid, that is, i is a
    valid index, if i 0, 1, 2, 3, 4, 5, 6, 7, 8,
    or 9.
  • The index, say index, of an array is in bounds if
    index gt0 and index lt arraySize-1.
  • If either index lt 0 or index gt arraySize-1,
    then we say that the index is out of bounds.
  • In C, there is no guard against indices that
    are out of bounds. That is, C does not check if
    the index value is with in range, that is,
    between 0 and arraySize-1.

25
  • A loop such as the following can set the index
    out of bounds.
  • for(i 0 i lt 10 i)
  • listi0
  • Here we assume that list is an array of 10
    components.

26
Some Restrictions on Array Processing
  • Suppose x and y are two arrays of the same type
    and size, say 25. Then the following statement is
    illegal
  • y x // illegal
  • In order to copy one array into another array we
    must copy component-wise. This can be
    accomplished by a loop like the following.
  • for(j 0 j lt 25 j)
  • yj xj
  • Similarly comparison of arrays, reading data into
    an array and printing the contents of an array
    must be done component-wise.
  • cingtgtx //illegal
  • coutltlty //illegal

27
  • if(x lt y) //illegal
  • .
  • .
  • .
  • C does not allows aggregate operations on
    array.
  • An aggregate operation on an array is any
    operation that manipulates the entire array as a
    single unit.

28
Arrays as Parameters to Functions
  • By Reference Only In C, arrays are passed by
    reference only.
  • Since arrays are passed by reference only, we do
    not use the symbol when declaring an array as a
    formal parameter.
  • When declaring a one-dimensional array as a
    formal parameter the size of the array is usually
    omitted.
  • If we specify the size of the one-dimensional
    array when it is declared as a formal parameter,
    it is ignored by the compiler.

29
  • void initialize(int list5)
  • int count
  • for(count 0 count lt 5 count)
  • listcount 0
  • The for loop inside the function executes 5
    times, the above function will correctly
    initialize any int array of size 5 to zero.
  • If we add another formal parameter, say size in
    the function heading, use the value of size to
    control the for loop iterations, and pass the
    name of the actual array together with its size
    during call, then we can use the same function
    for any size array.
  • void initialize(int list, int size)
  • int count
  • for(count 0 count lt size count)
  • listcount 0

30
  • Even though an array is always passed by
    reference, we can still prevent the function from
    changing the actual parameter.
  • This is accomplished by using the reserved word
    const in the declaration of the formal parameter.
  • void example(int x, const int y,
  • int sizeX, int sizeY)
  • .
  • .
  • .

31
  • Example 9-5
  • //Function to initialize an array to 0
  • void initializeArray(int x,int sizeX)
  • int counter
  • for(counter 0 counter lt sizeX counter)
  • xcounter 0
  • //Function to read data and store in an array
  • void fillArray(int x,int sizeX)
  • int counter
  • for(counter 0 counter lt sizeX counter)
  • cingtgtxcounter

32
  • //Function to print the array
  • void printArray(int x,int sizeX)
  • int counter
  • for(counter 0 counter lt sizeX counter)
  • coutltltxcounterltlt" "

33
  • //Function to find and return the sum of an array
  • int sumArray(int x,int sizeX)
  • int counter
  • int sum 0
  • for(counter 0 counter lt sizeX counter)
  • sum sum xcounter
  • return sum

34
  • // Function to find and return the index of the
  • // largest element of an array
  • int indexLargestElement(const int x,int sizeX)
  • int counter
  • int maxIndex 0 //Assume first element is the
    largest
  • for(counter 1 counter lt sizeX counter)
  • if(xmaxIndex lt xcounter)
  • maxIndex counter
  • return maxIndex

35
  • //Function to copy one array into another array
  • void copyArray(const int x, int y,
  • int length)
  • int counter
  • for(counter 0 counter lt length counter)
  • ycounter xcounter

36
Base Address of an Array
  • The base address of an array is the address (that
    is, the memory location) of the first array
    component.
  • If list is a one-dimensional array, then the base
    address of list is the address of the component
    list0.
  • When we pass an array as a parameter, the base
    address of the actual array is passed to the
    formal parameter.
  • Function can not return a value of the type
    array.
Write a Comment
User Comments (0)
About PowerShow.com