Vector problem - square addition

Discussion of chess software programming and technical issues.

Moderator: Ras

jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Vector problem - square addition

Post by jhaglund »

Okay, I've been stuck on this far too long...

What I've come up with is this:

Code: Select all

//addsq.hpp
double a_count, b_count, c_count, d_count, e_count, f_count, g_count, h_count;

int i, ii, iii, iv, v, vi, vii, viii;

int *ti = new int;
int *tii = new int;
int *tiii = new int;
int *tiv = new int;
int *tv = new int;
int *tvi = new int;
int *tvii = new int;
int *tviii = new int;

int a, b, c, d, e, f, g, h, j;
int sq_total = 64;

int sq[64] = {
0, 1, 1, 2, 2, 1, 1, 0, 
1, 1, 2, 2, 2, 2, 1, 1,
1, 2, 3, 3, 3, 3, 2, 1,
2, 2, 3, 4, 4, 3, 2, 2,
2, 2, 3, 4, 4, 3, 2, 2,
1, 2, 3, 3, 3, 3, 2, 1, 
1, 1, 2, 2, 2, 2, 1, 1,
0, 1, 1, 2, 2, 1, 1, 0
}; 

/*
add "vertically" every 8 for each file;
a  b     c    d     e   f    g      h
i  ii   iii   iv    v   vi   vii   viii   
8  12    18   22    22  18   12     8


*/

Code: Select all

// addsq.cpp
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <cstring>   
#include <algorithm>
#include <vector>

#include "addsq.hpp"
using namespace std;


void add_sq(int& i, int& ii, int& iii, int& iv, int& v, int& vi, int &vii, int &viii);

int main()
{
   
        vector<int> myvector (sq, sq+sq_total);
        a_count = (int) count (myvector.begin(), myvector.end(), sq[a]);
        b_count = (int) count (myvector.begin(), myvector.end(), sq[b]);
        c_count = (int) count (myvector.begin(), myvector.end(), sq[c]);
        d_count = (int) count (myvector.begin(), myvector.end(), sq[d]);
        e_count = (int) count (myvector.begin(), myvector.end(), sq[e]);
        f_count = (int) count (myvector.begin(), myvector.end(), sq[f]);
        g_count = (int) count (myvector.begin(), myvector.end(), sq[g]);
        h_count = (int) count (myvector.begin(), myvector.end(), sq[h]);
        
        a_count = (double) count (sq, sq+sq_total, 0);
        b_count = (double) count (sq, sq+sq_total, 0);
        c_count = (double) count (sq, sq+sq_total, 0);
        d_count = (double) count (sq, sq+sq_total, 0);
        e_count = (double) count (sq, sq+sq_total, 0);
        f_count = (double) count (sq, sq+sq_total, 0);
        g_count = (double) count (sq, sq+sq_total, 0);
        h_count = (double) count (sq, sq+sq_total, 0);
     
void add_sq(int&,int&,int&,int&,int&,int&,int&,int&);

a = 0;
b = 1;
c = 2;
d = 3;
e = 4;
f = 5;
g = 6;
h = 7;
  
add_sq(a,b,c,d,e,f,g,h);
system("pause");
}


void add_sq(int& i, int& ii, int& iii, int& iv, int& vi, int& vii, int& viii)
{
 
for(j = 0; j < sq_total;) 
{
         
cout << " a " << sq[i];
cout << " b " << sq[ii];
cout << " c " << sq[iii];
cout << " d " << sq[iv];
cout << " e " << sq[v];
cout << " f " << sq[vi];
cout << " g " << sq[vii];
cout << " h " << sq[viii];

*ti = i; 
*tii = ii; 
*tiii = iii; 
*tiv = iv; 
*tv = v; 
*tvi = vi;
*tvii = vii;
*tviii = viii;

i = i + 1;
ii = ii + 1;
iii = iii + 1;
iv = iv + 1;
v = v + 1;
vi = vi + 1;
vii = vii + 1;
viii = viii + 1;

cout << " a " << sq[*ti]+sq[i];
cout << " b " << sq[*tii]+sq[ii];
cout << " c " << sq[*tiii]+sq[iii];
cout << " d " << sq[*tiv]+sq[iv];
cout << " e " << sq[*tv]+sq[v];
cout << " f " << sq[*tvi]+sq[vi];
cout << " g " << sq[*tvii]+sq[vii];
cout << " h " << sq[*tviii]+sq[viii] << endl;
j = j + 1;

 }

} 
I've looked at this for hours...

