How do i write the code for balanced array...URGENT please

An array with an odd number of elements is said to be balanced if all elements (except the middle one) are strictly greater than the value of the middle element. Note that only arrays with an odd number of elements have a middle element.
Write a C++ function that accepts an integer array and returns 1 or “Balanced” if it is a balanced array, otherwise it returns 0 or “Not Balanced”.
We won't do your homework for you.
@lynn22,
Rules & Regulations for posting on C++ forums

1: Try your best to solve your problem on your own before posting; no one appreciates people dumping their homework assignments without even trying to do them first.

2: Explain your problem or assignment very clearly (or just post the assignment itself).

3: Post whatever code you have already written, so we can see that you've actually made some effort on it, as well as any compiler errors, warnings, etc that you are having trouble with. This is linked to 1 above.

4: When you post your code, use code tags:

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/


It makes your code easier to read, along with proper indenting (just copy and paste your code directly from your IDE to preserve formatting).
Rules&Regs, @JRmanx, from stackoverflow.com

Thanks!
max
Last edited on
How do i write the code

Text editor, a keyboard, some skull sweat and a compiler.

We won't do your homework for you, the assignment was given to you to solve.
some skull sweat

Or elbow grease ;)
@agent max,

Not sure how often programmers use elbow grease. Not a lot of physical labor is involved.

But OK.
but perhaps lots of finger grease.....
1. If the size is even then the array isn't balanced.
2. Go through the array comparing items to the middle one. If any item is less than or equal to the middle item then the array is unbalanced.
3. Otherwise the array is balanced.
@doug4,
Technically correct. "Elbow grease" is probably the wrong term to use there.

seeplus wrote:
but perhaps lots of finger grease...

Oh my yes. At the end of the day, my keyboard is disgusting– weird gunk all over the keys. So is my mouse.

Edit:
Sorry OP, we're getting kind of off topic.
Last edited on
Since it's a metaphor, it can mean anything you want. It's not like there's an actual elbow grease people apply to their joints.
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iomanip>
#include <iostream>
#include <iterator>

int main()
{
	const int numbs[] {3, 4, 5, 2, 6, 7, 8};
	bool bal {std::size(numbs) % 2};

	for (size_t midpos = std::size(numbs) / 2, i = 0; i < std::size(numbs) && (bal = (bal && (numbs[i] > numbs[midpos]))); i += 1 + (i == midpos - 1));

	std::cout << std::boolalpha << bal << '\n';
}

Last edited on
helios wrote:
It's not like there's an actual elbow grease people apply to their joints.

Actually...

My dad uses some kind of liniment on his elbows because they "feel creaky." Some kind of old people thing (hope he doesn't read this). It's some kind of weird greasy stuff he gets at the hardware store. He claims it works.

Ironically, the name of this stuff is actually "Elbow Grease."
he gets at the hardware store
Wot. Even if it's some kind of legit ointment, why would you buy it at the hardware store rather than at the pharmacy?
To be honest, I have no idea. He says it works, though. I have suggested various things to him before, but he wasn't having any of that.

He's the kind of guy, you know, drives a beat-up old Chevy pickup, wears old coveralls and a Yankees baseball cap, anti-vaccine, pro-gun, etc.

I kind of doubt he's ever even been to a pharmacy.
Topic archived. No new replies allowed.