Write a program that inserts 25 random integers from 0 to 100 in order in a linked list ob- ject. The program should calculate the sum of the elements and the floating-point average of the elements.

What will be an ideal response?

```
// List2 class template definition
// Enhances List by adding insertInOrder.
#ifndef LIST2_H
#define LIST2_H

#include "ListNode.h"
#include "List.h"

template< typename NODETYPE >
class List2 : public List< NODETYPE >
{
public:
void insertInOrder( const NODETYPE & );
}; // end class List2

// insert a node in order
template< typename NODETYPE >
void List2< NODETYPE >::insertInOrder( const NODETYPE &value )
{
if ( isEmpty() ) // list is empty
{
ListNode< NODETYPE > *newPtr = getNewNode( value );
firstPtr = lastPtr = newPtr;
} // end if
else // list is not empty
{
if ( firstPtr->getData() > value ) // value is the smallest
insertAtFront( value );
else if ( lastPtr->getData() < value ) // value is the largest
insertAtBack( value );
else
{
ListNode< NODETYPE > *currentPtr = firstPtr->getNextPtr();
ListNode< NODETYPE > *previousPtr = firstPtr;
ListNode< NODETYPE > *newPtr = getNewNode( value );

while ( currentPtr != lastPtr && currentPtr->getData() < value )
{
previousPtr = currentPtr;
currentPtr = currentPtr->getNextPtr();
} // end while

previousPtr->setNextPtr( newPtr );
newPtr->setNextPtr( currentPtr );
} // end else
} // end else
} // end function insertInOrder

#endif
```
```
#include
#include
#include
using namespace std;

#include "List2.h"

// integer specific list sum
int sumList( List2< int > &listRef )
{
List2< int > temp( listRef );
int sum = 0;
int value;

// until temp is empty
while ( !temp.isEmpty() )
{
temp.removeFromFront( value ); // remove from the front
sum += value; // add value to sum
} // end while

return sum;
} // end function sumList

// integer specific list average
double aveList( List2< int > &listRef )
{
List2< int > temp( listRef );
int sum = 0;
int value;
int count = 0;

// go through copy of listRef
while ( !temp.isEmpty() )
{
temp.removeFromFront( value ); // remove each element
count++; // increment the count
sum += value; // add into sum
} // end while

// return the average
return static_cast< double >( sum ) / count;
} // end function aveList

int main()
{
srand( time( 0 ) ); // randomize the random number generator

List2< int > intList;

// fill intList with 25 random numbers
for ( int i = 1; i <= 25; i++ )
intList.insertInOrder( rand() % 101 );

intList.print();

int sum = sumList( intList ); // calculate sum
double average = aveList( intList ); // calculate average

cout << "The sum of the elements is: " << sum << '\n';
cout << "The average of the elements is: " << average << '\n';
return 0; // indicates successful termination
} // end main
```
The list is: 2 4 14 23 31 33 33 36 43 52 53 56 56 67 73 76 81 81 82 83 90 93
95 98 98
All nodes destroyed
All nodes destroyed
The sum of the elements is: 1453
The average of the elements is: 58.12
Destroying nodes ...
2 4 14 23 31 33 33 36 43 52 53 56 56 67 73 76 81 81 82 83 90 93 95 98 98
All nodes destroyed

Computer Science & Information Technology

You might also like to view...

The IT department has rejected the password you submitted for approval to join the network. Your suggestion was "bobbie." Why did IT reject your choice?

a. Someone else is already using "bobbie." b. The word does not meet your company's policy for choice of passwords. c. It will be confusing because it could be written down as "Bobby" by mistake. d. Because everyone knows you have a dog called Bobbie, so the password is too predictable.

Computer Science & Information Technology

Antivirus (AV) software will help protect a computer from hackers.

a. true b. false

Computer Science & Information Technology