Hi! Im trying to make a program where I fill an array with elements from my defined class cars in cars.h /cars.cpp, and then sort the elements by using the selectionsort algoritm. Im guessing I have to replace my filling of the tab with random numbers with something like Cars<int>Cars[10]?

void selectionSort(int *tab, int antall){
	int indexLargestSoFar;
	int temp;
for(int i=antall-1; i>=1; --i)
	{
		indexLargestSoFar=0;
		for(int j=1;j<=i;++j)
		{
			if(tab[j]>tab[indexLargestSoFar]){
				
				indexLargestSoFar = j;
			}
		}
		temp=tab[i];
		tab[i]=tab[indexLargestSoFar];
		tab[indexLargestSoFar]=temp;
	}
}



void main(){
	
	const int tab_length = 10;
	int tab[tab_length];
	srand((unsigned int)time(NULL));

	cout << "Filling the tab with 10 elements" << endl;
	for(int i=0; i<tab_length;++i)
	{	
		tab[i]=abs(rand()%10);
//SUPPOSE THIS IS THE PLACE TO FILL IN......
	}
	for(int i = 0; i < 10; ++i)
	{
		cout << tab[i] << " " <<endl;
		
	}
	//cout << " " <<endl; 
	cout<<"Sorterer tabellen....." <<endl;
	selectionSort(tab,tab_length);
	
	for(int i = 0; i < 10; ++i)
	{
		cout << tab[i] << " " << endl;
	}

Hope someone could give me a clue what to do next..

Recommended Answers

All 7 Replies

>>give me a clue what to do next..
like replacing void main() with int main() ?

howcome? I thougth void main only reveals me from writing return 0; in the bottom? that wouldn't have any effect ..... . .. ..... .

>>howcome?
CLICK THE LINK. *Sigh*

>>howcome?
CLICK THE LINK. *Sigh*

sorry, Ididn't understand that:P but fine, I could always replace void main with int main. I usually use Int main anyway, but my program workes just fine, I just need some guidelines for how to fill the tab with elements from my class instead of random numbers like I have implemented it to do.. .

I have no ideas how to do that eiher because you didn't post the code for that class or how it is instantiated. Don't ask me to read a different thread because I won't. Each thread must be self-sufficient.

I have no ideas how to do that eiher because you didn't post the code for that class or how it is instantiated. Don't ask me to read a different thread because I won't. Each thread must be self-sufficient.

well sorry about that. I smply wanted a guideline, not a complete eksample therefore I didn't post the code for the class. mybe I was unpresice in my questioning.. sorry. So if you cant help, then thanks anyway;)

There is no one way to accomplish what you want to do. Each program is unique. Without knowing what cars.h looks like its impossible for anyone to tell you how to use an array of car classes for the data you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.