Thread: Errors with program

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  2. #17
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    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!!

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    > 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.

  4. #19
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Quote Originally Posted by master5001
    > 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.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nizbit
    My first post explains the purpose of the program.
    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?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by quzah
    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?

    Quzah.
    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.
    My best code is written with the delete key.

  7. #22
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >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.

  8. #23
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Quote Originally Posted by master5001
    >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.

  9. #24
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  10. #25
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    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.

  11. #26
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    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);
    }

  12. #27
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    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!

  13. #28
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Good show, you figured it out before I even had a chance to read it. See that means you are starting to get it.

  14. #29
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    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?

  15. #30
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Need help to solve program errors
    By pattop in forum C++ Programming
    Replies: 6
    Last Post: 05-28-2006, 01:57 AM
  3. mega compile errors for small program
    By s_UNI_ in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2005, 12:00 PM
  4. Average Rainfall Program Errors
    By JamesAnthony23 in forum C Programming
    Replies: 1
    Last Post: 09-11-2002, 10:44 PM
  5. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 10:40 AM