[2 days left] Whatâ€™s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

math logic using structures

Posted on 1998-12-13
Medium Priority
177 Views
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
Question by:ronron10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +2

LVL 1

Expert Comment

ID: 1255098
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

LVL 1

Expert Comment

ID: 1255099
Forgot to include the structure!

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

Matt
0

LVL 11

Accepted Solution

alexo earned 150 total points
ID: 1255100
{
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 Comment

ID: 1255101
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

LVL 84

Expert Comment

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

LVL 10

Expert Comment

ID: 1255103
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

LVL 84

Expert Comment

ID: 1255104
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

LVL 10

Expert Comment

ID: 1255105
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

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll