Errors with program

This is a discussion on Errors with program within the C Programming forums, part of the General Programming Boards category; It seems with the sequential sort. Here is the ouput when I execute my program: Code: Enter an integer to ...

  1. #31
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    It seems with the sequential sort. Here is the ouput when I execute my program:
    Code:
    Enter an integer to search the array or -1 to quit:12
    Sequential search found the value:  12 in element   -1 with 1000 items scanned.
    Binary search found the value:      12 in element   14 with    6 items scanned.
    Enter an integer to search the array or -1 to quit:2
    Sequential search found the value:   2 in element  877 with  878 items scanned.
    Binary search found the value:       2 in element    2 with    8 items scanned.
    Enter an integer to search the array or -1 to quit:14
    Sequential search found the value:  14 in element   -1 with 1000 items scanned.
    Binary search found the value:      14 in element   19 with   10 items scanned.
    Enter an integer to search the array or -1 to quit:32
    Sequential search found the value:  32 in element   -1 with 1000 items scanned.
    Binary search found the value:      32 in element   34 with   10 items scanned.

  2. #32
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sheesh talk about missing a big one...

    Code:
    int arraycpy(int *a, int *b) {	
     
    	int x; 
     
    	x=0;
    	while((a[x]=b[x])!='\0') {
    		x++;
    	} 
    }
    Ok the problem may not be apparent but since this looks for a null terminator to a numberic array...you are lucky you aren't getting seg faults more than anything. That said you should copy the arrays like this:

    Example:
    Code:
    int arraycpy(int *dst, int *src, size_t size) {	
      do {
        dst++=*src++;
      } while(--size);
    }
    I changed the names of the parameters to make things more clear. Why do this? Well it may have something to do with the fact that you were putting the random numbers int ia, but then having ib copy its values over into ia.

  3. #33
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I am getting a "line 15 invalid lvalue in assignment" for:
    Code:
    dst++=*src++;

  4. #34
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    That statement is really scary. You should try breaking it up into smaller pieces.

  5. #35
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I changed arraycpy to this and it seems to work now:
    Code:
    int arraycpy(int *a, int *b) {	
     
    	int x; 
     
    	x=0;
    	while(x!=1000) {
    		
            b[x]=a[x];
            x++;
    	} 
    }

  6. #36
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    oops that should be:

    Code:
    *dst++=*src++;
    This is what happens when you miss with your carret when copying and pasting

  7. #37
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Now both work. Thanks!

  8. #38
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah no problem and sorry about the typo. I don't really see lines such as that as too much obfustication, but maybe its because its just fairly common to see. You could break up the operation into smaller parts as sean_mackrory suggested.

    Example:
    Code:
    int arraycpy(int *dst, int *src, size_t size) {	
      do {
        *dst = *str;
        dst++;
        src++;
      } while(--size);
    }
    But as you can see thats not a exactly a far cry from how I originally had it.

Page 3 of 3 FirstFirst 123
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