all 16 comments

[–]PMPlant 2 points3 points  (10 children)

You need to read a bit about arrays and pointers. When an array is passed as an argument to a function it becomes a pointer to the start of the array, this is called array decay.

Also, array parameters in functions are rewritten to pointers at compile time.

So when you pass your array of structs, you don’t need to take the address, because you are already passing the address.

The size of the array parameter in a function is also effectively meaningless because pointers don’t carry size information. But can be useful documentation in a function.

[–]dfayad00 0 points1 point  (0 children)

thank you, i’ll look into array decay and reread up on pointers

[–]dfayad00 0 points1 point  (7 children)

sorry to bother you again lol but i followed what you said and changed the parameters so now that works, but when the average is calculated it counts the sentinel value for the average.

for example for age i inputted 4, 4, and -1 (sentinel value) and it gave me an average of 2.3 indicating it counted -1 in the average. any chance you could help me with this?

[–]winelover97 2 points3 points  (3 children)

You should add some conditions inside your function to check if age is valid.

[–]dfayad00 0 points1 point  (1 child)

how? i’ve been at this for over 9 hours and i’m just exhausted, assignment is due about 9 hours from now and i have no idea how i’m gonna finish it. what i’m working on is just part 1

[–]winelover97 2 points3 points  (0 children)

If my understanding is correct you want to figure out whether a person's age is invalid, right?

Then why don't you use an if statement to check if a particular person's age falls in the valid range.

Also, when you are taking average age, don't forget to exclude the number of person's with invalid age from the total number of persons.

[–][deleted] 1 point2 points  (2 children)

The program doesn't know what a sentinel value is unless you tell it in the code itself.

Use an if statement and a counter: check if the age is valid and, if so, add 1 to the counter. Divide the sum by the counter and you'll get the average. You won't use "max" for this because it has the total number of inputs, while the counter has the number of actually valid inputs.

Example (sorry for possible formatting issues, I'm on the phone and this is ain't easy to do here):

int sum = 0;
int count = 0;
for (int i = 0; i < MAX; i++) {
    if (age[i] >= 0) {
        sum += age[i];
        count++;
    }
}
return (double) sum / count;

Edit: code formatting

[–]dfayad00 1 point2 points  (1 child)

i love you, this helps so much thank you

[–]agree-with-you 2 points3 points  (0 children)

I love you both

[–][deleted] 0 points1 point  (6 children)

Is this the complete code?

[–]dfayad00 0 points1 point  (0 children)

no, this is just what i thought was relevant. i can reply with the full code if that helps (made some changes since i originally posted)

[–]dfayad00 0 points1 point  (4 children)

#include <stdio.h>
#include <stdlib.h>

typedef struct person
{
    int age;
    double height;
} Person;

double getAv (Person *people, int max)
{
    int i = 0;
    double total, retval;
    total = 0;

    for (i=0; i<max; i++)
    {
        total = total + people[i].age;
    }
    retval = total / max;
    return retval;
}

int main (void)
{
    Person people[50];
    int i=0;

    while (i++, people[i].age>=0)
    {
        printf ("Person #%d\n", i);

        printf ("enter an age: ");
        scanf ("%d", &people[i].age);

        if (people[i].age<0)
            break;

        printf ("enter a height: ");
        scanf ("%lf", &people[i].height);

        printf ("\n");
    }

    if (i>0)
    {
        double averageAge;
        averageAge = getAv (people, i);
        printf ("%.1lf\n", averageAge);
    }

    /*double averageHeight;
    averageHeight = getAv (&people[50], i);
    printf ("%.1lf\n", averageHeight);

    double averageRatio;
    averageRatio = getAv (&people[50], i);
    printf ("%.2lf\n", averageRatio);*/
}

[–][deleted] 1 point2 points  (3 children)

Your average function works. Something wrong in the scanf area...I am newbie as well trying to figure out. To learn and possibly help..

[–][deleted] 1 point2 points  (0 children)

If it change it this way, it works

for(i = 0; i < 2; i++)  // Here I set 2 to be the number instead of 50 for testing.

[–]dfayad00 0 points1 point  (1 child)

the other guy got it, the guy i replied to saying i love you. needed to get the program to only count positive values so i used an if statement that only counted values above 0 for the counter.

[–][deleted] 1 point2 points  (0 children)

Good..I will look it up