What I want to do is what it says in "addsq.hpp", add the file values in an array, but simplify it, using a vector, and not this:

Example:

Code: Select all

a = sq[0]+sq[8]+sq[16]+sq[24]+sq[32]+sq[40]+sq[48]+sq[56];
b...
c...
d = sq[3]+sq[11]+sq[19]+sq[27]+sq[35]+sq[43]+sq[51]+sq[59];
...
h = sq[7]+sq[15]+sq[23]+sq[31]+sq[39]+sq[47]+sq[55]+sq[63];
Any help is much appreciated,

Joshua
smatovic
Posts: 3321
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Vector problem - square addition

Post by smatovic »

not sure if this is what you look for:

Code: Select all

result[8] /* 0-7 => a-h */

for (i =0; i<64; i++)
    result[i-(i&56)]+=sq[i]
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Vector problem - square addition

Post by Edmund »

Sorry I don't know enough about the vector class, but if you don't need the values to be 4 bytes long, I would suggest to map a 64 bit variable over the address space and use this to save a couple of instructions.
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Code: Select all

j = 8;
int n = 0;
int aa = 1;
int result[8]; /* 0-7 => a-h */ 
 
 
for (i =0; i<64; i++) {
    result[i-(i&56)]+=sq[i];
     cout << sq[i];  
     n = n + 1;
            if(n==j*aa){
            aa = aa + 1;
       cout << "" << endl;  
   }      
      
}
Displays the array...

What is result[] doing ?

I want to add the a,b,c,d,e,f,g,h files scores independently and display them...

Thanks though, for your reply though!

Anymore thoughts?

Joshua
smatovic
Posts: 3321
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Vector problem - square addition

Post by smatovic »

What is result[] doing ?
Assumed the score for an position is stored in sq[] then you got the total score of a file in result[]. Where result[0] is file a and result[7] is file h.

Code: Select all

for (i =0; i<64; i++) {
    result[i-(i&56)]+=sq[i];
   }     
for (i =0; i<8; i++) {
     cout << result[i]; 
   }     
     
}

Code: Select all

(i&56)
i&56 gives the rank number of the poistion i.
Edmund
Posts: 670
Joined: Mon Dec 03, 2007 3:01 pm
Location: Barcelona, Spain

Re: Vector problem - square addition

Post by Edmund »

smatovic wrote:not sure if this is what you look for:

Code: Select all

result[8] /* 0-7 => a-h */

for (i =0; i<64; i++)
    result[i-(i&56)]+=sq[i]
i-(i&56)

is the same as

i & 7
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Thanks Srdja,

I had this code tried before your post:

Code: Select all

// addsq.cpp
#include "iostream"

using namespace std;

int main()
{
  int i;
  int result[8];/* 0-7 => a-h */ 
  
  int sq[64] = {
0, 1, 1, 2, 2, 1, 1, 0, 
1, 1, 2, 2, 2, 2, 1, 1,
1, 2, 3, 3, 3, 3, 2, 1,
2, 2, 3, 4, 4, 3, 2, 2,
2, 2, 3, 4, 4, 3, 2, 2,
1, 2, 3, 3, 3, 3, 2, 1, 
1, 1, 2, 2, 2, 2, 1, 1,
0, 1, 1, 2, 2, 1, 1, 0
}; 


for (i =0; i<64; i++) { 
    result[i-(i&56)]+=sq[i]; 
   }      
for (i =0; i<8; i++) { 
     cout << " " << result[i]; 
   }      
      system("pause");
}
It's output:

//incorrect
1972941486 6492708 6492586 2293582 1972941303 18 12 2293624

//correct
8 12 18 22 22 18 12 8

Simplified, but it displays the wrong results. It has to be something simple?! This is a mini example that goes throughout my program. If I can get this, it will reduce the overall code by the thousands of lines. :shock:

Joshua
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Code:

Code: Select all

result[8] /* 0-7 => a-h */ 

for (i =0; i<64; i++) 
    result[i-(i&56)]+=sq[i] 
 
i-(i&56)

is the same as

i & 7

I'm not familar with this technique. What is this technique called?
smatovic
Posts: 3321
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Vector problem - square addition

Post by smatovic »

hmm, maybe you should init the result array

Code: Select all

int result[] = {0,0,0,0,0,0,0,0};
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Vector problem - square addition

Post by jhaglund »

Thanks for the idea