Lecture7 - ECE 3090 Software for Engineering Systems Prof....

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

View Full Document Right Arrow Icon
ECE 3090 Software for Engineering Systems Prof. Hongwei Wu School of Electrical and Computer Engineering Georgia Institute of Technology (Savannah)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Arrays ± Array declaration (definition) ± Memory requirements of arrays ± Accessing array elements ± Applications: Reading data from a file into an array ± Array boundaries and overflow ± Array initialization ± Processing array elements ± Parallel arrays ± Arrays as function arguments
Background image of page 2
Array Declaration ± An array is a variable that can store a group of values of the same type. ± Array Declaration ² Data type: float, char, long, double, etc. ² Name of the array ² Size declarator: constant integer number > 0 ± Literal: int days[6] ± Named constant: const int NUM_DAYS = 6; int days[NUM_DAYS]; int days [6 ] type name size declarator
Background image of page 3

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

View Full DocumentRight Arrow Icon
Memory Requirement of Arrays ± Values of array elements are stored in consecutive memory locations. ± Amount of memory used by an array depends on the array’s data type and the number of elements Element 0 Element 1 Element 2 Element 3 Element 4 Element 5
Background image of page 4
Accessing Array Elements ± Each element of an array can be accessed and used as an individual variable through the array name and subscript of the element . days[3] = 30; ± Subscript numbering in C++ always starts at zero. ± The subscript of the last element of an array is one less than the total number of elements in the array.
Background image of page 5

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

View Full DocumentRight Arrow Icon
± Size declarator of an array must be a constant or literal. ± Subscript numbers can be stored in variables. #include <iostream> using namespace std; int main() { const int NUM_EMPLOYEES = 6; int hours[NUM_EMPLOYEES]; int count; // INput the hours worked for (count = 0; count < NUM_EMPLOYEES; count ++) { cout << "Enter the hours worked by employee "; cout << (count + 1) << ": "; cin >> hours[count]; } cout << "The hours your entered are:"; for (count = 0; count < NUM_EMPLOYEES; count ++) { cout << " " << hours[count]; } cout << endl; return 0; } for (count = 0; count < NUM_EMPLOYEES; count ++) The variable count starts at 0, which is the first valid subscript value The loop ends when count reaches the first invalid subscript value cout is incremented after each iteration
Background image of page 6
± Inputting data into an array must normally be done one element at-a-time cin >> hours; // Wrong! ± Outputting an array’s contents must normally be done one element at-a-time. cout << hours; // Wrong! ± Character arrays are exception char fName[] = "Henry"; cout << fName << endl; char firstName[20]; cout << “First name: ”; cin >> firstName;
Background image of page 7

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

View Full DocumentRight Arrow Icon
Application: Reading Data from a File into an Array 1. #include <iostream> 2. #include <fstream> 3. using namespace std; 4. 5.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 39

Lecture7 - ECE 3090 Software for Engineering Systems Prof....

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

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