simple C language programming question

A

Andy

Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.

The result:
# of integers: 4

so far I have this:)

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char ch;
int count = 0;
FILE *fp;
char prev;

fp = fopen("input.txt", "r");

prev = ' ';
while((ch = fgetc(fp)) != EOF)
{



prev = ch;
}

printf("# of integers: %d\n", count);
fclose(fp);
return 0;
}
 
O

osmium

Andy said:
Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.

<snip>

See what kind of magic you can perform with isdgit() in <ctype.h>.
 
D

Darrell Grainger

Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.

The result:
# of integers: 4

so far I have this:)

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char ch;
int count = 0;
FILE *fp;
char prev;

fp = fopen("input.txt", "r");

prev = ' ';
while((ch = fgetc(fp)) != EOF)
{



prev = ch;
}

printf("# of integers: %d\n", count);
fclose(fp);
return 0;
}

Think of it as a state machine. You start off in the state of
not_an_integer. Get characters and look for a numeric digit.

If you read something that is a numeric digit then you move to the state
might_be_an_integer. Now you start reading for a space or punctuation.

If you get a space or punctuation then increment your counter and move
back to not_an_integer.

Use a variable to figure out what state you are in, e.g. 0 =
not_an_integer, 1 = might_be_an_integer.

The only place this might go wrong is if the string is "The value for PI
is approximately 3.14159 or 7/22." The "3." might get read as one integer
and "14159 " would be a second. Write a check for that. Do you want 7/22
to be two integers? Will you handle that okay?

P.S. this is one of many approaches. It is not necessarily the best but it
fits well with what you already have.
 
E

Eric Sosman

Andy said:
Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.

The result:
# of integers: 4

so far I have this:)

#include <stdio.h>
#include <ctype.h>

int main(void)
{
char ch;
int count = 0;
FILE *fp;
char prev;

fp = fopen("input.txt", "r");

prev = ' ';
while((ch = fgetc(fp)) != EOF)

Question 12.1 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

explains why this is wrong.

Here's a hint: Once you've read an input character,
the future path of the program depends on whether that
character is a digit or not. There's a ready-made way
to make this decision, sitting there in the C library
just waiting for you to find it ...

Here's another hint: When you find a digit, should
you count a new number? Not always, clearly, because
then you'd count three numbers in "x1y23z". How can
you tell that the '2' and the '3' are part of a single
number, and not independent numbers? Can you decide
before you read the 'z'?
 
M

Martin Dickopp

Hi, I have a homework question, can some expert help me with this.

q.Write a program that reads a text file and reports the number of
integers and you may assume a number is defined as one or more digits
separated by one or whitespace characters or punctuations.

What if a sequence of digits is adjacent to a letter? A more complete
definition of "number" is needed. For simplicity, you might want to
consider a sequence of digits separated by non-digits a "number".
For example,
The amount is 5657 and the change is 55. The prices are 123 and 56.

The result:
# of integers: 4

so far I have this:)

Not bad so far.
#include <stdio.h>
#include <ctype.h>

int main(void)
{
char ch;
int count = 0;
FILE *fp;
char prev;

fp = fopen("input.txt", "r");

You should check if `fopen' returned a null pointer, which indicates an
error.
prev = ' ';
while((ch = fgetc(fp)) != EOF)

This is a bug: `fgetc' returns an `int', so you should make `ch' an
`int'. Specifically, `EOF' expands to a negative integer constant which
is outside the range of allowed characters.

For extra points, you could also check if `fgetc' returned `EOF' due to
an error or due to end-of-file condition. The functions `feof' or
`ferror' will be useful here.
{



prev = ch;
}

I guess your question is how to fill the empty space. :) Think about
it: Every number has exactly one beginning, so it is sufficient to count
/beginnings/ of numbers. You are at the beginning of a number if the
current character is a digit and the previous character is not a digit.
Can you express this in C? You'll need the `isdigit' function (as you
probably know, since you have already included <ctype.h>) and you need
to translate words like "and" and "not" in my previous sentence into
appropriate C operators.

