CSE100_final_arrays_more_page1_pdf

CSE100_final_arrays_more_page1_pdf - R ECURSIVE SAMPLE v...

Info icon This 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 while (arraysEqual && count < SIZE) { 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
Image of page 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern