12 if marksi 80 marksi 90 count count 1 It is also possible to check how the

12 if marksi 80 marksi 90 count count 1 it is also

This preview shows page 12 - 14 out of 15 pages.

12 if ((marks[i] >= 80) && (marks[i] <= 90)) { count = count + 1; } It is also possible to check how the elements of the array are related to each other. For example, we can find out how many adjacent elements in the marks array differ by more than 20 using the following code: for (i=0; i<9; i=i+1) { if ((marks[i+1] - marks[i] >= 20) || (marks[i] - marks[i+1] >= 20)) { cout << marks[i+1] << " and " << marks[i] << endl; } } For the values shown in Figure 9.2, the following output will be displayed: 65 and 87 56 and 81 In an array, if marks[i] is a valid element (i.e., i is within the bounds of the array), then marks[i-1] is the element immediately to the left of i . Similarly, marks[i+1] is the element immediately to the right of i . If i is 0, there is no left element. If i is 9, there is no right element. It should be observed that the condition in the second part of the for loop was changed from (i<10) to (i<9) . This is because of the use of the i + 1 subscript inside the for loop. Since i + 1 will eventually be equal to the highest location in the array (which is 9) it is important that i should be less than 9. 10.5.5 Copying Array Elements The elements of an array can be easily copied to another array using a for loop. For example, suppose that second is another integer array declared as follows: int second [10]; The values of the marks array can be copied to this new array as follows: for (i=0; i<10; i=i+1) { second[i] = marks[i]; } The values can be copied in reverse order by changing the subscripts as follows:
Image of page 12
13 for (i=0; i<10; i=i+1) { second[9-i] = marks[i]; } Note that when i is 0, 9 – i is 9, and when i is 1, 9 – i is 8, and so on. Thus, the value in location 0 in the marks array becomes the value in location 9 in the second array. Similarly, the value in location 1 in the marks array becomes the value in location 8 in the second array. This happens until all the elements are copied from the marks array to second ; at this point in time, the elements in second are the same as the elements in the marks array, except that they are in the reverse order. 10.6 Arrays that are not Completely Filled So far, we have been assuming that the marks array is completely filled with 10 elements. Consequently, whenever we need to traverse the array, we start at location 0 and stop at location 9. However, an array may not always be filled to capacity and there may be empty locations. If the array is not filled to capacity, the array processing code described in this lecture will not work since the code assumes that the array is completely filled. When working with arrays, it is customary to use a special integer variable to keep track of the amount of elements actually stored in an array. This number will usually be different from the size of the array. For the marks array, this variable could be numMarks . Suppose inputFile is the ifstream object used to connect to a file, marks.txt , containing a set of marks.
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 15 pages?

  • Fall '17
  • Mates

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes