Unit 5_Arrays1.ppt - Unit 5 Week 8 Data Structures Introduction to Arrays Unit Objective Introduction to Arrays Declaring and Referencing Arrays Arrays

Unit 5_Arrays1.ppt - Unit 5 Week 8 Data Structures...

This preview shows page 1 - 11 out of 39 pages.

Unit 5 Unit 5 Week 8Week 8Data StructuresData StructuresIntroduction to Arrays Introduction to Arrays
Background image
Unit ObjectiveIntroduction to ArraysDeclaring and Referencing ArraysArrays in MemoryInitializing ArraysArrays in FunctionsIndexed Variables as Function ArgumentsEntire Arrays as Function ArgumentsThe constParameter ModifierProgramming with ArraysPartially Filled Arrays
Background image
5.1One-dimensional ArrayOne-dimensional array is a list of related values with the same data type that is stored using a single group name.
Background image
5.1.1Declaring Arrays int temp [7]; will store up to seven integer temperaturetemp [0] = 85; will store 85 in the first element of array temp. Similarly temp [6] = 90; will store 90 in the seventh element of array temp.float temp [7] will store up to seven floating point temperature values in array temp. double salary [100] will store up to 100 double floating point numbers in array salary. temp [0] temp [1] temp [2] …………….temp [6]
Background image
ExampleWrite a program to prompt the user to enter 5 test grades and store them into a one-dimensional array and then find the average and print the result.#include <iostream>using namespace std;int main ( ){const int MAXG=5;int grade [MAXG];float total;for (int i = 0; i < MAXG; i++){cout << ”Enter a grade:”;cin >> grade [ i ];}cout << ”\n The Total of the grades”;for (int i=0; i<MAXG; i++){cout << ” “ << grade[i];total += grade [i];}cout << ”is” << total << endl;cout << ”The average of 5 grades = ” << total/MAXG << endl;return 0;}
Background image
Example (Cont..)The output of the above program is:Enter a grade: 88Enter a grade: 44Enter a grade: 55Enter a grade: 88Enter a grade: 56The total of grades 88 44 55 88 56 is 331The average of 5 grades = 66.199997
Background image
5.1.2 Declaring an Array of Characterschar name [10];This will create an array of 10 elements and you can store up to 9 character into this array.The last character, which is the escape sequence \0, is called the NULL character. The NULL character is automatically appended to all strings by the C++ compiler. It marks the end of a string.H e llo\0
Background image
5.1.3Initializing Arrays Array elements can be initialized within their declaration statements in the same manner as for regular variable, except that elements must be included in braces. For example, int grade [5] = { 78, 98, 100, 83, 93};double average [3] = { 93.5, 88.75, 90.00};char codes [7] = { ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’,'\0'};
Background image
5.1.3 (Cont..)The value of: grade [3] = 83average [2] = 90.00codes [1] = ‘a’Initializing values may extend across multiple lines as follows:int temp [8] = { 12, 19, 20,16, 33, 45,21, 50};
Background image
5.1.3 (Cont..)If the numbers filled in the array is less than the dimension of the array, they will be initialized to zero.
Background image
Image of page 11

You've reached the end of your free preview.

Want to read all 39 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture