completed function

Discussion in 'C Programming' started by Bill Cunningham, Feb 25, 2011.

  1. This function code compiles and I have inserted comments. I get this
    warning.

    p.c:12:25: warning: multi-line string literals are deprecated

    I see this is where the fprintfs are but I don't see the error. This is
    kind of an old compiler. gcc-3-x-x.

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>

    double ema(double ma1[], double ma2[], double ma3[], unsigned int days)
    {
    if (days == 0 || days == 1) {
    fprintf(stderr, "must have 2 or more days\n");
    return -1;
    }

    if (ma1 == 0 && ma2 == 0 && ma3 == 0) {
    fprintf(stderr, "must enter some value for moving average
    parameter(s)\n"); //something wrong here compiler says.
    return -1;
    }

    double ema = 2 / (days + 1);

    if (ma1 != 0 && ma2 == 0 && ma3 == 0) {
    double result = days/(*ma1 / ema);
    printf("%.2f\n", result); // I will probably take these prints out
    after testing.
    exit(errno); //Is this how to use errno? First time trying.
    }

    if (ma1 != 0 && ma2 != 0 && ma3 == 0) {
    double result1, result2;
    result1 = days/(*ma1 / ema);
    result2 = days/(*ma2 / ema);
    printf("%.2f %.2f\n", result1, result2);
    exit(errno);
    }

    if (ma1 != 0 && ma2 != 0 && ma3 != 0) {
    double result1, result2, result3;
    result1 = days/(*ma1 / ema);
    result2 = days/(*ma2 / ema);
    result3 = days/(*ma3 / ema);
    printf("%.2f %.2f %.2f\n", result1, result2, result3);
    exit(errno);
    }

    return 0;
    }

    I how this is more acceptable. Like I said it compiles into object code
    to be linked with main.

    B
    Bill Cunningham, Feb 25, 2011
    #1
    1. Advertising

  2. "Bill Cunningham" <> writes:

    > This function code compiles and I have inserted comments. I get this
    > warning.
    >
    > p.c:12:25: warning: multi-line string literals are deprecated
    >
    > I see this is where the fprintfs are but I don't see the error. This is
    > kind of an old compiler. gcc-3-x-x.


    You'd get it with a new compiler too. Your have a string "..." and it
    is on more than one line: "...
    ...." like that.

    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <errno.h>
    >
    > double ema(double ma1[], double ma2[], double ma3[], unsigned int days)


    You don't tell us what this function should do, so normally whatever
    code you right must be correct -- my best guess has to be that you want
    it to do what you've written -- but in this case what is does is so odd
    that I can't imagine that you intended it to do what it does.

    If you specify the function it would help everyone -- you included.

    <snip>
    --
    Ben.
    Ben Bacarisse, Feb 25, 2011
    #2
    1. Advertising

  3. Bill Cunningham

    osmium Guest

    "Bill Cunningham" wrote:

    > This function code compiles and I have inserted comments. I get this
    > warning.
    >
    > p.c:12:25: warning: multi-line string literals are deprecated
    >
    > I see this is where the fprintfs are but I don't see the error. This is
    > kind of an old compiler. gcc-3-x-x.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <errno.h>
    >

    // This function computes the square root of infinity and returns the golden
    mean.

    I have added a comment, if it is wrong, please correct it and repost.

    > double ema(double ma1[], double ma2[], double ma3[], unsigned int days)
    > {
    > if (days == 0 || days == 1) {
    > fprintf(stderr, "must have 2 or more days\n");
    > return -1;
    > }
    >
    > if (ma1 == 0 && ma2 == 0 && ma3 == 0) {
    > fprintf(stderr, "must enter some value for moving average
    > parameter(s)\n"); //something wrong here compiler says.
    > return -1;
    > }
    >
    > double ema = 2 / (days + 1);
    >
    > if (ma1 != 0 && ma2 == 0 && ma3 == 0) {
    > double result = days/(*ma1 / ema);
    > printf("%.2f\n", result); // I will probably take these prints out
    > after testing.
    > exit(errno); //Is this how to use errno? First time trying.
    > }
    >
    > if (ma1 != 0 && ma2 != 0 && ma3 == 0) {
    > double result1, result2;
    > result1 = days/(*ma1 / ema);
    > result2 = days/(*ma2 / ema);
    > printf("%.2f %.2f\n", result1, result2);
    > exit(errno);
    > }
    >
    > if (ma1 != 0 && ma2 != 0 && ma3 != 0) {
    > double result1, result2, result3;
    > result1 = days/(*ma1 / ema);
    > result2 = days/(*ma2 / ema);
    > result3 = days/(*ma3 / ema);
    > printf("%.2f %.2f %.2f\n", result1, result2, result3);
    > exit(errno);
    > }
    >
    > return 0;
    > }
    >
    > I how this is more acceptable. Like I said it compiles into object code
    > to be linked with main.
    >
    > B
    >
    >
    osmium, Feb 25, 2011
    #3
  4. Ben Bacarisse wrote:
    > "Bill Cunningham" <> writes:
    >
    >> This function code compiles and I have inserted comments. I get
    >> this warning.
    >>
    >> p.c:12:25: warning: multi-line string literals are deprecated
    >>
    >> I see this is where the fprintfs are but I don't see the error.
    >> This is kind of an old compiler. gcc-3-x-x.

    >
    > You'd get it with a new compiler too. Your have a string "..." and it
    > is on more than one line: "...
    > ..." like that.


    Ok I have never really written things on 2 lines before so I guess I
    wouldn't understand this diagnostic.

    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> #include <errno.h>
    >>
    >> double ema(double ma1[], double ma2[], double ma3[], unsigned int
    >> days)

    >
    > You don't tell us what this function should do, so normally whatever
    > code you right must be correct -- my best guess has to be that you
    > want it to do what you've written -- but in this case what is does is
    > so odd that I can't imagine that you intended it to do what it does.
    >
    > If you specify the function it would help everyone -- you included.


    The function is called ema() which returns a double. It should return to
    a function like main a double value. The first, second, and third parameters
    take from 1 up to three arrays of doubles and the function returns an
    exponentially weighted number. An average of the numbers. It would be called
    from main like this...

    int main(){
    double res,ans; //for result and answer.
    res[3.5,4,6];
    ans=ema(res,0,0,3);
    printf("%.2f\n",ans);
    // of course you could use the precision you like.

    I don't really need those printfs because I want main to printf what
    double ema() returns. Is this what you want as an explaination. printf
    should print 6.75.

    Bill
    Bill Cunningham, Feb 25, 2011
    #4
  5. osmium wrote:

    > // This function computes the square root of infinity and returns the
    > golden mean.
    >
    > I have added a comment, if it is wrong, please correct it and repost.


    huh? You need another // on your second line.

    B
    Bill Cunningham, Feb 25, 2011
    #5
  6. Bill Cunningham

    osmium Guest

    "Bill Cunningham" wrote:

    > This function code compiles and I have inserted comments. I get this
    > warning.
    >
    > p.c:12:25: warning: multi-line string literals are deprecated
    >
    > I see this is where the fprintfs are but I don't see the error. This is
    > kind of an old compiler. gcc-3-x-x.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <errno.h>
    >
    > double ema(double ma1[], double ma2[], double ma3[], unsigned int days)
    > {
    > if (days == 0 || days == 1) {
    > fprintf(stderr, "must have 2 or more days\n");
    > return -1;
    > }
    >
    > if (ma1 == 0 && ma2 == 0 && ma3 == 0) {
    > fprintf(stderr, "must enter some value for moving average
    > parameter(s)\n"); //something wrong here compiler says.
    > return -1;
    > }
    >
    > double ema = 2 / (days + 1);


    You are IN a function named ema. Now you introduce a VARIABLE named ema too.
    The compiler handles that with no problem but your mind can't. Never use one
    identifier for more than one thing. You have been told about this before.
    Can you find that in your notes? Change ema to xxx or something better of
    your choosing. If you have other emas, change them too. Fix and repost.
    osmium, Feb 25, 2011
    #6
  7. Martin Ambuhl wrote:

    > Is it possible that you don't see that
    > "must enter some value for moving average
    > parameter(s)\n"
    > is on more than one line, and has the appears to be intended to be one
    > string literal? I can't believe that. In any case, it's easy to fix:
    >
    > fprintf(stderr, "must enter some value for moving average"
    > "parameter(s)\n");


    Ok no I have never had a string move off of one line. I have always used
    very short error messages like
    "user error"
    "input error"
    "error 1"
    "error 5"

    and such.

    B
    Bill Cunningham, Feb 25, 2011
    #7
  8. osmium wrote:

    [snip]

    >> double ema = 2 / (days + 1);
    >>
    >> if (ma1 != 0 && ma2 == 0 && ma3 == 0) {
    >> double result = days/(*ma1 / ema);
    >> printf("%.2f\n", result); // I will probably take these
    >> prints out after testing.
    >> exit(errno); //Is this how to use errno? First time trying.
    >> }
    >>
    >> if (ma1 != 0 && ma2 != 0 && ma3 == 0) {
    >> double result1, result2;
    >> result1 = days/(*ma1 / ema);
    >> result2 = days/(*ma2 / ema);
    >> printf("%.2f %.2f\n", result1, result2);
    >> exit(errno);

    /*If I wanted to return result1 and result2. I have no idea how to do that.
    If I did I would remove the printfs
    */
    }
    >>
    >> if (ma1 != 0 && ma2 != 0 && ma3 != 0) {
    >> double result1, result2, result3;
    >> result1 = days/(*ma1 / ema);
    >> result2 = days/(*ma2 / ema);
    >> result3 = days/(*ma3 / ema);
    >> printf("%.2f %.2f %.2f\n", result1, result2, result3);
    >> exit(errno);
    >> }
    >>
    >> return 0;
    >> }
    >>
    >> I how this is more acceptable. Like I said it compiles into
    >> object code to be linked with main.
    >>
    >> B
    Bill Cunningham, Feb 25, 2011
    #8
  9. osmium wrote:

    > You are IN a function named ema. Now you introduce a VARIABLE named
    > ema too. The compiler handles that with no problem but your mind
    > can't. Never use one identifier for more than one thing. You have
    > been told about this before. Can you find that in your notes? Change
    > ema to xxx or something better of your choosing. If you have other
    > emas, change them too. Fix and repost.


    I do use the same names for variables and functions in different scopes.
    Notice that ema was declared in the function ema. The result variables are
    local to each if statement. Ok I can change variable names so not to confuse
    the reader. I can read it but in a few months down the road, I might forget
    the 2 emas myself.

    B
    Bill Cunningham, Feb 25, 2011
    #9
  10. Bill Cunningham

    osmium Guest

    "Bill Cunningham" wrote:

    > This function code compiles and I have inserted comments. I get this
    > warning.
    >
    > p.c:12:25: warning: multi-line string literals are deprecated
    >
    > I see this is where the fprintfs are but I don't see the error. This is
    > kind of an old compiler. gcc-3-x-x.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <errno.h>
    >
    > double ema(double ma1[], double ma2[], double ma3[], unsigned int days)
    > {
    > if (days == 0 || days == 1) {
    > fprintf(stderr, "must have 2 or more days\n");
    > return -1;
    > }
    >
    > if (ma1 == 0 && ma2 == 0 && ma3 == 0) {
    > fprintf(stderr, "must enter some value for moving average
    > parameter(s)\n"); //something wrong here compiler says.
    > return -1;
    > }
    >
    > double ema = 2 / (days + 1);
    >
    > if (ma1 != 0 && ma2 == 0 && ma3 == 0) {
    > double result = days/(*ma1 / ema);


    printf("%f %f\n", *ma1, ema);

    Incorporate the main you posted earlier into the code in your first post of
    the day. Then add this line of test information. Do you like what you see?
    I thought not. Try to fix the problems, there are at least two.

    Your three free-standing ifs are bad style. Do somnething like this:

    if ( )
    block 1
    else if( )
    block 2
    else if ( )
    block3;
    osmium, Feb 25, 2011
    #10
  11. "Bill Cunningham" <> writes:
    > Martin Ambuhl wrote:
    >> Is it possible that you don't see that
    >> "must enter some value for moving average
    >> parameter(s)\n"
    >> is on more than one line, and has the appears to be intended to be one
    >> string literal? I can't believe that. In any case, it's easy to fix:
    >>
    >> fprintf(stderr, "must enter some value for moving average"
    >> "parameter(s)\n");


    The output will include the word "averageparameter(s)". Add a space.

    > Ok no I have never had a string move off of one line. I have always used
    > very short error messages like
    > "user error"
    > "input error"
    > "error 1"
    > "error 5"
    >
    > and such.


    Ok, you've never had this problem before.

    Now you do have this problem. And the compiler is telling you exactly
    what the problem is. Are you waiting for an engraved invitation?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 25, 2011
    #11
  12. osmium wrote:

    > Your three free-standing ifs are bad style. Do somnething like this:
    >
    > if ( )
    > block 1
    > else if( )
    > block 2
    > else if ( )
    > block3;


    Oh ok... Exactly what I'm looking for! Tips to better coding style. Yes
    my code works and the switch statement as of yet still confuses me. But tips
    on making workable code better style wise I very much appreciate.

    B
    Bill Cunningham, Feb 25, 2011
    #12
  13. "Bill Cunningham" <> writes:

    > Ben Bacarisse wrote:
    >> "Bill Cunningham" <> writes:
    >>
    >>> This function code compiles and I have inserted comments. I get
    >>> this warning.
    >>>
    >>> p.c:12:25: warning: multi-line string literals are deprecated
    >>>
    >>> I see this is where the fprintfs are but I don't see the error.
    >>> This is kind of an old compiler. gcc-3-x-x.

    >>
    >> You'd get it with a new compiler too. Your have a string "..." and it
    >> is on more than one line: "...
    >> ..." like that.

    >
    > Ok I have never really written things on 2 lines before so I guess I
    > wouldn't understand this diagnostic.
    >
    >>> #include <stdio.h>
    >>> #include <stdlib.h>
    >>> #include <errno.h>
    >>>
    >>> double ema(double ma1[], double ma2[], double ma3[], unsigned int
    >>> days)

    >>
    >> You don't tell us what this function should do, so normally whatever
    >> code you right must be correct -- my best guess has to be that you
    >> want it to do what you've written -- but in this case what is does is
    >> so odd that I can't imagine that you intended it to do what it does.
    >>
    >> If you specify the function it would help everyone -- you included.

    >
    > The function is called ema() which returns a double. It should return to
    > a function like main a double value. The first, second, and third parameters
    > take from 1 up to three arrays of doubles and the function returns an
    > exponentially weighted number. An average of the numbers.


    Ah, well, it does not do that. It computes no average of any sort, and
    it does no weighting and it only every returns 0 or -1. Your
    specification does not say what 'days' is for, nor exactly what "the
    numbers" are that are to be averaged.

    Your use of pointers to indicate which parameters are to be used is
    limiting. Will you only ever want to consider up to three data items?

    Despite all the confusion, the name of the function and the presence of
    the formula 2/(days + 1) suggests that what you really want is a way to
    calculate the exponential moving average of some data series using the
    'days' parameter to determine the decay or weight. This program does
    that:

    #include <stdio.h>
    #include <stdlib.h>

    double new_weighted_avg(double decay, double data, double previous_avg)
    {
    return decay * data + (1 - decay) * previous_avg;
    }

    int main(int argc, char **argv)
    {
    if (argc > 1) {
    int days = atoi(argv[1]);
    double decay = 2.0 / (days + 1);
    double data;
    if (scanf("%lf", &data) == 1) {
    double average = data;
    printf("original EMA (with days=%d)\n", days);
    do {
    average = new_weighted_avg(decay, data, average);
    printf("%8.3f %8.3f\n", data, average);
    } while (scanf("%lf", &data) == 1);
    }
    }
    else fprintf(stderr, "No declay period given.\n");
    return 0;
    }

    Your three ma arrays suggests that you want, in the long run, to
    calculate three EMAs each with a different decay (or days) parameter.
    You could extend this program to do that I think.

    > It would be called
    > from main like this...
    >
    > int main(){
    > double res,ans; //for result and answer.
    > res[3.5,4,6];


    No type.

    > ans=ema(res,0,0,3);
    > printf("%.2f\n",ans);
    > // of course you could use the precision you like.
    >
    > I don't really need those printfs because I want main to printf what
    > double ema() returns. Is this what you want as an explaination. printf
    > should print 6.75.


    In what way is 6.75 any sort of weighted average of the data in the
    program? You need to start with a formula for the weighted average.

    --
    Ben.
    Ben Bacarisse, Feb 25, 2011
    #13
  14. On Fri, 25 Feb 2011 17:03:09 -0500, "Bill Cunningham"
    <> wrote:

    >osmium wrote:
    >
    >> Your three free-standing ifs are bad style. Do somnething like this:
    >>
    >> if ( )
    >> block 1
    >> else if( )
    >> block 2
    >> else if ( )
    >> block3;

    >
    > Oh ok... Exactly what I'm looking for! Tips to better coding style. Yes
    >my code works and the switch statement as of yet still confuses me. But tips
    >on making workable code better style wise I very much appreciate.
    >


    No, your code does not work. Your variable ema will always be 0 since
    days is at least 2. Even you know you can't divide by 0.

    Just out of curiosity, which line of your code do you expect to set
    errno. We won't even discuss why sometimes you return and sometimes
    you exit.

    --
    Remove del for email
    Barry Schwarz, Feb 26, 2011
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MajorTom
    Replies:
    4
    Views:
    2,268
    MajorTom
    May 27, 2005
  2. =?Utf-8?B?TXVzdGFx?=

    To Know bat file, execution is completed using VB.NET

    =?Utf-8?B?TXVzdGFx?=, Jun 23, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,758
    Francois Bonin [C# MVP]
    Jun 23, 2005
  3. kgs

    document completed parsing

    kgs, Dec 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    342
  4. =?Utf-8?B?U2FuZHk=?=
    Replies:
    3
    Views:
    730
    fd123456
    Feb 12, 2005
  5. despo
    Replies:
    3
    Views:
    144
    Scott Sauyet
    Feb 5, 2010
Loading...

Share This Page