adding colums to text

B

Bill Cunningham

Keith Thompson said:
Are you writing this new information *just* on the last line, or are
you appending information to each line? (I've been assuming the
latter.)

In other words, do you want the final output to look like this?

10.50
10.25
10.00
10.75
11.00 52.50 10.50

That's a rather odd thing to want to do. You're writing the summary
data on the same line as the last input data item, which seems quite
arbitrary.

Well I guess it would be alright to do this.

11.00
52.50 10.50


Bill
 
K

Keith Thompson

Bill Cunningham said:
Well I guess it would be alright to do this.

11.00
52.50 10.50

You managed not to answer my actual question.

Are you writing this new information *just* on the last line, or are
you appending information to each line?

And what is the basis for this requirement, anyway? Are you just
making up a problem for yourself (which is fine), or is there
something else behind it?
 
K

Keith Thompson

rio said:
Richard said:
I would recommend sprintf followed by fprintf. Why? You can see your
formatted string clearly in your debugger before you even start writing
to files and wasting time...

I would raccomand of not use [*]printf, [*]scanf
but write down your not bugged version one [even if more easy]
that detect overflow and all errors

I've restored the attribution line for the quoted text.

The *scanf functions are dangerous because they don't handle numeric
overflow (unless you restrict the number of digits in the input), but
what's dangerous about the *printf functions? The only thing I can
think of is that sprintf can overflow the target buffer if you're not
careful; the solution to that is either to use a target buffer that's
known to be big enough, or to use snprintf if it's available.

BTW, "rio", are you the same person who used to post here as
"RoSsIaCrIiLoIA", and later as "av <[email protected]>"? I've asked you this
before, and you never answered. If you're changing identities to
avoid killfiles, please stop. If you're not the same person, then
never mind.
 
B

Bill Cunningham

And what is the basis for this requirement, anyway? Are you just
making up a problem for yourself (which is fine), or is there
something else behind it?
I am trying to create a moving average of data. Whether it be for 5-10-30
days of securitites prices.

Bill
 
B

Bill Cunningham

After further consideration,
I think this is what he most probably wants:
10.50 10.50 10.50
10.25 20.75 10.38
10.00 30.75 10.25
10.75 41.50 10.38
11.00 52.50 10.50
[snip]

Yes that would work Pete. Your code is beyond my skills though. I'm not into
structs unfortunately. A moving average is what I'm looking for and that
data posted by you would fit the bill. If a person kept going like you have
it set up here one could choose a moving average of 5-10-30 or whatever days
of averaged data.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I am trying to create a moving average of data. Whether it be for 5-10-30
days of securitites prices.

I'm not sure, but I *think* you've finally answered the question I
asked at least twice, if only indirectly.

Each input line contains a number. Each output line contains the
number from the corresponding input line, followed by the sum of all
the input numbers seen so far, followed by the mean of all the input
numbers so far.

Is that correct? If so, why didn't you just say so in the first
place, or at least after I asked?

If (and only if!) you confirm that my description is an accurate
statement of the problem you're trying to solve, I'll give you some
hints about how to solve it. If I haven't described the problem
correctly, please clarify.

Your stubborn insistence on trying to solve a problem when you haven't
clearly stated what the problem is has caused a lot of people to waste
a lot of time.

Presumably you know what you want to accomplish. Go back to your
initial article in this thread, and ask yourself whether someone could
figure out what you're trying to do based only on your description.
Then think about how you could have made your description clear
enough. And next time, *please* try to formulate your question
clearly *before* you post it here.

Incidentally, I could probably write a working program to solve your
problem in about 5 minutes. Guiding you through the process takes a
lot longer. But handing you the solution wouldn't do you much good.
 
S

santosh

Keith said:
rio said:
Richard said:
I would recommend sprintf followed by fprintf. Why? You can see
your formatted string clearly in your debugger before you even
start writing to files and wasting time...

I would raccomand of not use [*]printf, [*]scanf
but write down your not bugged version one [even if more easy]
that detect overflow and all errors

I've restored the attribution line for the quoted text.

The *scanf functions are dangerous because they don't handle numeric
overflow (unless you restrict the number of digits in the input),

They are also dangerous with the unadorned %s specifier.

BTW, "rio", are you the same person who used to post here as
"RoSsIaCrIiLoIA", and later as "av <[email protected]>"? I've asked you this
before, and you never answered.

IMO, it isn't necessary to ask him (and why would he admit to have once
been a troll anyway?). His writing style, and his fake email addresses
are a dead giveaway.
 
B

Bill Cunningham

Keith Thompson said:
I'm not sure, but I *think* you've finally answered the question I
asked at least twice, if only indirectly.

Each input line contains a number. Each output line contains the
number from the corresponding input line, followed by the sum of all
the input numbers seen so far, followed by the mean of all the input
numbers so far.

Is that correct? If so, why didn't you just say so in the first
place, or at least after I asked?

[snip]

Yes that's what I want to do. There were several ways to go about this but
what you described is the way to go.

Bill
 
S

santosh

Bill said:
Keith Thompson said:
I'm not sure, but I *think* you've finally answered the question I
asked at least twice, if only indirectly.

Each input line contains a number. Each output line contains the
number from the corresponding input line, followed by the sum of all
the input numbers seen so far, followed by the mean of all the input
numbers so far.

Is that correct? If so, why didn't you just say so in the first
place, or at least after I asked?

[snip]

Yes that's what I want to do. There were several ways to go about this
but what you described is the way to go.

