A book on C - help [message #1851349] |
Sun, 03 April 2022 19:29 |
Lineke Notermanns Messages: 3 Registered: April 2022 |
Junior Member |
|
|
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 13:34 |
Stephanie Smith Messages: 1 Registered: May 2022 |
Junior Member |
|
|
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 19:33] Report message to a moderator
|
|
|
Re: A book on C - help [message #1852632 is a reply to message #1852600] |
Sat, 28 May 2022 10:34 |
Lineke Notermanns Messages: 3 Registered: April 2022 |
Junior Member |
|
|
The program is indeed not the problem. It is the way C was used 40 years ago and how to translate that to the way Eclipse works now.
The input in Eclipse works fine, most of the time. I just don't know how to call on data from a different file, in the input field, and what kind of file that should be.
Any help would still be appreciated.
[Updated on: Sat, 28 May 2022 10:35] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.24367 seconds