12 Replies - 419 Views - Last Post: 03 August 2014 - 08:34 PM Rate Topic: -----

#1 cjwhites  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-August 14

Average using an array and for loop in the process_sup

Posted 03 August 2014 - 12:51 PM

Hello all,

I am new to programming and i just took my first class, the only thing I've learned is Pseudocode, but I understand quite a bit of C, but I'm making a program that uses arrays and I'm looking for verification on a formula i wrote that calculates the average age of a bunch of family members that were input in the Input_sub.

set sum = 0.0
Set Count= 0
For (i=0; i<Fam_Count; i+1)
Sum=sum+age[i]
fam_count=Fam_Count[i]+1
end for
average=sum/Fam_Count[i]

I don't completely understand arrays and i have to do this in the process module which is another reason why I'm confused.

Is This A Good Question/Topic? 0
  • +

Replies To: Average using an array and for loop in the process_sup

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 10,616
  • Joined: 08-August 08

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 05:00 PM

Is Farm_Count an array??? Why are you using Farm_Count[i]???
Was This Post Helpful? 0
  • +
  • -

#3 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 239
  • View blog
  • Posts: 872
  • Joined: 18-April 09

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 06:05 PM

Hi,

You could also calculate the average age without the use of an array. That would be easier if you haven't already covered arrays in class.
Was This Post Helpful? 0
  • +
  • -

#4 cjwhites  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-August 14

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 06:30 PM

Fam_Count its from the input_sub. The only array in the second array is age

Fam_Count = 0
Input Name[Fam_Count]
While Name[Fam_Count] != *
Input Age[Fam_Count]
Input State [Fam_Count]
Fam_Count = Fam_Count + 1
Input Name[Fam_Count]
End While

set sum = 0.0
Set Count= 0
For (i=0; i<Fam_Count; i+1)
Sum=sum+age[i]
fam_count=Fam_Count+1
end for
average=sum/Fam_Count[i]
Was This Post Helpful? 0
  • +
  • -

#5 cjwhites  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-August 14

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 07:00 PM

View Postcjwhites, on 03 August 2014 - 06:30 PM, said:

Fam_Count its from the input_sub. The only array in the second array is age

Fam_Count = 0
Input Name[Fam_Count]
While Name[Fam_Count] != *
Input Age[Fam_Count]
Input State [Fam_Count]
Fam_Count = Fam_Count + 1
Input Name[Fam_Count]
End While

set sum = 0.0
Set Average= 0
For (i=0; i<Fam_Count; i+1)
Sum=sum+age[i]
fam_count=Fam_Count+1
end for
average=sum/Fam_Count[i]


Fam_Count = 0
Input Name[Fam_Count]
While Name[Fam_Count] != *
Input Age[Fam_Count]
Input State [Fam_Count]
Fam_Count = Fam_Count + 1
Input Name[Fam_Count]
End While

set sum = 0.0
Set Average= 0
For (i=0; i<Fam_Count; i+1)
Sum=sum+age[i]
fam_count=Fam_Count+1
end for
average=sum/Fam_Count
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,325
  • Joined: 05-May 12

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 07:31 PM

Do you have a specific question?
Was This Post Helpful? 0
  • +
  • -

#7 cjwhites  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-August 14

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 07:35 PM

is my pseudocode correct for my input and process subprograms below is what i have so far.

Fam_Count = 0
Input Name[Fam_Count]
While Name[Fam_Count] != *
Input Age[Fam_Count]
Input State [Fam_Count]
Fam_Count = Fam_Count + 1
Input Name[Fam_Count]
End While

set sum = 0.0
Set Average= 0
For (i=0; i<Fam_Count; i+1)
Sum=sum+age[i]
fam_count=Fam_Count+1
end for
average=sum/Fam_Count
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,325
  • Joined: 05-May 12

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 07:37 PM

Depends... Is it pseudo code that assumes it will be translated to C or is it generic pseudo code? Not all languages have arrays that are zero-based.

In your flavor of pseudo-code, is an undeclared variable assumed to have an initial value of zero? If so why bother declaring and setting the other variables to zero?
Was This Post Helpful? 0
  • +
  • -

#9 cjwhites  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-August 14

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 07:41 PM

so I don't need to set sum and average = to 0
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,325
  • Joined: 05-May 12

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 07:48 PM

It really depends on the convention used within your learning/teaching environment. If your convention is ththey are implicitly declared and initialized to zero, then don't clutter your pseudo code with it. If not, then take the space and time to do so.
Was This Post Helpful? 0
  • +
  • -

#11 cjwhites  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-August 14

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 07:51 PM

In class we always set our variables to 0 before we begin so thats why i did it here.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,325
  • Joined: 05-May 12

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 08:01 PM

The pseudo code looks okay. Even though it maybe pseudo code, formatting it nicely with appropriate indents would aid in readability. Avoid using parallel arrays. When you have associated data, keep them together in structs or classes instead of using parallel arrays. Also note that you could have been computing the sum within the same loop that was taking the input, but that really depends on whether you needed to all the input in one function, and then the average computation in another.
Was This Post Helpful? 0
  • +
  • -

#13 cjwhites  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-August 14

Re: Average using an array and for loop in the process_sup

Posted 03 August 2014 - 08:34 PM

Yea that was why i was a bit confused cause he doesn't want us to calculate the sum in the input_sub. I would calculate the sum during my input_sub then do average in my process_sub, but now i have to have two separate subprograms inside my Process_sub one for the average and another thats for another portion that i didn't post cause i'm sure that code is valid. its really stupid and what do you mean by parallel arrays ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1