# How do I store user input as an array of 0's and 1's?

• (2 Pages)
• 1
• 2

## 28 Replies - 1521 Views - Last Post: 10 February 2016 - 01:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=388373&amp;s=70ea4bae4b531bcb17f655f098cb5ef9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Lemon77

Reputation: 1
• Posts: 29
• Joined: 14-November 15

# How do I store user input as an array of 0's and 1's?

Posted 05 February 2016 - 11:41 AM

Instructions: Sets of numbers can be represented using array of 0s and 1s. The idea is that a[i]!=0 if i is in the set, and a[i] == 0 if it is not. For example, the array a[10] = {0, 0, 1, 0, 1, 1, 0, 0, 0, 0}wouldrepresenttheset{2, 4, 5} becausea[2],a[4],anda[5] have the value 1, and everywhere else a contains zeros. Since the array has a fixed bound, say N, the values in the set are restricted to the range 0...N-1.

2) The program will read in the number of element in the first set, for example, 4, then
read in the numbers in the set, for example, 3 6 8 9. The repeat for the second set. The
two sets do not necessarily are of the same size.
3) The sets are stored using arrays of 0s and 1s as described above.
4) Calculate the union and intersection of the two sets and display the result.

code:
```int main(void)
{
int i, j, A, B;
int temp_a = 0;
int temp_b = 0;
//Prompts user for size and values of array A and B
printf("Please enter the number of elements in set A: ");
scanf("%d" , &A);

int a[A];
printf("Enter the numbers in set A: ");
for (i = 0; i < A; i++)
scanf("%d", &a[i]);

//For-loop to organize user input for array A into an ascending order
for (i = 0; i < A; i++)
{
for (j = i + 1; j < A; j++)
if (a[i] > a[j])
{
temp_a = a[i];
a[i] = a[j];
a[j] = temp_a;
}
}

printf("Please enter the number of elements in set B: ");
scanf("%d", &B)/>;

int b[B];
printf("Enter the numbers in set B: ");
for (i = 0; i < B; i++)
scanf("%d", &b[i]);

//For-loop to organize user input for array B into ascending order
for (i = 0; i < B; i++)
{
for (j = i + 1; j < B; j++)
if (b[i] > b[j])
{
temp_b = b[i];
b[i] = b[j];
b[j] = temp_b;
}
}
//Calls function printUnion to get union of A and B
arrayUnion(a, b, A, B)/>;
arrayIntersection(a, b, A, B)/>;
return 0;

```

Is This A Good Question/Topic? 0

## Replies To: How do I store user input as an array of 0's and 1's?

### #2 modi123_1

• Suitor #2

Reputation: 13954
• Posts: 55,699
• Joined: 12-June 08

## Re: How do I store user input as an array of 0's and 1's?

Posted 05 February 2016 - 11:42 AM

Would you be more verbose on what your issue is and what you have tried.

### #3 Lemon77

Reputation: 1
• Posts: 29
• Joined: 14-November 15

## Re: How do I store user input as an array of 0's and 1's?

Posted 05 February 2016 - 11:44 AM

How do I store the values entered by the user as an array of 0's and 1's? Ex: Sets of numbers can be represented using array of 0s and 1s. The idea is that a[i]!=0 if i is in the set, and a[i] == 0 if it is not. For example, the array a[10] = {0, 0, 1, 0, 1, 1, 0, 0, 0, 0}would represent the set{2, 4, 5} because a[2],a[4],and a[5] have the value 1, and everywhere else a contains zeros.
I don't understand how I am supposed to get the user entered value from the array of 0's and 1's if they are reassigned as 0's and 1's.

