?
Solved

math logic using structures

Posted on 1998-12-13
8
Medium Priority
?
176 Views
Last Modified: 2010-04-15
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
Comment
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
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Expert Comment

by:MatthewL
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

by:MatthewL
ID: 1255099
Forgot to include the structure!

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

Matt
0
 
LVL 11

Accepted Solution

by:
alexo earned 150 total points
ID: 1255100
   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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ronron10
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

by:ozo
ID: 1255102
Is this question a test, assignment, or final_exam?
0
 
LVL 10

Expert Comment

by:RONSLOW
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

by:ozo
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

by:RONSLOW
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question