In: Computer Science
Design an algorithm that will read an array of 200 characters and display to the screen a count of the occurrences of each of the five vowels (a, e, i, o, u) in the array. Your string is: The quick brown fox jumped over the lazy dog
That is the question for my pseudo code question, I don't know if I have this correct can i have someone look this over and finish this and explain what i needed to add and or what i needed to change! Thank you.
Array Processing Challenge
MODULE Array_processing_challenge
Set total_a, total_e, total_i, total_o, total_u to zero
DO loop_index = 1 TO 200
IF characters (loop_index) = "a" or "A" THEN
add 1 total_a
ELSE
IF characters (loop_index) = "e" or "E" THEN
add 1 total_e
ELSE
IF characters (loop_index) = "i" or "I" THEN
add 1 total_i
ELSE
IF characters (loop_index) = "o" or "O" THEN
add 1 total_o
ELSE
IF characters(loop_index) = "u" or "U" THEN
add 1 total_a
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDDO
Print "Total occurrences of a: ", total_a
Print "Total occurrences of a: ", total_e
Print "Total occurrences of a: ", total_i
Print "Total occurrences of a: ", total_o
Print "Total occurrences of a: ", total_u
END Array_processing_challenge
Solution :
In your psedo code there are 2 mistakes
1. Iterate the loop till the length of the array not to 200 ( Because if your array contain only 50 characters then there is wastaage of time and memory for that by iterating it to 200 times)
2. Inside the loop you have to increment the value of loop_index so that it will point to next character in the array so add (loop_index =loop_index+1 inside do loop ). If you don't increment the value of loop_index then every time your program check condition for the first character only.
Algorithm :
Below is the coreect algorithm for processing the array
Array_processing( a){
total_a=0, total_e=0, total_i=0, total_o=0, total_u =0
//initializing index value to zero
index=0
len=sizeOf(a) //using the length of the array for the number of iteration
while(index<len) //we have to iterate till the length of the array
{
if(charAt(index)=='a' or charAt(index)=='A')
{
total_a=total_a+1
//closing the if 'a' condition
}
else if(charAt(index)=='e' or charAt(index)=='E')
{
total_e=total_e+1
//closing the if 'e' condition
}
else if(charAt(index)=='i' or charAt(index)=='I')
//closing the if 'i condition
{
total_i=total_i+1
}
else if(charAt(index)=='O' or charAt(index)=='O')
{
total_o=total_o+1
//closing the if 'o' condition
}
else if(charAt(index)=='U' or charAt(index)=='U')
{
total_u=totalu+1
//closing the if 'u' condition
}
index=index+1; //incrementing the value of index each time till we iterate
}
} //end of while loop
Print "Total occurrences of a: ", total_a
Print "Total occurrences of a: ", total_e
Print "Total occurrences of a: ", total_i
Print "Total occurrences of a: ", total_o
Print "Total occurrences of a: ", total_u