Unformatted text preview:

ArraysWhat Is an Array?Thinking About ArraysSlide 4Declaring ArraysAssigning Values to ArraysSlide 7Program Example for ArraysProgram Output of an ArrayAnother Program ExampleLect 11 P. 1 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArraysLecture 11Lect 11 P. 2 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionWhat Is an Array?•An array is a group of items of the same type under one variable name. It is an ordered list of elements where each is of the same data type.•As an example, the days of the week can be represented as a one-dimensional array named day where Sunday is the first element, Monday the second, and so on.Lect 11 P. 3 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionThinking About Arrays•The days of the month can be thought of as a two-dimensional array. Looking at a calendar, each row represents a week (which can be represented as a one-dimensional array), and the entire month is composed of an array of arrays, that is, a two-dimensional array.Lect 11 P. 4 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays•Taking the concept further, the days of the year can be represented as a three-dimensional array, that is, an array of arrays of arrays. The first dimension is the day of the week, the second the week of the month, and the third the month of the year.Lect 11 P. 5 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionDeclaring Arrays•A one-dimensional array is represented by specifying the array name followed by a number in square brackets [ ] to indicate the array size (or number of elements).•The statement:int day[ 7 ];would declare a seven element array of integers.•Declaring multi-dimensional arrays is similar: int month[ 4 ][ 7 ];would declare an array of integers with 4 rows and 7 columns.Lect 11 P. 6 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionAssigning Values to Arrays•Once an array is declared, the elements are accessed by specifying the array name and the number of the element. In C, the first element is number 0, so a seven element array would have elements numbered 0 through 6.•For the week of February 6th through the 12th, the array could be assigned as follows:day[0]=6; day[1]=7; day[2]=8; day[3]=9;day[4]=10; day[5]=11; day[6]=12;•The number in the brackets is called an index (or a subscript).Lect 11 P. 7 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionAssigning Values to Arrays•Optionally, the array can have values assigned to its elements when the array is declared.•For example, the statement: int day[ 7 ] = {6, 7, 8, 9, 10, 11, 12} ;declares a seven element array day and initializes (assigns values to) all seven of the elements.•The program on the next slide illustrates how that works.Lect 11 P. 8 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionProgram Example for Arrays/*The following program will declare the array day, assign values to the elements, and print data to the screen. */#include <stdio.h>int main ( ){ int n, day[ 7 ] = {6, 7, 8, 9, 10, 11, 12} ; for (n = 0 ; n < 7 ; n++ ) { printf ("day[ %d ] is February %d, 2005 \n", n, day[n] ) ; } return 0 ;}Lect 11 P. 9 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionProgram Output of an Array•When this program is run, it will produce the following output:day[0] is February 6, 2005 day[1] is February 7, 2005 day[2] is February 8, 2005 day[3] is February 9, 2005 day[4] is February 10, 2005 day[5] is February 11, 2005 day[6] is February 12, 2005Lect 11 P. 10 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionAnother Program Example/* Program to generate an array of 10 random numbers */ #include <stdio.h>#include <stdlib.h>#define SIZE 10 /* SIZE defined as symbolic constant */int main ( ){ int i, numbers [SIZE] ; for (i = 0 ; i < SIZE ; i++ ) { numbers[ i ] = rand ( ) ; printf ( "The number[ %d ] is %d \n", i, numbers[ i ]) ; } return 0


View Full Document

OSU ENGR H192 - Lecture 11 - Arrays

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 11 - Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 11 - Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 11 - Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?