Arrays in C - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays in C

Description:

Mrs. Bellacera When you need to store more than one value of a specific data type that is related For Example: Top ten high scores in a video game Daily high ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 13
Provided by: MSSD
Category:
Tags: arrays

less

Transcript and Presenter's Notes

Title: Arrays in C


1
Arrays in C
  • Mrs. Bellacera

2
When are Arrays Needed?
  • When you need to store more than one value of a
    specific data type that is related
  • For Example
  • Top ten high scores in a video game
  • Daily high temperatures for the month of April
  • Your math class test grades
  • A table of mileages between cities
  • An array in integers representing the checker
    status on a Connect-4 board

3
Declaring One Dimensional Arrays
  • int x10
  • float temp30
  • double grade32
  • int mileage1010
  • int board67
  • bool flag52
  • long distance8

4
Initializing Arrays
  • C arrays are NOT automatically initialized.
  • You must initialize arrays independently.
  • Array index starts at 0.
  • You can use initializer lists.
  • int weight55,10,20,50,100
  • C can determine the size.
  • int distance55,132,531,742,963,1072
  • (the size of array is set to the number of
    values in the list).
  • int i
  • for (i0ilt29i)
  • tempi0.0

5
Filling, Accumulating and Displaying
  • for (i0 ilt size-1i)
  • coutltltEnter the test gradeltltendl
  • cingtgtgradei
  • sumsumgradei
  • avgsum/size
  • for (i0 ilt 30i)
  • coutltltApril ltlt i1 ltlt ltlttempi ltltendl
  • if (max gt tempi)
  • maxtempi

6
Julian Day Assignment
  • Write a C program called Julian in which the
    month and day are input as integers. Compute the
    julian day which is the ordinal day of the year
    (i.e. Jan 1 is 1, Feb 1 is 32 and Dec 31 is 365).
    Every day is numbered from 1 to 365 in order.
    Assume that it is a non-leap year. Extras
    creditenter the year as well and take year into
    account. Output which julian day that month day
    is. (Note arrays can be used to do the
    computation.)

7
Parallel Arrays
  • Parallel arrays are a collections of one
    dimensional arrays that are related by the same
    index. They are typically different data types.
  • const int max_size15
  • string namemax_size
  • int hoursmax_size
  • float rate_of_paymax_size

8
Parallel Array Assignment
  • Create a store program. You must sell at least
    10 items at your store. Load data into three
    arrays, consisting of product name, quantity, and
    price. Allow the functionality to (1) list all
    the items (2) purchase the entered quantity of
    items (3) add inventory to any of your 10 items
    (4) change the price of an item (5)display your
    current total and (6) leave the store or exit.

9
Two Dimensional Arrays
  • Two dimensional arrays can be visualized as a
    table of information. An element of the array is
    referenced by the row and column designation. Row
    is always first.
  • float shipping68
  • coutltlt Enter weight (lbs) ltltendl
  • cingtgtpounds
  • coutltlt Enter shipping zoneltltendl
  • cin gtgtzone
  • coutltlt Enter shipping rateltltendl
  • cingtgt rate
  • shippingpoundszonerate

10
Looping through 2D Arrays
  • for (i0 iltrow_size-1 i)
  • for (j0 jltcol_size-1 j)
  • coutltlt arrayijltlt
  • coutltltendl
  • Great tutorial on Arrays
  • http//www.cplusplus.com/doc/tutorial/arrays/

11
2D Array Assignment
  • Open the word document for Two Dimensional
    Arrays.doc on my Quia site which has you create
    the SHIPRATE program.

12
Great Website on C Arrays
  • http//www.cplusplus.com/doc/tutorial/arrays/
Write a Comment
User Comments (0)
About PowerShow.com