Why is n an undeclared identifier

Started by
13 comments, last by rip-off 16 years, 8 months ago
Hi, thank you for your reply.

No, the program is not doing what I want it to do.
When I input an unsorted list of 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
It outputs the sorted list as 9, 10, 8, 7, 6, 5, 4, 3, 2, 1.

It seems that now the BubbleSort is not working as it should be, why is this please? (For code, see my last most recent post to this topic).

Thanks,
Chris.
Advertisement
Hi, thank you for your replies so far everyone.

After much experimenting, I've finally got my program working properly. I would now just like to clarify exactly what is going on and how it's working if that's OK. All this involves the BubbleSort function.

Initialise the variable subArrayEnd to define the end of the working sub array

Continue the loop while this is > 0 since at that point, the array will be sorted

On each pass of the for loop, for all elements in the working sub array, check if 1st > 2nd etc and if so, swap them using the integer temp or just leave them where they are.

By the lat pass of the for loop, everything is sorted and j = 0 so this makes subArrayEnd 0, which exits out of the while loop.

Here's my code:

#include <iostream>using namespace std;void BubbleSort(int data[10], int n);int main(){	int data[10];	int input = 0;	cout << "Enter ten unsorted integers: " << endl;	for(int i = 0; i < 10; ++i)	{		cout << "[" << i << "] = ";		cin >> input;		data = input;	}	cout << endl << "Unsorted List = ";	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data << ", ";		}		else		{			cout << data;		}	}	cout << endl << "Sorting..." << endl << "Sorted List = ";	BubbleSort(data, 10);	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data << ", ";		}		else		{			cout << data;		}	}	cout << endl << endl;}void BubbleSort(int data[10], int n){	int subArrayEnd = n - 1;	while( subArrayEnd > 0 )	{		int nextEnd = 0;		for(int j = 0; j < subArrayEnd; ++j)		{			if( data[j] > data[j+1] )			{				int temp = data[j];				data[j] = data[j+1];				data[j+1] = temp;				nextEnd = j;			}			else			{				data[j] = data[j];				data[j+1] = data[j+1];			}		}		subArrayEnd = nextEnd;	}}


Thanks,
Chris.
Quote:else
{
data[j] = data[j];
data[j+1] = data[j+1];
}

?!


EDIT: In other words, if the first element is larger than the second, swap them, but in any other case, make sure that the elements stay in place? Weren't they in the correct order then to start with? I think you understand what I mean: that else part is unnecessary, it doesn't do anything usefull.
Create-ivity - a game development blog Mouseover for more information.
Hi, thank you for your reply.

I've edited my code now to have an empty else statement, didn't realise you could have those, thanks!

Chris.
Quote:Original post by chockydavid1983
Hi, thank you for your reply.

I've edited my code now to have an empty else statement, didn't realise you could have those, thanks!

Chris.


You can drop the else completely if you want.

if( condition ) {   // stuff}// no else...

This topic is closed to new replies.

Advertisement