Errors with program

This is a discussion on Errors with program within the C Programming forums, part of the General Programming Boards category; I am taking a 1000 random integers and putting them in an array of 1000. Then I am copying the ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Errors with program

    I am taking a 1000 random integers and putting them in an array of 1000. Then I am copying the array to another to sort them least to greatest so I can do I binary sort. Then with the other array I am doing a squential sort. The user is typing in a number to search the array for. Then we have to print the number searched, where in the array it was found, and how many times scanned.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int BIN_NUM, SEQ_NUM;
    
    int arraycpy(int *ia, int *ib);
    int compare (void *a, void *b);			/* function prototype */
    int bin_search (int *data, int n, int key);
    int seq_search(int *data, int n, int key);
    
    int arraycpy(int *ia, int *ib) {	
     
    	int x; 
     
    	x=0;
    	while((ib[x]=ia[x])!='\0') {
    		x++;
    	} 
    } 
    
    int main() {
    
    	int x, y, z, ia[1000], ib[1000], key;
    	
    	srand(time(NULL));				/* seed random # generator */
    	
    	for(x=0; x<1000; x++) {			/* select 50 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:\n");
    	scanf("%d", &key);
    
    	while(key!=-1) {
    					
    	qsort(ia, 1000, sizeof(int), compare);		
    	z=bin_search(key);
    	printf("%d", z);
    	return 0;
    	}
    }
    
    int compare(void *a, 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);
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    And your question is?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Ooops, I dont know how or what I should do/use to get the arrays in the function arraycpy, so I can copy the one array to the other.

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    The error in your code lies here:
    Code:
    int bin_search(int *data, int n, int key);
    And:
    Code:
    z=bin_search(key);
    The function takes 3 parameters, but you are only sending one.

    Edit: More errors.

    Another is with qsort(). If you read this reference you should know that the fourth parameter takes const void, not void. For example:
    Code:
    int compare (const void *a, const void *b);
    Another error is with arraycpy(). You define the return value as int, but never return any thing.

    - Stack Overflow
    Last edited by Stack Overflow; 12-16-2004 at 05:42 PM. Reason: Displaying more errors.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Guess I dont understand.

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Alright,

    Let me break it down. You ask for three parameters in a function body. You can't send it one, and expect it to understand.

    For example, there is something terribly wrong with this code:
    Code:
    int add(int x, int y) {
    	return x + y;
    }
    
    int main() {
    	add(3);
    
    	return 0;
    }
    Can you find it? If so, then you know I didn't send y, just x. If you understand, then sending one parameter to bin_search() when it is looking for 3 should also make sense why it is returning an error.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    And to answer your original question, you would just send ia and ib normally to arraycpy().

    How is that possible? Because of the way C/C++ uses pointers and arrays, you can reference an array element either by subscription or * (the unary dereference operator). For example, you can pass strings into functions as pointers to characters or as character arrays.

    To explore in greater detail, this is how you would use the following code:
    Code:
    int arraycpy(int *ia, int *ib);
    
    // Using it
    arraycpy(ia, ib);
    Simple. ia and ib are arrays. Pointer arguments enable a function to access and change objects in the function that called it. Any operation that can be acheived by array subscripting can also be done with pointers.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I'm still getting compile errors. Part of my problem with this program the search code were given to us. If I wrote the code myself I would be easier. This is what I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int BIN_NUM, SEQ_NUM;
    
    int arraycpy(int *a, int *b);
    int compare (void *a, void *b);			/* function prototype */
    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:\n");
    	scanf("%d", &key);
    
    	while(key!=-1) {
    		a=bin_search(ib, ia, key);
    		b=seq_search(ia, ib, key);
    		arraycpy(ia, ib);				
    		qsort(ia, 1000, sizeof(int), compare);		
    		printf("%d", z);
    		printf("\nEnter an integer to search the array or -1 to quit:\n");
    		scanf("%d", &key);
    	}
    	return 0;
    	
    }
    
    int compare(void *a, 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);
    
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm still getting compile errors. Part of my problem with this program the search code were given to us. If I wrote the code myself I would be easier. This is what I have:
    Would it have killed you to actually include the errors?

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

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok well its throwing errors because you are calling your search functions incorrectly. They are supposed to find a specific int in the data source, not an array of ints:

    Erros:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int BIN_NUM, SEQ_NUM;
    
    int arraycpy(int *a, int *b);
    // add const in your compare function to shut the compiler up.
    int compare (const void *a, const void *b);			/* function prototype */
    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:\n");
    	scanf("%d", &key);
    
    	while(key!=-1) {
    		a=bin_search(ib, ia, key);
    		b=seq_search(ia, ib, key);
    		arraycpy(ia, ib);				
    		qsort(ia, 1000, sizeof(int), compare);		
    		printf("%d", z);
    		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);
    
    }

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Sure I can include errors, but wont they be different as the compilers are different? Line 8.1 syntax error.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but wont they be different as the compilers are different?
    Yes, but we're experienced enough at reading errors that the differences don't matter. An error message (or warning) is immensely helpful in determining where and what the problem is, so you should always post them if you have them.

    >Line 8.1 syntax error.
    When we ask for error messages, we mean a direct copy and paste of what you compiler spits out at you. If that's all your compiler says then you might consider getting another one with more informative errors.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Thats what my compiler says and that is what compiler I'm supposed to use. IBM RS/6000 Running AIX 5.2.0. Maybe you could recommend another compiler that I can use at home since this is the only one I know?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Thats what my compiler says
    What a shame. I guess it assumes you'll find the syntax error with just a general line number.

    >IBM RS/6000 Running AIX 5.2.0.
    If you don't have GCC then you might consider giving it a shot.
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Sorry I should have been more clear. I SSH into that server. It isn't mine. I need a compiler for windows.

Page 1 of 3 123 LastLast
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, 09:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21