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
You might also like to view...
Microsoft Excel is text and data processing software and is not suited for working with formulas.
Answer the following statement true (T) or false (F)
The Quick Selection Tool looks for a ________ in color and aligns the selection border to that contrast.
What will be an ideal response?
Over time, decision support systems, executive information systems, online analytical processing, business intelligence, and business analytics have gained in capabilities and expanded in scope to add new functionality, but all have had the same goal: deriving the most _____ from the data available.
Fill in the blank(s) with the appropriate word(s).
Virtually all CPUs today can process more than one piece of microcode at one time-a characteristic known as ____________________, which is the ability to process multiple instructions per cycle (IPC).
Fill in the blank(s) with the appropriate word(s).