# Average using an array and for loop in the process_sup

Page 1 of 1

## 12 Replies - 716 Views - Last Post: 03 August 2014 - 08:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=351514&amp;s=eeedb4b999e7a918c59f9d7da84ba9ef&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cjwhites

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

• D.I.C Lover

Reputation: 3466
• Posts: 12,349
• 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]???

• جوروترا

Reputation: 283
• Posts: 979
• 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.

### #4 cjwhites

Reputation: 0
• 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]

### #5 cjwhites

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

cjwhites, 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

### #6 Skydiver

• Code herder

Reputation: 4835
• Posts: 15,961
• 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?

### #7 cjwhites

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

### #8 Skydiver

• Code herder

Reputation: 4835
• Posts: 15,961
• 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?

### #9 cjwhites

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

### #10 Skydiver

• Code herder

Reputation: 4835
• Posts: 15,961
• 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.

### #11 cjwhites

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

### #12 Skydiver

• Code herder

Reputation: 4835
• Posts: 15,961
• 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.

### #13 cjwhites

Reputation: 0
• 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 ?

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}