A book on C - help [message #1851349] |
Sun, 03 April 2022 15:29  |
Eclipse User |
|
|
|
I borrowed 'A book on C' (1998) and came to Eclipse to practice. I'm absolutely a beginner at programming.
The book refers to UNIX and Windows 95/ NT so, of course, things are very different. (I have win10, Eclipse 2022-03 (4.23.0) and MinGW)
The following exercise has me stumped:
"This program has been designed to read numbers from a file. We can type them in from the keyboard, but if we do this, then what appears on the screen will not be formatted correctly. To test this program, we compile it and put the executble code in running_sum. Then we create a file called data and put the following numbers in it:
3 -5 7 -9 11 -13 15 -17 19 -21
Now, when we give the command
running_sum < data
the data appears on screen in the table ."
I created the data file, with the numbers in it, but typing 'running_sum < data ' leaves me with an empty table.
I can type the numbers and the program will use those.
Of course I've looked around on the internet for a way to use a file but nothing I found works.
Can anybody tell me what would be the appropiate action in this case?
Any help would be appreciated.
This is the program:
/* Compute a better average */
#include <stdio.h>
int main(void)
{
int i;
double x;
double avg = 0.0; /* a better average */
double navg;
double sum = 0.0;
printf("%5s%17s%17s%17s\n%5s%17s%17s%17s\n\n",
"Count", "Item", "Average", "Naive avg",
"-----", "----", "-------", "------");
for (i = 1; scanf( "%lf", &x) == 1; ++i){
avg += (x - avg) / i;
sum += x;
navg = sum / i;
printf("%5d%17e%17e%17e\n", i, x, avg, navg);
}
return 0;
}
|
|
|
|
Re: A book on C - help [message #1852600 is a reply to message #1851349] |
Thu, 26 May 2022 09:34   |
Eclipse User |
|
|
|
that's really awkward, because the program itself seems to be written correctly. I think that there is nothing to solve, the problem is not in the program. However, you can try to find the answer in some books. Here I would suggest to not read the whole books, but only the most important parts of it, or a summary. For example, recently I needed to read "salt to the sea", but I had no time and decided to read the summary on https://freebooksummary.com/category/salt-to-the-sea , and it was a very good decision. Maybe it'll work for you too.
[Updated on: Wed, 01 June 2022 15:33] by Moderator
|
|
|
|
|
|
Re: A book on C - help [message #1855363 is a reply to message #1854157] |
Mon, 10 October 2022 14:31  |
Eclipse User |
|
|
|
I apologise, apparantly I did not get an email that there was another respons, so my answer to your question is kinda late. Hopefully you will still read it.
I run the program in Eclipse and type the command in the console.
Nothing happens until I type either 'run' or 'running_sum < data'.
In both cases I get an empty table.
I tried making a file called data, data.txt and data.h.
|
|
|
Powered by
FUDForum. Page generated in 0.11207 seconds