whats wrong?

This is a discussion on whats wrong? within the C Programming forums, part of the General Programming Boards category; i wrote this program to sort and convert decimal integers into binary form. Code: #include <stdio.h> #include <stdlib.h> #include <math.h> ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    8

    whats wrong?

    i wrote this program to sort and convert decimal integers into binary form.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define MAX_ARRAY		5
    
    int fillarray(void);
    
    int main()
    {
    	int char_array[MAX_ARRAY];
    	char binary_array[MAX_ARRAY];
    	int lastchar, stop;
    	int lastcharprint;
    	int compare1, compare2;
    	int binary, binaryvalue, binarycount, binarycount2;
    	
    	lastchar = 0;
    	stop = 0;
    	lastcharprint = 0;
    	compare1 = 0;
    	
    	/* call function */
    	while (stop != 1) {
    		char_array[lastchar] = fillarray();
    		/* check for newline */
    		if (char_array[lastchar] == '\n' && char_array[lastchar] != 10) {
    			stop = 1;
    		}
    		else {
    			lastchar = lastchar + 1;
    		}
    		/* check for full array */
    		if (lastchar == MAX_ARRAY) {
    			stop = 1;
    			lastchar = lastchar - 1;
    		}
    		
    	}
    	
    	
    	/* sort values */
    	while (compare1 <= lastchar) {
    		compare2 = compare1 + 1;
    		while (compare2 <= lastchar) {
    			if (char_array[compare2] < char_array[compare1]) {
    				int temp;
    				temp = char_array[compare1];
    				char_array[compare1] = char_array[compare2];
    				char_array[compare2] = temp;
    			}
    			compare2 = compare2 + 1;
    		}
    		compare1 = compare1 + 1;
    	}
    	/* end of sort function */
    	
    	/* calculate binary */
    	int bintemp[32];
    	binarycount = 0;
    	binarycount2 = 1;
    	while (binarycount <= lastchar) {
    		binary = char_array[binarycount];
    		binaryvalue = 0;
    		while (binaryvalue < 32 && binary > 0) {
    			bintemp[binaryvalue] = binary % 2;
    			binary = binary / 2;
    			binaryvalue = binaryvalue + 1;
    		}
    		binaryvalue = binaryvalue - 1;
    		while (binaryvalue >= 0) {
    			binary_array[binarycount] = binary_array[binarycount] + (bintemp[binaryvalue] * pow(10, binaryvalue));
    			binaryvalue = binaryvalue - 1;
    		}
    		binarycount = binarycount + 1;
    	}
    
    	while (lastcharprint <= lastchar && char_array[lastcharprint] != '\n' || char_array[lastcharprint] == 10) {
    		printf("\n%d (decimal), %d (binary)", char_array[lastcharprint], binary_array[lastcharprint]);
    		lastcharprint = lastcharprint + 1;
    	}
    
    	printf("\n\nEnd of program\n");
    	return 0;
    }
    
    int fillarray()
    {
    	int c, value;
    	value = 0;
    	
    	c = getchar();
    	
    	while (c != '\n') {
    		value = 10 * value + c - '0';
    		c = getchar();
    	}
    
    	return (value);
    }
    this works perfectly for the sorting part, but when i input any number greater than 5 it will return some random value.
    hope you can help

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Look at your fillarray(). You set value to 0, then inside the while loop, you multiply value (0) by 10. This will ALWAYS be 0! 'value' is always equal to "c - '0' ".

    Also, you are only sending one int value back, so why do you need this while loop? If you want to return the 'value' why are you doing another getchar()?? Basically you are sending back the last value just before the newline character.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kcpilot View Post
    Look at your fillarray(). You set value to 0, then inside the while loop, you multiply value (0) by 10. This will ALWAYS be 0! 'value' is always equal to "c - '0' ".

    Also, you are only sending one int value back, so why do you need this while loop? If you want to return the 'value' why are you doing another getchar()?? Basically you are sending back the last value just before the newline character.
    I don't think that's wrong. c is (potentially)changing each time, so it should be fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    First:

    Code:
    char binary_array[MAX_ARRAY] = {0}; // Clear the array
    **EDIT**
    To resolve your problem change the above array to
    Code:
    int binary_array[MAX_ARRAY] = {0}; // Clear the array
    The overflow issue below is the source of your grief.

    Also, you have an overflow issue since you're using a char array when the input numbers are either 8 or 9 which will make binaryvalue = 3.

    Code:
    binary_array[binarycount] = binary_array[binarycount] + (bintemp[binaryvalue] * pow(10, binaryvalue));
    Finally, isn't that binary_array conversion a little redundant? It looks like bintemp would hold your binary bit representation?
    Last edited by BobS0327; 10-16-2007 at 07:24 PM. Reason: Provide a solution to the problem

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    wow thanks bob! i made the change to
    Code:
    int binary_array[MAX_ARRAY] = {0};
    but it still doesn't work. the first number i type in will give me a random 10 digit result, and the following outputs will be generally wrong sometimes. such as 2 coming out as 10, 11 or 12.

    again, thanks for the help.
    Last edited by samuelanonymous; 10-17-2007 at 03:48 AM.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I just can't recreate your error. So, Ill take a wild guess and say that the input array may have some extraneous junk it. Eliminate this potential problem by clearing the array:

    Code:
    int char_array[MAX_ARRAY]  = {0};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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