Click to See Complete Forum and Search --> : can somebody help me?


lfj
September 23rd, 2002, 12:59 AM
question is: i have 10 arrays,each array have 3 numbers.they are all double.
i want to get a number from each array and let their mul is between a range.
for example:a[i]={0.3,0.4,0,0.3}
b[i]={0.2,0.4,0,0.4}
c[i]={0.1,0.4,0,0.5}
d[i]={0.2,0.3,0,0.5}
e[i]={0.7,0.2,0,0.1}
f[i]={0.6,0.2,0,0.2}
g[i]={0.9,0.06,0,0.04}
h[i]={0.55,0.2,0,0.25}
j[i]={0.63,0.17,0,0.2}
k[i]={0.18,0.43,0,0.39}
l[i]={0.23,0.52,0,0.25}
m[i]={0.15,0.4,0,0.45}
n[i]={0.12,0.36,0,0.52}
double t=a[i]*b[i]*c[i]........n[i];
and (t>0.00000001 && t<0.0000001)
print out a[i] to n[i].
find out all the number.
so can u code it in c/c++.
i will online wait for your answer.

and my email address is leo_vs_yy@hotmail.com

jfaust
September 23rd, 2002, 10:06 AM
If you have a specific question, ask it. We will not do your homework for you.

Jeff

lfj
September 23rd, 2002, 07:39 PM
this is not a homework.

jfaust
September 24th, 2002, 12:44 AM
Well, this is very confusing. You say you have 10 arrays, and then you list 13 arrays. You say each array has 3 numbers, but in your example there are 4.

You want to multiply the nth element in each array? Well, it looks like you are doing that. I don't know what you are trying to say with "(t>0.00000001 && t<0.0000001)".

If you can provide a code sample that compiles and/or demonstrates the problem, maybe we can help.

Jeff

lfj
September 24th, 2002, 12:49 AM
int i;
double t;

a[i]={0.3,0.4,0,0.3}
b[i]={0.2,0.4,0,0.4}
c[i]={0.1,0.4,0,0.5}
d[i]={0.2,0.3,0,0.5}
e[i]={0.7,0.2,0,0.1}
f[i]={0.6,0.2,0,0.2}
g[i]={0.9,0.06,0,0.04}
h[i]={0.55,0.2,0,0.25}
j[i]={0.63,0.17,0,0.2}
k[i]={0.18,0.43,0,0.39}
l[i]={0.23,0.52,0,0.25}
m[i]={0.15,0.4,0,0.45}
n[i]={0.12,0.36,0,0.52}

for(int i=0;i<4;i++)
{
if(a[i]==0)//double型判断是不是为零,不能跟0比较的。
return 0;
else
t=a[i];
for(int i=0;i<4;i++)
{
if(b[i]==0)
return 0;
else
t* = b[i];
for(int i=0;i<4;i++)
{
if(c[i]==0)
return 0;
else
t* = c[i];
for(int i=0;i<4;i++)
{
if(d[i]==0)
return 0;
else
t* = d[i];
for(int i=0;i<4;i++)
{
if(e[i]==0)
return 0;
else
t* = e[i];
for(int i=0;i<4;i++)
{
if(f[i]==0)
return 0;
else
t* = f[i];
for(int i=0;i<4;i++)
{
if(g[i]==0)
return 0;
else
t* = g[i];
for(int i=0;i<4;i++)
{
if(h[i]==0)
return 0;
else
t* = h[i];
for(int i=0;i<4;i++)
{
if(j[i]==0)
return 0;
else
t* = j[i];
for(int i=0;i<4;i++)
{
if(k[i]==0)
return 0;
else
t* = k[i];
for(int i=0;i<4;i++)
{
if(l[i]==0)
return 0;
else
t* = l[i];
for(int i=0;i<4;i++)
{
if(m[i]==0)
return 0;
else
t* = m[i];
for(int i=0;i<4;i++)
{
if(n[i]==0)
return 0;
else
t* =n[i]; if(t>0.00000001&&t<0.0000001)
printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",a[i],b[i],c[i],d[i],e[i],f[i],g[i],h[i],j[i],
k[i],l[i],m[i],n[i]);
else if(t>=0.0000001 && t<0.000001)
printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",a[i],b[i],c[i],d[i],e[i],f[i],g[i],h[i],j[i],
k[i],l[i],m[i],n[i]);
else if(t>=0.000001 && t<0.00001)
printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",a[i],b[i],c[i],d[i],e[i],f[i],g[i],h[i],j[i],
k[i],l[i],m[i],n[i]);
else
return 1;

}
}
}
}
}
}
}
}
}
}
}
}
}