Then the solution is very easy. Set aside a variable to keep track of
the running total and another for the current average (to calculate
average of course, you'd need another variable to track the number of
input items encountered so far), and for each input item read, add the
item to the running sum, increment the counter tracking the number of
items read, calculate the average, print out the input item, print the
sum after the next tabstop and the average after that all followed by a
newline. Repeat for each line of the input file.

You won't need anything beyond fscanf and fprintf.
 
S

santosh

Bill said:
Keith Thompson said:
I'm not sure, but I *think* you've finally answered the question I
asked at least twice, if only indirectly.

Each input line contains a number. Each output line contains the
number from the corresponding input line, followed by the sum of all
the input numbers seen so far, followed by the mean of all the input
numbers so far.

Is that correct? If so, why didn't you just say so in the first
place, or at least after I asked?

[snip]

Yes that's what I want to do. There were several ways to go about this
but what you described is the way to go.

Then the solution is very easy. Set aside a variable to keep track of
the running total and another for the current average (to calculate
average of course, you'd need another variable to track the number of
input items encountered so far), and for each input item read, add the
item to the running sum, increment the counter tracking the number of
items read, calculate the average, print out the input item, print the
sum after the next tabstop and the average after that all followed by a
newline. Repeat for each line of the input file.

You won't need anything beyond fscanf and fprintf.
 
B

Bill Cunningham

Then the solution is very easy. Set aside a variable to keep track of
the running total and another for the current average (to calculate
average of course, you'd need another variable to track the number of
input items encountered so far),

Now I'm a little confused about how to go about a variable to keep track
of the input numbers so far.

and for each input item read, add the
 
B

Barry Schwarz

Now I'm a little confused about how to go about a variable to keep track
of the input numbers so far.

Initialize an int to zero. After reading each line, add one to the
int (or, if you are feeling brave, increment the int).
and for each input item read, add the


Remove del for email
 
B

Bill Cunningham

Initialize an int to zero. After reading each line, add one to the
int (or, if you are feeling brave, increment the int).

Could you give me an example of this? Would you use for ?

int count=0;
What would be the trigger to increment?

Bill
 
K

Keith Thompson

Bill Cunningham said:
Could you give me an example of this? Would you use for ?

int count=0;
What would be the trigger to increment?

You're asking questions that are so elementary that you're practically
asking us to write the program for you.

Hmm.

Ok, here's an outline in English:

Keep track of the number of lines you've read and of the sum of the
numbers so far. Both start as 0 or 0.0.
For each line in the input file:
Get a floating-point number from the line.
Update the line counter and the sum.
Print the following on the output file:
The number you just read,
The sum of the numbers you've read so far,
The average of the numbers you've read so far
(the sum divided by the count).
 
B

Bill Cunningham

You're asking questions that are so elementary that you're practically
asking us to write the program for you.

Hmm.

Ok, here's an outline in English:

Keep track of the number of lines you've read and of the sum of the
numbers so far. Both start as 0 or 0.0.
For each line in the input file:
Get a floating-point number from the line.
Update the line counter and the sum.
Print the following on the output file:
The number you just read,
The sum of the numbers you've read so far,
The average of the numbers you've read so far
(the sum divided by the count).

You're talking about reading. I need to write the data first. I've
considered writing two functions to do this, one to write and one to read. I
need to start writing the data first. I have a program that writes the
single numbers and that's all.

Bill
 
N

Nick Keighley

um, if you don't like C why don't you program
in another language and leave comp.lang.c alone?
nymshwhat?

nymshifting = name shifting

deliberatly changing your posting name to avoid recognition.
It's ok to use a couple of names (eg. work and home) if
there's a good reason, but changing names just to confuse people
is dishonest.

it doesn't interst me much how people know me or recognoze me;
people make error 90% of times,

must of us attempt to keep the error rate much lower than this
so it is the first thing to do
use our own mind and not the people thinking "all'ammasso"

"in the mass"?
 
K

Keith Thompson

Bill Cunningham said:
You're talking about reading. I need to write the data first. I've
considered writing two functions to do this, one to write and one to read. I
need to start writing the data first. I have a program that writes the
single numbers and that's all.

So you already have a program that writes the data. If you're having
a problem with that, feel free to ask about it, but you haven't given
a hint about that so far.

Once you've written the data (which I've been assuming you've already
done), you need to read it, adding the extra information you've been
talking about. I've given you an outline of how to do that.
 
B

Bill Cunningham

So you already have a program that writes the data. If you're having
a problem with that, feel free to ask about it, but you haven't given
a hint about that so far.

That's what this post has been all about. Appending data as stated
earlier in the thread to already written data. My program work like this, I
enter p 12.00 and continue with different numbers and a list is printed.

12.00
5.00
12.30

I enter "p" for print and each number and a text file is written. Reading it
mean I'm going to have to learn new functions and I'm happy to do it. If I
can identify them. I've been warned about fscanf and told to use it both.

Bill
 
K

Keith Thompson

Bill Cunningham said:
That's what this post has been all about. Appending data as stated
earlier in the thread to already written data. My program work like this, I
enter p 12.00 and continue with different numbers and a list is printed.

12.00
5.00
12.30

I enter "p" for print and each number and a text file is written. Reading it
mean I'm going to have to learn new functions and I'm happy to do it. If I
can identify them. I've been warned about fscanf and told to use it both.

I'm afraid you've lost me.

Upthread, I gave you an outline for how to read your existing
data and write the final result you're looking for, assuming that
you already have the single-column input file. You hadn't said
anything about needing to generate the single-column input file.
In response, you wrote:

| You're talking about reading. I need to write the data first. I've
| considered writing two functions to do this, one to write and one to read. I
| need to start writing the data first. I have a program that writes the
| single numbers and that's all.

Do you already have the single-column input file or not?
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top