code:
```int main(void)
{
int i, j, A, B;
int temp_a = 0;
int temp_b = 0;
//Prompts user for size and values of array A and B
printf("Please enter the number of elements in set A: ");
scanf("%d" , &A);

int a[A];
printf("Enter the numbers in set A: ");
for (i = 0; i < A; i++)
scanf("%d", &a[i]);

//For-loop to organize user input for array A into an ascending order
for (i = 0; i < A; i++)
{
for (j = i + 1; j < A; j++)
if (a[i] > a[j])
{
temp_a = a[i];
a[i] = a[j];
a[j] = temp_a;
}
}

printf("Please enter the number of elements in set B: ");
scanf("%d", &B)/>;

int b[B];
printf("Enter the numbers in set B: ");
for (i = 0; i < B; i++)
scanf("%d", &b[i]);

//For-loop to organize user input for array B into ascending order
for (i = 0; i < B; i++)
{
for (j = i + 1; j < B; j++)
if (b[i] > b[j])
{
temp_b = b[i];
b[i] = b[j];
b[j] = temp_b;
}
}
//Calls function printUnion to get union of A and B
arrayUnion(a, b, A, B)/>;
arrayIntersection(a, b, A, B)/>;
return 0;

```

### #4 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,804
• Joined: 19-March 11

## Re: How do I store user input as an array of 0's and 1's?

Posted 05 February 2016 - 11:50 AM

Closing duplicate topic.

### #5 Lemon77

Reputation: 1
• Posts: 29
• Joined: 14-November 15

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 04:25 PM

How do i assign user entered array values into their respective index? For example if a user entered 3 and 5, the array index 3 and 5 will be assigned to 1's and the rest of the array will be 0's.
my code:
``` int a[10] = {0};
printf("Enter the numbers in set A: ");
for (i = 0; i < A; i++)
scanf("%d", &a[i]);

for (i = 0; i < A; i ++)
if (a[i] != 0)
{
temp_a = a[i];
a[temp_a] = 1;
}

```

I am also having trouble printing the indexes with values of 1's. For example, the index at 3 and 5 have values of 1. How would I go about printing the 3 and 5, but excluding repeated indexes (unions)? Is this the right approach? my code:
``` for (i = 0; i < A; i++)
{
if (a[i] == 1)
printf("%d ", i);
else if (a[i] == 1 && b[i] == 1)
printf("%d ", i);
}

```

### #6 Skydiver

• Code herder

Reputation: 6111
• Posts: 21,033
• Joined: 05-May 12

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 05:36 PM

Please do not open new topics about the same problem that you are having. By staying on the same topic and showing us your updated code, we can see the progression of your code and how you are thinking. And even more it shows to us that you are putting forward effort to solve your problem.

### #7 Skydiver

• Code herder

Reputation: 6111
• Posts: 21,033
• Joined: 05-May 12

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 05:47 PM

Anyway, your basic problem is that you are not implementing what you said the idea was:

Lemon77, on 05 February 2016 - 01:41 PM, said:

The idea is that a[i]!=0 if i is in the set, and a[i] == 0 if it is not. For example, the array a[10] = {0, 0, 1, 0, 1, 1, 0, 0, 0, 0}wouldrepresenttheset{2, 4, 5} becausea[2],a[4],anda[5] have the value 1, and everywhere else a contains zeros.

What you are currently doing is reading in values into a[i]. Instead, you should be setting a[i] to be 1.

I can see partly where the confusion stems... Most programming teachers use the variable i as an iterator counter, but the assignment is using the variable i as an index into the array. Try this variation of the idea:

Lemon77, on 05 February 2016 - 01:41 PM, said:

The idea is that a[n]!=0 if n is in the set, and a[x] == 0 if it is not. For example, the array a[10] = {0, 0, 1, 0, 1, 1, 0, 0, 0, 0} would represent the set {2, 4, 5} because a[2], a[4], and a[5] have the value 1, and everywhere else a contains zeros.

### #8 Lemon77

Reputation: 1
• Posts: 29
• Joined: 14-November 15

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 06:13 PM

I am still quite confused. The instructions say to get the user input for the numbers in the set. Does that mean the index? the index at the user entered value would be 1?

### #9 Lemon77

