doubles and ints

B

Bill Cunningham

LOL. Nice one Bill. The Regs will keep at it. You're their way out!

You might think this is funny Richard but I'm genuinely stuck. I
appreciate any help to but it goes so far and stops. How about you give me a
page number from k&r2 or something. I could work outta the hole from there.
They're right I'm throwing all the code I know at this thing and it's over
my head.

Bill
 
R

Richard

Bill Cunningham said:
You might think this is funny Richard but I'm genuinely stuck. I
appreciate any help to but it goes so far and stops. How about you give me a
page number from k&r2 or something. I could work outta the hole from there.
They're right I'm throwing all the code I know at this thing and it's over
my head.

Bill

How about you look it up yourself?

You clearly have no interest in reading or understanding any reply given
to you.
 
B

blmblm

Bill Cunningham wrote:

[ snip ]
double x,y,z;

y=x;z=y/count; <-----

What value were you expecting to go into `z`?
x=strtod(argv[1],NULL);

Ah!
The computer can't guess you wanted this line before the one
you marked. Computers are very literal machines. They do
exactly what we tell them to, even if it makes no sense: the trick
is to tell them to do stuff in the right order.

Indeed.

I wonder, though, whether this is necessarily obvious to beginners:

I seem to remember reading somewhere, probably in a discussion
of teaching beginning programming, that some beginners mentally
associate the assignment operator with the mathematical notion
of equality [1], and think that once one has written "y=x",
changes in x will propagate to y, which doesn't happen [2].
Could this be the problem here?

[1] Perhaps only in languages that use "=" for assignment.

[2] For suitable definitions of "doesn't happen" that exclude
"if x and y are both pointers, changes in the thing x points
to obviously affect the thing y points to, since the two are
identical."
 
B

Bill Cunningham

How about you look it up yourself?

You clearly have no interest in reading or understanding any reply given
to you.

I'm unclear on whether to use while or for. But that's my next step. Not
understanding something yes not being interested no. Why would I want to
learn something so bad if I had no interest in it. I pulled out k&r2 the
other day and sat down to proceed with chapter 1. Or dive into about 10 foot
of water with no life raft and no prior swimming lessons.
I can focus on k&r2 or try, and my difficulties with that has nothing to
do with anyone here on usenet. I can do the excersies and I'm willing to but
I can tell you this. There will be 2 or 3 different answers from me for
every question that has one answer. This is as frustrating to me as anyone
else.


Bill
 
B

blmblm

Bill Cunningham said:


That is where you are going wrong. If you throw code at a problem, it is a
sign that you do not understand the problem. Program constructs are not
fungible. You can't just keep piling up ifs and whiles and fors and ints
and doubles until critical mass is reached, any more than you can heap
bricks and windowframes and mortar and slates in a pile and expect them to
become a house.

Stop throwing, and start thinking.

Synchronicity .... I was just about to make exactly this point.

It's tempting to suggest that the "pile up changes at random"
method might not be as hopeless as it seems -- isn't that sort of
what genetic algorithms do, very loosely speaking? and perhaps
there's also a comparison with the evolution of living things? --
but it doesn't seem like the most efficient method.

As I'm often tempted to tell students (in entry-level university
courses in programming), when they seem to be hoping that if they
type random changes fast enough something useful will happen ....:

Put down the keyboard. Take a deep breath. Think. *Then* you
may pick up the keyboard and type again.
 
R

Richard

Bill Cunningham said:
I'm unclear on whether to use while or for. But that's my next
step. Not

Why? What IS IT YOU WANT TO DO? Maybe best just to choose ONE and then go
back and look at it later and decide if the OTHER makes it more readable
and maintainable.
understanding something yes not being interested no. Why would I want to
learn something so bad if I had no interest in it. I pulled out k&r2
the

Don't be such a dumb arse. You are learning a computer language. How do
you KNOW you do not need something unless you first learn what it is.
other day and sat down to proceed with chapter 1. Or dive into about 10 foot
of water with no life raft and no prior swimming lessons.

Huh? K&R2 has a tutorial.
I can focus on k&r2 or try, and my difficulties with that has nothing to
do with anyone here on usenet. I can do the excersies and I'm willing to but
I can tell you this. There will be 2 or 3 different answers from me for
every question that has one answer. This is as frustrating to me as anyone
else.

So stop being so dim and work it yourself and when you have a real
question then post it.
 
S

soscpd

Hello Bill, List


Bill

If I can give you one advice, it will be: try another language before
C. C pull you too much to the edge. You need to do everything by hand,
and for you, as we can see, that is too hard to glark. Your problem
isn't really the C syntax, but transform problem in algorithm. Sorry
to say that, but your code doesn't make sense to me, and will probably
make no sense to others. Try some other language to learn how to do
that, and then move to C. Some here start working with C because they
have no options (but I don't think they get wedged as you are,
usually).

Loop through that links should be a nice start to you. Read and
understand them will (help to) avoid your while(1){nailing jelly to a
tree;}, and some day make your program's less dependent of the phase
of the moon:

http://en.wikipedia.org/wiki/Computer_Programming
http://en.wikipedia.org/wiki/Programming_language
http://en.wikipedia.org/wiki/Algorithm
http://en.wikipedia.org/wiki/Procedural_programming
http://en.wikipedia.org/wiki/Logic
http://en.wikipedia.org/wiki/Binary_numeral_system

Regards
Rafael
 
B

Bill Cunningham

soscpd said:
Hello Bill, List


Bill

If I can give you one advice, it will be: try another language before
C. C pull you too much to the edge. You need to do everything by hand,
and for you, as we can see, that is too hard to glark. Your problem
isn't really the C syntax, but transform problem in algorithm. Sorry
to say that, but your code doesn't make sense to me, and will probably
make no sense to others. Try some other language to learn how to do
that, and then move to C.

[snip]

I learned Basic many years ago. It was a piece of cake. Algorithms I know
nothing about. Basic itself wasn't a problems but putting things where they
needed to go was a bit.

Bill
 
N

Nick Keighley

    You might think this is funny Richard but I'm genuinely stuck. I
appreciate any help to but it goes so far and stops. How about you give me a
page number from k&r2 or something. I could work outta the hole from there.
They're right I'm throwing all the code I know at this thing and it's over
my head.

WHAT ARE YOU TRYING TO DO
 
N

Nick Keighley

36:19 GMT, "Bill Cunningham"]
wrote:
I seem to be having trouble here.


try to avoid starting identifiers with '_'. Only
The Implementor (the guy who writes the compiler and the
standard library) should use such identifiers.

