I am trying to finish up my homework but I don't really understand the question


Consider the array temps that the following statements define:

typedef double WeekType[DAY_PER_WEEK];
typedef WeekType YearType[WEEKS_PER_YEAR];
YearType temps;

temps[12] is a one-dimensional array of seven real numbers. Suppose that you want to compute the average of these seven numbers by calling a function average. Write the function average so that average(temps[12]) returns the desired average

Here is a sample code that I've started with. Tell me if im on the right track. thanks

int average(temps[12]){
        for(int index = 0, total = 0; index > 11; i++){
            total += temps[index];
        }
        return average = total/index;
    }
Comments
Nice first post, keep it up. :)

Welcome to DaniWeb. This is a nice first post, good job. Keep it up.

What you have posted is a function definition. When you write a function, how is the definition supposed to be formatted?

returnType functionName([parmType1 parmName1 [, ... [, parmTypeN parmNameN]]) {
  statements...
  [return returnValue;]
}

I would suggest that you look back through your typedefs. Is temps a type? No, it's an array of WeekType objects.

I can't really tell from your post, is temps globally-scoped, local to main(), or something else? The answer makes a big difference in how you write your function. Based on the way your prof wrote the problem, it should probably be declared local to main.

Edited 6 Years Ago by Fbody: n/a

for(int index = 0; index > 11; i++)

Couple of things with this line:
#1) Hopefully i++ is a typo for ++index or index++.

#2) The line as written says starting with index equal to zero increment i(ndex) by 1 each time through the loop as long as index is above 11. As written I don't see how this loop will do anything as index is less than 11 the first time through so the body of the loop will never be run. Change > to < and the loop should run 10 times.

Oh gee i didn't even realize how many mistakes i had in it already. I rushed it because i wasn't sure what to do so I threw up a little code so I could get started. MY problem is I don't understand if the problem requires filtering out non-real numbers and what have you. I assumed the array has 12 spaces each filled with a type YearType. I'm confused!

>>I rushed it because i wasn't sure what to do so I threw up a little code so I could get started
I avoided saying it earlier, but it shows. Rushing your code without thinking through it is never a good thing. You must constantly be thinking about everything involved in every statement you write.
I would take another look at the typedef statements:

typedef double WeekType[DAY_PER_WEEK];
typedef WeekType YearType[WEEKS_PER_YEAR];
YearType temps;

Assuming these constant definitions:

const int DAY_PER_WEEK = 7;
const int WEEKS_PER_YEAR = 52;

This would make the declaration YearType temps; equivalent to:

double temps[52][7];

Passing the value temps[12] to average() would be the equivalent of passing a 7-element array of doubles to the function because you are passing a week's worth of temperatures to be averaged.

Edited 6 Years Ago by Fbody: n/a

Fbody thanks for your help. The last line you wrote if I understand correctly means that temps[12] holds a column of y values if you are to look at it as an array holding values in an xy plane. 52 columns representing the weeks with 7 rows representing the days in each week. I think that is what you mean...blah this is confusing

The exact meaning is unclear. Depends whether you are using row major or column major terminology. In my experience, for what it's worth, usually the first index controls the rows and the second controls the columns (ie, row major). Under that scenario table[52][2] means 52 rows and 7 columns.

Generally the examples in literature have the first index being the row(s) and the second being the column(s). If you declare anArray[Y][X], you have Y rows of X elements/columns.
For example:

//declare an array
int myArray[10][5] = {0};

//in literature, this would generally be represented as:
// R C 0   1   2   3   4
// 0  [0] [0] [0] [0] [0]
// 1  [0] [0] [0] [0] [0]
// 2  [0] [0] [0] [0] [0]
// 3  [0] [0] [0] [0] [0]
// 4  [0] [0] [0] [0] [0]
// 5  [0] [0] [0] [0] [0]
// 6  [0] [0] [0] [0] [0]
// 7  [0] [0] [0] [0] [0]
// 8  [0] [0] [0] [0] [0]
// 9  [0] [0] [0] [0] [0]

//this statement
myArray[3][4] = 1;

//would produce
// R C 0   1   2   3   4
// 0  [0] [0] [0] [0] [0]
// 1  [0] [0] [0] [0] [0]
// 2  [0] [0] [0] [0] [0]
// 3  [0] [0] [0] [0] [1] //notice that column 4 of row 3 changed
// 4  [0] [0] [0] [0] [0]
// 5  [0] [0] [0] [0] [0]
// 6  [0] [0] [0] [0] [0]
// 7  [0] [0] [0] [0] [0]
// 8  [0] [0] [0] [0] [0]
// 9  [0] [0] [0] [0] [0]

This is all literary device though. Within the actual system, they aren't laid out like that, but it seems to be the generally-accepted abstraction for planning/design purposes.

This article has been dead for over six months. Start a new discussion instead.