Reputation: 1
• Posts: 29
• Joined: 14-November 15

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 07:10 PM

Would this be correct?

``` int a[10] = {0};
printf("Enter the numbers in set A: ");
for (i = 0; i < A; i++)
{
scanf("%d", &n);
if (n >= 0)
a[n] = 1;
}

```

### #10 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,683
• Joined: 08-August 08

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 07:13 PM

Write a function to output the contents of the array. Do the correct values get set?

### #11 Lemon77

Reputation: 1
• Posts: 29
• Joined: 14-November 15

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 07:45 PM

I seem to have correctly indexed all the values. My only problem now is printing the output. It is printing the array length when I only need it to print the index with values of 1, in this case the number elements the user entered. In the program, I asked the user to enter the number of elements in both arrays (i used 'A' and 'B'). I tried to create a variable 'max' and gave the value to the greater of the two (A and . I tried to implement max into a for loop in the function arrayUnion but it is still printing out 10 values (both of the array length is 10).

This is the function arrayUnion
```int i = 0;
printf("The union of set A and B is: ");
for (i = 0; i <	max; i++)
{
if (a[i] == 1)
printf("%d ", i);
else if (b[i]	== 1)
printf("%d ", i);
else if (a[i]	== 1 &&	b[i] ==	1)
printf("%d ", i);
}

```

### #12 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 08:38 PM

Hi, your instructions seem to suggest that the array is fixed in size - and being so, the values in the range are restricted. So arrays would be of the same size.

You can picture the relationship between the index value and the set value :

```indexed values{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
array a[10] = {0, 0, 1, 0, 1, 1, 0, 0, 0, 0}
represents set{-, -, 2, -, 4, 5, -, -, -, -}

```

So a 1 in the array says yes this (index) value is present in the set.

### #13 loudmouth

Reputation: 0
• Posts: 6
• Joined: 06-February 16

## Re: How do I store user input as an array of 0's and 1's?

Posted 06 February 2016 - 09:41 PM

Whats wrong with my code for this?
I used the above advice for filling the array and setting numbers that occur while scanning to 1 but when i use the
It prints too many numbers for union and sometimes for intersection. How do I get around this?

```printf("How many numbers are in the first set (set A)? ");
scanf("%d", &a_length);
int a[a_length];
for (i = 0 ; i < a_length; i++){
printf("Please enter the digit number %d in set A: ", i+1);
scanf("%d", &v);
if (v > 0)
a[v] = 1;
}

printf("How many numbers are in the second set (set B)/>? ");
scanf("%d", &b_length);
int b[b_length];
for (i=0 ; i < b_length; i++){
printf("Please enter the digit number %d in set B: ", i+1);
scanf("%d", &k);
if (k > 0)
b[k] = 1;
}

/*prints the union of the sets*/
printf("The union of sets A and B: ");
for (i=0; i <10; i++){
if (a[i]==1 )
printf(" %d", i);
else if(b[i]==1)
printf(" %d", i);
else if(a[i]==1 && b[i]==1)
printf(" %d", i);
}
printf("\nThe intersection of sets A and B: ");
for (i =0; i <10; i++){
if (a[i]==1 && b[i]== 1)
printf(" %d", i);
}

```

### #14 Skydiver

• Code herder

Reputation: 6111
• Posts: 21,033
• Joined: 05-May 12

## Re: How do I store user input as an array of 0's and 1's?

Posted 07 February 2016 - 08:33 AM

I don't use VLA's so I don't know if they guarantee that the array will be zero-filled when allocated:
```int a[a_length];
:
int b[b_length];
:

```

### #15 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,683
• Joined: 08-August 08

## Re: How do I store user input as an array of 0's and 1's?

Posted 07 February 2016 - 09:16 AM

Your code seems to assume that no number in the set will be outside the range 0 to (a_length-1). If you have this set:
{1,2,3,2}
or this one:
{0,4,6}
Will the code work? I'm guessing it might work on the first, but not the second.