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 PM Rate Topic: -----

#1 Lemon77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.

1) Name your program sets.c.
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  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13954
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Lemon77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;



Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11022
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Lemon77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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);
        }



Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • 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

You already have a topic open about this. Merging threads...

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.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • 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:

View PostLemon77, 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:

View PostLemon77, 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.

Was This Post Helpful? 0
  • +
  • -

#8 Lemon77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#9 Lemon77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;
        }


Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3777
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#11 Lemon77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 B). 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);
        }


Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 loudmouth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}
 

Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • 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];
:


Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3777
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2