jfaust
September 24th, 2002, 09:53 AM
What is your question?

Jeff

jfaust
September 24th, 2002, 10:17 AM
This cleans up your code a little, and it now compiles. I didn't try to fix any of the logic in it since I don't understand what it's trying to do. At least it should be a better starting point.


#include <iostream>

double go()
{
const int ROWS = 13;
const int COLS = 4;
double ary[ROWS][COLS]= {
{0.3,0.4,0,0.3},
{0.2,0.4,0,0.4},
{0.1,0.4,0,0.5},
{0.2,0.3,0,0.5},
{0.7,0.2,0,0.1},
{0.6,0.2,0,0.2},
{0.9,0.06,0,0.04},
{0.55,0.2,0,0.25},
{0.63,0.17,0,0.2},
{0.18,0.43,0,0.39},
{0.23,0.52,0,0.25},
{0.15,0.4,0,0.45},
{0.12,0.36,0,0.52}
};


for(int c = 0; c < COLS; ++c)
{
double t = 0;
int r;
for(r = 0; r < ROWS; ++r )
{
if( 0 == ary[r][c] )
{
return 0;
}

t *= ary[r][c];
}

for(r = 0; r < ROWS; ++r )
{
std::cout << ary[r][c] << ', ';
}

std::cout << std::endl;
}

return 1;
}

int main()
{
go();
return 0;
}

lfj
September 24th, 2002, 08:58 PM
// zucai.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"

int main(int argc, char* argv[])
{
const int n=3;
int i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12;
double sum;
double a[n]={0.39,0.32,0.29};
double b[n]={0.46,0.29,0.25};
double c[n]={0.62,0.38,0.01};
double d[n]={0.03,0.45,0.53};
double e[n]={0.10,0.46,0.44};
double f[n]={0.59,0.39,0.02};
double g[n]={0.10,0.46,0.44};
double h[n]={0.43,0.46,0.11};
double j[n]={0.50,0.07,0.43};
double k[n]={0.22,0.38,0.40};
double l[n]={0.04,0.43,0.52};
double m[n]={0.93,0.05,0.02};
double o[n]={0.73,0.05,0.21};
for(i0=0;i0<n;i0++)
{
//sum =a[i];
for(i1=0;i1<n;i1++)
{
//sum *=b[j];
for(i2=0;i2<n;i2++)
{
for(i3=0;i3<n;i3++)
{
for(i4=0;i4<n;i4++)
{
for(i5=0;i5<n;i5++)
{
for(i6=0;i6<n;i6++)
{
for(i7=0;i7<n;i7++)
{
for(i8=0;i8<n;i8++)
{
for(i9=0;i9<n;i9++)
{
for(i10=0;i10<n;i10++)
{
for(i11=0;i11<n;i11++)
{
for(i12=0;i12<n;i12++)
sum =a[i0]*b[i1]*c[i2]*d[i3]*e[i4]*f[i5]*g[i6]*h[i7]*j[i8]*k[i9]*l[i10]*m[i11]*o[i12];
if(sum>0.00000001 && sum<0.0000001)
printf("first:" "%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f\n",a[i0],b[i1],c[i2],d[i3],e[i4],f[i5],g[i6],h[i7],j[i8],k[i9],l[i10],m[i11],o[i12]);

else{
if (sum>=0.0000001 && sum<0.000001)
printf("second:" "%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f\n",a[i0],b[i1],c[i2],d[i3],e[i4],f[i5],g[i6],h[i7],j[i8],k[i9],l[i10],m[i11],o[i12]);

else {
if (sum>=0.000001 && sum<0.00001)
printf("third:" "%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f,%4.2f\n",a[i0],b[i1],c[i2],d[i3],e[i4],f[i5],g[i6],h[i7],j[i8],k[i9],l[i10],m[i11],o[i12]);

else
continue;
}
}
}
}
}
}
}
}

}
}
}
}
}
}
system("pause");
return 0;
}

jfaust
September 24th, 2002, 10:39 PM
I still don't see a question here.

Jeff

tiesun
September 24th, 2002, 10:57 PM
I understand your meaning . but I dont know what are you question ? you already code it out.

