what is the error

Reply

Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

what is the error

 
0
  #1
Apr 8th, 2005
please where is the error in this code doesn't run
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6. int ages[1000];
  7. int size=1000;
  8. int i,a,b;
  9. for( i=0;i<size;i++)
  10. {
  11. ages[i]=rand();
  12. for( a=1;a<size;a++)
  13. {
  14. for( b=size-1;b>=a;b--)
  15. {
  16. if(ages[b-1]<ages[b])
  17. {
  18. i=ages[b-1];
  19. ages[b-1]=ages[i];
  20. ages[b]=i;
  21. }
  22. }
  23. }
  24. }
  25. cout<<ages[i];
  26.  
  27. return 0;
  28. }
Code indented and tags added. -Narue
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: what is the error

 
0
  #2
Apr 8th, 2005
>ages[b-1]=ages[i];
That's your error. i isn't an index, it's a content value, and the value could very well exceed 999, which is the largest possible index for ages. Change that line to:
  1. ages[b-1]=ages[b];
You probably also want to restructure your code so that it has specific sections for initializing the array, sorting the array, and printing the sorted array:
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6. int ages[1000];
  7. int size=1000;
  8. int i,a,b;
  9. for(i=0;i<size;i++)
  10. {
  11. ages[i]=rand();
  12. }
  13. for( a=1;a<size;a++)
  14. {
  15. for( b=size-1;b>=a;b--)
  16. {
  17. if(ages[b-1]<ages[b])
  18. {
  19. i=ages[b-1];
  20. ages[b-1]=ages[b];
  21. ages[b]=i;
  22. }
  23. }
  24. }
  25. for( i=0;i<size;i++)
  26. {
  27. cout<<ages[i]<<' ';
  28. }
  29.  
  30. return 0;
  31. }
I'm also not sure if you really wanted this functionality or not, but as it is the sort is descending, not ascending, which is the most common preference.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: what is the error

 
0
  #3
Apr 8th, 2005
thanks a lot Naure
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC