Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   completed function (http://www.velocityreviews.com/forums/t744194-completed-function.html)

Bill Cunningham 02-25-2011 06:09 PM

completed function
 
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



Ben Bacarisse 02-25-2011 06:30 PM

Re: completed function
 
"Bill Cunningham" <nospam@nspam.invalid> 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.

osmium 02-25-2011 06:33 PM

Re: completed function
 
"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
>
>




Bill Cunningham 02-25-2011 06:41 PM

Re: completed function
 
Ben Bacarisse wrote:
> "Bill Cunningham" <nospam@nspam.invalid> 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 02-25-2011 06:44 PM

Re: completed function
 
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



osmium 02-25-2011 07:30 PM

Re: completed function
 
"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.




Bill Cunningham 02-25-2011 07:52 PM

Re: completed function
 
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 02-25-2011 07:54 PM

Re: completed function
 
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 02-25-2011 07:57 PM

Re: completed function
 
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



osmium 02-25-2011 09:11 PM

Re: completed function
 
"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;




All times are GMT. The time now is 01:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.