Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

pseudo code pseudocode

  • Please log in to reply
5 replies to this topic

#1 wolfman

wolfman

    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 06 October 2011 - 07:27 PM

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
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 07 October 2011 - 06:37 AM

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

ti-99-sig.png
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

    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 07 October 2011 - 10:03 PM

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
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 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

Posted 08 October 2011 - 12:25 PM

Generally, you have to specify the value of testScores before you allocate the array, but it really depends on the language/pseudocode spec.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 10 October 2011 - 06:55 AM

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

ti-99-sig.png
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

    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 12 October 2011 - 06:07 PM

Just wanted to thank you guys for your help! I made the applicable changes and scored an A+!
  • 0





Also tagged with one or more of these keywords: pseudo code, pseudocode

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download