hey guys,

Im doing an assignment for tech... the tutor is somewhat useless

I need to make an array that can loop 10 random numbers and then display them, pass that to a function and then have that display all the numbers in ascending order using for, while and if condititional statements...

I've got the first part its the last bit Im having a bit of a fuss with...

heres my code:

#include <iostream>
#include <conio>
#include<stdio.h>
#pragma hdrstop
# include<cstdlib>
void printAsc(int[]);
using namespace std;
//---------------------------------------------------------------------------

#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
	srand( time(0) );
	int randmNum[10];
	int i = 0;

	for (i = 0; i < 10; i++)
	{
		randmNum[i] = rand()%100+1;
		cout << "Random Number "  << i+1 << ": "
		<< " - " << randmNum[i] << endl;
	}
	getch();
	clrscr();

	printAsc(randmNum);
	getch();




	return 0;
}
//---------------------------------------------------------------------------
void printAsc(int randNum[10])
{
	int smallest;
	int previousSmallest;
	int previousLargest;
	int largest;

	smallest = randNum[0];
	largest = randNum[0];
	previousSmallest <= smallest;
	smallest <= largest;


	for (int i = 0; i < 10; i++)
	{

		if ( ( randNum[i] <= smallest ) )
		{
			previousSmallest = smallest;
			smallest = randNum[i];
			cout << smallest << endl;
		}
		if (randNum[i] >= largest)
		{
		previousLargest = largest;
		largest = randNum[i];
		cout << largest << endl;
		}
	}

	getch();
}

I just chucked this together in like 20 mins earlier today... hope its pretty readable

Is it a working code or a sample snippet.

commented: Go away spammer -1
commented: FU spammer -2

Is it a working code or a sample snippet.

its a working code all it does is displays the 4 variables in the void function instead of all numbers in ascending order

Just do a simple bubble sort. See http://www.cprogramming.com/tutorial.../sorting1.html for an example.

i dont think thats going to help me much because its just gonna keep overrideing y

previousSmallest <= smallest;
smallest <= largest;

These two lines are not doing anything (they compile but as statements it will only evaluate to true or false and have no effect).

I would still sort them in the function and just print it out with a loop. You're just printing out what is currently the smallest as you traverse the array.

previousSmallest <= smallest;
smallest <= largest;

These two lines are not doing anything (they compile but as statements it will only evaluate to true or false and have no effect).

I would still sort them in the function and just print it out with a loop. You're just printing out what is currently the smallest as you traverse the array.

yea thats the issue im having trouble with. The quoted text you highlighted was an example given by the lecturer... but as previously stated hes a bit useless... the problem with the loop area im having is that it print in duplicates so;
Example
68
68
46
46
22
22 etc

im trying to get it that previous smallest prints continuously while largest adn smallest print only once...

Just move line 63 outside of the loop if you want the largest to print only once. I still don't think that's going to get you what you need though. If you've already printed the number out and a smaller one comes along how will you account for that?
Honestly, you're making this difficult on yourself. The sort takes 5 lines and you won't need lines 53-63 anymore. If I were giving this assignment that's what I would be aiming for.

Just move line 63 outside of the loop if you want the largest to print only once. I still don't think that's going to get you what you need though. If you've already printed the number out and a smaller one comes along how will you account for that?
Honestly, you're making this difficult on yourself. The sort takes 5 lines and you won't need lines 53-63 anymore. If I were giving this assignment that's what I would be aiming for.

understandable... but how is that going to sort tho? i will try it tomorrow seeing as i cant run the dam application at home... the thing is tho it looks like it makes the previous statements null and void by over riding its previous adjustment... as in x = y, y = z, z =x.... that says that x=z without needing to ad y... unless i run some extra code before that it might work... but yea i will test it tomorrow and see if it works

Ok the issue with the bubble sort is we aren't allowed to code it that the array changes position or the numbers in the array so i've gotten to here:

void printAsc(int randNum[10])
{
	int largest;
	int smallest;
	int previousSmallest;
	int largerNum;

	largest = randNum[0];
	smallest = randNum[0];

	for (int i = 0; i < 10; i++)
	{
		if (randNum[i] <= smallest)
		{
			largerNum = previousSmallest;
			previousSmallest = smallest;
			smallest = randNum[i];
		}
		else if (randNum[i] >= smallest && randNum[i] <= previousSmallest)
		{
			largerNum = previousSmallest;
			previousSmallest = randNum[i];
		}
	}
	cout << "Smallest Number: " << smallest << endl;
	cout << "Larger Nums:" << endl;

	for (int i = 0; i < 10; i++)
	{
		if (randNum[i] >= previousSmallest && randNum[i] <= largerNum)
		{
			largerNum = randNum[i];
			cout << largerNum << endl;
		}
		else if (randNum[i] < largest)
		{
			largerNum = randNum[i];
			cout << largerNum << endl;
		}


	}

	for (int i = 0; i < 10; i++)
	{
		if (randNum[i] >= largest )
		{
			largest = randNum[i];
		}
	}

	cout << "Largest Number: " << largest << endl;

	getch();
}

basically the larger number is almost there i just need to try work it that it prints everything smaller than it but as long as its not printing smallest and largest

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.