Docsity

C++ Programming: Understanding Arrays, Slides of Introduction to Computing

Partial preview of the text

Download C++ Programming: Understanding Arrays and more Slides Introduction to Computing in PDF only on Docsity!

Programming Fundamentals

Lecture-

Arrays

Today’s agenda

• Array concept

• Declaring an array

• Accessing array elements

  • short array
  • Different indexing

• Bound checking in C++

• Array initialization

  • Full
  • Partial

• Processing array contents

• Multi-array operations

• Printing array contents

7.1 Arrays Hold Multiple values

  • Unlike regular variables, arrays can hold

multiple values.

Figure 7-

int count Enough memory for 1 int

12345

float price Enough memory for 1 float

char letter Enough memory for 1 char

A

Declaring an array

• To declare an array in C++, the programmer

specifies the type of the elements and the

number of elements required by an array as

type arrayName [ arraySize ];

• This is called a single-dimension array.

• The arraySize must be an integer constant

greater than zero and type can be any valid

C++ data type.

Program 7-

// This program asks the user for the number of hours worked // by 6 employees. It uses a 6-element int array to store the // values.

#include <iostream.h>

void main(void) { short hours[6];

cout << "Enter the hours worked by six employees: "; cin >> hours[0]; cin >> hours[1]; cin >> hours[2]; cin >> hours[3];

Program continues

cin >> hours[4];
cin >> hours[5];
cout << "The hours you entered are:";
cout << " " << hours[0];
cout << " " << hours[1];
cout << " " << hours[2];
cout << " " << hours[3];
cout << " " << hours[4];
cout << " " << hours[5] << endl;
  • Figure 7-
  • Table 7-
  • Figure 7-

Program 7-

// This program asks the user for the number of hours worked // by 6 employees. It uses a 6-element short array to store the // values. #include <iostream.h>

void main(void) { short hours[6];

cout << "Enter the hours worked by six employees: "; for (int count = 0; count < 6; count++) cin >> hours[count]; cout << "The hours you entered are:"; for (count = 0; count < 6; count++) cout << " " << hours[count]; cout << endl; }

Program 7-

// This program asks the user for the number of hours worked // by 6 employees. It uses a 6-element short array to store the // values. #include<iostream.h>

void main(void) { short hours[6];

cout << "Enter the hours worked by six employees.\n"; for (int count = 1; count <= 6; count++) { cout << "Employee " << count << ": "; cin >> hours[count - 1]; } cout << "The hours you entered are\n";

Program continues

for (count = 1; count <= 6; count++)
cout << "Employee " << count << ": ";
cout << hours[count - 1] << endl;

7.3 No Bounds Checking in C++

  • C++ gives you the freedom to store data past

an array’s boundaries.

Program 7-

// This program unsafely accesses an area of memory by writing // values beyond an array's boundary. // WARNING: If you compile and run this program, it could cause // the computer to crash. #include <iostream.h>

void main(void) { short values[3]; // An array of 3 short integers.

cout << "I will store 5 numbers in a 3 element array!\n"; for (int count = 0; count < 5; count++) values[count] = 100; cout << "If you see this message, it means the computer\n"; cout << "has not crashed! Here are the numbers:\n"; for (int count = 0; count < 5; count++) cout << values[count] << endl; }