Hiding things in macros makes your code obscure.
int main (int argc,char *argv[]) {
if (argc!=2) {
puts("usage error");
_ex;
}
int count=1;
double x,y,z;

you are mixing ststements with definitions which is not legal C89
(the most widely available standard).
y=x;z=y/count; <-----

please use whitespace

     y = x;
     z = y / count;

x is an uninitialised variable. Hence x, y, and z
and indetereminate values. Why do you divide
by count which is equal to 1 (one)?
x=strtod(argv[1],NULL);

no error checking
FILE *fp;
fp=fopen("s","a");

no error checking
char *string="%.2f\t%.2f\t%.2f\t%i\n";
fprintf(fp,string,x,y,z,count);

x is equal to whatever strtod() returned
y and z are indeterminate.
fclose(fp);
return 0;

OK. here's the output I want and what I want to do.

10.00    10.00    10.00    1
10.50    20.50    10.50    2
10.00    30.50     10.67    3

And so on.[/QUOTE]

I know nothing about the financial world (which I'm guessing
from previous posts is your application domain) so this
might as well be a series of random numbers.

What is your input and how is input derived from output?

In pseudo code you want somthing like this

LOOP UNTIL finished
read input
calc value
print output
END LOOP


--
Nick Keighley

Felix Bloch as a young man once told Heisenberg that
"space is a field of linear operators". "Nonsense" replied Heisenberg,
"space is blue and birds fly through it".
 
B

Bill Cunningham

I know nothing about the financial world (which I'm guessing
from previous posts is your application domain) so this
might as well be a series of random numbers.

What is your input and how is input derived from output?

In pseudo code you want somthing like this

LOOP UNTIL finished
read input
calc value
print output
END LOOP


Nick The above looks exactly like what I want but I really don't think
I've gotten that far. I am working to create a mean of numbers. Calculated
from each input. I want 3 rows of doubles separated by tabs. Then in a final
column to the right a row of ints. These ints are what I am going to divide
my last row of doubles with. I can separate these into functions and call
them from main or whatever is simplest. On another note, is strtod and atof
about the same function ? Or should I say do they do about the same thing?

Bill
 
N

Nick Keighley

Bill, could you persuade your news reader to quote correctly?
You also snip very heavily.

news:6d5ef1e2-6f98-441c-a1c9-661a8c7925ab@m44g2000hsc.googlegroups.com...

Nick Keighley here:
I know nothing about the financial world (which I'm guessing
from previous posts is your application domain) so this
might as well be a series of random numbers.

What is your input and how is input derived from output?

In pseudo code you want somthing like this

    LOOP UNTIL finished
         read input
         calc value
         print output
    END LOOP
To be honest from your code at the beginning of the thread
and your sample output I can deduce what you are trying to do.
I could write the program for you. But I don't think that would help
you. One skill that is essential to programming is the ability
to express yourself clearly. You don't have it. If you can't
explain what you want to do how can you expect to write a program
to do it?

In your original code did you notice that you performed a calculation
using x BEFORE YOU HAD READ A VALUE INTO x?

You need to call strtod() BEFORE you calculate y.

Bill: from here
    Nick The above looks exactly like what I want but I really don't think
I've gotten that far. I am working to create a mean of numbers.

what numbers? Do you read them from a file? You can't calculate a mean
without a loop!

double mean (double a[], int n)
{
double sum;

for (i = 0; i < n; i++)
sum += a;

return sum / n;
}

So is y supposed to be the sum in your program?

Calculated from each input.

you can't calculate a mean "from each input"

I want 3 rows of doubles separated by tabs.

don't tell me the format! Tell me what the doubles *mean*
Then in a final
column to the right a row of ints. These ints are what I am going to divide
my last row of doubles with.

why? I thought I understood what you were doing but no longer!
I can separate these into functions and call
them from main or whatever is simplest.

STEP 1. DECIDE WHAT YOU WANT TO DO!!!!!!!!!!!!

what is the value in the first column (I guess the input-
from a file?). What is column 2 (the sum of numbers so far?)
and column 3 (the mean so far?)

why can't you say!!

On another note, is strtod and atof
about the same function ?

roughly. strtod() handles errors better. atof() returns
0.0 on error. atof("BAD WOLF") returns 0.0, which may not
be what you want.
Or should I say do they do about the same thing?

well since you don't do error checking they are
pretty much identical. This is a suggestion to
do some error checking.

1. decide what you want to do
2. write it down on a peice of paper
3. write pseudo code (like mine above)
4. desk check it- run it in your head
as if you were the computer
5. if it seems ok, translate the p-code
to C
6. test it

this may seem dull and boring but your way of
just throwing random program structures at a
non-specific reuirement doesn't yield working
programs.


--
Nick Keighley

Every sentence I utter must be understood not as an affirmation,
but as a question.
Niels Bohr
 
B

Bill Cunningham

Bill, could you persuade your news reader to quote correctly?
You also snip very heavily.

news:6d5ef1e2-6f98-441c-a1c9-661a8c7925ab@m44g2000hsc.googlegroups.com...

Nick Keighley here:
I know nothing about the financial world (which I'm guessing
from previous posts is your application domain) so this
might as well be a series of random numbers.

What is your input and how is input derived from output?

In pseudo code you want somthing like this

LOOP UNTIL finished
read input
calc value
print output
END LOOP
To be honest from your code at the beginning of the thread
and your sample output I can deduce what you are trying to do.
I could write the program for you. But I don't think that would help
you. One skill that is essential to programming is the ability
to express yourself clearly. You don't have it. If you can't
explain what you want to do how can you expect to write a program
to do it?

In your original code did you notice that you performed a calculation
using x BEFORE YOU HAD READ A VALUE INTO x?

You need to call strtod() BEFORE you calculate y.

Bill: from here
Nick The above looks exactly like what I want but I really don't think
I've gotten that far. I am working to create a mean of numbers.

what numbers? Do you read them from a file? You can't calculate a mean
without a loop!

double mean (double a[], int n)
{
double sum;

for (i = 0; i < n; i++)
sum += a;

return sum / n;
}

So is y supposed to be the sum in your program?


Yes. Yes. Exactly, z is supposed to be the actual average of the y
divided by count. I have been thinking while loops and perhaps all this time
I've been needing for loops. Maybe nested for loops.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Yes. Yes. Exactly, z is supposed to be the actual average of the y
divided by count. I have been thinking while loops and perhaps all this time
I've been needing for loops. Maybe nested for loops.

Bill, most of the time you manage to post correct, but every now and
then you post a followup where the quoted material isn't marked.
*Please* spend some time figuring out what you're doing differently.
Whatever you did this time, please don't do it again.

I'm guessing that only the last paragraph above is yours, but I can't
be sure.

I think part of your trouble, as numerous people have told you, is
that you throw random language features at a problem with
understanding what you're doing.

Imagine a carpenter saying, "I've been thinking about using a hammer,
but maybe I need a screwdriver. Say, how about a Phillips screwdriver.
Or a bandsaw!". The carpenter needs to understand what he's building
*before* he can determine which tools to use.

I think you're doing something similar.
 
B

Ben Bacarisse

Bill Cunningham said:
... I have been thinking while loops and perhaps all this time
I've been needing for loops.

This does not make sense in C. The relation ship between a for loop
an a while is so close that you can always use either. The choice is
partly a matter of style, and partly what you want to emphasise to
readers of you code: A while loop stresses the condition for
continuing, a for loop draws attention to the initial action and the
change made at each step.

But the bottom line is: if you need a for, you can use a while and
vice-versa.
Maybe nested for loops.

Now the *structure* of loops and conditionals *does* reflect what a
program is really doing. In this case you won't need any nested
loops.

As far as I can tell you want:

double total = 0;
int data_count = 0;
double data_point;
while (fscanf(infile, "%lf", &data_point) == 1) {
total += data_point;
data_count += 1;
fprintf(outfile, "%7.3f %7.3f %7.3f %7d\n",
data_point, total, total/data_count, data_count);
}
 
N

Nick Keighley

your posting is totally screwed. Skip to the end of the post
and look for <START HERE>


Bill, could you persuade your news reader to quote correctly?
You also snip very heavily.

news:6d5ef1e2-6f98-441c-a1c9-661a8c7925ab@m44g2000hsc.googlegroups.com....

Nick Keighley here:
I know nothing about the financial world (which I'm guessing
from previous posts is your application domain) so this
might as well be a series of random numbers.
What is your input and how is input derived from output?
In pseudo code you want somthing like this
LOOP UNTIL finished
read input
calc value
print output
END LOOP

To be honest from your code at the beginning of the thread
and your sample output I can deduce what you are trying to do.
I could write the program for you. But I don't think that would help
you. One skill that is essential to programming is the ability
to express yourself clearly. You don't have it. If you can't
explain what you want to do how can you expect to write a program
to do it?

In your original code did you notice that you performed a calculation
using x BEFORE YOU HAD READ A VALUE INTO x?

You need to call strtod() BEFORE you calculate y.

Bill: from here
Nick The above looks exactly like what I want but I really don't think
I've gotten that far. I am working to create a mean of numbers.

what numbers? Do you read them from a file? You can't calculate a mean
without a loop!

double mean (double a[], int n)
{
   double sum;

   for (i = 0; i < n; i++)
       sum += a;

   return sum / n;

}

So is y supposed to be the sum in your program?

    Yes. Yes. Exactly, z is supposed to be the actual average of the y
divided by count. I have been thinking while loops and perhaps all this time
I've been needing for loops. Maybe nested for loops.


<START HERE>

you see you can do it!

so for each line in the file you want to print
<value> <sum so far> <mean so far> <count>

pretty easy huh?

Well since you want to do something FOR EACH line you obviously
need a loop (or two?). Since you do the same thing for each
line I think you can see you only need one loop. The trick to
deciding what sort of loop you want is to think about when the
loop finishes. Are you looping for a fixed count? No you
don't know how big the file is until you read it. Are you
looping until a particular value occurs? Well you could but
it limits the program. Are you looping until you get to the
end of the file? Yes!

So we need to loop until the end of the file is reached.
This suggests

WHILE NOT END-OF-FILE
read number
print line
END

which probably corresponds to a C while-loop

Since the file is just numbers (I assume) you need to
look at fscanf(). Look it up. How do you know when
you've reached the end of the file? Use a while loop.

If you read your past posts (and the replies) I think
you'll find I've told you how to do this before.


--
Nick Keighley

"At terrestrial temeratures matter has complex properties
 which are likely to prove most difficult to unravel;
 but it is reasonable to hope that in a not too distant future
 we shall be competent to understand so simple thing as a star.
  Aurthur S. Eddington  "The Internal Constitution of Stars"
 
B

Bill Cunningham

Bill, most of the time you manage to post correct, but every now and
then you post a followup where the quoted material isn't marked.
*Please* spend some time figuring out what you're doing differently.
Whatever you did this time, please don't do it again.

I'm guessing that only the last paragraph above is yours, but I can't
be sure.

Using OE and I have to do all manually.

Bill
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top