Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is there any suggestion to rewrite these simple codes?

Reply
Thread Tools

Is there any suggestion to rewrite these simple codes?

 
 
Junmin H.
Guest
Posts: n/a
 
      09-16-2007
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 )

================================
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      09-16-2007
Junmin H. wrote:
>
> 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 */

--
pete
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      09-16-2007
Junmin H. wrote:
>


> =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;
}

--
pete
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-17-2007
"pete" <(E-Mail Removed)> a écrit dans le message de news:
http://www.velocityreviews.com/forums/(E-Mail Removed)...
> Junmin H. wrote:
>>
>> 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:


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;
}

--
Chqrlie.


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-17-2007
[comp.lang.c] Charlie Gordon <(E-Mail Removed)> wrote:

> 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.)

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-17-2007
Charlie Gordon wrote:
>
> "pete" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...
> > Junmin H. wrote:
> >>
> >> 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:

>
> 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,


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);
}



> putchar('\n');
> }
> return 0;
> }


--
pete
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-18-2007
"Christopher Benson-Manica" <(E-Mail Removed)> a écrit dans le
message de news: fcm759$oer$(E-Mail Removed)...
> [comp.lang.c] Charlie Gordon <(E-Mail Removed)> wrote:
>
>> 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.

--
Chqrlie.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for pointers/suggestion - how to make a webbrowser with these restriction? Anthony Kong Python 4 09-26-2012 02:04 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Is there any way to turn off these STUPID!!!! event sounds in 1.0.4... peterf Firefox 12 07-08-2005 11:28 AM
is there a way to access (write rewrite append) local drive with javascript plus some code HALLES Javascript 17 06-02-2005 07:32 PM
Is there a way to write append rewrite a XML Multilingual file HALLES C Programming 5 05-30-2005 11:44 PM



Advertisments