I've been given a homework assignment in which I have to create a linked list, then store 25 random integers within it, after I've stored the values I have to find the sum of these 25 numbers, and then the floating point average of them. So for the most part this works. It stores the values into the list, but the member function I've created called "DataAt(int)" returning type of 'int' is only returning a value of 34 everytime. This is not what I need to assign the current sum and average outputs are:
sum = 850
avg = 34
So I need a little help with the matter because I don't seem to be able to find the problem; I've tried two different loops and and neither are wanting to work. The results should be the following based on the output from the items in the list:
sum = 1181
avg = 47.24
So anyone who can help with the matter is greatly appreciated. I've learned the main objective of the homework assignment, but I can't figure out what I'm doing wrong to retrieve data individually. For example in C# you can retrieve the data from a linked list at a specific location like you would with an array:
List<int> MyList;
int i = MyList[4]; // Valid
So I've been trying to figure it out since last night and can't seem to get it. Below is my source code and any help is appreciated in solving this matter.
// Main.cpp
#include <iostream>
#include "Random.h"
#include "LinkedList.h"
using namespace std;
using namespace LinkedList;
int main() {
Random r;
List<int> l;
int sum = 0;
float avg = 0;
for (int i = 0; i < 25; i++) {
cout << "Test " << i + 1 << ": " << r.Between(0, 100) << ", " << r.Solid() << "\n";
l.InsertAtFront(r.Between(0, 100));
sum = sum + l.DataAt(i);
}
avg = sum / 25;
cout << "\n\n";
l.Print();
cout << "Sum: " << sum << "\nAverage: " << avg << "\n";
cout << "Length of list: " << l.Length() << "\n";
system("pause");
}
// Random.h
#include <stdio.h>
#include <stdlib.h>
class Random {
public:
int Between(int min, int max);
int Solid();
};
int Random::Between(int min, int max) { return rand() % max + min; }
int Random::Solid() { return rand(); }
// LinkedList.h
#include <iostream>
namespace LinkedList {
template<typename T> class List;
template<typename T>
class Node {
friend class List<T>;
public:
Node(const T &);
T GetData() const;
private:
T Data;
Node<T> *NextPointer;
};
template<typename T>
Node<T>::Node(const T &info) : Data(info), NextPointer(0) {
}
template<typename T>
T Node<T>::GetData() const {
return Data;
}
template<typename T>
class List {
public:
List();
~List();
void InsertAtFront(const T &);
void Print() const;
bool IsEmpty() const;
int DataAt(int Index);
int Length();
private:
Node<T> *FirstPointer;
Node<T> *LastPointer;
Node<T> *GetNewNode(const T &);
};
template<typename T>
List<T>::List() : FirstPointer(0), LastPointer(0) { }
template<typename T>
List<T>::~List() {
if (~IsEmpty()) {
cout << "Destroying nodes...\n";
Node<T> *CurrentPointer = FirstPointer;
Node<T> *TemporaryPointer;
while (CurrentPointer != 0) {
TemporaryPointer = CurrentPointer;
cout << TemporaryPointer->Data << "\n";
CurrentPointer = CurrentPointer->NextPointer;
delete TemporaryPointer;
}
}
cout << "All nodes destroyed...\n\n";
}
template<typename T>
void List<T>::InsertAtFront(const T &value) {
Node<T> * NewPointer = GetNewNode(value);
if (IsEmpty())
FirstPointer = LastPointer = NewPointer;
else {
NewPointer->NextPointer = FirstPointer;
FirstPointer = NewPointer;
}
}
template<typename T>
bool List<T>::IsEmpty() const { return FirstPointer == 0; }
template<typename T>
Node<T> *List<T>::GetNewNode(const T &value) { return new Node<T>(value); }
template<typename T>
void List<T>::Print() const {
if (IsEmpty()) {
cout << "The list is empty...\n\n";
return;
}
Node<T> *CurrentPointer = FirstPointer;
cout << "The list is: ";
while(CurrentPointer != 0) {
cout << CurrentPointer->Data << " ";
CurrentPointer = CurrentPointer->NextPointer;
}
cout << "\n\n";
}
template<typename T>
int List<T>::DataAt(int Index) {
Node<T> *CurrentPointer = FirstPointer;
for (int i = 0; i < Index; i++)
CurrentPointer = CurrentPointer->NextPointer;
return CurrentPointer->Data;
}
template<typename T>
int List<T>::Length() {
int i = 0;
Node<T> *CurrentPointer = FirstPointer;
while (CurrentPointer != LastPointer) {
CurrentPointer = CurrentPointer->NextPointer;
i++;
}
if (i == 0)
return i;
return i + 1;
}
}
int i = MyList[4]; // Valid
Here is my source code and maybe someone here can help me: