| | |
what is the error
![]() |
•
•
Join Date: Apr 2005
Posts: 129
Reputation:
Solved Threads: 0
please where is the error in this code doesn't run
Code indented and tags added. -Narue
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstdlib> using namespace std; int main() { int ages[1000]; int size=1000; int i,a,b; for( i=0;i<size;i++) { ages[i]=rand(); for( a=1;a<size;a++) { for( b=size-1;b>=a;b--) { if(ages[b-1]<ages[b]) { i=ages[b-1]; ages[b-1]=ages[i]; ages[b]=i; } } } } cout<<ages[i]; return 0; }
>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:
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:
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.
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:
C++ Syntax (Toggle Plain Text)
ages[b-1]=ages[b];
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstdlib> using namespace std; int main() { int ages[1000]; int size=1000; int i,a,b; for(i=0;i<size;i++) { ages[i]=rand(); } for( a=1;a<size;a++) { for( b=size-1;b>=a;b--) { if(ages[b-1]<ages[b]) { i=ages[b-1]; ages[b-1]=ages[b]; ages[b]=i; } } } for( i=0;i<size;i++) { cout<<ages[i]<<' '; } return 0; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Error Loading operating System (Windows NT / 2000 / XP)
- svchost.exe error (Windows NT / 2000 / XP)
- New Hardware Causing Error (Windows NT / 2000 / XP)
- office 2000 install error (Windows NT / 2000 / XP)
- VMWare Unrecoverable Error (*nix Software)
- Error in Wrox Book (Perl)
Other Threads in the C++ Forum
- Previous Thread: drawing - stickman with array
- Next Thread: Simple Program will not exit gracefully
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






