Bobby
Bobby

Reputation: 223

C program: Checking digits entered are 1s and 0s only using arrays

So I am trying to make a smaller simple program to solve a problem. For this, I am trying to check to make sure the user inputs a number containing ONLY 1s and 0s and then I will use this number to perform a division. For example the number 11010 or 10111. Now my problem is, if I declare an integer to store this number (as follows) there wouldn't be a way to check all the digits are 1s or 0s right?

int userInput;

Now, I can use an array for this. (I know how to do this BUT this leads to my second problem.). Like so:

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

int main (int argc, char *argv[]){
    int myArray[20];
    int i, input, length;
    printf("Please enter how long your number is: \n");
    scanf("%d", &length);
    for (i = 0; i < length; i++){
        printf("Please enter digit %d of your array\n", i+1);
        scanf("%d", &input);
        assert ((input == 1) || (input ==0));


    }

For example, for the first digit the user enters a '1', the second digit the user enters a '0', then the third digit the user enters '0'. I need to "grab" this number though. How would I grab this number "100" and perform arithmetic operations on it. I hope this makes sense, if not moderators please give me a chance to clear it up.

EDIT: Many have suggested the modulo approach. BUT I still want to know if I can do this with an array. That is creating a integer variable and set that equal to each element the user has entered in the array.

Upvotes: 0

Views: 2018

Answers (6)

Varun
Varun

Reputation: 691

This solution is only for the second part of the problem. You can take the input as characters and use atoi to convert them to integer.

char arrayOfNumbers[MAX_LENGTH];
char tmpChar;
int number;
for (i = 0; i < length; i++){
    printf("Please enter digit %d of your array\n", i+1);
    scanf("%c", &tmpChar);
    assert ((tmpChar == '1') || (tmpChar == '0'));
    arrayOfNumbers[i] = tmpChar;
}

/*make sure it is NULL terminated*/
arrayOfNumbers[length] = '\0';
number = atoi(arrayOfNumbers);

Upvotes: 0

bsd
bsd

Reputation: 2717

You could cheat your way by making sure the program always deals with ints. Your array basically holds the binary representation of a base 10 number. Why not use it convert to an int. Then you can apply all operations to that number just as you would operate on ints.

Here is what i mean

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

int main (int argc, char *argv[]){
    int i, input, length;
    int num = 0; /* holds the actual number */


    printf("Please enter how long your number is <= %d: \n", 8 * sizeof(num) - 1);
    scanf("%d", &length);
    for (i = 0; i < length; i++){
        printf("Please enter digit %d of your array\n", i+1);
        scanf("%d", &input);
        assert ((input == 1) || (input ==0));
        /* humans enter numbers L TO R */
        /* set that bit if it is one */
        num |= (input << (length - i - 1)) ;
    }

    printf("Your number in base 10 is %d\n",num); 
    /* Now you do normal multiplications, additions, divisions */
    /* The only thing remaining now is to convert from base 10 to base 2. */
}

Sample run
Please enter how long your number is <= 31: 
5
Please enter digit 1 of your array
1
Please enter digit 2 of your array
1
Please enter digit 3 of your array
1
Please enter digit 4 of your array
1
Please enter digit 5 of your array
0
Your number in base 10 is 30

Upvotes: 0

MAK
MAK

Reputation: 26586

You don't really need to get the number one digit at a time.

It is easy to extract the digits of an int. Notice that a number such as 12345 is actually 5 * 10^0 + 4 * 10^1 + 3 * 10^2 + 2 * 10^3 + 1 * 10^4.

To get the lowest digit you can just take the remainder of the number when divided by 10 (i.e. mod it by 10 using the % operator). So, 12345 % 10 is 5. To get the next digit, you can divide the number by 10 (getting 1234) and then mod by ten again - giving you 4. Keep doing this as long as you have digits left in the number (i.e. the number is > 0).

#include <stdio.h>


int is_valid(int number) {
  if (number == 0) return 1; // its a 0.
  while (number != 0) {
    int digit = number % 10;
    if (digit != 1 && digit != 0) return 0;
    number = number / 10;
  }
  return 1; // no other digits were found.
}

int main() {
  int n;
  scanf("%d", &n);
  if (is_valid(n)) printf("valid\n");
  else printf("not valid\n");
  return 0;
}

Here's another idea:

Just write the number again into a string. Then iterate over the string checking each character. This is somewhat less efficient, but simpler to understand/code.

#include <stdio.h>

int is_valid(int n) {
  char buffer[20];
  char *c;
  sprintf(buffer, "%d", n);
  for(c = buffer; *c != '\0'; c++) {
    if (*c != '0' && *c != '1') return 0;
  }
  return 1;
}

int main() {
  int n;
  scanf("%d", &n);
  if (is_valid(n)) printf("valid\n");
  else printf("not valid\n");
  return 0;
}

EDIT: Since you mentioned in your edit that you are not interested in alternate approaches and just need a way to "grab" the digits as they are fed in, I'm adding this to my answer.

Keep a variable initially set to 0. Now as each digit comes in, (I am assuming the user enters higher digits before lower ones), we multiply our variable by 10 and add the new digit to it. Thus, if the user enters 1, 0, 0, our variable is initially 0, the its 1 (0 * 10 + 1), then its 10 (1*10 + 0) and finally 100 (10 * 10 + 0), which is what we needed.

Upvotes: 1

loxxy
loxxy

Reputation: 13151

You don't need an array to store all the digits.

Keep dividing by 10 & taking modulo to get each digit & keep a single flag to mark if all digits are 1 or 0.

Of course works only for max word size of the integer...

Something like:

char isBinary = 1;

int copyInput = +input;

while(copyInput  && (isBinary=copyInput%10<=1)) copyInput/=10;

Upvotes: 0

Adrian Krupa
Adrian Krupa

Reputation: 1937

Maybe something like this would be enough

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

int main (int argc, char *argv[]){
    int myArray[21];
    printf("Please enter your number (max 20 digits)\n");
    scanf("%s", myArray);
    for (i = 0; i < strlen(myArray); i++){
        assert ((myArray[i] == 1) || (myArray[i] == 0));
    }

Upvotes: 0

MAnyKey
MAnyKey

Reputation: 567

I think this could be more simple:

bool is_zeros_and_ones(int n) {
  for (; n != 0; n /= 10) {
    int mod = n % 10;
    if (0 != mod && 1 != mod) {
      return false;
  }
  return true;
}

So you can input whole number and test it without arrays.

Upvotes: 1

Related Questions