I have a small problem with my code, I need to make the 'Climbed' output be random.
I have already tried to put some code in my function to do this but it doesn't seem to work.
It's in my coursework.cpp file under the 'insertNodeAtEnd' function where the comments are //THIS HAS BEEN CHANGED
Anyone any ideas??? Much appreciated if so.
#include "Header.h"
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <stdio.h>
LinkedList::LinkedList() //constructor
{
head = NULL;
last = NULL;
count = 0;
}
void LinkedList::addAnotherHill()
{
while (head == NULL)
{
Node *nextNode;
int randomheight;
int randomdistanceFromHome;
int randomgridReference;
nextNode = new Node; //new node
randomheight = rand()%1400 + 3000; //passed via the parameter
nextNode->height = randomheight;
randomdistanceFromHome = rand()%1400 + 3000;
nextNode->distanceFromHome = randomdistanceFromHome;
nextNode->climbed = 0;
randomgridReference = rand()%100000 + 400000;
nextNode->gridReference = randomgridReference;
count++; //we’ve added a node, so add 1 to count
nextNode->hillNumber = count;
nextNode->link = head;
head = nextNode;
}
insertNodeAtEnd();
}
void LinkedList::insertNodeAtEnd()
{
int randomheight;
int randomdistanceFromHome;
int randomgridReference;
Node *nextNode, *tempNode; //declare and create new
nextNode = new Node; //new node called nextNode
randomheight = rand() %1400 + 3000;
nextNode->height = randomheight;
randomdistanceFromHome = rand() %1400 + 300 ;
nextNode->distanceFromHome = randomdistanceFromHome;
randomgridReference = rand()%100000 + 400000;
nextNode->gridReference = randomgridReference;
int randomClimbed = rand() %2;
if (randomClimbed=1)
{
nextNode->climbed = 0; /////THIS HAS BEEN CHANGED
}
else
{
nextNode->climbed = 1;
}
nextNode->link = NULL; // give the node’s link field the value currently in tail (last)
//i.e. the new node now points to the old first node
last = nextNode; //and the tail (last) now points to the new node
count++; //we’ve added a node, so add 1 to count
nextNode->hillNumber = count;
tempNode = head;
while (tempNode->link !=NULL)//while tempNode's link field is not NULL
{ //give tempNode's link field the value currently
tempNode = tempNode->link; //in the parameter tempNode
} //end while loop
tempNode->link=nextNode; //give tempNode's link field the value currently in
//nextNode
}
int LinkedList::getNodeCount()
{
return count;
}
Node* LinkedList::getFirst()
{
return head;
}
void LinkedList::deleteFirstNode()
{
if(count > 0)
{
Node *temp = head;//Declare a pointer to a Node and give it the value
//of the first Node (the one pointed to by head).
head = head->link; //Give head the value of the second Node (i.e.
// the one pointed to by the first Node).
delete temp; //Delete the original first Node.
count--;
}
else
{
cout<<"There are no nodes to delete."<<endl;
}
}
void LinkedList::displayHills()
{
Node *current; //Pointer to Current
current = getFirst();
print(current);
cout<<""<<endl;
char nextPhase; cout<<"Preparing to pass values into an Array...Press any key to continue" ;cin.get(nextPhase);
// bSort(current);
}
void LinkedList::populatearray(ArrayList Array[])
{
Node * ptr = head;
for (int n=0; n<40; n++)
{
Array[n].height = ptr->height;
Array[n].distanceFromHome = ptr->distanceFromHome;
Array[n].climbed = ptr->climbed;
Array[n].gridReference = ptr->gridReference;
ptr = ptr->link;
}
}
void LinkedList::print(Node* aNode)
{
for (int i=0; i<40; i++)
{
cout <<"*************"<< endl;
cout <<"Height \t\t\t"<<aNode->height<< endl;
cout <<"Distance from home \t"<<aNode->distanceFromHome<< endl;
cout <<"Climbed? \t\t"<<aNode->climbed<<endl;
cout <<"Grid Reference \t\t"<<aNode->gridReference<<endl;
aNode = aNode->link;
}
}
void LinkedList::print()
{
Node *current = head;
for (int i=0; i<40; i++)
{
cout <<"Height \t\t\t"<<current->height<< endl;
cout <<"Distance from home \t"<<current->distanceFromHome<< endl;
cout <<"Climbed? \t\t"<<current->climbed<<endl;
cout <<"Grid Reference \t\t"<<current->gridReference<<endl;
current = current->link;
}
}
void LinkedList::swapfunction(Node * ptr)
{
int temp = 0;
temp = ptr->height;
ptr->height = ptr->link->height;
ptr->link->height = temp;
}
//function to swap two items in the array
void LinkedList::swap(int value1Index, int value2Index)
{
int temp;
temp = anArray[value1Index];
anArray[value1Index] = anArray[value2Index];
anArray[value1Index] = temp;
}
//function that adds an item to the array
/*void LinkedList::insert( int aValue)
{
anArray[noOfItemsInArray] = aValue;
noOfItemsInArray++;
}*/
//function to display the items in the array
/*void LinkedList::display()
{
for(int loop = 0; loop < noOfItemsInArray; loop++)
{
cout << anArray[loop] << " ";
}
cout << endl;
}*/
/*int LinkedList::partition(int value1Index, int value2Index)
{
int pivot;
int smallIndex;
pivot = anArray[value1Index];
smallIndex = value1Index;
for(int index = value1Index+1; index<=value2Index; index++)
{
if(anArray[index] < pivot)
{
smallIndex++;
swap(smallIndex, index);
}
}
swap(value1Index, smallIndex);
return smallIndex;
}*/
#include "Header.h"
usingnamespace std;
ArrayList bSort(ArrayList anArray[])
{
int noOfItemsInArray = 40;
ArrayList temp;
for( int i = 1; i<noOfItemsInArray; i++)
{
//We start j at the first element in the array - 0 -
//and end j 2 short of the number of elements, otherwise in the
//last loop anArray[j+1] would be undefined.
for( int j = 0; j<(noOfItemsInArray-1); j++)
{
if(anArray[j].height > anArray[j+1].height)
{
temp = anArray[j]; //swap the values
anArray[j] = anArray[j+1];
anArray[j+1] = temp;
}
}
}
return temp;
}
void printArray(ArrayList print[])
{
for (int n=0; n<40; n++)
{
cout <<"*************"<< endl;
cout <<"Height \t\t\t"<<print[n].height<< endl;
cout <<"Distance from home \t"<<print[n].distanceFromHome<< endl;
cout <<"Climbed? \t\t"<<print[n].climbed<<endl;
cout <<"Grid Reference \t\t"<<print[n].gridReference<<endl;
}
cout <<""<<endl;
cout <<"Values in Array have been sorted using the Bubble Sort Method."<<endl;
cout <<"The values are now in ascending order of their height parameter."<<endl;
}
int main()
{
LinkedList hills; //new LinkedList called hills
ArrayList hillArray [40];
for (int i=0; i<40; i++)
{
hills.addAnotherHill();
}
hills.displayHills();
hills.populatearray(hillArray);
cout << hillArray[0].climbed << endl;
//hillArray = hills.populatearray();
bSort(hillArray);
//hillArray() = hills.bSort(hillArray);
printArray(hillArray);
return 0;
}
Thanks antoin for posting this helpful code. It helped me better understand linked lists and class information hiding.
And thanks jsmith for your solution to the problem.