This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
int count(int *a, int *b, int n) {
int i=0,j=0,roz=0;
while(i<n) {
while(j<n) {
if(a[i]==b[j])
roz++;
j++;
}
i++;
}
return roz;
}
n is the size of a, or b array (both are of the same size). The nested while loop seems to work only once, for i = 0. The next thing that seems to be happening is magically changing n into 1 (it is 5 at the beginning), so that the nested while doesn't loop the second time. Why is that so?
The interesting thing is that echoing n right before the return displays the right value, which is 5...
EDIT. For loops work properly here, but the question is still present.
You never reset the value of j to 0.
You should reset j to 0 between two nested loops.
while (i < n)
{
j = 0;
while (j < n)
{
if (a[i] == b[j])
roz++;
}
}
You could compute the intersection of two arrays in a more efficient way (there is a O(N*log N) solution).
The value of n is not changed here. The reason nested loop is not executing for i = 1 or later is that you are not resetting the value of j. Say, n is 5. When i = 0 nested loop executes properly and the value of j is 5 after it finishes. When i = 1 then j is still 5 and so it never enters the nested loop.
while(i<n)
{
j = 0; // reset j here to solve the problem
while(j<n) {}
Well by looking at the logic,it seems that you are conuting the number of common elements in both the arrays.
the mistake you are doing is you are not resetting the value of j to 0 in the first while loop.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am having trouble finding understanding complexity. Could someone help me understand what the complexity of the code below is and why.
for (int i = 1; i < n; i++) { // (n is a number chosen by the user)
for (int j = i - 1; j >= 0; j--) {
printf("i=%d, j=%d", i, j);
}
}
An explanation would be great.
Assuming i starts at 0, the complexity would be constant. The complexity is always expressed relative to a variable defining the number of executions, which is not the case here.
If one term should be used to describe this behavior, it is "constant". There will be a number of executions, but this number will never change
Original Question: Because i's initial value is undefined, the behavior of the code is unpredictable. There is no way to usefully answer the question other than that the complexity is undefined. There is no way to know how many operations the code will perform.
Updated Question: It's O(1). The code will always do precisely the same amount of work.
You can compute the time complexity of this code fragment by evaluating the number of operations, namely the number of calls to printf() which for simplicity's sake we shall assume to be equivalent:
Assuming i starts at 1 (you initially forgot to initialize it), the outer loop runs 99 times, for each iteration, the inner loop runs i times. Gauss was supposedly 9 years old when he computed the resulting number of iterations to be 99 * (99 + 1) / 2.
The complexity of the original piece of code was O(1) since it did not depend on any variable, but since instead you updated the code as:
void fun(int n) {
for (int i = 1; i < n; i++) {
for (int j = i - 1; j >= 0; j--) {
printf("i=%d, j=%d", i, j);
}
}
}
The time complexity would come out as O(n2).
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Can someone please explain why this algorithm
for (i = 0;i<5;i++)
{
for(j = 0;j<5;j++)
{
b[j] = a[i];
break;
}
}
gives strange output while this
for (i = 0;i<5;i++)
{
b[i] = a[i];
}
works perfectly?
The question was Write a program to copy elements of one array into another array.
Your first code is wrong. It assigns b[0] = a[0], then b[0] = a[1] etc. Your break prevents the loop from going to j = 1.
Here:
for (i = 0;i<5;i++)
{
for(j = 0;j<5;j++)
{
b[j] = a[i];
break;
}
}
You are breaking after the setting b[0] each one iteration by i. So, by the end you have b[0] equal a[4] and the rest is a garbage, as you never set it.
Your algorithm is equivalent to:
b[0] = a[4];
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have the below code. But, I think 4 is the answer. Am I right?
int a, i;
if (execute)
{
int count = 0;
for (i=0; i<5; i++)
{
if(pcnt[i]) count ++;
}
a = (count > 0)
}
else
{
a= 0;
}
Will a be a=1 or a=4 for the IF case?
the a will be 1 or 0
1: If execute != 0 and count >0
0: otherwise
Note:
count >0 if and only if pcnt[0]!=0 or pcnt[1]!=0 or pcnt[2]!=0 or pcnt[3]!=0 or pcnt[4]!=0
First, this won't compile because of a missing semi-colon. It also might have difficulty if the variables are not declared properly, but for the sake of answering, I will assume that they have been.
Now, look at what a is assigned to. I see a = (count > 0) and a = 0. Well (count > 0) will only ever be a 0 or a 1 (in C boolean expressions will resolve to 1 for true and 0 for false). So basically, you have a = 0 or 1 and a = 0. This simplifies to a being either 0 or 1.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have an error somewhere in my code, I think I am entering an infinite loop.
Basically I get a matrix and two indexes, i,j, and i need to count how many neighbors around [i][j] have a value of 1 or 2.
this is my code:
int number_of_living_neighbors(matrix mat,int i, int j, int n,int m)
{
int counter=0,row_index=0,column_index=0;
for(row_index=i-1;row_index<=i+1;row_index++)
{
for(column_index=j-1;column_index=j+1;column_index++)
{
if((row_index>=0)&&(row_index<n)&&(column_index>=0)&&(column_index<m))
{
if((mat[row_index][column_index]==1)||(mat[row_index][column_index]==2))
counter++;
if((row_index==i)&&(column_index==j)&&(mat[i][j]==1))
counter--;
}
}
}
printf("The number of living neighbors is %d", counter);
return counter;
}
it doesnt print anything.
mat is the matrix i get, i,j are the pointers, n is number of rows, m is number of columns.
for(column_index = j-1 ; column_index = j+1; column_index++)
Your condition contains an assignment, not a comparison. You probably want to use <= instead of =:
for(column_index = j-1 ; column_index <= j+1; column_index++)
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
int array[2][2] = {0, 1, 2, 3};
int i;
int sum = 0;
for (i =0; i < 4; ++i)
{
int x, y;
x = i % 2;
if (x)
{
y = 0;
}
else
{
y = 1;
}
sum += array[x][y];
}
printf("%d\n", sum);
It's short enough that you could walk through it yourself (since this is homework) and run each line yourself on paper. If there is any line that you can't figure out, ask a more specific question. Just use pencil, make a box to show the values of x, y, i, sum and all 4 elements of the array. Then walk through changing the values in those boxes as you examine lines of code and you will see exactly what's happening. One thing you should know is that "if (x)" will treat x as true when it is 1.