Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > completed function

Reply
Thread Tools

completed function

 
 
Keith Thompson
Guest
Posts: n/a
 
      02-25-2011
"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) kst- <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"
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      02-25-2011
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


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-25-2011
"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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      02-26-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
submit button - onClick function is not completed to the end despo Javascript 3 02-05-2010 02:58 PM
To Know bat file, execution is completed using VB.NET =?Utf-8?B?TXVzdGFx?= ASP .Net 1 06-23-2005 01:00 PM
Win32Execption: The operation completed successfuly MajorTom ASP .Net 4 05-27-2005 06:40 PM
Completed MCSA/MCSE in 4 Days Indian MCSE 12 11-20-2003 03:54 AM
Completed MCSA Steven Fullman MCSE 6 11-19-2003 09:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57