comp.lang.ada
 help / color / mirror / Atom feed
* Word counting
@ 2003-12-11 22:01 wave
  2003-12-11 22:45 ` David C. Hoos
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: wave @ 2003-12-11 22:01 UTC (permalink / raw)


Hello, been following this newsgroup for a few months now and I can
tell it's a great place for help judging by some of the threads I've
been reading.

I'm extremely new to ada after coding in a handful of newer languages
such as PHP and the like. I'm having some difficulties with a program
I am trying to design using the various functions available to me.

The program is this:

I'm trying to read a file of which each sentence consists of a line of
words, separated obviously by one or more blank spaces. I'm trying to
get it to read these lines of the file and count the number of words
that have one letter, two letters, and so on, up to a maximum of
fifteen letters.

I'm actually having loads more difficulty than I thought with a 'case'
designed version of the program, although I have now changed that to
loops as, to me, it looks much 'cleaner'.

I've got the following code so far, I'm wondering if anyone could be
of assistance and tell me whats up with it.

   Max_Chars    :          Integer := 100;  
   Max_Words    : constant Integer := 15;  
   Max_Word_Len : constant Integer := 20;  

   Position,  
   Number   : Integer := 1;  
   Length,  
   Counted_Words : Integer := 0;  

   Current_Line : String (1 .. Max_Chars);  

   type Words_Of_Length is array (Integer range 1 .. 10) of Integer; 
   type Word_Count_Line is array (Integer range 1 .. 10) of Integer; 
   Word_Count : Word_Count_Line;  
   Word_Length : Words_Of_Length;  

begin

   while (not End_Of_File) loop

      Position := 1;
      Get_Line(Current_Line,Length);

      for Position in 1..Length loop
         if (Current_Line(Position) = ' ')  then
            Word_Length(Number) := Word_Length(Number) + 1;
            Number := Number + 1;
            Counted_Words := Counted_Words + 1;
         end if;

            Put(Current_Line(Position));

      end loop;
   end loop;

   Put("Counted words: " & Integer'Image(Counted_Words));
   New_Line(5);
      
   for Index in 1..10 loop
      Put(Integer'Image(Index) & " " &
Integer'Image(Word_Length(Index)));
      New_Line;
   end loop;

   New_Line(5);

end;


I appreciate any feedback you could give me on this, as it's really
been annoying me.



Cheers,

Mut.



^ permalink raw reply	[flat|nested] 13+ messages in thread
* RE: Word counting
@ 2003-12-11 22:47 amado.alves
  0 siblings, 0 replies; 13+ messages in thread
From: amado.alves @ 2003-12-11 22:47 UTC (permalink / raw)
  To: comp.lang.ada

(Sorry if this shows up illformatted. Right now I'm limited to a crappy webmail system.)

	<<Hello, been following this newsgroup for a few months now and I can
	tell it's a great place for help judging by some of the threads I've
	been reading.>>

	

	You bet! 

	<<I'm extremely new to ada after coding in a handful of newer languages
	such as PHP and the like.>>

	Welcome. A warning: there are no ex-Adaists ;-)

	<<I'm trying to read a file of which each sentence consists of a line of
	words, separated obviously by one or more blank spaces. I'm trying to
	get it to read these lines of the file and count the number of words
	that have one letter, two letters, and so on, up to a maximum of
	fifteen letters.>>

	<<...
	   type Words_Of_Length is array (Integer range 1 .. 10) of Integer;
	>>

	Shouldn't the upper bound be Max_Words?

	<<
	   while (not End_Of_File) loop
	
	      Position := 1; -- LOOSE THIS
	      Get_Line(Current_Line,Length);
	
	      for Position in 1..Length loop
	         if (Current_Line(Position) = ' ')  then -- MISSES WORDS AT END OF LINE, WHICH ARE NOT FOLLOWED BY SPACE
	            Word_Length(Number) := Word_Length(Number) + 1;
	            Number := Number + 1;
	            Counted_Words := Counted_Words + 1; -- EQUAL TO Number, NO?
	         end if;
	
	            Put(Current_Line(Position));
	
	      end loop;
	   end loop;
	...>>

	Too many problems so far. Some of logic. Rethink your program. Write in pseudo-code first. Or in Pascal. Assume functions like Get_Next_Word. We'll flesh them out after. Post the pseudo code, and I'll gladly teach you the Ada.




^ permalink raw reply	[flat|nested] 13+ messages in thread
* RE: Word counting
@ 2003-12-11 22:53 amado.alves
  0 siblings, 0 replies; 13+ messages in thread
From: amado.alves @ 2003-12-11 22:53 UTC (permalink / raw)
  To: comp.lang.ada@ada.eu.org

<<package Word_Parser is...>>

Good David doesn't have a nose for homework :-)




^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: Word counting
@ 2003-12-12  2:39 ada_wizard
  2003-12-12  9:49 ` wave
  0 siblings, 1 reply; 13+ messages in thread
