1. ## Lost on columns

I have to put some output in columns, but I don't even know where to get started. I just wondered if anybody could tell me what I should read about or how I may I get started, that would be a really big help, thanks.
Basically, its just numbers but instead of the output being something like:
1
2
3
4
5
and so on, it would look like:

1 2 3 5

6 7 8 9... and so on

thanks,
Extro

2. Perhaps you could post the source code for the attempt you have made. Then we can help. If you need a hint to get started, think about how you might use some sort of a loop, and the % (modulus) operator to generate the output you are looking for.

~/

3. This is the code:

Code:
```include <stdio.h>

int main (){
int	open=0, skip;
int	cnt = 0, skipped=0;

printf ("How many mail boxes does he skip?\n");
scanf("%d" , &skip);

while (cnt < 150){
cnt++;
if (cnt % skip == 0){
open++;
printf("%d = closed\n", cnt);
}else{
skipped++;
printf("%d = open\n", cnt);
}
}
printf("Opened a total of %d boxes\nSkipped a total of %d boxes\n", open, skipped);

return 0;
}```
But right now it outputs it:

1 = open

2 = open
3= open
4-closed

etc... depnding on the input
but I need it to display it like:

1 = open 2 = open 3 = open 4 = open

5 = closed 6 = closed 7 = open

and so on to 150.

Most of the instructions I have found so far on how to do this are hard to understand.
Thanks,
Extro

4. The \n in those printf() statements will cause newline character to be written. As you're doing it in both printf()s, you'll be starting a newline every time.
Use the modulos operator to determine when to start a newline, and output a \n then only.

5. You mean something like this?

Code:
```

#include <stdio.h>

int main (){
int	open=0, skip;
int	cnt = 0, skipped=0;
printf ("How many mail boxes does he skip?\n");
scanf("%d" , &skip);

while (cnt < 150){
cnt++;
if (cnt % skip == 0){
open++;
printf("%5d\n = closed\n", cnt);
}else{
skipped++;
printf("%5d\n = open\n", cnt);
}
}
return 0;
}```

6. After compiling your (above) code, did you find it did what you wanted? What Hammer suggested will work for you - get rid of the newlines, and use the modulus operator to determine when you need to insert the newline - from what I understand, you want a newline after every fourth column. So you ought to be checking your code something like this:

Code:
`some_var % 4`
edit::

You are iterating through your loop, in this case, 150 times - every time you go through the loop, increment a counter, and every fourth time, insert a newline.

~/

7. So far, it isnt working, although I understand what your saying it doesnt seem to wanna work
Mostly I'm having a hard time figuring out where the modulus should go
Thanks,
Extro

8. Originally Posted by Extropian
So far, it isnt working, although I understand what your saying it doesnt seem to wanna work
Mostly I'm having a hard time figuring out where the modulus should go
Thanks,
Extro
Perhaps you could do a simpler program, which prints out the numbers 1 to 20 in four columns - if you can get that to work, you will likely see how to modify this program to work the way you want it to.

~/

9. Im sorry but I wouldnt know how? where does the mod operator go to create 4 columns? I dont mean to bother but Im really stuck

10. Try something like this:

Code:
```// Loop through all values in the array.
for (i = 0; i < size; i++) {

// Print the current value.
printf ("%i", array[i]);

// If the current index is the end of a row (of size width), print a newline.
if (i % width == width - 1)
printf ("\n");

// Otherwise print a tab character to make spacing clean.
else
printf ("\t");
}```

11. can somebody attempt this for me a little? I hate asking but I'm hoping if I see it maybe I'll understand because this is just not sinking into the my little brain lol
Thanks.

12. Well, methods have already been suggested, but I'll try this.
Code:
```#include <stdio.h>

int main(void)
{
int array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
size_t i, size = 4;
for ( i = 0; i < sizeof array / sizeof *array; ++i )
{
printf("%3d", array[i]);
if ( i % size == size - 1 )
{
putchar('\n');
}
else
{
putchar(' ');
}
}
putchar('\n');
return 0;
}

/* my output
1   2   3   4
5   6   7   8
9  10  11  12
13  14  15  16
17  18
*/```

13. Thanks, I'm really kind of a see and learn guy. I had a hunch it was an array but couldn't get it to work.
Thanks a bunch,
Extro

14. It doesn't have to be an array, I just figured you had an array of values.

Here it is with just plain sequential integers:
Code:
```// Loop through all values in the array.
for (i = 0; i < max; i++) {

// Print the current value.
printf ("%i", i);

// If the current index is the end of a row (of size width), print a newline.
if (i % width == width - 1)
printf ("\n");

// Otherwise print a tab character to make spacing clean.
else
printf ("\t");
}```

15. Originally Posted by Extropian
I had a hunch it was an array but couldn't get it to work.
It need not be.
Code:
```#include <stdio.h>

int main(void)
{
static const char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if (file)
{
int value;
size_t i = 0, size = 4;
while ( fscanf(file, "%d", &value) == 1 )
{
printf("%3d", value);
if ( i % size == size - 1 )
{
putchar('\n');
}
else
{
putchar(' ');
}
++i;
}
putchar('\n');
}
return 0;
}

/* file.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
*/

/* my output
1   2   3   4
5   6   7   8
9  10  11  12
13  14  15  16
17  18
*/```