I need a simple algorithm that generates all possible test cases for a certain value of inputs. For example, inputs a and b, would have possible cases being: 0,0 0,1 1,0 and 1,1. The number of inputs should be 2^n, but I am having trouble figuring out an algorithm in C that would do this.
Asked
Active
Viewed 56 times
0
-
Just iterate over each possible combination. In pseudocode: for a in 0 1 { for b in 0,1 { use a and b } } – KamilCuk Nov 30 '21 at 21:51
-
What has your class studied recently? Do you know how to use the operators `|`, `&`, and `>>`? How will you know how many inputs there are? Do you know how to declare an array and how to put values into array elements? What specific parts of the task are you having trouble with? Also, if the number of inputs should be 2^n, what is n? Or do you mean the number of inputs is n, and the number of combinations of values is 2^n? If there are 2^n possible cases formed from 0s and 1s, can you think of any way to represent those cases, e.g., some way to arrange bits to specify one of those cases? – Eric Postpischil Nov 30 '21 at 22:02
-
@EricPostpischil yes I have used those operators and the number of inputs are given, but not at compile time. I do know how to declare an array using malloc. The part I am having trouble is finding out an algorithm that will generate all possible test cases for the number of given inputs. Finally, yes I did mean that number of inputs is n, and the number of combinations of values is 2^n. – Alex Harris Dec 01 '21 at 19:25
-
@AlexHarris: So you have n elements each of which must have a value of 0 or 1. That can be represented as a string of n 0s or 1s. A string of 0s or 1s can be held in an integer, and its binary representation is the string of 0s or 1s. The operators you have learned can be used to extract individual bits from an integer, and there is a very easy way to iterate through all the cases from all 0s to all 1s. – Eric Postpischil Dec 01 '21 at 20:04
-
@EricPostpischil So how would you do so in terms of a loop that knows when all possible cases have been generated? – Alex Harris Dec 01 '21 at 20:16
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Dec 05 '21 at 22:09