Not Yet Answered # Solve a problem

Gonbe 32 Discussion Starter fsdhf_20 Discussion Starter fsdhf_20 Adak 419 Discussion Starter fsdhf_20 CrazyDieter 4 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

-2

A cheesy way would be to brute force, you'd end up with something like this:

```
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
// The treshold mentioned in the exercise.
#define TRESHOLD (0.0001)
// Test number.
#define N (0.123456789)
int max (const int l, const int r);
bool solve (const double n, int* const a, int* const b, int* const c, int* const d);
int max(const int l, const int r)
{
return (l > r) ? l : r;
}
bool solve (const double n, int* const a, int* const b, int* const c, int* const d)
{
// Iterate over the possible 'a' values.
for ((*a) = 24; (*a) <= 100; (*a)++)
{
// The check for 24 <= b <= 100 is implicit due to a's bounds.
// Should make it explicit if you want the ranges as variables.
for ((*b) = max(73 - (*a), 24); (*b) <= 121 - (*a); (*b)++)
{
// iterate over the possible 'c' values.
for ((*c) = 24; (*c) <= 100; (*c)++)
{
// Same as for 'b' here.
for ((*d) = max(90 - (*c), 24); (*d) <= 134 - (*c); (*d)++)
{
// Found an answer. Should add a a treshold for accuracy due to floating points.
if (fabs(n - ((double)((*a) * (*c)) / ((*b) * (*d)))) <= TRESHOLD)
{
// Found the answer.
return true;
}
}
}
}
}
return false;
}
int main()
{
int a, b, c, d;
// Will get rounded.
printf("Trying to solve the equation for n ~= %.15f..\n", N);
if (solve(N, &a, &b, &c, &d))
{
printf("Solution found: (%d / %d) * (%d / %d) ~= %.15f\n", a, b, c, d, N);
}
else
{
printf("No solution found!\n");
}
return 0;
}
```

Doing it efficiently is more interesting though, wouldn't be quite sure how I'd do that. Your ideas so far would probably be good, assuming you spend more time pondering on it than I did.

-2

I thouht about this problem but i couldnt find the answer.It was so hard for me.I need the solution...

-2

Dear Gonbe,thanks alot for answering this question.I have to check it.

2

I'm no math whiz, but don't you have to find the range of possible answers for a,b,c,d, separately, in every equation, and then look at the range where all 4 variable's ranges, overlap?

Emphasis on the highest and lowest values for each variable, of course. Everything in between should be golden. (good)

-1

Dear Adak i did this but in this way there's a lot of answers and it took a long time.

0

Well, it seems that the last equation is useless, because the first one already means that n - (a/b)*(c/d) == 0 .. The other thing is that there is no condition on n, so we just have to find a,b,c and d that match equations 2,3 and 4 - we also now that a,b,c,d are integers - So maybe it's dumb but i'd solve it this way :

```
#include <stdio.h>
int main(void) {
int a, b, c, d;
for(a=24; a<=100; a++) {
for(b=24; b<=100; b++) {
for(c=24; c<=100; c++) {
for(d=24; d<=100; d++) {
if( (a+b) >= 73 ) {
if( (a+b) <= 121 ) {
if( (c+d) >= 90 ) {
if( (c+d) <= 134) {
double n = (a/(double)b) * (c/(double)d);
fprintf("Found answer : a=%d, b=%d, c=%d, d=%d, n=%f\n", a, b, c, d, n);
}
}
}
}
}
}
}
}
}
```

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...