>I need a compiler for windows.
Bloodshed's Dev-C++ is a good IDE and comes with GCC as the back-end compiler.
>I need a compiler for windows.
Bloodshed's Dev-C++ is a good IDE and comes with GCC as the back-end compiler.
My best code is written with the delete key.
Thanks for the help everyone. I thought I knew C, but dont. About 2 weeks ago it seemed I did, but whatever knowledge I possessed is gone. I dont even undertsand the basic concepts anymore. Thanks!!
> Sure I can include errors, but wont they be different as the compilers are different? Line 8.1 syntax error.
Yes, but at the same time short of errors that call attention to themself its a pain to have to read through a large body of code. However, yours is hardly a large body, and even reading over it I'm not entirely sure that i knew what you were trying to do. If you are wanting to look for an array of values, you should loop and stop if there is a mismatch. I'm not sure if you are intending to find strings within the array or just want to know if each value of the search array needs to be found in the searched array.
My first post explains the purpose of the program.Originally Posted by master5001
They have problems reading. Don't pay any attention to them. They can't even recognize a direct quote as their own words. How can they be expected to read yours?Originally Posted by nizbit
Quzah.
Hope is the first step on the road to disappointment.
This is okay for private boards quzah, but I really think we should keep flames in the thread that caused them. That way we don't risk legitimate Q&A turning into pointless and confusing flame wars.Originally Posted by quzah
My best code is written with the delete key.
>My first post explains the purpose of the program.
Ah yes, indeed it does. Ok then you will want to loop through your search array, doing a search for each individual element.
Oh I understand that fine. I even somewhat understand the functions. Getting everything to work in main and tying it all togetther is whats completely over my head.Originally Posted by master5001
Ok well this isn't overly complicated but if you are more specific maybe we can be of more help.
Take some time to try understanding the functions though, they are both teaching you different ways of accomplishing the same task. As you program more you will learn that even though there may be several ways to do the same thing, only one way will work best. The programmer's job is to do their best to find the most effective way of accomplishing the task.
I dont know what I should be passing to the functions. I dont know from what I have, what I should be putting in there.
OPk I figured out what I was doing wrong. The program is somewhat working now. The only prolem left is that I'm not getting how many times the array was scanned for the search. I think I'm using the wrong variable. This is the working code:
Code:#include <stdio.h> #include <stdlib.h> #include <time.h> int BIN_NUM, SEQ_NUM; int arraycpy(int *a, int *b); int compare(const void *a, const void *b); /* function prototypes */ int bin_search(int *data, int n, int key); int seq_search(int *data, int n, int key); int arraycpy(int *a, int *b) { int x; x=0; while((a[x]=b[x])!='\0') { x++; } } int main() { int x, y, z, a, b, ia[1000], ib[1000], key; srand(time(NULL)); /* seed random # generator */ for(x=0; x<1000; x++) { /* select 1000 numbers */ y=rand()%1000+1; /* generate random numbers from 1-1000 */ ia[x]=y; } printf("\nEnter an integer to search the array or -1 to quit:"); scanf("%d", &key); while(key!=-1) { a=seq_search(ib, 1000, key); arraycpy(ia, ib); qsort(ia, 1000, sizeof(int), compare); b=bin_search(ia, 1000, key); printf("Sequential search found the value:%4d in element %4d with %4d items scanned.\n", key, a, SEQ_NUM); printf("Binary search found the value:%8d in element %4d with %4d items scanned.", key, b, BIN_NUM); printf("\nEnter an integer to search the array or -1 to quit:\n"); scanf("%d", &key); } return 0; } int compare(const void *a, const void *b) { /* function to sort array */ return *(int*)a-*(int*)b; } int bin_search (int *data, int n, int key) { int found, midpoint, first, last, BIN_NUM; found = 0; first = 0; BIN_NUM = 0; last = n - 1; while ( ( first <= last ) && ! found ) { midpoint = (first + last) / 2; if ( data[midpoint] == key ) found = 1; else if ( data[midpoint] > key ) last = midpoint - 1; else first = midpoint + 1; BIN_NUM++; } if ( found ) return (midpoint); else return (-1); } int seq_search(int *data, int n, int key) { int found, i, SEQ_NUM; i = 0; found = 0; SEQ_NUM = 0; while ( ! found && ( i < n ) ) { if ( data[i] == key ) found = 1; else i++; SEQ_NUM++; } if ( found ) return (i); else return (-1); }
I got it I was delcaring BIN_NUM and SEQ_NUM in the functions when they are already declared as global variables.
I want to say thank you to all. I know you were just trying lead me to the correct path not show me the path. I just get a frustrated and then get nuts. So I'm sorry if I acted like a baby. Thank you!
Good show, you figured it out before I even had a chance to read it. See that means you are starting to get it.
One more question though. Maybe I'm not understanding the way searches work, but since I'm just copying the array into another, shouldn't both searches return the number I'm looking for?? For example, if I'm searching for 23, as long it was generated, shouldn't I find 23 in both arrays, just in different locations?
Maybe. But keep in mind one array is filled with random values, the other array is filled with the same random values but sorted. There is a statistical probability that you could indeed have a random number generated at an index that would be correctly sorted. But that is very unlikely given how many values there are and the fact that the numbers are random.
Short answer: yes.
I gather that for some reason you aren't getting the same number in both arrays. Which array aren't you finding your number in?