cristid9 cristid9 - 18 days ago 5
C Question

Bad exercise statement

Given the problem:

enter image description here

The statement is a bit ambiguous. I don't really understand what they want.
I can display the desired result using just a regular

for loop
:

int step = 0
for(int i = 1; i < m + 1; i++)
{
if(i != p)
{
printf("(%d, %d)", step, i);
step++;
}
}


Is this what they really want? I see that they are talking about linear time, so I think it can't be that easy? Am I supposed to build the vector that they are talking about, and then delete it?

Answer

Something like this? (compile with gcc test.c -lm)

#include <stdio.h>
#include <math.h>

int main(void) {
    int i, j;
    int n = 4;
    int m = (int)pow(2,n);
    int p = 5;

    for(i = 1; i <= n; i++)
    {
        for(j = (int)pow(2,i-1); j < ((int)pow(2,i)); j++)
        {
            if(j == p) {
                continue;
            }
            printf("%d, %d\n", i, j);
            // remove vector[j]
            if((j == (m-1)) && (m != p)) {
                printf("%d, %d\n", i, m);
                // remove vector[m]
            }
        }
    }
    return 0;
}

Run result n=3, p=5:

1, 1
2, 2
2, 3
3, 4
3, 6
3, 7
3, 8

Run result n=4, p=5:

1, 1
2, 2
2, 3
3, 4
3, 6
3, 7
4, 8
4, 9
4, 10
4, 11
4, 12
4, 13
4, 14
4, 15
4, 16