The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] A c program problem
@ 2018-08-22 12:01 cc
  2018-08-22 12:24 ` Donald ODona
  2018-08-22 14:21 ` Dan Cross
  0 siblings, 2 replies; 10+ messages in thread
From: cc @ 2018-08-22 12:01 UTC (permalink / raw)
  To: tuhs

[-- Attachment #1: Type: text/plain, Size: 488 bytes --]

Hello everyone

I had a problem compiling a piece of c code from the book. The result of running the book is 5050, but the compiler is 100. I don't know which is right, please help me to see which is wrong. Thank you very much!

#include <stdio.h>

int main(void)
{
       int i, sum = 0;
       i = 1;
       while ( i <= 100) {
       sum = sum + 1;
       i++;
       }
       printf("%d\n", sum)
       return 0;
}




| |
cc
|
|
邮箱:caipenghui_c@163.com
|

签名由 网易邮箱大师 定制

[-- Attachment #2: Type: text/html, Size: 3456 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 12:01 [TUHS] A c program problem cc
@ 2018-08-22 12:24 ` Donald ODona
  2018-08-22 12:54   ` Mark Longridge
  2018-08-22 14:21 ` Dan Cross
  1 sibling, 1 reply; 10+ messages in thread
From: Donald ODona @ 2018-08-22 12:24 UTC (permalink / raw)
  To: tuhs, caipenghui_c



At 22 Aug 2018 12:18:08 +0000 (+00:00) from cc <caipenghui_c@163.com>:
> Hello everyone
> 
> I had a problem compiling a piece of c code from the book. The result of running the book is 5050, but the compiler is 100. I don't know which is right, please help me to see which is wrong. Thank you very much!
> 
> #include <stdio.h>
> 
> int main(void)
> {
>        int i, sum = 0;
>        i = 1;
>        while ( i <= 100) {
>        sum = sum + 1;
>        i++;
>        }
>        printf("%d\n", sum)
>        return 0;
> }
> 
> 
> 
> 
> | |
> cc
> |
> |
> 邮箱:caipenghui_c@163.com
> |
> 
> 签名由 网易邮箱大师 定制

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 12:24 ` Donald ODona
@ 2018-08-22 12:54   ` Mark Longridge
  0 siblings, 0 replies; 10+ messages in thread
From: Mark Longridge @ 2018-08-22 12:54 UTC (permalink / raw)
  To: Donald ODona; +Cc: tuhs

Looks like a typographical error.

if you change the line:

sum = sum + 1;

to

sum = sum + i;

then you will get 5050.

Also the line with printf should have a semi-colon at the end.

On 8/22/18, Donald ODona <mutiny.mutiny@india.com> wrote:
>
>
> At 22 Aug 2018 12:18:08 +0000 (+00:00) from cc <caipenghui_c@163.com>:
>> Hello everyone
>>
>> I had a problem compiling a piece of c code from the book. The result of
>> running the book is 5050, but the compiler is 100. I don't know which is
>> right, please help me to see which is wrong. Thank you very much!
>>
>> #include <stdio.h>
>>
>> int main(void)
>> {
>>        int i, sum = 0;
>>        i = 1;
>>        while ( i <= 100) {
>>        sum = sum + 1;
>>        i++;
>>        }
>>        printf("%d\n", sum)
>>        return 0;
>> }
>>
>>
>>
>>
>> | |
>> cc
>> |
>> |
>> 邮箱:caipenghui_c@163.com
>> |
>>
>> 签名由 网易邮箱大师 定制
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 12:01 [TUHS] A c program problem cc
  2018-08-22 12:24 ` Donald ODona
@ 2018-08-22 14:21 ` Dan Cross
  2018-08-22 14:26   ` cc
  1 sibling, 1 reply; 10+ messages in thread
From: Dan Cross @ 2018-08-22 14:21 UTC (permalink / raw)
  To: caipenghui_c; +Cc: TUHS main list

[-- Attachment #1: Type: text/plain, Size: 1749 bytes --]

Note, TUHS probably isn't the *best* forum to ask for help with basic C
programs. :-)

That said, I suspect you mis-transcribed the program and that the line,
`sum = sum + 1;` should be `sum = sum + i;`, or more idiomatically, `sum +=
i;`.  Indeed, the whole program could be written:

#include <stdio.h>

int
main(void)
{
        int i, sum = 0;
        for (i = 1; i <= 100; i++)
                sum += i;
        printf("%d\n", sum);
        return 0;
}

If you're using a C99 or later compiler, you can be slightly more succinct:

#include <stdio.h>

int
main(void)
{
        int sum = 0;
        for (int i = 1; i <= 100; i++)
                sum += i;
        printf("%d\n", sum);
        return 0;
}

Hope that helps!

       - Dan C.


On Wed, Aug 22, 2018 at 8:17 AM cc <caipenghui_c@163.com> wrote:

> Hello everyone
>
> I had a problem compiling a piece of c code from the book. The result of
> running the book is 5050, but the compiler is 100. I don't know which is
> right, please help me to see which is wrong. Thank you very much!
>
> #include <stdio.h>
>
> int main(void)
> {
>        int i, sum = 0;
>        i = 1;
>        while ( i <= 100) {
>        sum = sum + 1;
>        i++;
>        }
>        printf("%d\n", sum)
>        return 0;
> }
>
>
>
> cc
> 邮箱:caipenghui_c@163.com
>
> <https://maas.mail.163.com/dashi-web-extend/html/proSignature.html?ftlId=1&name=cc&uid=caipenghui_c%40163.com&iconUrl=https%3A%2F%2Fmail-online.nosdn.127.net%2Fwzpmmc%2Fad31362036872a624ad455630205a987.jpg&items=%5B%22%E9%82%AE%E7%AE%B1%EF%BC%9Acaipenghui_c%40163.com%22%5D>
>
> 签名由 网易邮箱大师 <https://mail.163.com/dashi/dlpro.html?from=mail88> 定制
>

[-- Attachment #2: Type: text/html, Size: 4661 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 14:21 ` Dan Cross
@ 2018-08-22 14:26   ` cc
  2018-08-22 14:29     ` cc
  0 siblings, 1 reply; 10+ messages in thread
From: cc @ 2018-08-22 14:26 UTC (permalink / raw)
  To: Dan Cross; +Cc: TUHS main list

[-- Attachment #1: Type: text/plain, Size: 1616 bytes --]

Sorry, I won't be sending it next time. Oh oh, maybe I forgot to add; . I typed the code on the book, and the result of running the book was 5050, but the compiler was 100.


| |
cc
|
|
邮箱:caipenghui_c@163.com
|

签名由 网易邮箱大师 定制

On 08/22/2018 22:21, Dan Cross wrote:
Note, TUHS probably isn't the *best* forum to ask for help with basic C programs. :-)


That said, I suspect you mis-transcribed the program and that the line, `sum = sum + 1;` should be `sum = sum + i;`, or more idiomatically, `sum += i;`.  Indeed, the whole program could be written:


#include <stdio.h>


int
main(void)
{
        int i, sum = 0;
        for (i = 1; i <= 100; i++)
                sum += i;
        printf("%d\n", sum);
        return 0;
}


If you're using a C99 or later compiler, you can be slightly more succinct:


#include <stdio.h>


int
main(void)
{
        int sum = 0;
        for (int i = 1; i <= 100; i++)
                sum += i;
        printf("%d\n", sum);
        return 0;
}


Hope that helps!


       - Dan C.




On Wed, Aug 22, 2018 at 8:17 AM cc <caipenghui_c@163.com> wrote:

Hello everyone

I had a problem compiling a piece of c code from the book. The result of running the book is 5050, but the compiler is 100. I don't know which is right, please help me to see which is wrong. Thank you very much!

#include <stdio.h>

int main(void)
{
       int i, sum = 0;
       i = 1;
       while ( i <= 100) {
       sum = sum + 1;
       i++;
       }
       printf("%d\n", sum)
       return 0;
}




| |
cc
|
|
邮箱:caipenghui_c@163.com
|

签名由 网易邮箱大师 定制

[-- Attachment #2: Type: text/html, Size: 8313 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 14:26   ` cc
@ 2018-08-22 14:29     ` cc
  2018-08-22 14:32       ` Ralph Corderoy
  2018-08-22 15:40       ` Toby Thain
  0 siblings, 2 replies; 10+ messages in thread
From: cc @ 2018-08-22 14:29 UTC (permalink / raw)
  To: Dan Cross; +Cc: TUHS main list

[-- Attachment #1: Type: text/plain, Size: 1798 bytes --]

I want to figure out if the book is right or the compiler is right.


| |
cc
|
|
邮箱:caipenghui_c@163.com
|

签名由 网易邮箱大师 定制

On 08/22/2018 22:26, cc wrote:
Sorry, I won't be sending it next time. Oh oh, maybe I forgot to add; . I typed the code on the book, and the result of running the book was 5050, but the compiler was 100.


| |
cc
|
|
邮箱:caipenghui_c@163.com
|

签名由 网易邮箱大师 定制

On 08/22/2018 22:21, Dan Cross wrote:
Note, TUHS probably isn't the *best* forum to ask for help with basic C programs. :-)


That said, I suspect you mis-transcribed the program and that the line, `sum = sum + 1;` should be `sum = sum + i;`, or more idiomatically, `sum += i;`.  Indeed, the whole program could be written:


#include <stdio.h>


int
main(void)
{
        int i, sum = 0;
        for (i = 1; i <= 100; i++)
                sum += i;
        printf("%d\n", sum);
        return 0;
}


If you're using a C99 or later compiler, you can be slightly more succinct:


#include <stdio.h>


int
main(void)
{
        int sum = 0;
        for (int i = 1; i <= 100; i++)
                sum += i;
        printf("%d\n", sum);
        return 0;
}


Hope that helps!


       - Dan C.




On Wed, Aug 22, 2018 at 8:17 AM cc <caipenghui_c@163.com> wrote:

Hello everyone

I had a problem compiling a piece of c code from the book. The result of running the book is 5050, but the compiler is 100. I don't know which is right, please help me to see which is wrong. Thank you very much!

#include <stdio.h>

int main(void)
{
       int i, sum = 0;
       i = 1;
       while ( i <= 100) {
       sum = sum + 1;
       i++;
       }
       printf("%d\n", sum)
       return 0;
}




| |
cc
|
|
邮箱:caipenghui_c@163.com
|

签名由 网易邮箱大师 定制

[-- Attachment #2: Type: text/html, Size: 11825 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 14:29     ` cc
@ 2018-08-22 14:32       ` Ralph Corderoy
  2018-08-22 14:34         ` cc
  2018-08-22 15:40       ` Toby Thain
  1 sibling, 1 reply; 10+ messages in thread
From: Ralph Corderoy @ 2018-08-22 14:32 UTC (permalink / raw)
  To: cc; +Cc: TUHS main list

> I want to figure out if the book is right or the compiler is right.

5050 is the right answer.  It's the 100th triangular number.
https://en.wikipedia.org/wiki/Triangular_number

Please let this off-topic thread die.

-- 
Cheers, Ralph.
https://plus.google.com/+RalphCorderoy

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 14:32       ` Ralph Corderoy
@ 2018-08-22 14:34         ` cc
  0 siblings, 0 replies; 10+ messages in thread
From: cc @ 2018-08-22 14:34 UTC (permalink / raw)
  To: Ralph Corderoy; +Cc: TUHS main list

[-- Attachment #1: Type: text/plain, Size: 417 bytes --]

Ok, That you.


| |
cc
|
|
邮箱:caipenghui_c@163.com
|

签名由 网易邮箱大师 定制

On 08/22/2018 22:32, Ralph Corderoy wrote:
> I want to figure out if the book is right or the compiler is right.

5050 is the right answer.  It's the 100th triangular number.
https://en.wikipedia.org/wiki/Triangular_number

Please let this off-topic thread die.

--
Cheers, Ralph.
https://plus.google.com/+RalphCorderoy

[-- Attachment #2: Type: text/html, Size: 3979 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 14:29     ` cc
  2018-08-22 14:32       ` Ralph Corderoy
@ 2018-08-22 15:40       ` Toby Thain
  2018-08-22 15:46         ` Caipenghui
  1 sibling, 1 reply; 10+ messages in thread
From: Toby Thain @ 2018-08-22 15:40 UTC (permalink / raw)
  To: tuhs

On 2018-08-22 10:29 AM, cc wrote:
> I want to figure out if the book is right or the compiler is right.
> 
> 	
> cc


Donald and Dan identified the issue in earlier replies.

$ cc foo.c -o foo
$ ./foo
5050
$ cat foo.c
#include <stdio.h>
int main(void)
{
       int i, sum = 0;
       i = 1;
       while ( i <= 100) {
       sum = sum + i;
       i++;
       }
       printf("%d\n", sum);
       return 0;
}

--Toby



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [TUHS] A c program problem
  2018-08-22 15:40       ` Toby Thain
@ 2018-08-22 15:46         ` Caipenghui
  0 siblings, 0 replies; 10+ messages in thread
From: Caipenghui @ 2018-08-22 15:46 UTC (permalink / raw)
  To: tuhs, Toby Thain

On August 22, 2018 11:40:18 PM GMT+08:00, Toby Thain <toby@telegraphics.com.au> wrote:
> On 2018-08-22 10:29 AM, cc wrote:
> > I want to figure out if the book is right or the compiler is right.
> > 
> > 	
> > cc
> 
> 
> Donald and Dan identified the issue in earlier replies.
> 
> $ cc foo.c -o foo
> $ ./foo
> 5050
> $ cat foo.c
> #include <stdio.h>
> int main(void)
> {
>        int i, sum = 0;
>        i = 1;
>        while ( i <= 100) {
>        sum = sum + i;
>        i++;
>        }
>        printf("%d\n", sum);
>        return 0;
> }
> 
> --Toby

Thank you very much for pointing this out, and I'm thrilled. Thank you for your kind help.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-08-22 15:47 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-22 12:01 [TUHS] A c program problem cc
2018-08-22 12:24 ` Donald ODona
2018-08-22 12:54   ` Mark Longridge
2018-08-22 14:21 ` Dan Cross
2018-08-22 14:26   ` cc
2018-08-22 14:29     ` cc
2018-08-22 14:32       ` Ralph Corderoy
2018-08-22 14:34         ` cc
2018-08-22 15:40       ` Toby Thain
2018-08-22 15:46         ` Caipenghui

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).