completed function

B

Bill Cunningham

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
 
B

Ben Bacarisse

Bill Cunningham said:
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>
 
O

osmium

Bill Cunningham said:
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
 
B

Bill Cunningham

Ben said:
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
 
B

Bill Cunningham

osmium said:
// 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
 
O

osmium

Bill Cunningham said:
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.
 
B

Bill Cunningham

Martin said:
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
 
B

Bill Cunningham

osmium wrote:

[snip]
/*If I wanted to return result1 and result2. I have no idea how to do that.
If I did I would remove the printfs
*/
}
 
B

Bill Cunningham

osmium said:
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
 
O

osmium

Bill Cunningham said:
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;
 
K

Keith Thompson

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?
 
B

Bill Cunningham

osmium said:
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
 
B

Ben Bacarisse

Bill Cunningham said:
Ben said:
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.
 
B

Barry Schwarz

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.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top