CSE100_final_arrays_more_page1_pdf

CSE100_final_arrays_more_page1_pdf - R ECURSIVE SAMPLE: v...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 8 (Arrays) Arrays Size Index Length Arrays of Class Objects Elements of array can be class objects subscript must be used with objects multiple values of the same data type store together in consecutive memory location Initialization Entire Array Declaration Tracing code with array Implement functions using arrays To pass array as argument to function pass the name of array pass array as parameters Manipulations with arrays Processing pay=hours[3]*rate; ++score [2]; score [3]++; if (cost [20] <cost [0]) while (value [place] !=0) Copy one array to another // This program illustrates how to copy one array to another. #include <iostream> using namespace std; int main() { const int SIZE = 6; int arrayA[SIZE] = {10, 20, 30, 40, 50, 60}; int arrayB[SIZE] = { 2, 4, 6, 8, 10, 12}; // Copy the elements of arrayB to arrayA for (int index = 0; index < SIZE; index++) arrayA[index] = arrayB[index]; // Display the contents of the two arrays cout << "ArrayB still holds the values: "; for (int index = 0; index < SIZE; index++) cout << arrayB[index] << " "; cout << "\nArrayA now also holds the values: "; for (int index = 0; index < SIZE; index++) cout << arrayA[index] << " "; cout << endl; return 0; } Comparing Two Arrays // This program illustrates how to correctly compare two arrays. #include <iostream> using namespace std; int main() { const int SIZE = 5; int arrayA[SIZE] = { 5, 10, 15, 20, 25 }; int arrayB[SIZE] = { 5, 10, 15, 20, 25 }; bool arraysEqual = true; // Flag variable int count = 0; // Loop counter variable // Determine whether the elements contain the same data { if (arrayA[count] != arrayB[count]) arraysEqual = false; count++; } // Display the appropriate message if (arraysEqual) cout << "The arrays are equal.\n"; else cout << "The arrays are not equal.\n"; return 0; } Sum, Average, Highest, Lowest // This program uses an array to store monthly sales figures // for a company's regional offices. It then finds and displays // the total, average, highest, and lowest sales amounts. // The data to fill the array is read in from a file. #include <iostream> #include <fstream> // Needed to use files #include <iomanip> using namespace std; int main() { const int NUM_OFFICES = 12; ifstream dataIn; int office; // Loop counter double sales[NUM_OFFICES], // Array to hold the sales data totalSales = 0.0, // Accumulator initialized to zero averageSales, highestSales, lowestSales; // Open the data file dataIn.open("sales.dat"); if (!dataIn) cout << "Error opening data file.\n"; else { // Fill the array with data from the file for (office = 0; office < NUM_OFFICES; office++) dataIn >> sales[office]; dataIn.close(); // Sum all the array elements for (office = 0; office < NUM_OFFICES; office++) totalSales += sales[office]; // Calculate average sales averageSales = totalSales / NUM_OFFICES; // Find highest and lowest sales amounts highestSales = lowestSales = sales[0]; for (office = 1; office < NUM_OFFICES; office++) { if (sales[office] > highestSales)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/09/2010 for the course CSE 100 taught by Professor Na during the Fall '09 term at Arizona.

Ask a homework question - tutors are online