Is there any suggestion to rewrite these simple codes?

J

Junmin H.

Hello, just wrote down 4 simple codes to print numbers with order
for practice in C.

Just would like to know if you guys have any suggestion about them.
thanks

Junmin


=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=
#include <stdio.h>

main(void){
int i = 0;
while(i < 9){
int j = 0;
while(j < i){
printf("- ");
++j;
}
int x = ++i;
while(x <= 9){
printf("%d ",x);
++x;
}
printf("\n");
}
return 0;
}

OUTPUT:
junmin@T61Gentoo ~/C $ ./number
1 2 3 4 5 6 7 8 9
- 2 3 4 5 6 7 8 9
- - 3 4 5 6 7 8 9
- - - 4 5 6 7 8 9
- - - - 5 6 7 8 9
- - - - - 6 7 8 9
- - - - - - 7 8 9
- - - - - - - 8 9
- - - - - - - - 9

=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=
#include <stdio.h>

main(void){
int i = 0;
int k = 9;
while(i < 5){
int j = 0;
while(j < i){
printf("- ");
++j;
}
int x = ++i;
while(x <= k){
printf("%d ", x);
++x;
}
--k;
printf("\n");
}
return 0;
}

OUTPUT:
junmin@T61Gentoo ~/C $ ./number2
1 2 3 4 5 6 7 8 9
- 2 3 4 5 6 7 8
- - 3 4 5 6 7
- - - 4 5 6
- - - - 5

=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=
#include <stdio.h>

main(void){
int i;
for(i = 0; i < 9; i++,printf("\n")){
int j = 1;
while(j <= 9){
printf("%d ",j);
if(j == i){
printf("+");
}
++j;
}
}
return 0;
}

OUTPUT:
junmin@T61Gentoo ~/C $ ./number3
1 2 3 4 5 6 7 8 9
1 +2 3 4 5 6 7 8 9
1 2 +3 4 5 6 7 8 9
1 2 3 +4 5 6 7 8 9
1 2 3 4 +5 6 7 8 9
1 2 3 4 5 +6 7 8 9
1 2 3 4 5 6 +7 8 9
1 2 3 4 5 6 7 +8 9
1 2 3 4 5 6 7 8 +9


=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=
#include <stdio.h>

main(void){
int i;
for(i = 0; i < 9; i++,printf("\n")){
int j = 1;
while(j <= 9){
if(j == i)
printf("(");
printf("%d ", j);
if(j == i)
printf("+");
if(j == i + 1 && i + 1 != 1)
printf(")");
++j;
}
}
return 0;
}

OUTPUT:
junmin@T61Gentoo ~/C $ ./number4
1 2 3 4 5 6 7 8 9
(1 +2 )3 4 5 6 7 8 9
1 (2 +3 )4 5 6 7 8 9
1 2 (3 +4 )5 6 7 8 9
1 2 3 (4 +5 )6 7 8 9
1 2 3 4 (5 +6 )7 8 9
1 2 3 4 5 (6 +7 )8 9
1 2 3 4 5 6 (7 +8 )9
1 2 3 4 5 6 7 (8 +9 )

================================
 
P

pete

Junmin said:
Hello, just wrote down 4 simple codes to print numbers with order
for practice in C.

Just would like to know if you guys have any suggestion about them.
thanks

Junmin

=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=1=
#include <stdio.h>

main(void){
int i = 0;
while(i < 9){
int j = 0;
while(j < i){
printf("- ");
++j;
}
int x = ++i;
while(x <= 9){
printf("%d ",x);
++x;
}
printf("\n");
}
return 0;
}

Explicitly declaring main as returning type int,
is the right way to write modern code.
However, declaring automatic objects anywhere
except at the begining of a compound statement,
is a little too modern for my old compiler.
When I first started learning C,
I didn't want to learn any other IO functions besides printf,
because I thought the IO library was complicated,
but some of the other functions like putchar, are worth learning.

I like to rewrite other people's code:

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int i, j, x;

for (i = 0; 9 > i; ++i) {
for (j = 0; i > j; ++j) {
printf("- ");
}
for (x = j + 1; 9 >= x; ++x) {
printf("%d ",x);
}
putchar('\n');
}
return 0;
}

/* END new.c */
 
P

pete

=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=2=
#include <stdio.h>

main(void){
int i = 0;
int k = 9;
while(i < 5){
int j = 0;
while(j < i){
printf("- ");
++j;
}
int x = ++i;
while(x <= k){
printf("%d ", x);
++x;
}
--k;
printf("\n");
}
return 0;
}

#include <stdio.h>

int main(void)
{
int i, j, k, x;

for (k = 9, i = 0; 5 > i; --k, ++i) {
for (j = 0; i > j; ++j){
printf("- ");
}
for (x = j + 1; k >= x; ++x) {
printf("%d ", x);
}
putchar('\n');
}
return 0;
}
=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=3=
#include <stdio.h>

