I Love Arrays - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

I Love Arrays

Description:

An array is a group of memory locations with the same name and ... Because I love digital music, photos, and movies. Digital music is stored as 1-D arrays a[n] ... – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 13
Provided by: Anto234
Category:
Tags: arrays | love

less

Transcript and Presenter's Notes

Title: I Love Arrays


1
I Love Arrays
  • Someday youll learn to love them too

2
What is an array?
  • An array is a group of memory locations with the
    same name and same type
  • Arrays contain multiple elements of the same data
    type (int, char, float, double)
  • Arrays DO NOT mix different types of data
  • Structures can mix types, but youll learn about
    those in later courses

3
Array Indexing
53
c0
2
  • Elements of an array are referred to by their
    subscript or index
  • This index is contained in the brackets after the
    array name
  • In C, the first element of an array has an index
    of 0, the second has index of 1, etc.

c1
-35
c2
1024
c3
0
c4
1
c5
-562
c6
-3
c7
1024
c8
4
Array Indexing
53
c0
2
  • WARNING!
  • An n-element array will only have an index up to
    n-1
  • This is a common source of errors

c1
-35
c2
1024
c3
0
c4
1
c5
-562
c6
-3
c7
1024
c8
5
Array Indexing
  • Expressions can be used to evaluate the index
  • Example if a 2, and b 3, then
  • cab 2 is perfectly valid
  • Other okay expressions
  • printf(d, c0c1)
  • x c5 / 2

6
Defining Arrays
  • A twelve element array would be defined with the
    following code
  • int c12
  • Two arrays of the same type can be defined in the
    same line
  • int c12, d256

7
Initializing Arrays
  • After defining an array, it must still be
    initialized
  • There are many ways to initialize an array
  • The most direct way is to use an initializer list
  • int n5 56, 23, 1, 0, 8

8
Initializing Arrays
  • If the initializer list has fewer elements than
    defined by the array, the rest of the elements
    will be assigned as zero
  • That means then that you can initialize an array
    to have all zero values with the code
  • int g16 0

9
Initializing Arrays
  • Initializer lists can also be used without first
    defining the array
  • int g 1, 2, 3, 4, 5
  • In this case, the number of elements in the array
    will then be the same as the number of elements
    in the initializer list

10
Initializing Arrays
0
c0
1
c1
  • Arrays can also be initialized with more complex
    code

4
c2
9
c3
16
c4
int c9 for (int i 0 i lt 9 i) ci
ii
25
c5
36
c6
49
c7
64
c8
11
Working with Arrays
  • Why do I love arrays?
  • Because I love digital music, photos, and movies
  • Digital music is stored as 1-D arrays an
  • Digital images are stored as 2-D arrays amn
  • Digital movies are stored as 3-D
    arrays amnt

12
Working with Arrays
  • Later on in this course we could use Matlab to
    start programming with sound and images
  • For now, well keep things simple with C
  • Examples
  • survey.c
  • dice.c
Write a Comment
User Comments (0)
About PowerShow.com