Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple Loop Question

39 views
Skip to first unread message

G. B

unread,
Jun 3, 2019, 12:10:59 PM6/3/19
to
This is the starting code:

for (int i = 0; i < 10; i++)
{
for (int j = 1; j < 7; j++)
{
// Console.Write("{0} ", j);
printf("%d",j);
}
// Console.WriteLine();
cout << "\n";
}

This code will produce:

1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

How do I change it {neatly} so that it produces:

1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
37 38 39 40 41 42
43 44 45 46 47 48
49 50 51 52 53 54
55 56 57 58 59 60



Ben Bacarisse

unread,
Jun 3, 2019, 12:53:45 PM6/3/19
to
"G. B" <g...@gb.com> writes:

> This is the starting code:

Questions like this are best not cross-posted. The solutions might
become radically different in C and in C++. I've set followup-to
comp.lang.c as my answer is in C.

> for (int i = 0; i < 10; i++)
> {
> for (int j = 1; j < 7; j++)
> {
> // Console.Write("{0} ", j);
> printf("%d",j);
> }
> // Console.WriteLine();
> cout << "\n";
> }
>
> This code will produce:
>
> 1 2 3 4 5 6

No, there is no space in the printf so you don't get any in the output!

> 1 2 3 4 5 6
> 1 2 3 4 5 6
> 1 2 3 4 5 6
> 1 2 3 4 5 6
> 1 2 3 4 5 6
> 1 2 3 4 5 6
> 1 2 3 4 5 6
> 1 2 3 4 5 6
> 1 2 3 4 5 6
>
> How do I change it {neatly} so that it produces:
>
> 1 2 3 4 5 6
> 7 8 9 10 11 12
> 13 14 15 16 17 18
> 19 20 21 22 23 24
> 25 26 27 28 29 30
> 31 32 33 34 35 36
> 37 38 39 40 41 42
> 43 44 45 46 47 48
> 49 50 51 52 53 54
> 55 56 57 58 59 60

I'm guessing you don't want the crazy spacing. I'd just have one loop
to numbers from 1 to 60 and a newline when the counter is = 0 mod 6:

for (int i = 1; i <= 60; i++)
printf("%-6d", i, i % 6 ? "" : "\n");

(I've guessed how you want the single-digit numbers aligned.)

--
Ben.

😉 Good Guy 😉

unread,
Jun 3, 2019, 12:54:06 PM6/3/19
to

Can something like this work for you?

#include <stdio.h>
int main()
{
    int i = 0;

    for (i = 1; i < 61; i++)
    {
        printf("%2d  ", i);
        if (i % 6 == 0)
            printf("\n");
    }

    return 0;
--
With over 950 million devices now running Windows 10, customer satisfaction is higher than any previous version of windows.

F. Russell

unread,
Jun 3, 2019, 1:11:33 PM6/3/19
to
On Mon, 03 Jun 2019 12:10:00 -0400, G. B vomited this verbiage that I
considered to be worthy of my attention for one of a variety of possible
reasons:

>
> How do I change it {neatly}
>

printf("%d",j+i*6);

Szyk Cech

unread,
Jun 3, 2019, 1:56:15 PM6/3/19
to
You are very kind to this lazy student. Please don't reply futher such
stupid questions - if you do it poster will never gain motivation to
work on him self to be wise...

Sjouke Burry

unread,
Jun 3, 2019, 6:27:47 PM6/3/19
to
On 03.06.19 18:10, G. B wrote:
> This is the starting code:
>
> for (int i = 0; i < 10; i++)
> {
> for (int j = 1; j < 7; j++)
> {
> // Console.Write("{0} ", j);
> printf("%d",j);
> }
> // Console.WriteLine();
> cout << "\n";
cut

The c++ newsgroup is around the corner.

james...@alumni.caltech.edu

unread,
Jun 3, 2019, 6:48:59 PM6/3/19
to
comp.lang.c++ is right here - were you referring to some other
newsgroup? That message was cross-posted to both comp.lang.c and
comp.lang.c++.
While his message contains code that's specific to C++, his
question was not about that part of the code. If that part had
been written to use only <stdio.h> functions, it would have been
entirely appropriate to cross-post the message.

Sam

unread,
Jun 3, 2019, 9:52:57 PM6/3/19
to
G. B writes:

> This is the starting code:
>
> for (int i = 0; i < 10; i++)
> {
> for (int j = 1; j < 7; j++)
> {
> // Console.Write("{0} ", j);
> printf("%d",j);
> }
> // Console.WriteLine();
> cout << "\n";
> }
>
> This code will produce:
>
> [ ... ]
>
> How do I change it {neatly} so that it produces:
>
> [ ... ]

Your first step is to decide which programming language you wish to use, C
or C++, and use either the C library's "printf()" or the formatted output
operator, <<, from C++.

Mixing both, in this fashion, produces unspecified behavior, and is a bug.


Öö Tiib

unread,
Jun 4, 2019, 6:53:33 AM6/4/19
to
IMHO neither ostream << nor ::printf have not much to be proud of.
Consider:

#include <iostream>
#include <stdio.h>
#include <cstdint>

int main()
{
std::uint8_t answer = 42;
std::cout << "\"" << answer << "\"\n";
::printf("\"%d\"\n", answer);
}

The << gives wrong answer while printf is type unsafe.
Performance of both is typically mediocre and so ... may
be useful for debug tracing.

Juha Nieminen

unread,
Jun 4, 2019, 10:26:19 AM6/4/19
to
Öö Tiib <oot...@hot.ee> wrote:
> std::uint8_t answer = 42;
> std::cout << "\"" << answer << "\"\n";

We need a strongly typed std::uint8_t that's different from
an unsigned char.
0 new messages