#include<stdio.h>
#include<stdbool.h>
#include<malloc.h>
// function called
long long good_triplets (int* arr, int n) {
int count=0;
if(1<=n<=100000)
{
for(int i=0;i<=n;i++)
{
if((((arr[i]+arr[i+1]+arr[i+2])%arr[i ])==0) &&
(((arr[i]+arr[i+1]+arr[i+2])%arr[i+1])!=0) &&
(((arr[i]+arr[i+1]+arr[i+2])%arr[i+2])!=0) )
count=count + 6;
if((((arr[i]+arr[i+1]+arr[i+2])%arr[i+1])==0) &&
(((arr[i]+arr[i+1]+arr[i+2])%arr[i ])!=0) &&
(((arr[i]+arr[i+1]+arr[i+2])%arr[i+2])!=0) )
count=count + 6;
if((((arr[i]+arr[i+1]+arr[i+2])%arr[i+2])==0) &&
(((arr[i]+arr[i+1]+arr[i+2])%arr[i+1])!=0) &&
(((arr[i]+arr[i+1]+arr[i+2])%arr[i])!=0) )
count=count + 6;
}
}
return count;
}
//main funtion
int main() {
int n;
scanf("%d", &n);
int i_arr;
int *arr = (int *)malloc(n * sizeof(int));
for(i_arr=0; i_arr<n; i_arr++)
scanf("%d", &arr[i_arr]);
long long out_ = good_triplets(arr, n);
printf("%lld", out_);
}
'remove floating point exception'
'it is a hacker earth que'
'1.Remove floating point exception
2.help quickly please'
QUESTION: why am I getting the Floating Point Exception and how do I fix it?
if(1<=n<=100000) is wrong. It does not work this way in C. My proposition for that is to use logical and statement (&&) by writing:
if (1 <= n && n <= 100000)
You try to limit the loop to n in line: for(int i=0;i<=n;i++) but you should have < sign instead of <=. Try to use: for (int i=0; i<n; i++) to limit it to n.
Additionally, When you are limitting it to n then you should not use arr[i+1] or arr[i+2], because you are accessing values out of the array bounds (from memory which is just after the array). This is a reason of that exception. If you have some non-zero trash, then most probably your code will not have a floating point exception. Because you are using arr[i+1] and arr[i+2] in modulo operation and in some way they could be zero, then according to standard you can have the undefined behavior.
Try to use (according to your code; I do not know a purpose of it):
long long good_triplets(int *arr, int n) {
int count = 0;
if (3 <= n && n <= 100000) {
for (int i = 0; i < (n - 2); i++) {
if ((((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i]) == 0) &&
(((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i + 1]) != 0) &&
(((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i + 2]) != 0))
count = count + 6;
if ((((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i + 1]) == 0) &&
(((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i]) != 0) &&
(((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i + 2]) != 0))
count = count + 6;
if ((((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i + 2]) == 0) &&
(((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i + 1]) != 0) &&
(((arr[i] + arr[i + 1] + arr[i + 2]) % arr[i]) != 0))
count = count + 6;
}
}
return count;
}
Related
I have this assignment for college where i recieve the matrix A and i need to create the matrix B, where in each cell there whould be the neighbor's average of a specific cell from matrix A.
for example: https://i.stack.imgur.com/1ZuRA.png
i have the real number struct which looks like this:
typedef struct fraction
{
int num, numerator, denominator;
} fraction;
The function is yet to be complete but problem is, its easy to find the num field, but i struggling to find the numerator and denominator fields...(below is my function, im not allowed to change the decleration):
fraction neighborFractionAverage(int A[][COLS], int i, int j, int rows, int cols)
{
// your code:
fraction result;
int counter = 0;
int mone;
result.num = 0, result.numerator = 1, result.denominator = 1;
if ((i - 1 >= 0 && i - 1 < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i - 1][j - 1];
}
if ((i - 1 >= 0 && i - 1 < rows) && (j >= 0 && j < cols))
{
counter++;
result.num += A[i - 1][j];
}
if ((i - 1 >= 0 && i - 1 < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i - 1][j + 1];
}
if ((i >= 0 && i < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i][j - 1];
}
if ((i >= 0 && i < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i][j + 1];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i + 1][j - 1];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j >= 0 && j < cols))
{
counter++;
result.num += A[i + 1][j];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i + 1][j + 1];
}
result.num /= counter;
}
so I saw this code
b[2080];
main(j) {
for (;;) {
printf("\x1b[H");
for (j = 1; j < 2080; j++)
b[j] = j < 2000 ? (b[j + 79] + b[j + 80] + b[j] + b[j - 1] + b[j + 81]) / 5 : rand() % 4 ? 0 : 512, j < 1840 ? putchar((j % 80) == 79 ? '\n' : " .:*#$H#" [b[j] >> 5]) : 0;
usleep(20000);
}
}
so I tried to rewrite it, why even divide 32?
The array got to declare as global else it won't work. any idea?
also why 512?
here is my attempt so far, any problem?
for (int j = 1; j < 2080; j++)
{
if (j < 2000) {
b[j] = (b[j + 79] + b[j + 80] + b[j] + b[j - 1] + b[j + 81]) / 5;
}
else if (rand() % 4 != 0) { // Generate random integers in range 0 to 4
b[j] = 0;
}
else
{
b[j] = 512;
}
}
}
Here's the functionally equivalent code with the ternary operators converted into if/else statements:
if (j < 2000) {
b[j] = (b[j + 79] + b[j + 80] + b[j] + b[j - 1] + b[j + 81]) / 5;
} else if (rand() % 4) {
b[j] = 0;
} else {
b[j] = 512;
}
if (j < 1840) {
if ((j % 80) == 79) {
putchar('\n');
} else {
putchar(" .:*#$H#"[b[j] / 32]);
}
}
As for the question of what does the right shift >> 5 do, it divides by 32 (25), and then indexes the array " .:*#$H#" with that divided value.
edit: As for why the array is global, it's probably just to get it initialised to zero without extra code (the whole thing seems to be written as short as possible, e.g., using implicit int types and j from the argument).
Note that there is an access out of bounds bug in the (original) code: b[j + 81] can be accessed when j is 1999 (since that is < 2000), but 1999 + 81 == 2080, and b[2080] is out of bounds. You can replace the array with a local int b[2081] = { 0 }; but it changes the output slightly while fixing the bug.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
This is a part of my code:
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
for(k = 0; k < rows - i && k < columns - j; k++)
{
if(i + k > rows || j + k > columns) break;
if(b->board[i + k][j + k] == 1) counter++;
if(b->board[i + k][j + k] == 2) counter = 0;
if(counter > max_pd)
{
plh = counter;
jkee = j;
kkee = k;
}
}
counter = 0;
}
if(plh > max_pd)
{
max_pd = plh;
plh = 0;
for(n = 0; n < 1; n++)
{
if(i + kkee > rows || jkee + kkee + 1 > columns) break;
if(b->board[i + kkee + 1][jkee + kkee + 1] == 0 && b->board[i + kkee][jkee + kkee + 1] != 0) play_pd = jkee + kkee + 1;
}
}
counter = 0;
plh = 0;
jkee = 0;
kkee = 0;
}
I get a segmentation fault when I try to run this code at the line :
if(b->board[i + kkee + 1][jkee + kkee + 1] == 0 && b->board[i + kkee][jkee + kkee + 1] != 0) play_pd = jkee + kkee + 1;
However in the exact above line I clearly state that if the numbers put in board are not in there, break.
(the board is defined in a struct like this: b->board[rows][columns])
The problem is here:
if(i + kkee > rows || jkee + kkee + 1 > columns) break;
if(b->board[i + kkee + 1][jkee + kkee + 1] == 0 && b->board[i + kkee][jkee + kkee + 1] != 0) play_pd = jkee + kkee + 1;
The index for the row an column of the bord must be respectively < rows and < columns.
The test to break should be
if(i + kkee + 1 >= rows || jkee + kkee + 1 >= columns) break;
This will ensure that the next instruction will not use indexes out of bounds.
You have the same problem above with the instructions:
if(i + k > rows || j + k > columns) break;
if(b->board[i + k][j + k] == 1) counter++;
if(b->board[i + k][j + k] == 2) counter = 0;
The test for the break should be
if(i + k >= rows || j + k >= columns) break;
I'm trying to check if a 2d array is sorted like this way:
for (i = 0; i <= nl - 2; i++)
{
for (j = 0; j <= nc - 2; j++)
{
if (M[i][j] > M[i + 1][j + 1])
cr++;
else if (M[i][j] < M[i + 1][j + 1])
dc++;
}
}
if (cr == nl - 1)
printf("Sorted\n");
if (dc == nl - 1)
printf("Sorted\n");
But doesn't work well sometimes, i would like to get your help!
I have a typical algorithm for matrix multiplication. I am trying to apply and understand loop unrolling, but I am having a problem implementing the algorithm when I am trying to unroll k times when k isn't a multiple of the matrices size. (I get very large numbers as a result instead). That means I am not getting how to handle the remaining elements after unrolling. Here is what I have:
void Mult_Matx(unsigned long* a, unsigned long* b, unsigned long*c, long n)
{
long i = 0, j = 0, k = 0;
unsigned long sum, sum1, sum2, sum3, sum4, sum5, sum6, sum7;
for (i = 0; i < n; i++)
{
long in = i * n;
for (j = 0; j < n; j++)
{
sum = sum1 = sum2 = sum3 = sum4 = sum5 = sum6 = sum7 = 0;
for (k = 0; k < n; k += 8)
{
sum = sum + a[in + k] * b[k * n + j];
sum1 = sum1 + a[in + (k + 1)] * b[(k + 1) * n + j];
sum2 = sum2 + a[in + (k + 2)] * b[(k + 2) * n + j];
sum3 = sum3 + a[in + (k + 3)] * b[(k + 3) * n + j];
sum4 = sum4 + a[in + (k + 4)] * b[(k + 4) * n + j];
sum5 = sum5 + a[in + (k + 5)] * b[(k + 5) * n + j];
sum6 = sum6 + a[in + (k + 6)] * b[(k + 6) * n + j];
sum7 = sum7 + a[in + (k + 7)] * b[(k + 7) * n + j];
}
if (n % 8 != 0)
{
for (k = 8 * (n / 8); k < n; k++)
{
sum = sum + a[in + k] * b[k * n + j];
}
}
c[in + j] = sum + sum1 + sum2 + sum3 + sum4 + sum5 + sum6 + sum7;
}
}
}
Let's say size aka n is 12. When I unroll it 4 times, this code works, meaning when it never enters the remainder loop. But I am losing track of what's going on when it does! If anyone can direct me where I am going wrong, I'd really appreciate it. I am new to this, and having a hard time figuring out.
A generic way of unrolling a loop on this shape:
for(int i=0; i<N; i++)
...
is
int i;
for(i=0; i<N-L; i+=L)
...
for(; i<N; i++)
...
or if you want to keep the index variable in the scope of the loops:
for(int i=0; i<N-L; i+=L)
...
for(int i=L*(N/L); i<N; i++)
...
Here, I'm using the fact that integer division is rounded down. L is the number of steps you do in the first loop.
Example:
const int N=22;
const int L=6;
int i;
for(i=0; i<N-L; i+=L)
{
printf("%d\n", i);
printf("%d\n", i+1);
printf("%d\n", i+2);
printf("%d\n", i+3);
printf("%d\n", i+4);
printf("%d\n", i+5);
}
for(; i<N; i++)
printf("%d\n", i);
But I recommend taking a look at Duff's device. However, I do suspect that it's not always a good thing to use. The reason is that modulo is a pretty expensive operation.
The condition if (n % 8 != 0) should not be needed. The for header should take care of that if written properly.