1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0

#### C programming- recursion

Hi
I need to write a program that generates all k-element subsets of a set of numbers from 1 to n
This is my source code
#include <stdio.h>
#include <stdlib.h>

void podzbiory2(int *Tab, int *Komb, int n, int k, int pos, int delta)
{
int i;
//if we chose a number of elements equal to k
if (pos == k)
{
for(i=0;i<k;i++)
{
printf("%2d ", Komb[i]);
}
printf("\n");
}
else if(pos < k && delta < n)
{
// delta detemines the iterations of the loop
// so as not to take i.e. 2 3 3
for(i=delta;i < n;i++)
{
Komb[pos] = Tab[i];
podzbiory2(Tab, Komb, n, k, pos+1, i+1);
}
}
}

void podzbiory(int *Tab, int n,int k)
{

int *Komb;
Komb = malloc(k* sizeof(int));

podzbiory2(Tab, Komb, n, k, 0,0);
free(Komb);
}

int main()
{
//czytaj zmienne
int n=0;
int k=0;
int *Tab;
int i;

printf("Proszę podać N: ");
scanf("%d", &n);

printf("Proszę podać K: ");
scanf("%d", &k);

//invalid conditions
if(n<1 || k<1 || k>n)
{
}

//initiate tables of elements and fill them with numbers from 1 to n
Tab = malloc(n* sizeof(int));

for(i=0;i<n;i++)
{
Tab[i] = i+1;
}

podzbiory(Tab, n, k);
free(Tab);

printf("\n");
return 0;
}
when i compile this code i get a bunch of warnings null characters ignored enabled by default
and 1 warning "unknown escape sequence '/000' enabled by default
Could somebody help me with this?
Thanks,
2. I believe the warnings you are getting are caused by whatever editor you used inserting hidden characters.

The more serious problems are the two uses of malloc. You need to perform a cast on those.
3. You need to use code tags in order to preserve your code's indentation. Without code tags, this forum's HTML will strip out all leading spaces, making your code unreadable.

This is what code tags are and how to use them:

[code] < copy-and-paste your code listing here > [/code]

This is what your code would have looked like with code tags (original indentation retrieved via the Reply button):
Code:
```#include <stdio.h>
#include <stdlib.h>

void podzbiory2(int *Tab, int *Komb, int n, int k, int pos, int delta)
{
int i;
//if we chose a number of elements equal to k
if (pos == k)
{
for(i=0;i<k;i++)
{
printf("%2d ", Komb[i]);
}
printf("\n");
}
else if(pos < k && delta < n)
{
// delta detemines the iterations of the loop
// so as not to take i.e. 2 3 3
for(i=delta;i < n;i++)
{
Komb[pos] = Tab[i];
podzbiory2(Tab, Komb, n, k, pos+1, i+1);
}
}
}

void podzbiory(int *Tab, int n,int k)
{

int *Komb;
Komb = malloc(k* sizeof(int));

podzbiory2(Tab, Komb, n, k, 0,0);
free(Komb);
}

int main()
{
//czytaj zmienne
int n=0;
int k=0;
int *Tab;
int i;

printf("Proszę podać N: ");
scanf("%d", &n);

printf("Proszę podać K: ");
scanf("%d", &k);

//invalid conditions
if(n<1 || k<1 || k>n)
{
}

//initiate tables of elements and fill them with numbers from 1 to n
Tab = malloc(n* sizeof(int));

for(i=0;i<n;i++)
{
Tab[i] = i+1;
}

podzbiory(Tab, n, k);
free(Tab);

printf("\n");
return 0;
}```
I did not get any warnings when I compiled that code (MinGW gcc will all warnings turned on, run on WinXP). If bullet's guess is correct and you are using a word processor or a text editor in a mode that embeds formatting information, then you need to instead use a simple text editor.

For that matter, it would help us to know what editor you are using.