user9587342
user9587342

Reputation:

Declare an Array without Size in C programming

I am writing a program that converts a given bit string (up to 32-bits) into decimal assuming the input is given in unsigned magnitude and two's complement. I am reading each bit in from the user one char at a time and attempting to store it into an array, but the array doesn't have a required size. Is there a way to get the array to go through the loop without the array size being known? I also am trying to figure out a way to not use the pow and multiplication functions. I am posting my code below, if you have any ideas please

#include "stdio.h"
#include "math.h"

#define MAX_BITS 32
#define ENTER '\n'
#define NUMBER_TWO 2

int main()
{
        int unsignedMag;
        int twosComp;
        int negation[n];
        int bitStore[n];
        char enter;

        //Input from the User
        printf("Enter up to 32 bits (hit 'enter' to terminate early): ");

        //Reads the first bit as a character
        char bit = getchar();
        while (getchar != enter) {
                bit = bit - '0';
                scanf("%c", &bitStore[bit]);
                getchar();
        }

        //Terminates if user hits enter
        if (bit == enter) {
                return 0;
        }

        //Continue through code
        else {
                //Loop to calculate unsigned magnitude
                for (int i = 0; i < bitStore[i]; i++) {
                        unsignedMag = unsignedMag + (bitStore[i] * pow(NUMBER_TWO, i));
                }

                //Loop to calculate complete negation
                for (int j = 0; j < bitStore; j++) {
                        negation[j] = ~bitStore[j]
                }
                negation = negation + 1;
                for (int l = 0; l < negation; l++) {
                        twosComp = twosComp + (negation[l] * pow(NUMBER_TWO, l));
                }


        }
        return 0;

}

Upvotes: 0

Views: 2149

Answers (1)

chux
chux

Reputation: 153348

"Is there a way to get the array to go through the loop without the array size being known?"

No. Array sizes are fixed at the point the array is declared and the size is knownable: e.g. @Observer

size_t size = sizeof bitStore/sizeof bitStore[0];

Instead, since code has "given bit string (up to 32-bits) ", define the array as size 32 (or 33 is a string is desired).
Keep track of how much of the array was assigned.

//int bitStore[n];
int bitStore[MAX_BITS];
int count = 0;

// char bit = getchar();
int bit = getchar(); // Use `int` to account for potentially 257 different values

//while (getchar != enter) {
while (count < MAX_BITS && (bit == '0' || bit == '1')) {
    bit = bit - '0';

    // Do not read again, instead save result. 
    //scanf("%c", &bitStore[bit]);  
    bitStore[count++] = bit;

    // getchar();
    bit = getchar();
}

to not use the pow and multiplication functions.

Simply add or multiply by 2 via a shift. It is unclear why OP has a goal of not using "multiplication". I see little reason to prohibit *. A good compiler will emit efficient code when the underlying multiplication is expensive as *2 is trivial to optimize.

    // int unsignedMag;
    unsigned unsignedMag = 0; // initialize

    // for (int i = 0; i < bitStore[i]; i++) {
    for (int i = 0; i < count; i++) {
      // preferred code, yet OP wants to avoid * for unclear reasons 
      // unsignedMag = unsignedMag*2 + bitStore[i];
      unsignedMag = unsignedMag + unsignedMag + bitStore[i];
    }

pow() is good to avoid for many reasons here. Most of all, using double math for an integer problem runs into precision issues with wide integers.


converts a given bit string (up to 32-bits) into decimal

Note that a bitStore[] array is not needed for this task. Simply form unsignedMag as data is read.

Upvotes: 2

Related Questions