Solved

math logic using structures

Posted on 1998-12-13
8
172 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

856 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