Jump to content

Conditional sum of pruduct

- - - - -

  • Please log in to reply
8 replies to this topic

#1
VITUSH

VITUSH

    Newbie

  • Members
  • PipPip
  • 11 posts
Hi Iam trying to write this in c++:
http://latex.codecog...e j}^{n}x_i-x_j
So far Ive got this:
double ljmi[max]; 

	int i=0;

	int j;

	int p = 0;

	for(j=0;j<k, i<k;j++)       //k <=> n in this case

	{

		if(j=i && j-1<0)

		{

			j = j+1;

			do

			{

			ljmi[p] *= (x[i]-x[j]);

			}

			while(j=k);

			{

			p+1;

			i+1;

			}

		}

		else if(j=i && j-1>0)

		{

			j = j-1;

			do

			{

			ljmi[p] *= (x[i]-x[j]);

			}

			while(j=k);

			p+1;

			i+1;

		}

		else

		{

			do

			{

			ljmi[p] *= (x[i]-x[j]);

			}

			while(j=k);

			p+1;

			i+1;

		}

	}
(I used conditions instead i!=j because it gave me bad result)
This should fill me an array ljmi[max].
Now when I do:
cout << arraysum(ljmi, k);
or
cout << ljmi[a]; //"a" is any number between 0 and k-1
Nothing happens, there is no value printed and I cant exit window with ENTER.
Yeah its a mess but I dont have much experience with cycles - where is mistake and is there a way to make it Small and Stupid?
thx4re

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You need an inner and outer loop. The outer loop contains the inner loop and an increment of sum. The inner loop accumulates a product as long as its variable isn't the same value as the outer variable.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
VITUSH

VITUSH

    Newbie

  • Members
  • PipPip
  • 11 posts
thanks for your reply. I have this:
for(int i=0;i<k;i++)

	{

		for(int j=0;j<k;j++)

		{

			product[i] *= x[i] - x[j];

		}

	}
but i dont know how to make a condition that i != j. If I put it into inner loop:
for(int j=0;i!=j,j<k;j++)
Iam getting exactly the same result for:
for(int i = 0;i<k;i++)

      cout << product[i] << endl;
Which is this:
0

-0

0

-0

.

.

.
Could you please be more specific about that condition? Thanks

#4
rocketboy9000

rocketboy9000

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
The comma operator in C simply discards its first argument.
You want to put a if statement inside the for loops.

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
By if you probably mean logical expression, such as and &&, or ||, not !, complement ~, xor ^.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
for(int i=0;i<k;i++)
{
  product = 1; 
  for(int j=0;j<k;j++)
  {
    if (i!=j) product *= x[i] - x[j];
  }
  sum += product;
}



Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
VITUSH

VITUSH

    Newbie

  • Members
  • PipPip
  • 11 posts

WingedPanther said:


for(int i=0;i<k;i++)

{

  product = 1; 

  for(int j=0;j<k;j++)

  {

    if (i!=j) product *= x[i] - x[j];

  }

  sum += product;

}



Thanks for the code, but I had something similar to this and its giving me bad result (same behavior as my code). For example:

for input k=3; x[3-1] = {1, 2, 3}; it gives me: 6 thats not correct:
(1-2)*(1-3)+(2-1)*(2-3)+(3-1)*(3-1) != 6 but 3
and for input k=3; x[3-1] = {4, 5, 6}; it gives me 9 also not correct:
(4-5)*(4-6)+(5-4)*(5-6)+(6-4)*(6-5) != 9 but also 3

#8
rocketboy9000

rocketboy9000

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
k=3; x[3-1] = {1, 2, 3};
That code above is wrong. You declare x to be 3-1 = 2 numbers, and then give it 3 numbers.

#9
VITUSH

VITUSH

    Newbie

  • Members
  • PipPip
  • 11 posts

rocketboy9000 said:

k=3; x[3-1] = {1, 2, 3};
That code above is wrong. You declare x to be 3-1 = 2 numbers, and then give it 3 numbers.

that's not code man. That's input...
=> for given x[0], x[1], x[2]




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users