main(void){
int i;
for(i = 0; i < 9; i++,printf("\n")){
int j = 1;
while(j <= 9){
printf("%d ",j);
if(j == i){
printf("+");
}
++j;
}
}
return 0;
}

#include <stdio.h>

int main(void)
{
int i, j;

for (i = 0; 9 > i; ++i) {
for (j = 1; 9 >= j; ++j){
printf("%d ",j);
if (j == i){
putchar('+');
}
}
putchar('\n');
}
return 0;
}
=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=4=
#include <stdio.h>

main(void){
int i;
for(i = 0; i < 9; i++,printf("\n")){
int j = 1;
while(j <= 9){
if(j == i)
printf("(");
printf("%d ", j);
if(j == i)
printf("+");
if(j == i + 1 && i + 1 != 1)
printf(")");
++j;
}
}
return 0;
}

#include <stdio.h>

int main(void)
{
int i, j;

for (i = 0; 9 > i; i++) {
for (j = 1; 9 >= j; ++j){
if (j == i) {
putchar('(');
}
printf("%d ", j);
if (j == i) {
putchar('+');
}
if (j == i + 1 && j != 1) {
putchar(')');
}
}
putchar('\n');
}
return 0;
}
 
C

Charlie Gordon

pete said:
Explicitly declaring main as returning type int,
is the right way to write modern code.
However, declaring automatic objects anywhere
except at the begining of a compound statement,
is a little too modern for my old compiler.
When I first started learning C,
I didn't want to learn any other IO functions besides printf,
because I thought the IO library was complicated,
but some of the other functions like putchar, are worth learning.

I like to rewrite other people's code:

so do I ;-)
/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int i, j, x;

for (i = 0; 9 > i; ++i) {
for (j = 0; i > j; ++j) {
printf("- ");
}
for (x = j + 1; 9 >= x; ++x) {
printf("%d ",x);
}
putchar('\n');
}
return 0;
}

Your for loops are non-conventional. When writing code for best readability
for all programmers, including newbie, it is essential to use classic proven
idioms. writing your comparisons backwards from conventional use is
surprising. You probably do this as a out of consistency with 0 == x being
less error prone than x == 0, but even this idiom is largely irrelevant with
modern compilers that can complain about suspicious assignments in
conditional expressions.

This version is more "classic"

#include <stdio.h>

int main(void)
{
int i, j;

for (i = 0; i < 9; i++) {
for (j = 0; j < i; j++) {
printf("- ");
}
for (; j < 9; j++) {
printf("%d ", j + 1);
}
putchar('\n');
}
return 0;
}
 
C

Christopher Benson-Manica

[comp.lang.c] Charlie Gordon said:
This version is more "classic"
(snip for loops)

Well, since the for loops seem to be up for debate, I figure the fewer
the better:

#include <stdio.h>

int main( void ) {
int i;
char *dashes = "- - - - - - - - ";
char *nums = "1 2 3 4 5 6 7 8 9";

for (i = 0; i < 9; i++) {
printf( "%.*s%s\n", i*2, dashes, nums+i*2 );
}
return 0;
}

:) (One could arrange to build dashes and nums on the fly, of course.)
 
P

pete

Charlie said:
so do I ;-)


Your for loops are non-conventional.
When writing code for best readability
for all programmers, including newbie,
it is essential to use classic proven idioms.
writing your comparisons backwards from conventional use is
surprising.
You probably do this as a out of consistency with 0 == x being
less error prone than x == 0,

I don't like to use the "less than" operator.
It started a long time ago when I was implementing various
sorting algorithms and I noticed that I only needed
a "greater than" comparison to implement any of the algorithms
that I was using. I'm aware that outside of that context,
that it's now just a quirk in my writing style.
but even this idiom is largely irrelevant with
modern compilers that can complain about suspicious assignments in
conditional expressions.

This version is more "classic"

Yes, it is.
#include <stdio.h>

int main(void)
{
int i, j;

for (i = 0; i < 9; i++) {
for (j = 0; j < i; j++) {
printf("- ");
}


for (; j < 9; j++) {
printf("%d ", j + 1);
}

while (j++ < 9) {
printf("%d ", j);
}
 
C

Charlie Gordon

Christopher Benson-Manica said:
[comp.lang.c] Charlie Gordon said:
This version is more "classic"
(snip for loops)

Well, since the for loops seem to be up for debate, I figure the fewer
the better:

#include <stdio.h>

int main( void ) {
int i;
char *dashes = "- - - - - - - - ";
char *nums = "1 2 3 4 5 6 7 8 9";

for (i = 0; i < 9; i++) {
printf( "%.*s%s\n", i*2, dashes, nums+i*2 );
}
return 0;
}

:) (One could arrange to build dashes and nums on the fly, of course.)

This code is quite elegant.
It does produce different output than the loops (missing trailing space
before the new-line), but that is probably an improvement.
I would prefer to use const char * variables to hold pointers to literal
character arrays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top