0

I am trying to write a code in C that will print at a given range (0 - 100).

x would only print from 0 to 3, and then 8 to 11, and then 16 to 19 and so on. y would only print the rest, for example from 4 to 7, and then 12 to 15, and then 20 to 23 and so on.

The output should look like this:

x = 0 1 2 3 8 9 10 11 ... 92 93 94 95   
y = 4 5 6 7 12 13 14 15 ... 96 97 98 99
blackbeard
  • 385
  • 2
  • 12

3 Answers3

1

Using % won't get you far... k = i % divisor; will make sure k is somewhere between [0,4] (since divisor = 4) which is not what you want.

What about using 2 loops?

int main(){
  int i,j;
  for(i = 0; i < 100; i+=8){   // increment by 8 to make 0 -> 8 -> 16 jumps
    for(j = 0; j < 4; j++){    // print 4 values for x and y, starting from the initial value i, and i+4
      printf("x = %d\ty = %d\n", i+j, i+j+4);
    }
  }
    return 0;
}
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

The problem specification is self-inconsistent. It specifies:

x = 0 1 2 3 8 9 10 11 ... 93 94 95 96
y = 4 5 6 7 12 13 14 15 ... 97 98 99 100

There are at least two problems:

  1. Groups of 4 should start on even numbers, so the last sets are wrongly grouped.
  2. The x line should end with 96 97 98 99 and the y line should end with 92 93 94 95 (and maybe 100 too, if that should be included in the output).

The code in the question is flawed because of the way it uses k = i; k = i % divisor; and because there isn't the necessary nested loop structure. I suppose that strictly you could write the code without nested loops, but using nested loops is simpler.

The two lines are a bit inconveniently long for display, so it would be better, perhaps, to wrap them at a suitable length. The code below deals with these various issues. It exploits the fact that printf() returns the number of characters printed. It ignores the possibility that printf() returns an error (a negative return value).

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

/*
** tag      = string at start of line
** start    = starting value
** finish   = last value to be printed
** major    = major increment
** minor    = number of elements in each sub-sequence
** num_wid  = width to use for numbers (1 for minimum width)
** line_wid = wrap lines after this width
*/
static void print_sequence(const char *tag, int start, int finish,
                    int major, int minor, int num_wid, int line_wid)
{
    int count = printf("%s =", tag);
    for (int i = start; i <= finish; i += major)
    {
        for (int j = 0; j < minor && i + j <= finish; j++)
        {
            if (count > line_wid)
            {
                putchar('\n');
                count = printf("%*s  ", (int)strlen(tag), "");
            }
            count += printf(" %*d", num_wid, i + j);
        }
    }
    putchar('\n');
}

int main(void)
{
    print_sequence("x", 0, 100, 8, 4, 2, 70);
    print_sequence("y", 4, 100, 8, 4, 2, 70);

    print_sequence("x", 0, 100, 8, 4, 2, 160);
    print_sequence("y", 4, 100, 8, 4, 2, 160);

    print_sequence("x", 0, 100, 8, 4, 3, 70);
    print_sequence("y", 4, 100, 8, 4, 3, 70);

    return 0;
}

Example output:

x =  0  1  2  3  8  9 10 11 16 17 18 19 24 25 26 27 32 33 34 35 40 41 42
    43 48 49 50 51 56 57 58 59 64 65 66 67 72 73 74 75 80 81 82 83 88 89
    90 91 96 97 98 99
y =  4  5  6  7 12 13 14 15 20 21 22 23 28 29 30 31 36 37 38 39 44 45 46
    47 52 53 54 55 60 61 62 63 68 69 70 71 76 77 78 79 84 85 86 87 92 93
    94 95 100
x =  0  1  2  3  8  9 10 11 16 17 18 19 24 25 26 27 32 33 34 35 40 41 42 43 48 49 50 51 56 57 58 59 64 65 66 67 72 73 74 75 80 81 82 83 88 89 90 91 96 97 98 99
y =  4  5  6  7 12 13 14 15 20 21 22 23 28 29 30 31 36 37 38 39 44 45 46 47 52 53 54 55 60 61 62 63 68 69 70 71 76 77 78 79 84 85 86 87 92 93 94 95 100
x =   0   1   2   3   8   9  10  11  16  17  18  19  24  25  26  27  32
     33  34  35  40  41  42  43  48  49  50  51  56  57  58  59  64  65
     66  67  72  73  74  75  80  81  82  83  88  89  90  91  96  97  98
     99
y =   4   5   6   7  12  13  14  15  20  21  22  23  28  29  30  31  36
     37  38  39  44  45  46  47  52  53  54  55  60  61  62  63  68  69
     70  71  76  77  78  79  84  85  86  87  92  93  94  95 100

You probably shouldn't submit this code as the answer to the homework assignment. It is probably too configurable for it to be safe.

Single loop code

This implementation of the print_sequence() function presents the same external interface, but the implementation is a bit different.

static void print_sequence(const char *tag, int start, int finish,
                    int major, int minor, int num_wid, int line_wid)
{
    assert(major > 0 && minor > 0 && major > minor);
    int count = printf("%s =", tag);
    int p_limit = start + minor;        // print limit
    int c_limit = start + major - 1;    // count limit
    for (int i = start; i <= finish; i++)
    {
        if (i < p_limit)
        {
            if (count > line_wid)
            {
                putchar('\n');
                count = printf("%*s  ", (int)strlen(tag), "");
            }
            count += printf(" %*d", num_wid, i);
        }
        else if (i == c_limit)
        {
            p_limit += major;
            c_limit += major;
        }
    }
    putchar('\n');
}

This produces the same output as before. It is marginally less efficient than the original, but you'd be hard pressed to measure the cost of incrementing i a few extra times around the loop.

Also note that both these solutions can print up to num_wid + 1 characters beyond the specified line length. You can fix that by adjusting line_wid by num_wid + 1. The code could also check that major is at least as big as minor, and that both are strictly positive; also that line_wid is big enough for the tag, the = and at least one number. There's an element of GIGO — if you provide bogus parameters, the output will be correspondingly bogus.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
-1

I've written a code that has a two lined output The first line will consist of x values and the second consists of y values. Hope this would help you out.

#include<stdio.h>

int main() {


    int lim, i,j;
    int limit = 100;

    printf("x = ");

    for(i = 0; i <= limit; i = i + 8){
       lim = i;
       for (j = i; j <= lim + 3; j++){ 
           printf("%d ",j);
    }
 }
   printf("\n");


   printf("y = ");

   for(i = 4; i <= limit; i = i + 8){
   lim = i;

    for (j = i; j <= lim + 4; j++){ 

      if (j > 100)
      break;

      printf("%d ",j);
    }
  } 

}

Let me explain:

Here I've written 2 for loops because according to the sequence, the 4 consecutive numbers have a difference of 8 between them. As this sequence has a sub sequence, it is necessary to use 2 for loops.The elements to be removed also follows a similar pattern so, I printed them too.

VVV
  • 47
  • 6
  • Why do you have `lim` as an alias for `i`? Why do you have separated conditions (`for` limit and an `if-break`) to exit the second loop? – Prune Jul 25 '17 at 17:51
  • Yes i agree that using lim was of no use. I used it just for the sake of better understanding. The break statement is necessary without which the inner loop will print numbers greater than limit = 100. (That is, 101 102 103 104) – VVV Jul 26 '17 at 18:07