Solved

math logic using structures

Posted on 1998-12-13
8
171 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
  • 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 50 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

770 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