• Status: Solved
• Priority: Medium
• Security: Public
• Views: 182

# math logic using structures

i am trying to write a function that will add all the interger values in an array with the integer values of other arrays, placing the total in its own variable all within the same structure.
0
ronron10
• 2
• 2
• 2
• +2
1 Solution

Commented:
Hi ronron10;

I'm not sure if I understand the problem clearly.  Is this the structure you had in mind?
Or is there a second int array in the structure to hold the sums?

Matt
0

Commented:
Forgot to include the structure!

typedef struct      {      int numbers[20];
int size;
} intArray;

Matt
0

Commented:
{
int i;
assert(pTo->size == pFrom->size); /* see below */
for (i = 0; i < pTo->size; ++i)
pTo->numbers[i] += pFrom->numbers[i];
}

If the arrays are not of the same size, the result is probably neaningless (unless I misunderstood the question).
0

Author Commented:
struct students
{
char name[25];
int test[6];
int assignment[10];
int total_points;
int final_exam;

}class_[25];
i'm trying to put the sum of test[], assignment[],and final_exam into total_points
0

Commented:
Is this question a test, assignment, or final_exam?
0

Commented:
Is this what you mean (assuming you have an int i)

student.total_points = 0;
for (i = 0; i < 6; i++) {
student.total_point += student.test[i];
}
for (i = 0; i < 10; i++) {
student.total_point += student.assignment[i];
}
student.total_point += student.final_exam;

It's not really that hard... did you try it yourself first?

0

Commented:
Did you try it first RONSLOW?:)  Is this what you mean

class_[j].total_points = 0;

ronron10, if you can tell us what you tried, and what difficulties you had,
it may be easier for us to understand what help you need.
0

Commented:
just showed him how to accumulate a single student.  Don't want to do TOO much for him as it does seem like homework.  But if you want to be more specific...

for (j = 0; j < 25; j++) {
class_[j].total_points = 0;
for (i = 0; i < 6; i++) {
class_[j].total_point += class_[j].test[i];
}
for (i = 0; i < 10; i++) {
class_[j].total_point += class_[j].assignment[i];
}
class_[j].total_point += class_[j].final_exam;
}

this assumes you have int i,j;

Of course, you'd need to wrap this up into a function and pass a students[] array to it

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.