proxima centaur
September 24th, 2002, 11:00 PM
Maybe it's because there isn't any, jfaust
:D

lfj, do yourself a favour, formulate a clear and elaborate question. Otherwise, we can only be mystified by the lack of information in your post.

lfj
September 25th, 2002, 01:14 AM
Do u know chinese?My English is so poor,so i am hard to explain
my meaning.So here,i try to do my best .
I want to input 13 arrays,and each array have 3 items.They r all input by keyboard.The programm is trying to find out all the items
in each array can make their mul in the range ,and then try to printing out them.Just like the coder i were wrote.But i found it's very slow to show all .How can it be faster?And make the result printed like that show all the first,and then show all the second,third,ect.

Paul McKenzie
September 25th, 2002, 01:53 AM
Originally posted by lfj
Do u know chinese?My English is so poor,so i am hard to explain
my meaning.So here,i try to do my best .
I want to input 13 arrays,and each array have 3 items.They r all input by keyboard.The programm is trying to find out all the items
in each array can make their mul in the range ,and then try to printing out them.Just like the coder i were wrote.But i found it's very slow to show all .How can it be faster?And make the result printed like that show all the first,and then show all the second,third,ect. Instead of trying to explain, how about giving us a sample of what is input, and what is output? Not in code, just numbers and a visual example. For example here is a question that is formulated correctly:

I would like to write a program that eliminates duplicates. For example:

{1,2,3,4,4,5,2} --> {1,2,3,4,5}

{0,0,0,1} --> {0,1}

(Then question is followed by some code)

Also, is there really a need for 13 to explain what the problem is? Why not first try 2 or 3? The code already is confusing, so cut it down to just 2 or 3 of these arrays. Trying to explain to us in C++ isn't going to be much help.

Regards,

Paul McKenzie

lfj
September 25th, 2002, 02:32 AM
input:
a[n]={3,5,6}
b[n]={4,6,9}
c[n]={7,9,4}
****************
sum=a[i]*b[j]*c[k]
for example:
(sum>50 && sum<100)
printf("%d,%d,%d\n",a[i],b[j],c[k])
(sum>100 && sum <200)
printf("%d,%d,%d\n",a[i],b[j],c[k])

just likt that.

TruBic
September 25th, 2002, 04:35 AM
Hi lfj

Your problem is a combination problem. I re-state the problem as follow: find all the combinations of 10 out of 3 (10 positions <=> your 10 array, 3 state <=> 0, 1, 2 = the indices to the double in each array). For these sort of combination it is best to solve by recursive.

The idea is we change the state of each pos one at a time, until all states are covered. Let take an example of 2 states and 3 pos (for short)

Pos_: 0 1 2 -> 0 1 2 -> 0 1 2 -> 0 1 2 -> 0 1 2
State: 0 0 0 -> 1 0 0 -> 1 1 0 -> 1 1 1 -> 0 1 1

Notice that for each step the states of the current step differs the states of the previous step in AT MOST one position. That's the rule.

Here is the code: (I did not check the syntax correctness of it. You got the idea and please help me to do it)


#define MAX_STATE 2 // 0 to 2
#define MAX_POS = 10

// your array
double
n0[3] = {...},
n1[3] = {...},
...
n9[3] = {...};

int States[MAX_POS-1] = {0, 0, ...0}; // initial state
double* A[MAX_POS-1] = {n0, n1, ... n9};

bool CheckResult(doulbe result, double minVal, double maxVal)
{
// do what ever kind of check you want here
// also print out whatever you want to print here
// example
// if (minVal <= result && result <= maxVal)
// print whatever you interested in here
}

void DoMul(int curPos, double minVal, double maxVal)
{
int pos;
// flag for us to stop the recursive
// we stop when the states at every pos is zero
bool stop = true;
double result = 1;
for (pos = 0 ; pos < MAX_POS ; ++pos)
{
result *= A[pos][States[pos]];
if (States[pos] != 0)
stop = false;
}
// CheckResult will do the comparision and output
CheckResult(result, minVal, maxVal);
if (stop)
return;

// try to increase the state of the current pos
if (State[curPos] < MAX_STATE)
++States[curPos]
else // we need to find out the next pos to increase
for (pos = curPos ; pos < MAX_POS ; ++pos)
if (States[pos] < MAX_STATE) // found the right pos now
{
curPos = pos;
++States[curPos];
DoMul(curPos, minVal, maxVal);
}
// if we got here it means all pos reach the MAX_STATE
// then we set the State of the current pos back to 0
States[curPos] = 0;
DoMul(curPos, minVal, maxVal);
}


