Sunday, August 2, 2009

C++: how do i sort numbers in descending order without using an array?

I have to sort 5 randomly generated numbers into descending (and ascending) order. The only problem is I havent actually learned arrays and are not really allowed to use them.


How would I make these numbers sort without the use of an array?

C++: how do i sort numbers in descending order without using an array?
If you have learned Linked list,then I can give you a solution:





#include%26lt;stdio.h%26gt;


#include%26lt;stdlib.h%26gt;





typedef struct node


{


int val;


struct node *next;


}*listptr;





listptr getnode (void);





void main()


{


int i,x;


listptr a,b,c;


b = getnode();


//generate the numbers randomly and put them in the list


randomize();


for(i = 0;i%26lt;5;i++)


{


x = rand();/*the function is r-a-n-d without the hyphens,don't know why it's coming as **** */


a = getnode();


a-%26gt;val = x;


a-%26gt;next = NULL;


if(i != 0) b-%26gt;next = a;


if(i == 0) c = a;


b = a;


}


b = c;


printf("\nOriginal list :-\n");


for(a = c;a != NULL;a = a-%26gt;next)


printf("%d ",a-%26gt;val);


//Now sort them


for(a = c;a-%26gt;next != NULL;a = a-%26gt;next)


for(b = a-%26gt;next;b != NULL;b = b-%26gt;next)


if(a-%26gt;val %26gt; b-%26gt;val) /*for ascending order '%26gt;',for descending order.'%26lt;' */


{


x = a-%26gt;val;


a-%26gt;val = b-%26gt;val;


b-%26gt;val = x;


}


printf("\nSorted list :-\n");


for(a = c;a != NULL;a = a-%26gt;next)


printf("%d ",a-%26gt;val);


}


listptr getnode (void)


{


listptr a;


a=(listptr)malloc(sizeof(struct node));





return(a);


}
Reply:You can use the functions that C++ has already supplied, which inherently uses arrays. You should probably let us know how you're storing them. Supposing they are just Ints being randomly generated, you can brute force it. Just compare the numbers to each other - there are only so many comparisons since you only have 5 numbers. However, I'm still not sure how you would store them since you can't use arrays.





EDIT: I am assuming you've got a unique variable for each randomly generated number.
Reply:Ooooo...


This is DIFFICULT. The best way to do this IS WITH ARRAYS. Since you don't know, you can learn:


An array is a pointer. Well, actually its name is a pointer. A declaration like:


int num[5];


Tells your compiler to reserve 5 ints in memory (consecutively, as you will see). When you say something like:


a=num[2];


It expands to:


a=*(num+2);


Which works because pointer arithmetic states that adding an unsigned value to a pointer will shift the address of that pointer by that many types (ints are usually two bytes, so the previous statement shifts the address of num by 4 bytes). The * operator (nothing to do with declaring a pointer by stating int *a) dereferences, or gets the value at the address of, its input. The first statement declares five contiguous memory locations to hold ints, which can be accessed (and assigned) by referencing:


num[0] //First


num[1] //Second


num[2] //Third


...


Sorting an array is pretty easy (remember that arrays are pointers, so saying a function accepts a pointer is the same as saying it accepts an array. Remember, also, that since pointers are addresses, modifying a pointers dereferenced value WILL CHANGE THE VALUE OF THE MEMORY IN THE CALLER):





void Sort(int *a)


{


int pass, num, temp;


for(pass=0; pass%26lt;(sizeof(a)/sizeof(int)); pass++)


for(num=0; num%26lt;(sizeof(a)/sizeof(int))-1; num++)


if(a[num]%26gt;a[num+1]){


temp=a[num];


a[num]=a[num+1];


a[num+1]=temp;}


}





Temp is required because of the destructive read-in principle.


Read some more, and sorry I couldn't help without arrays.





Oh, and the function doesn't return, but the passed array comes back sorted.
Reply:Well you could put them in an array:





int arr[5];


arr[0] = x; arr[1] = y; [...]





OR





You could hard-code the actual sorting, but this is very messy. It would do something like:


(let's name the variables x0, x1, ...x4)





SORTING DESCENDING:





if (x0 %26lt; x1) swap(x0, x1);


if (x0 %26lt; x2) swap(x0, x2);


if (x0 %26lt; x3) swap(x0, x3);


[...]


if (x3 %26lt; x4) swap(x3, x4);
Reply:How are you storing these 5 numbers at the moment ?
Reply:1) Use individual variables for each of the numbers,


name them Num1, Num2, ... Num5





2) Use another set of numbers to store their position in the sorted order, name them Index1, Index2, ... Index5





3)


Num1 = random();





Num2 = random();





Num3 = random();





Num4 = random();





Num5 = random();








4) Check


if (Num1 %26lt; Num2)


{


if (Num1 %26lt; Num3)


{


if (Num1 %26lt; Num4)


{


if (Num1 %26lt; Num5)


{


//Num1 is smallest


Index1 = 0;


}


}


}


}





* this is a very strange logic but can work based on your limitations.





* this is not the complete solution but just a hit so complete the rest if you have picked my point





* I am your teacher.





* Just kidding ;)
Reply:Since you cannot use an array, you can use a linked list to hold the values. Then do your sort on the list. Hey, you are not using an array.

gladiolus

No comments:

Post a Comment