|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (6 bit)
Join Date: Sep 2001
Location: Cali
Posts: 48
|
C++ program, Compiler isnt detecting any errors, but there is an error(s)
Hello everyone,
For my C++ programming class, I had to write the source file. It's a program that generates 200 integers randomly and stores them in an array. Sorts those numbers in descending order, seaches the array for the number 65, and then finds the integer whih occurs the most frequently. I'm using MicroSoft Visual C++ 6.0 and the compiler isnt detecting any errors, but when I build and run the program, I get an error message in windows stating something about an illegal operation. Can anyone help? Here's the code: #include #include using namespace std; void rnum(int *); void sort(int [], int); void swap(int *, int *); void search(int [], int); int frequency(int[], int); int main() { const int size=200; //declarations int i; int ray[size]={0}; rnum(ray); //call rnum function sort(ray, size); //call sort function for(i=0; i<200; i++){ cout< cout<<"Searching for number 65 \n"<< endl; search(ray, size); cout<<"The number that occurs the most frequently is "< return 0; } /***********************************End of Main Function********************************/ /***********************************Start Rnum Function*********************************/ void rnum(int *rptr) { int i, k=0; rand(); for(i=1; i<=200; i++){ k=10 +rand()%(100-10+1); //generate random # *rptr=k; //store number in array *(rptr++); } } /***********************************End Rnum Function***********************************/ /**********************************Start Sort Function**********************************/ void sort(int ray[], int size) { int pass, i; for(pass=0; pass swap(&ray[i], &ray[i+1]); //call swap function } } /***********************************End Sort Function***********************************/ /**********************************Start Swap Function**********************************/ void swap(int *oneptr, int *twoptr) { int temp; temp=*oneptr; *oneptr=*twoptr; //swapping values *twoptr=temp; } /***********************************End Swap Function***********************************/ /*********************************Start Search Function*********************************/ void search(int ray[], int size) { int low=0, high=size-1, middle; while(low<=high){ middle=(low+high)/2; //divide array in half if(ray[middle]==65) cout<<"Success, the number 65 was found" < if(65 else low=middle+1; //search lower half if(low==high) cout<<"The number 65 was not found"< } //end while } /*********************************End Search Funtion************************************/ /******************************Start Frequency Function*********************************/ int frequency(int ray[], int size) { int num,i, k=0, c1=0, c2=0; const int size2=90; int freq[size2]={0}; //declaring array for(num=0; num for(i=1; i c1++; } freq[c2]=c1; //assign to freq array c2++; } sort(freq, size2); return freq[0]; } Edit: Opps...My logic is way off on finding the most frequently occuring number. Last edited by chpco2000; 11-10-2001 at 01:46 PM. |
|
|
|
|
|
#2 |
|
Member (7 bit)
Join Date: May 2000
Location: Atlanta, Georgia
Posts: 95
|
in the search routine, what's goin on with this line?
cout<<"Success, the number 65 was found" < else{ try.. cout<<"Success, the number 65 was found"; else{ AND.... instead of.. cout<<"The number 65 was not found"< } //end else try.. cout<<"The number 65 was not found"; } //end else i didn't analyze the code too hard, but this just stood out. drop a line if you're still having trouble. hope this helps Last edited by kcbhiw; 11-19-2001 at 04:19 AM. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|