What will be printed?
Given:
for (int i = 2; i < 4; i++) {
for (int j = 0; j < 4; j += 2) {
System.out.println("i = " + i + ", j = " + j);
}
}
This is something you can easily do yourself through a compiler or even by counting on your fingers. Here is your output:
i = 2, j = 0
i = 2, j = 2
i = 3, j = 0
i = 3, j = 2
If you need to compile anything and you dont have a Java IDE to do so, you can do it online with https://www.tutorialspoint.com/compile_java_online.php
Related
Im learning Intrinsics. I dont know how to load a matrix correctly. I want to do matrix multiplication.
This is my code:
int i, j, k;
__m128 mat2values = _mm_setzero_ps();
__m128 mat1values = _mm_setzero_ps();
__m128 r = _mm_setzero_ps();
for (i = 0; i < N; ++i)
{
for (j = 0; j < N - 3; j += 4)
{
for (k = 0; k < N - 3; k += 4)
{
mat1values = _mm_load_ps(&mat1[i][k]);
mat2values = _mm_load_ps(&mat2[k][j]);
r = _mm_add_ps(r, _mm_mul_ps(mat1values, mat2values));
}
result[i][j] = r.m128_f32[0] + r.m128_f32[1] + r.m128_f32[2] + r.m128_f32[3];
for (; k < N; k++)
result[i][j] += mat1[i][j] * mat2[k][j];
}
}
When debugging result will still hold all 0 values after loop.
Are you sure the expression
_mm_load_ps(mat1[i][k])
returns the correct memory address in float*?
I am bubble sorting a 2D array that looks like this. I am confuse on how to make my largest value as 1 and make the 2nd row's value follow to 1st row's counterpart.
Input:
13 9 1 8 5
1 2 3 4 1
Actual output:
1 5 8 9 13
1 2 3 4 1
This is the expected output that i am trying to make.
Output:
5 8 9 13 1
1 4 2 1 1
Here is my code for sorting the cards (col = 5 and row = 2):
void sortedCards(int card[][col])
{
int i, j, k, temp;
printf("\n\nSorted Cards\n");
for (k = 0; k < 10; k++)
{
for (i = 0; i < row - 1; i++)
{
for (j = 0; j < col - 1; j++)
{
if (card[i][j] > card[i][j + 1])
{
temp = card[i][j];
card[i][j] = card[i][j + 1];
card[i][j + 1] = temp;
}
}
}
}
for (i = 0; i < row; i++)
{
if (i == 1)
{
printf("\n");
}
for (j = 0; j < col; j++)
{
printf("%i ", card[i][j]);
}
}
}
If your sorting is only dependent on the first row, there is no need to iterate through the second row. Just set both rows at the same time while checking the first row.
Also, if you want 1 to be treated as larger than all other numbers, you need to add that to your Boolean logic. Adjusting your for loop like below should do it.
int j, k, temp, temp2;
for (k = 0; k < 10; k++)
{
for (j = 0; j < col-1; j++)
{
//here we only test row 0, and we check if the value is 1
if (card[0][j] == 1 || (card[0][j] > card[0][j+1] && card[0][j+1] != 1))
{
//all other reassignment is the same but you do both rows at the same time
temp = card[0][j];
temp2 = card[1][j];
card[0][j] = card[0][j + 1];
card[1][j] = card[1][j + 1];
card[0][j + 1] = temp;
card[1][j + 1] = temp2;
}
}
}
I'm very unexperienced in programming and were trying to create a code for a homework to generate 6 random numbers (like in a dice) in two groups of three, and compare them, lower with lower, higher with higher, etc., and register the results, much like "war" game, but in a great number of plays.
So, i ended up with the following code:
rodadas = 3;
for (i = 0; i < 1000; i++) {
ataque_vitorias=0;
ataque[1] = rand() % 6 + 1;
ataque[2] = rand() % 6 + 1;
ataque[3] = rand() % 6 + 1;
defesa[1] = rand() % 6 + 1;
defesa[2] = rand() % 6 + 1;
defesa[3] = rand() % 6 + 1;
j=0;
k=0;
for (j=0 ; j < 2 ; j++)
{
for (k=0 ; k < 2 ; k++)
{
if (ataque[k+1] < ataque[k])
{
t = ataque[k];
ataque[k] = ataque[k + 1];
ataque[k + 1] = t;
}
}
}
j=0;
k=0;
for (j=0 ; j < 2 ; j++)
{
for (k=0 ; k < 2 ; k++)
{
if (defesa[k+1] < defesa[k])
{
t = defesa[k];
defesa[k] = defesa[k + 1];
defesa[k + 1] = t;
}
}
}
if (ataque[1] > defesa[1])
ataque_vitorias++;
if (ataque[2] > defesa[2])
ataque_vitorias++;
if (ataque[3] > defesa[3])
ataque_vitorias++;
if (ataque_vitorias == 0) total_0++;
if (ataque_vitorias == 1) total_1++;
if (ataque_vitorias == 2) total_2++;
if (ataque_vitorias == 3) total_3++;
}
printf("total_0: %d\n", total_0);
printf("total_1: %d\n", total_1);
printf("total_2: %d\n", total_2);
printf("total_3: %d\n", total_3);
return 0;
And defined all the variables ind the main void. The problem is when i try to run it, i keep getting the error:
Disallowed system call: SYS_socketcall
I don't have any idea of how to fix it so the code can run.
Sorry for the bad english (i'm not native) and any help would be much appreciated!
I'm having a hard time optimizing the following while loop by means of Loop Unswitching. I have tried applying the example from wiki, however I am having a hard time applying it to a while loop. I have the following code:
int n = 5,
m = 5,
i = 0,
val = 0;
while (i < n ) {
j = 0;
while (j < m ) {
if (i < j ) {
val = val + i ;
}
else if ( j == i ) {
val = val - 1;
}
else {
val = val + j ;
}
j = j + 1;
}
i = i + 1;
}
And have tried unswitching it the following way:
while (i < n ) {
j = 0;
if (i < j ) {
while (j < m ) {
val = val + i;
j = j + 1;
}
}
if ( j == i ) {
while (j < m) {
val = val - 1;
j = j + 1;
}
}
if (i > j) {
while (j < m) {
val = val + j;
j = j + 1;
}
}
i = i + 1;
}
What could I be doing wrong.
Such loops are best unrolled with the help of pencil and paper. You want the sum of the following grid:
0 1 2 3 4 | 5 n
0 -1 0 0 0 0 | 0 0
1 0 -1 1 1 1 | 1 1
2 0 1 -1 2 2 | 2 2
3 0 1 2 -1 3 | 3 3
m 0 1 2 3 -1 | 4 4
The grid can be subdivided into three parts: the diagonal, the upper and lower triangles next to the diagonal in the square part and the rectangular block when n and m differ.
Let's represent the dimension of the grid by means of the square part, k² and the rectangular part, k·r:
k = min(n, m)
r = max(m, n) - k
Now you can see which sums the three parts contribute:
val = 2·∑(k - i - 1)·i # two triangles
+ r·∑(i) # rectangle
- k # diagonal
(All sums run from i = 0; i < n; i++.) This sum can be rearranged to:
val = 2·(k - 1)·∑(i) - 2*∑(i²) + r·(i) - k
= (2·k + r - 2)·∑(i) - 2*∑(i²) - k
This reduces your two nested loops two two independent loops to do the sums of the natural numbers and of their squares. Fortunately, these sums can be expressed by simple relations:
∑(i) = (n - 1)·n / 2
∑(i²) = (2·n - 1)·(n - 1)·n / 6
You now have a constant-time formula for your resulting sum:
int val(int n, int m)
{
int k = (n < m) ? n : m;
int r = ((n > m) ? n : m) - k;
return (2*k + r - 2) * (k - 1) * k / 2
- (2*k - 1) * k * (k - 1) / 3 - k;
}
All this doesn't have anything to do with loop unrolling, of course.
Loop unswitching according to wikipedia is a compiler optimization so I'm a little confused about why you'd need to do this yourself, but I think this is as good as it gets in terms of breaking apart the for..ifs
for (i = 0; i < n; ++i) {
// j < i
for (j = 0; j < i; ++j) {
val = val + j;
}
// j == i
val = val - 1;
// j > i
for (j = i + 1; j < m; ++j) {
val = val + i;
}
}
This is not a loop you can traditionally unswitch because the conditional variable here is the loop variable.
Given a piece of code:
void twoDimFunc(int (*p)[HEIGHT])
{
int i = 0, j = 0;
for (;i < WIDTH ; i++)
for (;j < HEIGHT; j++)
{
*((int*)p + i * HEIGHT + j) = -1;
}
}
in main() program I call this function, and then output the matrix, and then I got the following matrix in stdout:
-1 -1 -1
0 0 0
However, for this code
void twoDimFunc(int (*p)[HEIGHT])
{
int i = 0, j = 0;
for (i = 0;i < WIDTH ; i++)
for (j = 0;j < HEIGHT; j++)
{
*((int*)p + i * HEIGHT + j) = -1;
}
}
The output is
-1 -1 -1
-1 -1 -1
Does initialization inside for loop round brackets behave different way than initializing before for loop?
Thanks.
This two codes have a different behavior. In this code :
void twoDimFunc(int (*p)[HEIGHT])
{
int i = 0, j = 0;
for (i = 0;i < WIDTH ; i++) //loop 1
for (j = 0;j < HEIGHT; j++) //j = 0 at each turn of loop 1
{
*((int*)p + i * HEIGHT + j) = -1;
}
}
j is never reinitialized at 0 in the first code you gave:
void twoDimFunc(int (*p)[HEIGHT])
{
int i = 0, j = 0;
for (;i < WIDTH ; i++) //loop 1
for (;j < HEIGHT; j++) //j isn't initialized to 0 for each turn of loop 1
{
*((int*)p + i * HEIGHT + j) = -1;
}
}
Initializing before the loop and inside the parenthesis makes no semantic difference (maybe it makes a difference for the optimizer, who knows...).
But in your first nested loops, you don't reinitialize j every time so that the inner loop is never more executed.