Jump to content

Pseudo Code... just need feedback and maybe some help

- - - - -

  • Please log in to reply
5 replies to this topic

#1
wolfman

wolfman

    Newbie

  • Members
  • PipPip
  • 18 posts
I have an assignment that asks the user to input their test grades and display their average accordingly. The kicker is I can accept 5 test grades max, but the user can enter 3 if they wanted. I have to use an array to store the grades and use a looping structure to initialize the elements of my array.

This is what I have so far, I'm using pseudo "code".


class averageGrades

       main()

num maxGrades  =  5

num count[maxGrades]

num index  =  1

num sum = 0


	output “Welcome to the Grade Average Program”


while index <= maxGrades

        count[index] = 0

        index  =  index + 1

endWhile


index = 1

output “Please enter one of your test grade: ”

input count[index]


while count[index] >= 0

   	       sum = count[index] + sum

	       output “Please enter another test grade: ”

          	       input count[index]

endWhile


average = sum/(index - 1)

output “You grade average is: ” average


       return

endClass



Thanks,

-=wolfman

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I'm not sure what language you'll choose to implement this, but typically in most languages, array indices begin with 0 for the first element, not 1. So in your case, the array positions would be denoted by the indices 0, 1, 2, 3, and 4. With the way you have it written now, it will try to access array position 5, which has not been allocated, resulting in a variety of different errors depending on what language is used to implement it. (for example, ArrayIndexOutOfBoundsException for Java.)

Also, this portion:

while count[index] >= 0

   	       sum = count[index] + sum

	       output “Please enter another test grade: ”

          	       input count[index]

endWhile

seems malformed to me. The value index is never changed here. The loop will continue to execute for all values that are greater than or equal to zero. The logic will permit me to enter more than 5 values here, which is not what I think you want. Also, it is not counting the number of grades a user inputs, so when you calculate the average at the end, it will not be the correct value.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
wolfman

wolfman

    Newbie

  • Members
  • PipPip
  • 18 posts
I went and redid all the code using FOR loops. I have a couple of questions though. I didn't declare the array size, num testScores, would it be possible to declare the size like this num testScores <= 5? I couldn't find anything in my book relating to that, so it's probably wrong. I figured I ask anyway.

I was just going to get the size from the input of the user. But then I got to thinking even though I say the max is 5, if it was a real program, I have nothing there to stop the user from entering another grade.

class GradeAverageProgram


main()


	     num testScores                   //Size of the array

	     num scores [testScores]    //Declare an array

	     num sumScores                  //Variable for the scores added together


	     output “How many test scores do you have? (MAX 5)”

	     input testScores

	

	     for count = 0; count < = testScores; count + 1

		output “Test score #” count + 1 “: ”

		input scores[count]

	     endfor


	     for count = 0; count < =  testScores; count + 1

	     	sumScores = scores[count]

	     endfor


	     output “Your grade average is :” sumScores/testScores		


return

endClass


Thank you
-=[ w | o | l | f | m | a | n ]=-

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Generally, you have to specify the value of testScores before you allocate the array, but it really depends on the language/pseudocode spec.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
In addition to what WingedPanther pointed out, this line:

for count = 0; count < = testScores; count + 1

should be:

for count = 0; count < testScores; count + 1


Keep in mind, when you're talking about index arrays, the indices run from 0 to size-1. (So in your case, 0, 1, 2, 3, and 4.) You want the loop to stop after the last element, which is when count = testScores, so make sure you don't use an equivalence comparison there.

If you think about it this way, it makes more sense: The way you have it written now, the loop condition will test true for the following values of count: 0, 1, 2, 3, 4, and 5, which is a total of 6 loop iterations, more than you have room for.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#6
wolfman

wolfman

    Newbie

  • Members
  • PipPip
  • 18 posts
Just wanted to thank you guys for your help! I made the applicable changes and scored an A+!
-=[ w | o | l | f | m | a | n ]=-




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users