[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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
Asked:
ronron10
  • 2
  • 2
  • 2
  • +2
1 Solution
 
MatthewLCommented:
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
 
MatthewLCommented:
Forgot to include the structure!

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

Matt
0
 
alexoCommented:
   void AddArray(intArray* pTo, intArray* pFrom)
    {
        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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ronron10Author 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
 
ozoCommented:
Is this question a test, assignment, or final_exam?
0
 
RONSLOWCommented:
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
 
ozoCommented:
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
 
RONSLOWCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now