Even better if you have access to STL, use the next_permutation to calculate the states, the code will be much shorter.

TruBic
September 25th, 2002, 02:51 PM
Hi jlf

My previous code was wrong! It loop forever :). I didn't check it at all. Here is the tested code with optimization as well (the idea is the same).

The optimization is based on the fact that every time we change the state of the current position we dont have to do the multiplication again we can calculate the new result from the previous result as follow:

newResult = oldResult * valueOfNewStateAtCurentPos / valueOfOldStateAtCurentPos. The only time we have to do the multiplication is when valueOfOldStateAtCurentPos == 0 which makes oldResult == 0, and 0 multiply by whatever is always 0 that's why we have to re-calculate it.

Here is the new code

// Combi.cpp : Defines the entry point for the console application.
//


#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#define MAX_STATE 1 // 0 to 2
#define MAX_POS 3

// Make all these variable global to save the stack space, since we will do recursive
enum NLoopDirection {nldForward, nldBackward};
NLoopDirection LoopDirection;
int CurPos, States[MAX_POS];
double A[MAX_POS][MAX_STATE+1];
double MinValue, MaxValue, Result;

// helper to check the result and output interested value
void CheckResult()
{
int pos;
if (MinValue <= Result && Result <= MaxValue)
{
for (pos = 0 ; pos < MAX_POS ; ++pos)
printf("A%d[%d] = %f, ", pos, States[pos], A[pos][States[pos]]);
printf("Result = %f\n", Result);
}
}

// helper for calculating the saving result
void CalculateResult(int oldState)
{
// the calculation is as follows: Result = Result * newValueAtCurPos / oldValueAtCurPos
if (Result)
Result *= A[CurPos][States[CurPos]] / A[CurPos][oldState];
else if (0 == A[CurPos][oldState])
{
// result == 0 because the oldValueAtCurPos == 0, ===>>> we have to re-calculate the result
int i;
Result = 1;
for (i = 0 ; i < MAX_POS ; ++i)
if (0 == (Result *= A[i][States[i]]))
break;
}
}

// real part here
void DoMul()
{
bool stop = false;
int pos, oldState;

// check to increase the state of the current pos
oldState = States[CurPos];
if (LoopDirection == nldForward)
{
if (States[CurPos] < MAX_STATE)
++States[CurPos];
else
{
// need to find out the next position to increase
for (pos = CurPos ; pos < MAX_POS ; ++pos)
{
if (States[pos] < MAX_STATE)
{
// it's ok to increase the state of this pos and update the value of result
CurPos = pos;
oldState = States[CurPos];
++States[CurPos];
break;
}
}
if (MAX_POS == pos)
{
LoopDirection = nldBackward;
CurPos = MAX_POS-2;
oldState = States[CurPos];
++States[CurPos];
}
}
}
else
{
if (States[CurPos] > 0)
--States[CurPos];
else
{
// need to find out the next position to decrease
for (pos = CurPos ; pos >= 0 ; --pos)
{
if (States[pos] > 0)
{
// it's ok to decrease the state of this pos and update the value of result
CurPos = pos;
oldState = States[CurPos];
--States[CurPos];
break;
}
}
if (pos < 0)
return; // done
}
}

CalculateResult(oldState);
CheckResult();
DoMul();
}

void DoTest()
{
int pos, state;

// init MinValue, MaxValue
MinValue = 0;
MaxValue = 1.0;

// Init the states array
memset(States, 0, MAX_POS*sizeof(int));

// Init the double arrays
for (pos = 0 ; pos < MAX_POS ; ++pos)
for (state = 0 ; state <= MAX_STATE ; ++state)
A[pos][state] = (double)rand()/RAND_MAX;

// Calculate the Result for the first permutation.
// result is used as the saving value in each permutation to speed up the performance.
Result = 1;
for (pos = 0 ; pos < MAX_POS ; ++pos)
if (0 == (Result *= A[pos][0]))
break;

// check the first result
CheckResult();

// and call DoMul
CurPos = 0;
LoopDirection = nldForward;
DoMul();
};



int main(int argc, char* argv[])
{
DoTest();
return 0;
}