Sorting, Arrays, linkedlists

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.

[/code]

Coursework.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#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;
}*/


Main.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Header.h"
using namespace 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;
}
Looks like you meant

 
if( randomClimbed == 1 )


on line 56, not

 
if( randomClimbed = 1 )


the latter of which always sets randomClimbed to 1 and is always true.
Thanks very much jsmith, gentleman. Very simple, and because of it's simplicity I was so confused. cheers again mate.
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.
Topic archived. No new replies allowed.