HELP, HELP...

M

martinmontielr

Hi... If somebody here had a program in C++ that makes the NUMERIC
CENTER please,,


is this.... the numbers are separate in two parts 1,2,3,4,5 and the
numeric center is 6,,, because, the next numbers 7and 8 give us 15.

1+2+3+4+5= 15

6

7+8= 15

ok THANKS AGAIN.
 
A

andrepersaud

Hey this program cannot be too hard to write. Here's an implementation
I think would work:

Assuming that the data is stored in an array and the array is already
sorted in ascending order, I would have two nested for loops. The
outer loop will step through the array and whichever number it is on
( i ), it is the potential numeric center. The interior loop will add
up the numbers to the left of the whatever i is and it will add up the
numbers to the right of i and check if they are equal, if they are
then that number i the numeric center.

// Special Cases
if (ARRAYSIZE == 0) { error }
if (ARRAYSIZE == 1) { return the only number in the array }

otherwise

for (int i = 0; i < ARRAYSIZE; i++) {
int leftsum, rightsum;
for (int j = 0; j < ARRAYSIZE; j++) {
if ( j < i )
leftsum += ARRAY[j];
elseif ( j > i )
rightsum += ARRAY[j];
// don't do nothing when j == i.
}
if (rightsum == leftsum) { return ARRAY; }
}

ARRAYSIZE does not have to be a constant, I just put it like that for
simplicity sake.
If you need more clarification or help with anything else feel free to
email me, I don't mind brushing up on my programming skills every once
and a while.

- Dre-On
 
Z

Zeppe

Hey this program cannot be too hard to write. Here's an implementation
I think would work:

No, it wouldn't. To solve the homework for other people is evil, and
that's why: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.3

Anyway, the problem is not well-posed in the message above, for example
what if the numbers can't be divided in a lower and upper subset that
sum up to the each result? your program doesn't return anything, for
example, which is an error for sure.

Anyway, if you want to be really helpful, help the people think instead
of posting the code. That's what the homeworks are for: they are not
given because the professor is not able to find the solution, but
because the student should become able to find the solution *by himself*.

Thank you,

Zeppe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,242
Latest member
BarbMott55

Latest Threads

Top