C++ - ArraysinC+(Chapter12) onethingafteranother Problem

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Arrays in C++  (Chapter 12) one thing after another
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Problem Given 5 numbers, read them in and  calculate their average THEN print out the ones that were  above average
Background image of page 2
Data Structure Needed Need some way to hold onto all the  individual data items after processing  them making individual identifiers x1, x2,  x3,. .. is not practical or flexible the answer is to use an ARRAY a data structure - bigger than an  individual variable or constant
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
an Array you need a way to have many variables  all with the same name but  distinguishable! in math they do it by subscripts or  indexes x 1 , x 2 , x and so on in programming languages, hard to use  smaller fonts, so use a different syntax x [1], x[0], table[3], point[i]
Background image of page 4
Semantics numbered from 0 to n-1 where n is the  number of elements all elements of an array have the same  type     0     1      2       3      4     5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Properties of an array Homogeneous Contiguous Have random access to any element Ordered (numbered from 0 to n-1) Number of elements does not change -  MUST be a constant when declared
Background image of page 6
Syntax Declaration   "type name[howmany];" refer to individual elements uses [ ] with index in the brackets most of the time cannot refer to the  whole array as one thing, or just by the  array name (only time you can is when  passing a whole array to a function as  an argument)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
  8 Declaration of an Array The index is also called the  subscript  In C++, the first array element always has  subscript 0,  the second array element has  subscript 1, etc. The  base address  of an array is its beginning  address in memory 
Background image of page 8
Using a named constant it is very common to use a named  constant to set the size of an array const int SIZE = 15; int arr[SIZE]; useful because it can be used to control  loops throughout program easy to change if size of array needs to  be changed 
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Solution to problem int ct = 0, n[5], total = 0; float average; while (ct < 5) { cout << "enter a number "; cin >> n[ct]; total = total + n[ct]; ct = ct + 1; }  
Background image of page 10
Solution to problem - cont'd average = total / 5; ct = 0; while (ct < 5) { if (n[ct] > average)   cout << n[ct]; ct = ct + 1;    }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
the for loop since you need a loop with a counter often  with an array, use a specialized one syntax   for (initialization; condition; increment)  body as usual, body can be one statement or a  block of statements initialization usually sets a counter to zero condition tests counter for upper limit increment adds a value to counter
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2011 for the course CS 115 taught by Professor Staff during the Fall '08 term at Kentucky.

Page1 / 45

C++ - ArraysinC+(Chapter12) onethingafteranother Problem

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online