From: ada_wizard @ 2003-12-12  2:39 UTC (permalink / raw)
  To: comp.lang.ada

MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
User-Agent: Internet Messaging Program (IMP) 3.2.1

mutilation@bonbon.net (wave) writes:

> I'm extremely new to ada after coding in a handful of newer
> languages such as PHP and the like. I'm having some difficulties
> with a program I am trying to design using the various functions
> available to me.

Welcome to the One True Language :).

> I'm trying to read a file of which each sentence consists of a line of
> words, separated obviously by one or more blank spaces. I'm trying to
> get it to read these lines of the file and count the number of words
> that have one letter, two letters, and so on, up to a maximum of
> fifteen letters.
> 
>    Max_Chars    :          Integer := 100;  
>    Max_Words    : constant Integer := 15;  
>    Max_Word_Len : constant Integer := 20;  

These make sense.

>    Position,  
>    Number   : Integer := 1;  
>    Length,  
>    Counted_Words : Integer := 0;  

These too.

>    Current_Line : String (1 .. Max_Chars);  
> 
>    type Words_Of_Length is array (Integer range 1 .. 10) of Integer; 
>    type Word_Count_Line is array (Integer range 1 .. 10) of Integer; 
>    Word_Count : Word_Count_Line;  
>    Word_Length : Words_Of_Length;  

Now you've lost me. What is stored in Word_Length (1)? the number of
words of length one? It will help you (as well as me) to say that in a
clear comment.

> begin
> 
>    while (not End_Of_File) loop

the parens are not necessary, and give away that you are used to C :).

>       Position := 1;
>       Get_Line(Current_Line,Length);
> 
>       for Position in 1..Length loop
>          if (Current_Line(Position) = ' ')  then
>             Word_Length(Number) := Word_Length(Number) + 1;

Since this logic does not depend on Position, I can't see that it does
anything useful. 

> <snip rest of program>
> 
> I appreciate any feedback you could give me on this, as it's really
> been annoying me.

What does it do that you did not expect? I know "it doesn't work", but
can you be more specific?

Show an example input, and say what output you expected. 

Process the example input yourself, following your code. That will
help you figure out what it should do, and why it isn't doing it.

In general, your code is fairly good style.

Hope to hear more from you,

-- 
-- Stephe

___________________________________________________________
This mail sent using ToadMail -- Web based e-mail @ ToadNet



^ permalink raw reply	[flat|nested] 13+ messages in thread
* RE: Word counting
@ 2003-12-12 12:56 amado.alves
  0 siblings, 0 replies; 13+ messages in thread
From: amado.alves @ 2003-12-12 12:56 UTC (permalink / raw)
  To: comp.lang.ada

<<
While not the end of the file
        Read the next line from the file
        Move the cursor one place and if character is within a..Z boundaries
that character is stored in an array word_length(number) for that word
length
        if the chracter is a space, then we've hit the end of that word
length so increment our word length to search for by 1 (number). we've
also hit the end of a word to increment our word count.
End of loop;
>>
 
This logic still has faults. Consider:
 
LET WORD_COUNT BE AN ARRAY OF MAX_LENGTH INTEGERS, ALL SET TO ZERO
While not the end of the file
        Read the next line from the file
        LOOP
             GET NEXT WORD
             IF NONE, EXIT LOOP
             INCREMENT WORD_COUNT (LENGTH OF WORD) BY 1
        END OF LOOP
End of loop;

When you see the prior faults and how they were solved here call back.
 



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2003-12-13  6:09 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-11 22:01 Word counting wave
2003-12-11 22:45 ` David C. Hoos
2003-12-12  1:17 ` Jeffrey Carter
2003-12-12  3:22 ` Steve
2003-12-12 14:33 ` Martin Krischik
  -- strict thread matches above, loose matches on Subject: below --
2003-12-11 22:47 amado.alves
2003-12-11 22:53 amado.alves
2003-12-12  2:39 ada_wizard
2003-12-12  9:49 ` wave
2003-12-12 18:26   ` Jeffrey Carter
2003-12-13  3:40   ` Steve
2003-12-13  6:09     ` tmoran
2003-12-12 12:56 amado.alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox