simple array question

This is a discussion on simple array question within the C Programming forums, part of the General Programming Boards category; Hi, i am trying am dealing with numbers in two's complement. If I pass a negative number to the function, ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    simple array question

    Hi, i am trying am dealing with numbers in two's complement. If I pass a negative number to the function, it turns it into positive and works out its binary equivalent. The bit I am stuck on is writing a loop that will do the following: take the least significant bit from array1 (i.e. its last element) and copy it into array2; so it keeps on copying until it encounters first 1, and from then on all 1's are copied as 0's and vice versa, so, for example -25:
    array1 -> array2
    Code:
    11001 -> 00111
    this is the code that i have, the only thing that doesn't work is the loop if flag == 1, can anyone help:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void binary_converter (int number,int binary[32], int n)
    {
        int *data;
        int temp[32];
        data = (int*) calloc (n, sizeof (int));
        int i, k = 0, remainder, flag = 0;
        
        if (number < 0){
        	number = number * -1;
        	flag = 1;
        }
        
        for (i = 0; i < n; i++){
            remainder = number % 2;
            number = number / 2;
            data[i] = remainder;
        }
        
        while (i >= 0){
        	binary[k++] = data[--i]; 
        }
        
        if (flag == 1){
        	while (k >= 0){
        		if (binary[k] != 1){
        			temp[k] = binary[k]; k--;
        		}else{
        			temp[k] = binary[k]; k--;
        			if (binary[k] == 1){
        				binary[k] = 0;
        				temp[k] = binary[k]; k--;
        			}else {
        				binary[k] = 1;
        				temp[k] = binary[k]; k--;
        			}
        		}
        	}
        }
        printf ("%d\n", k);
        for (i = 0; i < 5; i++){
        	printf ("%d ", temp[i]);
        }
        printf ("\n");
        
    }
      
    
    int main (void)
    {
    	int binary[32] = {0};
    	int i;
    	binary_converter (-3 binary, 5);
    	for (i = 0; i < 5; i++){
    		printf ("%d ", binary[i]);
    	}
    	printf ("\n");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    is there something wrong with this bit of code?
    Code:
    	binary_converter (-3 binary, 5);
    does this compile? have you used the debugger to insure you don't get past the "flag == 1" ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 12:59 PM
  2. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  3. simple array question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2002, 09:43 PM
  4. simple array question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 03:46 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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