Re: Rookie Student C++ Array question (reading from a file)

From: Phrank (no email)
Date: 03/11/04


Date: Thu, 11 Mar 2004 08:04:52 -0500

On Mon, 08 Mar 2004 13:45:39 +0100, Karl Heinz Buchegger
<kbuchegg@gascad.at> wrote:

>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.

Thank you SO much for this guidance Mr. Buchegger!! I appologize for
not getting back right away, but I had encountered a computer problem
(which certainly didn't help me with finishing this!). I had looked
at the previous suggestion of mapping, but that's beyond where I
instructor wants us to be at the moment. Your suggestion however, is
right inline with what we need to do. Overall, you hit the nail on
the head when you suggested I was trying to do too much at once. I
guess it's a matter of mental time management - I've got so much on my
plate to take care of right now, I'm just hoping to sit down, take a
look and punch out the code in one fell swoop and have it work the
first time everytime. But that doesn't seem to happen. So, I'll back
up and regroup and follow your guidelines - it's just exactly what we
are needing to do. I will post back after I finish this. Thank you
again!!

Frank



Relevant Pages

  • Re: Rookie Student C++ Array question (reading from a file)
    ... >Start by writing down in plain english words what needs to be done. ... >int main ... >That's your overall plan. ... >do the additions inline in your real source code) ...
    (alt.comp.lang.learn.c-cpp)
  • Re: "EXT3-fs error" after resume from s2ram
    ... INT A disabled ... writing 0xe2000008) ... connection state: disconnected -> associated ... setting latency timer to 64 ...
    (Linux-Kernel)
  • Re: Some days you stuggle
    ... plan or map or set of directions--even if vague. ... It's those course corrections and changes that happened in Books 1 and 2 ... But the original whinge is becoming a moot point: I've gotten past the ... I am writing the story I want--it just happens to be a five-book story. ...
    (rec.arts.sf.composition)
  • Re: Updating via timesheet
    ... If you get to the stage of writing ... it is better to plan to too much detail than too ... for more or less frequent reports as experience then dicatates. ... apply to manage the situation - can local management cope or do you need to ...
    (microsoft.public.project)
  • Re: read and write columns
    ... You attempt to open the same file, "data", for writing and reading, ... Read the documentation for fscanf. ... int main ...
    (comp.lang.c)