Google Groups Home
Help | Sign in
Problem 77: Greedy Gift Givers
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 51 - Collapse all   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Bert  
View profile
 More options Jul 5, 8:31 pm
Newsgroups: comp.lang.c
From: Bert <albert.xtheunkno...@gmail.com>
Date: Sat, 5 Jul 2008 17:31:31 -0700 (PDT)
Local: Sat, Jul 5 2008 8:31 pm
Subject: Problem 77: Greedy Gift Givers
Problem is documented at http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1

Why doesn't this work?

/*
ID: albert.4
LANG: C
TASK: gift1
*/

#include <stdio.h>

#define MAXNAMELEN 14

main()
{
    struct friend {
        int money, ngifts;            /* no. gifts               */

        char name[MAXNAMELEN];
    };

    int NP;                           /* no. people              */

    FILE* in  = fopen("gift1.in" ,  "r");
    FILE* out = fopen("gift1.out",  "w");

    fscanf(in, "%d", &NP);

    struct friend f[NP];

    int i = 0;

    for (; i < NP; i++)
    {
        fscanf(in, "%s", f[i].name);
    }

    /* Until input tells us how much each friend has (later in this
prog), they're all broke */
    i = 0;

    for (; i < NP; i++)
    {
        f[i].money = 0;
    }

    char temp[MAXNAMELEN];

    for (i = 0; i < NP; i++)
    {
        fscanf(in, "%s", temp);

        if (f[i].name == temp)
        {
            int init;                  /* initial amount of money */

            fscanf(in, "%d %d", init, f[i].ngifts);

            f[i].money  = init * -1;   /* init is given to people */
            f[i].money += init % f[i].ngifts;

            int amteachpersongets = init / f[i].ngifts;

            int j = 0;

            char temp2[MAXNAMELEN];

            for (; j < f[i].ngifts; j++)
            {
                fscanf(in, "%s", temp2);

                int k = 0;

                for (; k < NP; k++)
                {
                    if (f[k].name == temp2)
                    {
                        f[k].money += amteachpersongets;
                        break;
                    }
                }
            }
        }
    }

    for (i = 0; i < NP; i++)
    {
        fprintf(out, "%s %d\n", f[i].name, f[i].money);
    }

    exit(0);


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Walter Roberson  
View profile
 More options Jul 5, 9:07 pm
Newsgroups: comp.lang.c
From: rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Date: Sun, 6 Jul 2008 01:07:24 +0000 (UTC)
Local: Sat, Jul 5 2008 9:07 pm
Subject: Re: Problem 77: Greedy Gift Givers
In article <b46a1e5a-b903-4ed5-b1f8-04b972c87...@25g2000hsx.googlegroups.com>,

Bert  <albert.xtheunkno...@gmail.com> wrote:
>Problem is documented at http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1
>Why doesn't this work?
>main()
>{

That line tells us that you are using a C89 compiler. A C99 compiler
would not have compiled that line because C99 does not the recognize
implicit int return type. Every C99 function must define -some- return
type, even if that return type is given as 'void' (which would
not be a good idea for the 'main' routine.)

>    int NP;                           /* no. people              */
>    fscanf(in, "%d", &NP);
>    struct friend f[NP];

But those lines tell us that you are using C99. C89 array lengths
must be defined at compile time. C99 allows variable length arrays
(VLA) in some circumstances.

You need to pick a compiler standard and stick with it.
If you mix-and-match features between compiler versions, we
cannot tell you what the program means.
--
  "There are some ideas so wrong that only a very intelligent person
  could believe in them."                            -- George Orwell


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bert  
View profile
 More options Jul 5, 9:24 pm
Newsgroups: comp.lang.c
From: Bert <albert.xtheunkno...@gmail.com>
Date: Sat, 5 Jul 2008 18:24:41 -0700 (PDT)
Local: Sat, Jul 5 2008 9:24 pm
Subject: Re: Problem 77: Greedy Gift Givers
On Jul 6, 11:07 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:

OK - I've been referring to K&R 2nd Edition.
main()
becomes int main()

Can you now tell me what's wrong with the rest of the program?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile
 More options Jul 5, 9:58 pm
Newsgroups: comp.lang.c
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Sun, 06 Jul 2008 02:58:51 +0100
Local: Sat, Jul 5 2008 9:58 pm
Subject: Re: Problem 77: Greedy Gift Givers

Bert <albert.xtheunkno...@gmail.com> writes:
> On Jul 6, 11:07 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
<snip>
>> You need to pick a compiler standard and stick with it.
>> If you mix-and-match features between compiler versions, we
>> cannot tell you what the program means.
>> --
>>   "There are some ideas so wrong that only a very intelligent person
>>   could believe in them."                            -- George
>> Orwell

Best not to quote sig blocks.

> OK - I've been referring to K&R 2nd Edition.
> main()
> becomes int main()

> Can you now tell me what's wrong with the rest of the program?

The main things are (in no particular order):

(1) No error checking on things that can fail like fopen and fscanf.
(2) No decomposition into functions.
(3) Dangerous unbounded %s input formats.
(4) Misunderstanding how fscanf reads strings.  Read FAQ 12.18a [1]
(4) Comparing strings by using == on pointers to them.  FAQ 8.2.

[1] http://c-faq.com/

--
Ben.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
CBFalconer  
View profile
 More options Jul 5, 11:07 pm
Newsgroups: comp.lang.c
From: CBFalconer <cbfalco...@yahoo.com>
Date: Sat, 05 Jul 2008 23:07:28 -0400
Local: Sat, Jul 5 2008 11:07 pm
Subject: Re: Problem 77: Greedy Gift Givers
Bert wrote:

> Problem is documented at http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1

> Why doesn't this work?

... snip ...

Because it doesn't compile under either C90 or C99.

[1] c:\c\junk>cc junk.c
junk.c: In function `main':
junk.c:17: warning: ISO C89 forbids variable-size array `f'
junk.c:17: warning: ISO C89 forbids mixed declarations and code
junk.c:31: warning: ISO C89 forbids mixed declarations and code
junk.c:37: warning: format argument is not a pointer (arg 3)
junk.c:37: warning: format argument is not a pointer (arg 4)
junk.c:42: warning: ISO C89 forbids mixed declarations and code
junk.c:49: warning: ISO C89 forbids mixed declarations and code
junk.c:64: warning: implicit declaration of function `exit'
junk.c:35: warning: `init' might be used uninitialized in this
function

[1] c:\c\junk>gcc -std=c99 -pedantic -W -Wall -Wwrite-strings
junk.c
junk.c: In function `main':
junk.c:37: warning: format argument is not a pointer (arg 3)
junk.c:37: warning: format argument is not a pointer (arg 4)
junk.c:64: warning: implicit declaration of function `exit'

This is line 37 as I have compressed it:

>             fscanf(in, "%d %d", init, f[i].ngifts);

--
 [mail]: Chuck F (cbfalconer at maineline dot net)
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bert  
View profile
 More options Jul 6, 12:30 am
Newsgroups: comp.lang.c
From: Bert <albert.xtheunkno...@gmail.com>
Date: Sat, 5 Jul 2008 21:30:15 -0700 (PDT)
Local: Sun, Jul 6 2008 12:30 am
Subject: Re: Problem 77: Greedy Gift Givers
Why doesn't it do the problem correctly now?

/*
ID: albert.4
LANG: C
TASK: gift1
*/

#include <stdio.h>

#define MAXNAMELEN 14

main()
{
    struct friend {
        int money, ngifts;            /* no. gifts               */

        char name[MAXNAMELEN];
    };

    int NP;                           /* no. people              */

    FILE* in  = fopen("gift1.in" ,  "r");
    FILE* out = fopen("gift1.out",  "w");

    fscanf(in, "%d", &NP);

    struct friend f[NP];

    int i = 0;

    for (; i < NP; i++)
    {
        fscanf(in, "%s", f[i].name);
    }

    /* Until input tells us how much each friend has (later in this
prog), they're all broke */
    i = 0;

    for (; i < NP; i++)
    {
        f[i].money = 0;
    }

    char temp[MAXNAMELEN];

    for (i = 0; i < NP; i++)
    {
        fscanf(in, "%s", temp);

        if (strcmp(f[i].name, temp) == 0)
        {
            int init;                  /* initial amount of money */

            fscanf(in, "%d %d", &init, &f[i].ngifts);

            f[i].money  = init * -1;   /* init is given to people */

            if (f[i].ngifts != 0)
            {
                f[i].money += init % f[i].ngifts;
            }

            int amteachpersongets = 0;

            if (f[i].ngifts != 0)
            {
                int amteachpersongets = init / f[i].ngifts;
            }

            int j = 0;

            char temp2[MAXNAMELEN];

            for (; j < f[i].ngifts; j++)
            {
                fscanf(in, "%s", temp2);

                int k = 0;

                for (; k < NP; k++)
                {
                    if (strcmp(f[k].name, temp2) == 0)
                    {
                        f[k].money += amteachpersongets;
                        break;
                    }
                }
            }
        }
    }

    for (i = 0; i < NP; i++)
    {
        fprintf(out, "%s %d\n", f[i].name, f[i].money);
    }

    exit(0);


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Schwarz  
View profile
 More options Jul 6, 12:41 am
Newsgroups: comp.lang.c
From: Barry Schwarz <schwa...@dqel.com>
Date: Sat, 05 Jul 2008 21:41:52 -0700
Local: Sun, Jul 6 2008 12:41 am
Subject: Re: Problem 77: Greedy Gift Givers
On Sat, 5 Jul 2008 17:31:31 -0700 (PDT), Bert

<albert.xtheunkno...@gmail.com> wrote:
>Problem is documented at http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1

>Why doesn't this work?

Would you care to describe "doesn't work" so we have a fighting chance
to help.

So your compiler has extensions that allow variable length arrays and
definitions to follow executable statements.  Many of us don't which
limits the number of people who can help you.

>    int i = 0;

>    for (; i < NP; i++)
>    {
>        fscanf(in, "%s", f[i].name);

If you gave us a sample of your input it would also help.

>    }

>    /* Until input tells us how much each friend has (later in this
>prog), they're all broke */
>    i = 0;

>    for (; i < NP; i++)
>    {
>        f[i].money = 0;

Is there some reason you chose not to do this in the previous loop?

>    }

>    char temp[MAXNAMELEN];

>    for (i = 0; i < NP; i++)
>    {
>        fscanf(in, "%s", temp);

Don't you think this should be in front of the loop?

>        if (f[i].name == temp)

This is guaranteed to always be false.  If you want to compare
strings, you need to use strcmp et al.  Read the faq (www.c-faq.com),
section 8.2.  Then read the rest of the faq also.

>        {
>            int init;                  /* initial amount of money */

>            fscanf(in, "%d %d", init, f[i].ngifts);

>            f[i].money  = init * -1;   /* init is given to people */

Is there some reason -init does not provide the value you want?

>            f[i].money += init % f[i].ngifts;

What is this quantity supposed to represent?

Remove del for email

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
santosh  
View profile
 More options Jul 6, 3:08 am
Newsgroups: comp.lang.c
From: santosh <santosh....@gmail.com>
Date: Sun, 06 Jul 2008 12:38:10 +0530
Local: Sun, Jul 6 2008 3:08 am
Subject: Re: Problem 77: Greedy Gift Givers

Bert wrote:
> Problem is documented at
> http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1

Do you realise that the link leads to a page that asks for
authentication?

<snip>


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
CBFalconer  
View profile
 More options Jul 6, 5:41 am
Newsgroups: comp.lang.c
From: CBFalconer <cbfalco...@yahoo.com>
Date: Sun, 06 Jul 2008 05:41:47 -0400
Local: Sun, Jul 6 2008 5:41 am
Subject: Re: Problem 77: Greedy Gift Givers

Not to mention the use of undefined functions (exit) and implicit
int (main).

--
 [mail]: Chuck F (cbfalconer at maineline dot net)
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Heathfield  
View profile
 More options Jul 6, 8:20 am
Newsgroups: comp.lang.c
From: Richard Heathfield <r...@see.sig.invalid>
Date: Sun, 06 Jul 2008 12:20:08 +0000
Local: Sun, Jul 6 2008 8:20 am
Subject: Re: Problem 77: Greedy Gift Givers
CBFalconer said:

ITYM "undeclared functions". Either the presence of a standard C library
function in the library supplied with the implementation counts as a
definition or it doesn't. If it does, then exit() is defined. And if it
doesn't, the mere addition of a declaration (via #include <stdlib.h>) will
not provide a definition.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the