9 Replies - 48643 Views - Last Post: 27 November 2008 - 08:31 PM Rate Topic: -----

#1 curiose  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

how to create an ascending order by using arrays?

Post icon  Posted 28 October 2007 - 12:55 PM

curiose



Hi....actually thats my first time of trying to send any questionabout programming, and i hope that you are going to help me in finding the answers about my doubts

my question is :if im going to enter 4 no. into an integer array,and then i have to display them in ascending order ????


I think that it maybe like this:

#include<stdio.h>
void main()
{
int num[4],i;


printf("please enter 4 numbers");
for(i=0;i<4;i++)
{ 
scanf("%i",&num[i]);
}


if(num[0]<num[1] && num[0]<num[2] && num[0]<num[3])
  {
  printf("\n%i",num[0]);
  }

else if(num[1]<num[0] && num[1]<num[2] && num[1]<num[3])
  {
  printf("\n%i",num[1]);
  }

 else if(num[2]<num[0] && num[2]<num[1] && num[2]<num[3])
  {
  printf("\n%i",num[2]);
   }

else if(num[3]<num[0] && num[3]<num[1] && num[3]<num[2])
  {
  printf("\n%i",num[3]);
  }

}


my question is how to display the ascending order,shall i use a loop or what to do???
thank you very much

Is This A Good Question/Topic? 0
  • +

Replies To: how to create an ascending order by using arrays?

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: how to create an ascending order by using arrays?

Posted 28 October 2007 - 01:18 PM

This question should probably be in the C/C++ forum...

If you do a search for sorting and C++ on the site, you will find a ton of answers. There is also a current thread over in the C/C++ forum related to your problem - it's in C++, but there's a good pseudocode for sorting as well.

And please post your code using the code tags like this: :code:

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 curiose  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

Re: how to create an ascending order by using arrays?

Posted 28 October 2007 - 02:07 PM

Thank you very much Mr. jjhaaq.... i Abreciate your help, but please if you dont mind i need your point of view according to your experience in c++ programming language, actually i have read lots of the titles related to my question ,but i still have some doubts...its about using the if statement in this case is it the best solution or what shall i use insted of it??i really would like to know diffenrent ways for solving such a problem
thanks alot
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: how to create an ascending order by using arrays?

Posted 28 October 2007 - 03:57 PM

I don't think that your method is going to do what you want it to...for instance, if you entered the sequence 10 5 2 7, only the number 2 would print, because the only if statement with a true condition would be the third.

Because you're dealing with arrays, I would recommend that you sort the array, and then display each element in sequence. There are a number of C snippets on sorting; I would check out:
Bubble Sort
qsort()
Binary Search

If you're trying to maintain the original order of the array, you wouldn't have to actually swap the values and get the array sorted; you could use something like a bubblesort and simply display the element that would normally get swapped into the earlier position in the array.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 curiose  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

Re: how to create an ascending order by using arrays?

Posted 29 October 2007 - 08:15 AM

thank u again....i have got a good clear idea about the bubblesort and swaping from different books and from your links....but in my program i'm still facing a problem with looping the swaping

here is my program:

 #include<stdio.h>
void main()
{
int num[4],i,x;


printf("please enter 4 numbers");
for(i=0;i<4;i++)
{ 
scanf("%i",&num[i]);
}
	if (num[i]>num[i+1])
		  x=num[i];
		  num[i]=num[i+1];
	num[i+1]=x; 
printf("\nthe ascending oreder is\n");
  for(i=0;i<4;i++)
   printf("%i\t",num[i]);

}


i have tried to put :
for(i=0;i<4;i++)
for(j=0;j<4-1;j++)

ofcourse before the if statement...and after initializing the int j....and it didn't work and i didn't get the required result
please.. i need a soluotin to get the correct answer
Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: how to create an ascending order by using arrays?

Posted 29 October 2007 - 01:22 PM

Please remember to use code tags: :code:

The basic idea behind the bubble sort is this:

You start with the first element as your focal position. Run through the remaining elements, looking for values that are less than the current value. If you find one, you swap the values. Then move on to the next element, and repeat. The search proceeds through all of the indices starting at the index AFTER the focal position (not the first position in the array), and goes all the way to the end of your array.

The pertinent section of your code would be:
for (i=0; i<4; ++i) {
	for (j=i+1; j<4; ++j) {
		if (num[i]>num[j]) {
			x=num[i];
			num[i]=num[j];
			num[j]=x;
		}
	}
}


Pay careful attention to the indices that are used in the inner and outer loops. The outer loop (defines your focal position) runs through all of the values in the array. The inner loop (defines your comparison position) runs from the position AFTER the focal position to the end of the array. If you were to change the inner loop header to for(j=0;j<4;++j), the code would not work properly - can you see why?

And when you're using if, if-else, for- etc. types of statements, you should probably use braces to enclose all of the code that you want to run within the body of those conditions. Otherwise, only the first line of code following your for statement or if statement will execute conditionally. Take a look at the for loops and the if statements in the code above, paying careful attention to where the braces are. Without the braces in those positions, the code would again not work properly.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#7 curiose  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

Re: how to create an ascending order by using arrays?

Posted 31 October 2007 - 02:14 PM

finally i have got the right result ......oh
it works properly now ...thanx
Was This Post Helpful? 0
  • +
  • -

#8 cabagnot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-November 08

Re: how to create an ascending order by using arrays?

Posted 27 November 2008 - 05:52 AM

i have a question??

can i use the same program in descending order or can i use a WHILE looping statement??

tnx...

just new here...
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: how to create an ascending order by using arrays?

Posted 27 November 2008 - 06:02 AM

View Postcabagnot24, on 27 Nov, 2008 - 04:52 AM, said:

i have a question??

can i use the same program in descending order or can i use a WHILE looping statement??

tnx...

just new here...


If you used a while loop you would need to increment the values for 'i' and 'j' inside the loop (see how much they are required) and you would need to be careful in what your tests in while (true) are going to be.

So yes you could do it but the question that springs to mind is "why?".
Was This Post Helpful? 0
  • +
  • -

#10 manasa s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 08

Re: how to create an ascending order by using arrays?

Posted 27 November 2008 - 08:31 PM

Hi ..
Same piece of code can be used for displaying elements in descending order, but with a small correction in logic. we can implement this using while loop also, but for loop is prefered.

[code]

for(i=0;i<n;i++)
for(j=0;j<n-1;j++)

if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
for(i=0;i<n;i++)
printf("%d \n" , a[i]);
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1