C++ Exercise 6.9 . Given the following definition of array list1: int list1[5] =
ID: 654400 • Letter: C
Question
C++
Exercise 6.9 .
Given the following definition of array list1: int list1[5] = {10, 20, 30, 40, 50};
Show the output of each of the following program segments:
a.for(int j = 0 ; j < 5 ; j ++ )
cout << endl << (list1[j] + j );
b.for (int j = 4 ; j >= 0 ; j - - )
cout << endl << list1[j];
2. Given the following definition of array list2: double list2[10];
a. write a program segment to read values into array list2.
b. write a program segment to compute the average of the elements of array list2 and print it
Explanation / Answer
import java.util.*;
public class SortedLinkedList<E extends Comparable<? super E>> extends
LinkedList<E> {
public void mergeLists(SortedLinkedList<E> list1, SortedLinkedList<E> list2) {
// List Iterator for the first list
ListIterator<E> itr1 = list1.listIterator();
// List Iterator for the second list
ListIterator<E> itr2 = list2.listIterator();
E item1 = null; // variable to first node in first list
E item2 = null; // variable to first node in second list
if (itr1.hasNext())
item1 = itr1.next();
if (itr2.hasNext())
item2 = itr2.next();
while (item1 != null && item2 != null) {
if (item1.compareTo(item2) < 0) {
addLast(item1);
if (itr1.hasNext())
item1 = itr1.next();
else {
addLast(item2);
break;
}
} else {
addLast(item2);
if (itr2.hasNext())
item2 = itr2.next();
else {
addLast(item1);
break;
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.