Your assignment of ' ' to `prev' before the loop is indeed a nice way to
take care of the case that the first character in the file is a digit.
printf("# of integers: %d\n", count);
fclose(fp);
return 0;
}

Martin
 
M

Mike Wahler

Martin Dickopp said:
What if a sequence of digits is adjacent to a letter? A more complete
definition of "number" is needed.

Imo the definition given by OP is fairly 'complete', except
that perhaps the term 'punctuation' needs to be clarified.
Perhaps those chars giving 'true' result from 'ispunct()'
would be sufficient.


-Mike
 
M

Martin Dickopp

Mike Wahler said:
Imo the definition given by OP is fairly 'complete', except
that perhaps the term 'punctuation' needs to be clarified.
Perhaps those chars giving 'true' result from 'ispunct()'
would be sufficient.

It is still unclear to me if a character sequence like "x4y2z" should
be considered to contain zero, one, or two numbers. If I take the
definition literally, it contains zero numbers, because there is not a
single digit which is separated by either whitespace or punctuation
characters. Likewise, the phrase "1mi is approximately 1609m." would
not contain any numbers. I just wasn't sure if this is intended.

To the OP: If this is indeed intended, my answer to your question is
wrong. Please disregard it in this case.

Martin
 
C

CBFalconer

Mike said:
Imo the definition given by OP is fairly 'complete', except
that perhaps the term 'punctuation' needs to be clarified.
Perhaps those chars giving 'true' result from 'ispunct()'
would be sufficient.

Better returning false from isdigit();

#include <stdio.h>
#include <ctype.h>

int main(void)
{
int numcount, innumber, ch;

numcount = innumber = 0;
while (EOF != (ch = getchar())
if (!isdigit(ch)) innumber = 0;
else if (!innumber) {
innumber = 1;
numcount++;
}
/* exercise - display results */
return 0;
} /* untested */
 
M

Mabden

CBFalconer said:
Better returning false from isdigit();

<snip code>

Geez guys... doing kids' homework? And for free, yet... If you're bored, run
through the K&R exercises.

Bad form, I say!

Even the hints are too much. I'd rather feed a troll than feed a student.
I've worked with the result of these people. They drain the group by running
back and forth, getting other people to do their work - and for good pay, no
less!

Please show some restraint.
 
M

Martin Dickopp

Mabden said:
And for free, yet... If you're bored, run through the K&R exercises.

I dislike this attitute. Who are you to tell me how I should spend my
spare time and how much I should charge for my spare time activities?
Are you one of those people who believe they know better what's good for
me than I do myself, and therefore feel compelled to protect me from
myself?
Even the hints are too much. I'd rather feed a troll than feed a
student. I've worked with the result of these people. They drain the
group by running back and forth, getting other people to do their work
- and for good pay, no less!

Helping people with C problems (including homework problems) is the very
topic of this newsgroup. Of course, it does not help people if somebody
else actually *does* their (home)work, but I don't think that has
happened in this case.

Martin
 
C

CBFalconer

Mabden said:
<snip code>

Geez guys... doing kids' homework? And for free, yet... If you're
bored, run through the K&R exercises.

Bad form, I say!

Even the hints are too much. I'd rather feed a troll than feed a
student. I've worked with the result of these people. They drain
the group by running back and forth, getting other people to do
their work - and for good pay, no less!

Please show some restraint.

In this case the OP had made a creditable effort. I see no harm
in exposing him to better code.
 
M

Mabden

Martin Dickopp said:
I dislike this attitute. Who are you to tell me how I should spend my
spare time and how much I should charge for my spare time activities?
Are you one of those people who believe they know better what's good for
me than I do myself, and therefore feel compelled to protect me from
myself?

Oh, woe is you! I have obviously tread upon your favorite pass-time -
playing with children.
I wished only to suggest a better hobby, one that doesn't do future damage;
I certainly never attempted to control your small, paranoid mind.
Helping people with C problems (including homework problems) is the very
topic of this newsgroup. Of course, it does not help people if somebody
else actually *does* their (home)work, but I don't think that has
happened in this case.

I don't believe this newsgroup, or any, is about doing students' homework.
That's what I saw happening. There were whole programs there!

I saw a wrong thing and spoke out about it. You may disagree with me, but
you won't change my opinion. And I care not at all what you may think about
my protest.

I can't stop you from doing a students work for them, I merely pointed out
why I object - I have worked with these people. I found them
counter-productive. So is doing homework when you are not in school.

But many people have perversions worse than yours. No matter. Have your fun.
 
S

Scott J. McCaughrin

: Hi, I have a homework question, can some expert help me with this.

: q.Write a program that reads a text file and reports the number of
: integers and you may assume a number is defined as one or more digits
: separated by one or whitespace characters or punctuations.


The problem is ill-posed, as stated. 123.45 is not an integer, but '.'
is punctuation, so this number would be reported as 2 integers.

: For example,
: The amount is 5657 and the change is 55. The prices are 123 and 56.

: The result:
: # of integers: 4
 
M

Martin Dickopp

Mabden said:
Oh, woe is you! I have obviously tread upon your favorite pass-time -
playing with children. I wished only to suggest a better hobby, one
that doesn't do future damage; I certainly never attempted to control
your small, paranoid mind. [...]
But many people have perversions worse than yours. No matter. Have
your fun.

The fact that you apparently have to resort to such ad hominem attacks
proves that you have no point.

Followup-To: poster set to prevent further flaming in the group.

Martin
 
P

Paul D. Boyle

: :> Mike Wahler wrote:
:> >> (e-mail address removed) (Andy) writes:
:> >>
:> >>> q.Write a program that reads a text file and reports the number
:> >>> of integers and you may assume a number is defined as one or more
:> >>> digits separated by one or whitespace characters or punctuations.
:> >>
:> >> What if a sequence of digits is adjacent to a letter? A more
:> >> complete definition of "number" is needed.
:> >
:> > Imo the definition given by OP is fairly 'complete', except
:> > that perhaps the term 'punctuation' needs to be clarified.
:> > Perhaps those chars giving 'true' result from 'ispunct()'
:> > would be sufficient.
:>
:> Better returning false from isdigit();

: <snip code>

: Geez guys... doing kids' homework? And for free, yet... If you're bored, run
: through the K&R exercises.

: Bad form, I say!

: Even the hints are too much. I'd rather feed a troll than feed a student.
: I've worked with the result of these people. They drain the group by running
: back and forth, getting other people to do their work - and for good pay, no
: less!

If you read CBFalconer's code, you see that he didn't conform to the
student assignment exactly. He used getchar(), which reads from stdin.
The assignment specifies to read from a text file. I thought CBFalconer's
code was appropriate given that the OP made what seemed like an honest
effort.


Paul
 
M

Mabden

Martin Dickopp said:
Mabden said:
Oh, woe is you! I have obviously tread upon your favorite pass-time -
playing with children. I wished only to suggest a better hobby, one
that doesn't do future damage; I certainly never attempted to control
your small, paranoid mind. [...]
But many people have perversions worse than yours. No matter. Have
your fun.

The fact that you apparently have to resort to such ad hominem attacks
proves that you have no point.

Followup-To: poster set to prevent further flaming in the group.

You know, I wasn't even replying to you, but rather to CBFalconer.

Why are you jumping in?

All I see is YOU attacking ME.

<plonk!>
 
J

Joona I Palaste

Mabden said:
Martin Dickopp said:
Mabden said:
Oh, woe is you! I have obviously tread upon your favorite pass-time -
playing with children. I wished only to suggest a better hobby, one
that doesn't do future damage; I certainly never attempted to control
your small, paranoid mind. [...]
But many people have perversions worse than yours. No matter. Have
your fun.

The fact that you apparently have to resort to such ad hominem attacks
proves that you have no point.

Followup-To: poster set to prevent further flaming in the group.
You know, I wasn't even replying to you, but rather to CBFalconer.
Why are you jumping in?
All I see is YOU attacking ME.

So you have insulted CBFalconer instead of Martin. Does that mean
Martin's argument about you resorting to ad hominem attacks is wrong? I
don't think so. When you have to resort to ad hominem attacks, it is
irritating regardless of whom you insult, CBFalconer, Martin, me or
anyone else. I've had it with your arrogance.
*PLONK*
 
M

Mabden

CBFalconer said:
In this case the OP had made a creditable effort. I see no harm
in exposing him to better code.

OK, here's is Andy's code:

==================================
#include <stdio.h>
#include <ctype.h>

int main(void)
{
char ch;
int count = 0;
FILE *fp;
char prev;

fp = fopen("input.txt", "r");

prev = ' ';
while((ch = fgetc(fp)) != EOF)
{



prev = ch;
}

printf("# of integers: %d\n", count);
fclose(fp);
return 0;
}
==================================

Where's the "creditable effort".

I mean, he opened a file and read a character at a time. The instructor, or
the text book would have told him this part. The assignment is to figure out
how to detect integers in a text stream. He did NOTHING towards this goal.

You may justify your involvement anyway you wish, but I don't see any effort
on Andy's part. Yet I look at your code and see something close to the
solution. You admit it is untested and I admit I haven't examined it enough
to see if it works, but my opinion (I _am_ allowed to have my own opinion on
things, aren't I?) is that you are to close to a solution to justify it as a
"hint".

That is about all I'm willing to say on the subject, so further discussion
will most likely be ignored or killfiled.

After all, it was just a suggestion....
 
M

Mabden

Joona I Palaste said:
Mabden said:
Martin Dickopp said:
Oh, woe is you! I have obviously tread upon your favorite pass-time -
playing with children. I wished only to suggest a better hobby, one
that doesn't do future damage; I certainly never attempted to control
your small, paranoid mind.
[...]
But many people have perversions worse than yours. No matter. Have
your fun.

The fact that you apparently have to resort to such ad hominem attacks
proves that you have no point.

Followup-To: poster set to prevent further flaming in the group.
You know, I wasn't even replying to you, but rather to CBFalconer.
Why are you jumping in?
All I see is YOU attacking ME.

So you have insulted CBFalconer instead of Martin. Does that mean
Martin's argument about you resorting to ad hominem attacks is wrong? I
don't think so. When you have to resort to ad hominem attacks, it is
irritating regardless of whom you insult, CBFalconer, Martin, me or
anyone else. I've had it with your arrogance.
*PLONK*

"Playing with children" was not meant to be taken sexually, if that is what
is coming across!
I merely meant, if you are going to do student's (children's) homework for
them, then you are playing with them, and their minds, and their future. It
is better they learn the hard way than to be coddled. You are toying with
their potential, and only for your own amusement or to be seen as a great
knowledgeable person in their eyes over a trivial exercise. I don't believe
this is healthy for them.
 
M

Mabden

Paul D. Boyle said:
: :> Mike Wahler wrote:
:> >> (e-mail address removed) (Andy) writes:
:> >>
:> >>> q.Write a program that reads a text file and reports the number
:> >>> of integers and you may assume a number is defined as one or more
:> >>> digits separated by one or whitespace characters or punctuations.
:> >>
:> >> What if a sequence of digits is adjacent to a letter? A more
:> >> complete definition of "number" is needed.
:> >
:> > Imo the definition given by OP is fairly 'complete', except
:> > that perhaps the term 'punctuation' needs to be clarified.
:> > Perhaps those chars giving 'true' result from 'ispunct()'
:> > would be sufficient.
:>
:> Better returning false from isdigit();

: <snip code>

: Geez guys... doing kids' homework? And for free, yet... If you're bored, run
: through the K&R exercises.

: Bad form, I say!

: Even the hints are too much. I'd rather feed a troll than feed a student.
: I've worked with the result of these people. They drain the group by running
: back and forth, getting other people to do their work - and for good pay, no
: less!

If you read CBFalconer's code, you see that he didn't conform to the
student assignment exactly. He used getchar(), which reads from stdin.
The assignment specifies to read from a text file. I thought CBFalconer's
code was appropriate given that the OP made what seemed like an honest
effort.


Paul

See my comments to him. I don't think the kid did much at all other than
open a file and read characters until EOF. The whole logic of figuring out
what is an integer was the exercise. That's the part CB solved for him/her.
Where the data came from was insignificant.

I'm getting a lot of grief over this. It's just my opinion, and just a
suggestion. Live your own life. Do things well, do things badly, I don't
care.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top