Lec05Arrays_6per.pdf - EECS 402 Further Updates To Sum...

Info icon This preview shows pages 1–2. Sign up to view the full content.

1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 05 Savitch Ch. 5 Arrays Multi-Dimensional Arrays EECS EECS 402 EECS 402 Andrew M Morgan 2 Consider This Program int i0, i1, i2; int sum; cout << "Enter int #1: "; cin >> i0; cout << "Enter int #2: "; cin >> i1; cout << "Enter int #3: "; cin >> i2; cout.setf(ios::right); cout << " " << setw(4) << i0 << endl; cout << "+ " << setw(4) << i1 << endl; cout << "+ " << setw(4) << i2 << endl; cout << " ----" << endl; sum = i0 + i1 + i2; cout << " " << setw(4) << sum << endl; Write a program to input 3 ints and output each value and their sum, formatted like a math problem Enter int #1: 54 Enter int #2: 102 Enter int #3: 7 54 + 102 + 7 ---- 163 EECS EECS EECS EECS 402 EECS 402 Andrew M Morgan 3 Update To Program #1 Your boss was so impressed, you are asked to update the program to work with 5 ints instead of 3 int i0, i1, i2, i3, i4; int sum; cout << "Enter int #1: "; cin >> i0; cout << "Enter int #2: "; cin >> i1; cout << "Enter int #3: "; cin >> i2; cout << "Enter int #4: "; cin >> i3; cout << "Enter int #5: "; cin >> i4; cout.setf(ios::right); cout << " " << setw(4) << i0 << endl; cout << "+ " << setw(4) << i1 << endl; cout << "+ " << setw(4) << i2 << endl; cout << "+ " << setw(4) << i3 << endl; cout << "+ " << setw(4) << i4 << endl; cout << " ----" << endl; sum = i0 + i1 + i2 + i3 + i4; cout << " " << setw(4) << sum << endl; Enter int #1: 50 Enter int #2: 30 Enter int #3: 108 Enter int #4: 1215 Enter int #5: 74 50 + 30 + 108 + 1215 + 74 ---- 1477 EECS 402 402 EECS 402 Andrew M Morgan 4 Further Updates To Sum Program The previous programs worked fine and solved the problem that was presented Changing from 3 to 5 ints was easy – lots of copy/paste operations Now you boss asks for a program that works on 100 ints Do you copy/paste 95 more inputs and outputs, update the variable names, and hope you did everything correctly? What if you are then requested to write one for 87 ints, and then 1000 ints, and then 743 ints, etc? EECS 402 EECS 402 Andrew M Morgan 5 Intro To Arrays Array: A list of variables, all of the same data type that can be accessed via a common name The length of an array (the number of elements in the list) can be of any fixed length Syntax for declaring an array: dataType arrayName[arrayLength]; dataType: Any available data type (int, float, user-defined types, etc) arrayName: The name of the array (i.e. the common name used to access any variable in the list) arrayLength: The number of elements that can be accessed via this array Example: int quizGrades[10]; Declares an array of 10 integer elements, with the name "quizGrades" EECS EECS 402 EECS 402 Andrew M Morgan 6 More Info On Arrays Individual elements of the array are accessed by "indexing" To index into an array, use the square brackets In C/C++ array indices start at 0, and end at (length – 1) Example: quizGrades[4] accesses the fifth element of the array [0] would be the first, [1] the second, [2] the third, [3] the fourth, etc. "quizGrades" is an array, but "quizGrades[4]" is an int, and can be used anywhere an int variable can be used If an int variable requires 4 bytes of memory, then the declaration: int quizGrades[10]; sets aside 40 bytes (10 ints at 4 bytes each) of memory Elements can be accessed using the following: quizGrades[0], quizGrades[1], quizGrades[2], quizGrades[3], quizGrades[4],
Image of page 1

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

Image of page 2
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