7 Replies - 43592 Views - Last Post: 11 November 2008 - 08:13 PM Rate Topic: -----

#1 bestbat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 14-February 07

[C Language] Sorting array into descending order

Posted 21 February 2007 - 08:43 PM

I'm trying to determine the size of the array and then display it in descending order:

#include <stdio.h>

int main(void)
{
int my_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int test, count;
test=sizeof my_array / sizeof my_array[0];
printf("Size of my array is: %d\n", test);

	for (count=test; count>=0; count--)
	{
		printf(" %d% ", my_array[count]);   */
	}; 
}


1) When I compile and run it I get a size of 40, surely the size should be 10? Fixed added / sizeof my_array[0].
2) Also printing in descending order works with '9' but not with 'test'.
3) What would be the simplest/easiest way to order (not just print) the array in descending order.
At the moment the only way i can think of is to create another array of size 'test' and read/write each value of my_array in reverse order.

Thanks in advance.

Edit: I figured out the size of array part, still stuck on sorting.

This post has been edited by bestbat: 21 February 2007 - 08:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [C Language] Sorting array into descending order

#2 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 786
  • View blog
  • Posts: 7,618
  • Joined: 05-May 05

Re: [C Language] Sorting array into descending order

Posted 21 February 2007 - 08:46 PM

When using sizeof, you use it like this:

value = sizeof(the_array);



Keep in mind it returns the byte size of the array. Since each int is 4 bytes long, you just divide by 4. If it were an array of char, it would return 10. But since an int is a 32-bit variable, it returns 4 since it takes up 4 bytes of space.

So, it might be like this:
int the_array[5];
value = sizeof(the_array);
// value is now equal to 5*4 or 20


This post has been edited by WolfCoder: 21 February 2007 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 bestbat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 14-February 07

Re: [C Language] Sorting array into descending order

Posted 21 February 2007 - 08:50 PM

I found an alternative solution by googling.

But thanks WolfCoder, from your post I think I understand it better.

This post has been edited by bestbat: 21 February 2007 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 Haze Genova  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-April 08

Re: [C Language] Sorting array into descending order

Posted 20 April 2008 - 05:13 PM

wow people still write in c?
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: [C Language] Sorting array into descending order

Posted 20 April 2008 - 05:54 PM

View PostHaze Genova, on 20 Apr, 2008 - 06:13 PM, said:

wow people still write in c?



I see a lot of C around here.
Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: [C Language] Sorting array into descending order

Posted 21 April 2008 - 03:32 AM

Quote

wow people still write in c?

The computer you are currently using along with other external hardware devices, have all their drivers written in C.
Was This Post Helpful? 0
  • +
  • -

#7 EricFG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 10-November 08

Re: [C Language] Sorting array into descending order

Posted 11 November 2008 - 03:44 PM

I have a question regarding this same topic, but it makes no sense to start another thread. I am trying to sort my array into descending order, but am having a few problems. Do I need to do a bubble sort to accomplish this?

void rev(const int array[], int count)
  
{
		  
  int i;
		  
  for(i = count - 1; i > 0; --i){
	  printf(" %5d", array[i]);
  
}		 
  
  printf("\n\n");
	
}


Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: [C Language] Sorting array into descending order

Posted 11 November 2008 - 08:13 PM

Yes, just start at the other "end" of the array and base your for loops accordingly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1