1

I'm sorry if this is confusing....So far I'm converting a decimal number into binary. While doing this, i store the digits for the binary representation into an int array.

EX: for the number 4. (this is done in dec2bin below)

    temp[0] = 1
    temp[1] = 0
    temp[2] = 0

i would like to store this array into another array (say BinaryArray) that will contain multiple 'temp' arrays.

I would like the BinaryArray to declared main, passed to dec2bin, and the save a copy of the current temp array. then go to the next number.

I'm having trouble with figuring out the pointers and what not needed for this. If someone could help me with how to declare the needed array in main and how add to it from dec2bin.

Thanks! Main:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {      

      void dec2bin(int term, int size);

      int size, mincount;
      int * ptr;
      int x;
      x=0;

      scanf("%d %d", &size, &mincount);
      printf("Variables: %d\n", size);
      printf("Count of minterms: %d\n", mincount);

      int input[mincount+1];

      while(x < mincount){
        scanf("%d", &input[x]);
        x++;
      }
      x = 0;

      while(x < mincount){
        dec2bin(input[x], size);

Dec2bin :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 32

    void
    dec2bin(int term,int size){
      int i, j, temp[size], remain, quotient;
      quotient = term;
      i = size-1;
      // set all temp to 0
      for(j=size-1;j>=0; j--){
        temp[j] = 0;
        }

      //change to binary
      while(quotient != 0){
        remain = quotient % 2;
        quotient/=2;
        if(remain != 0){
          temp[i] = 1;
         } else {
          temp[i] = 0;
         }
         i--;
        }

        //print array
        for(i=0; i<size; i++)
          printf("%d", temp[i]);

        printf("\n");
    }
5
  • Hi edit please, main is posted twice :)\ Commented Sep 18, 2012 at 19:33
  • Dang. Just left the house. Will have to add the right stuff in a few Commented Sep 18, 2012 at 19:42
  • Well, as far as I can go right now without the full code: why not declare/store as int **? That is, intuitively, an array of array of ints. Predeclaring the sizes may not be possible if you have different sized numbers, leading to mallocs, leading to huge headaches because you made an assumption about one of the array sizes, etc, but ... Commented Sep 18, 2012 at 19:46
  • 1
    ... what I'd do is declare an int** to hold all your data AS WELL AS an int* (or int[]) that stores the sizes of each int array. That way you can (relatively) safely traverse your data Commented Sep 18, 2012 at 19:47
  • Thanks AK, I will try it in a bit, gotta take a break from the comp!..lol, I'll check back to see if your answer changes at all based on the addition of the correct info :) Commented Sep 18, 2012 at 20:06

2 Answers 2

6

Don't have sure if i understood what you want to do, but it seems that you want to create a "array of arrays of int". Example:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int i;
    int n;
    int **myArray;

    n = 10;
    myArray = (int**)malloc(n*sizeof(int*));

    //Usage example
    int myIntArray[] = {1,2,3,4,5};

    myArray[0] = myIntArray;

    //This call should print "4"
    printf("%d\n",myArray[0][3]);

    return;
}   

This way you will have a array (myArray) that each element is a array of ints.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain what "(int**)malloc(nsizeof(int));" does? is it declaring an array of size 10, does it need to know the size of "myIntArray"
0

To "set all temp to 0", use memset(). I assume you want to display an integer in binary. You can check each bit by performing a logical and with 0x80000000 and then left shifting the variable. Here is a crude example:

int x = 27;
string bin;

for ( int index = 0; index < sizeof(int) * 8; ++index ) {
if ( x & 0x80000000 ) {
    bin += '1';
} else {
    bin += '0';
}
x = x << 1;
}
cout << bin << endl;

Why do you want to store a binary representation of an integer in an array of ints? I can't think of a reason to do this.

1 Comment

Trying to compare for kmaps. Think I will try something I found online with using the variable letters instead though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.