Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » A book on C - help(Translation between the book and Eclipse)
A book on C - help [message #1851349] Sun, 03 April 2022 19:29 Go to next message
Lineke Notermanns is currently offline Lineke NotermannsFriend
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 #1852586 is a reply to message #1851349] Wed, 25 May 2022 11:49 Go to previous messageGo to next message
Connor Ferguson is currently offline Connor FergusonFriend
Messages: 1
Registered: May 2022
Junior Member
hm, that's a really tricky exercise. Difficult to answer..
anyone got a solution?
i dont :(

[Updated on: Thu, 26 May 2022 13:30]

Report message to a moderator

Re: A book on C - help [message #1852600 is a reply to message #1851349] Thu, 26 May 2022 13:34 Go to previous messageGo to next message
Stephanie Smith is currently offline Stephanie SmithFriend
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 Go to previous messageGo to next message
Lineke Notermanns is currently offline Lineke NotermannsFriend
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

Re: A book on C - help [message #1853468 is a reply to message #1852632] Sun, 03 July 2022 20:20 Go to previous messageGo to next message
tepalia02 02 is currently offline tepalia02 02Friend
Messages: 19
Registered: April 2022
Junior Member
I ran the code in online C compiler and got the following output:

Count             Item          Average        Naive avg
-----             ----          -------           ------

3
1     3.000000e+00     3.000000e+00     3.000000e+00
2
2     2.000000e+00     2.500000e+00     2.500000e+00
4
3     4.000000e+00     3.000000e+00     3.000000e+00
Re: A book on C - help [message #1854157 is a reply to message #1851349] Wed, 03 August 2022 21:15 Go to previous messageGo to next message
timothy johnson is currently offline timothy johnsonFriend
Messages: 3
Registered: August 2022
Junior Member
You say you are running on win. Where are you typing the command running_sum < data ?

Since it is not whining about not finding the file we can assume for the moment it is actually reading the file.

Data can be stored in text format or in binary format. It may be that you are storing in binary format and none of the values you are reading have a visible interpretation for printf(). Things like space, tab, bell, ... can be printed - but nothing happens.
Re: A book on C - help [message #1855363 is a reply to message #1854157] Mon, 10 October 2022 18:31 Go to previous message
Lineke Notermanns is currently offline Lineke NotermannsFriend
Messages: 3
Registered: April 2022
Junior Member
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.
Previous Topic:[OFFTOPIC] Can't login to eclipse bugzilla
Next Topic:Indexer fails: ambiguous node while parsing .cpp
Goto Forum:
  


Current Time: Fri Apr 26 01:40:07 GMT 2024

Powered by FUDForum. Page generated in 0.03354 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top