Re: Rookie Student C++ Array question (reading from a file)
From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 03/08/04
- Next message: Tydr Schnubbis: "Re: C char array question"
- Previous message: Phrank: "Re: Rookie Student C++ Array question (reading from a file)"
- In reply to: phrankndonna_at_charter.net: "Rookie Student C++ Array question (reading from a file)"
- Next in thread: Phrank: "Re: Rookie Student C++ Array question (reading from a file)"
- Reply: Phrank: "Re: Rookie Student C++ Array question (reading from a file)"
- Reply: Phrank: "Re: Rookie Student C++ Array question (reading from a file)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 08 Mar 2004 13:45:39 +0100
phrankndonna@charter.net wrote:
>
> Hello,
>
[snip]
> start the counter. I'd appreciate any help and advice. Thanks!
Start by writing down in plain english words what needs to be done. You
can do this in form of comments:
int main()
{
// open input file
// Repeat the following. Repeat it as long
// as a number could be read from the file
{
// see if that number already occoured in the list
// of all numbers so far.
// if the number has occoured, just increment the counter
// else allocate a new slot and start the counter with 1
}
// close files
// output results
}
That's your overall plan. The next step will be to include the details
(or refine the plan if you think it is not detailed enough in certain
parts).
I'l pick just one spot:
// see if that number already occoured in the list
// of all numbers so far.
That needs to search the array for some number. From this it follows
that you need some way to identify how much of the array has been used
up to now. In your original source, I think the variable 'index' served
that purpose.
found = -1;
for( int j = 0; j < index; ++j ) {
if( holder == numbers[j] ) {
found = j;
}
}
// at this place found contains either the index where the number
// has occoured or equals -1 indicating that the number is a
// new one.
***
The program with the addition thus looks like this (of course you will
do the additions inline in your real source code)
int main()
{
// open input file
// Repeat the following. Repeat it as long
// as a number could be read from the file
{
// see if that number already occoured in the list
// of all numbers so far.
found = -1;
for( int j = 0; j < index; ++j ) {
if( holder == numbers[j] ) {
found = j;
}
}
// at this place found contains either the index where the number
// has occoured or equals -1 indicating that the number is a
// new one.
// if the number has occoured, just increment the counter
// else allocate a new slot and start the counter with 1
}
// close files
// output results
}
Now the next poart is easy:
// if the number has occoured, just increment the counter
// else allocate a new slot and start the counter with 1
Given the immediatly previous comment (about the purpose of
variable found, this one is easy:
int main()
{
// open input file
// Repeat the following. Repeat it as long
// as a number could be read from the file
{
// see if that number already occoured in the list
// of all numbers so far.
found = -1;
for( int j = 0; j < index; ++j ) {
if( holder == numbers[j] ) {
found = j;
}
}
// at this place found contains either the index where the number
// has occoured or equals -1 indicating that the number is a
// new one.
// if the number has occoured, just increment the counter
if( found != -1 )
counter[found]++;
// else allocate a new slot and start the counter with 1
else {
numbers[index] = holder;
counter[index] = 1;
index++;
}
}
// close files
// output results
}
Working outwards, the next thing you could work on, is the part
// Repeat the following. Repeat it as long
// as a number could be read from the file
That's easy again:
while( fin >> holder ) {
Leading to a (sub)total of:
int main()
{
// open input file
// Repeat the following. Repeat it as long
// as a number could be read from the file
while( fin >> holder ) {
// see if that number already occoured in the list
// of all numbers so far.
found = -1;
for( int j = 0; j < index; ++j ) {
if( holder == numbers[j] ) {
found = j;
}
}
// at this place found contains either the index where the number
// has occoured or equals -1 indicating that the number is a
// new one.
// if the number has occoured, just increment the counter
if( found != -1 )
counter[found]++;
// else allocate a new slot and start the counter with 1
else {
numbers[index] = holder;
counter[index] = 1;
index++;
}
}
// close files
// output results
}
Now you take over and finish the assignment.
But the point is: Start with a plan. Then fill in
the details. If you start filling in the details from
inside to outside (just as I have done right now), or
the other way doesn't matter. From outside to inside
(or top to bottom) is usually better, because you immediatly
will have code that can be tested. Let us assume that oder.
So what things would you do in what order?
work on: open file
just write the code which opens the file and test if that works.
Also check the other way round: If the specified file does not
exist, is it reported correctly?
work on: read numbers from file
to test that part, simply output the numbers. But: Before doing
more work on the processing part, make sure that reading really
works as expected (a somewhat smaller input file of say 2 to 8
or 10 numbers will be a good thing for testing anyway).
work on: search if the number has occoured allready.
By outputting test mesages that should be an easy exercise. Again:
A small input file, containing no more then 10 numbers is a good
candidate for testing.
work on: increment the counter
Again: Doing some test outputs (just as you have done already) should
verify that this part works.
etc..
The important thing is: Intersperse testing with writing. Don't write
a program in one big rush. Write parts of it and make sure they indeed
do what they should do. How do you know what they should do? Well. You
have your plan in the form of comments in your code. These comments will
guide you on what the next goal in your program is.
-- Karl Heinz Buchegger kbuchegg@gascad.at
- Next message: Tydr Schnubbis: "Re: C char array question"
- Previous message: Phrank: "Re: Rookie Student C++ Array question (reading from a file)"
- In reply to: phrankndonna_at_charter.net: "Rookie Student C++ Array question (reading from a file)"
- Next in thread: Phrank: "Re: Rookie Student C++ Array question (reading from a file)"
- Reply: Phrank: "Re: Rookie Student C++ Array question (reading from a file)"
- Reply: Phrank: "Re: Rookie Student C++ Array question (reading from a file)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|