0

I am working on a C program that deals with sets, and I am having trouble getting the values for an array in my C program. I am thinking that there is a logical error in the function below.

sizeA is 26, and setA is a boolean set of size 26.

Here is how the function should turn out if sizeA is 5:

Enter the first element in Set A: //user enters h

Enter the next element of Set A: //user enters i

Enter the next element of Set A: //user enters j

Enter the next element of Set A: //user enters k

Enter the next element of Set A: //user enters l

fffffftttttffffffffffffff

However it turns out like this if sizeA is 5:

Enter the first element in Set A: //user enters h

Enter the next element of Set A: //user enters i

Enter the next element of Set A: //user enters j

Enter the next element of Set A: //user enters k

Enter the next element of Set A: //user enters l

ffffff 

I would like to know how to fix this problem.

Here is the code:

void getSetA(bool setA[], int sizeA)
{
      letters element, letter;
      int position = 0, num, i;

      for(i = 0; i < sizeA; i++)      //sizeA is inputted before
      {
            setA[i] = FALSE;      
      }
      printf("\nEnter the first element in Set A: ");
      element =  getcharNoBreaks();
      if (element >= 'a' && element <= 'z') 
      {
              setA[element-'a'] = TRUE;
      }
      for(num = 1; num < sizeA; num++)
      {
            printf("\nEnter next element of Set A: ");
            element =  getcharNoBreaks();
            if(element >= 'a' && element <= 'z')
            {
                       setA[element - 'a'] = TRUE;          
            }
            else       printf("Element out of range");
      }
      printf("\n");
      for(i = 0; i < sizeA; i++)
      {
            if(setA[i] == TRUE) printf("t");   
            else                printf("f");     
      }      
}

Note that letters, is a type I have defined (as all the letters of the alphabet), and getcharNoBreaks() is a function that is equivalent to getchar().

Thank you :)

5
  • 1
    So now what should the function do? "It should print ffffffftttttfffffff but it prints ffffff" is not quite helpful for us... Commented Aug 5, 2013 at 13:24
  • 1
    puts("fffffftttttffffffffffffff") Commented Aug 5, 2013 at 13:25
  • But from what I can tell, this code will actually result in undefined behavior, as setA (I presume) is an array of only 5 elements, but indexing with 'l' - 'a' is index 11, which is clearly out of bounds. Commented Aug 5, 2013 at 13:27
  • @H2CO3 The function is meant to print the boolean array; for example if setA[0] = true, setA[1] = false, it will print tf. The size of the array is 26. Commented Aug 5, 2013 at 13:39
  • @Dukeling In the boolean array, the user enters an element, and for them the true flag will be set. The size of the array is 26. There fore if the user enters h,i,j,k,l, the true flag will be set for those letters Commented Aug 5, 2013 at 13:41

2 Answers 2

1

So , 'h' - 'a' = (int) 7 and if 'z' - 'a' = 25. Right? Question is :

. Is ths size of setA big enough?

If it a big array . Add printf("intput c is :%c\n",element); You should check what elem really was indeed.

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

1 Comment

the size of setA is 26
1

void getSetA(bool setA[], int sizeA) - sizeA should be 26. everything else looks fine.

As i understood you program works with set of english letters a..z, user enters some letters that will be enabled - for them true flag will be set.

4 Comments

Yes you understood correctly, but I do not understand how to fix it?
@Paul Filch, make sizeA const = 26 and you need setA[] of size 26 as you are working with 26 elements
The problem is that, sizeA is inputted by the user in the other part of the program. The program at first will ask how many letters you want to have in a set. Plus, set A is already size 26, I initialized it first like this: setA[26]
@Paul Filch you should consider to change logic of you function. User shoudn`t provide size of the set, as it always 26 until some reform in English language will took place and additional letters will appear which is very unlucky)). just ask user to enter empty string or some special symbol to show that he has done with an input

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.