-2

I have the following code but it is not working correctly:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>

#define MAXN 100
const unsigned n=4;
const unsigned k=2;
int taken[MAXN];

void print(unsigned i)
{
    unsigned l;
    printf(" ( ");
    for (l=0; l<=i-1; l++) printf("%u ", taken[l] + 1);
    printf(")\n");
}

void variate(unsigned i) 
{
    unsigned j;
    if (i>=k) 
    {
        print(i);
        return;
    }
    for (j=0;j<n;j++)
        {
        taken[i]=j;
        variate(i+1);
        }    
}

void condition(unsigned number) 
{
    unsigned j;
    if (number>=k) 
    {
        print(number);
        return;
    }
    for (j=0;j<n;j++)
        {
        taken[number]=j;
        variate(number+1);
        }

}

int main(void)
{
    variate(0);

      int number;
      printf("Enter number from 1 to 4: \n", number);
      scanf("%d", &number);

      printf("All varians of the combinations with your number are: \n");
    condition(0);

 system ("pause");
  return 0;
}

The program is printing all possible combinations of the numbers 1, 2, 3 and 4 and it works correctly. But the void condition is not working fine. After printing all possible combinations of the four numbers the user have to enter a number between 1 and 4 and all combinations with user's number have to appear and none of the rest of the combinations.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • You forgot to ask a question. – David Schwartz Jan 19 '19 at 11:31
  • A "void condition" is a condition without a result, and that's impossible in C. All conditions return a result. As for how to solve your problem, you need to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). With a debugger you can step through the code line by line while monitoring variables and their values. – Some programmer dude Jan 19 '19 at 11:32
  • It looks like C code but the headers included are C++. – Weather Vane Jan 19 '19 at 11:34
  • @Someprogrammerdude I suppose the sentence refers to the function `void condition(unsigned number)` – Weather Vane Jan 19 '19 at 11:35
  • [`printf("%u ", taken[l] + 1);` invokes UB](https://stackoverflow.com/q/16864552/995714) since the parameter is `int`, not `unsigned int`. And in `printf("Enter number from 1 to 4: \n", number);` you don't have any specifier for `number` – phuclv Jan 19 '19 at 13:08

1 Answers1

0

More the remarks already done there are several strange things in your program

  • the read number is never used, so there is no way to give a result depending on it
  • you modify taken to give all the couples but you do not reset it before to give the couples with the read number, supposing taken is useful how that can works in this case ?
  • and globally why so complicated ?

You can do like that, with two ways to do the second part :

#include <stdio.h>

#define MIN 1
#define MAX 4

int main()
{
  /* print all couples */
  for (unsigned i = MIN; i <= MAX; ++i) {
    for (unsigned j = MIN; j <= MAX; ++j) {
      printf("(%u %u)\n", i, j);
    }
  }

  unsigned number;
  printf("Enter number from %d to %d\n", MIN, MAX);
  scanf("%u", &number);

  /* first way following the same order as before */
  puts("same order");

  if ((number >= MIN) && (number <= MAX)) {
    for (unsigned i = MIN; i <= MAX; ++i) {
      for (unsigned j = MIN; j <= MAX; ++j) {
        if ((i == number) || (j == number))
          printf("(%u %u)\n", i, j);
      }
    }
  }

  /* an other way, faster but not in the same order */
  puts("different order");

  printf("(%u %u)\n", number, number);
  for (unsigned i = MIN; i < number; ++i) {
    printf("(%u %u)\n(%u %u)\n", number, i, i, number);
  }
  for (unsigned i = number + 1; i <= MAX; ++i) {
    printf("(%u %u)\n(%u %u)\n", number, i, i, number);
  }

  return 0;
}

Execution :

(1 1)
(1 2)
(1 3)
(1 4)
(2 1)
(2 2)
(2 3)
(2 4)
(3 1)
(3 2)
(3 3)
(3 4)
(4 1)
(4 2)
(4 3)
(4 4)
Enter number from 1 to 4
2
same order
(1 2)
(2 1)
(2 2)
(2 3)
(2 4)
(3 2)
(4 2)
different order
(2 2)
(2 1)
(1 2)
(2 3)
(3 2)
(2 4)
(4 2)
bruno
  • 32,421
  • 7
  • 25
  • 37