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.
This question is being attempted from here
How to find out modified element in an array?
Eg: Array A= {1,2,3,4,5,6} before modifiction After modification
A={1,2,3,7,5,6}. Here the element 4 is replaced with element 7.
Some one used a XOR Property to solve this problem
int getModifiedElement(int arr1[ ],int arr2[ ])
{
int xor1 = arr1[0];
int xor2 = arr2[0];
for(int i=1;<i<arr1.length;i++)
{
xor1 = xor1 ^ arr1[i];
xor2 = xor2 ^ arr2[i];
}
return xor1^xor2;
}
I am not clear with what XOR property has been used to solve the problem?
xor is associative and commutative, so when xor1 xors together all elements from array 1 and xor2 those from array 2, the unmodified elements are present in both, so with xor1 ^ xor2, they're eliminated because x ^ x == 0, so what remains is old ^ new in xor1 ^ xor2, that is the modification leading from the old to the changed element of the array.
It does, however, neither say what the old element was, nor what the new is, so it does not solve the problem as stated.
Related
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 10 years ago.
I'm trying to initialise all elements of a 3D array "A". The array consists of 2000x100x4 integer elements of the 3D array and is stored in row-major order. Each index at position [i,j,k] in "A" must be initialised with the value i*i*i + j*j*j.
How can I do this using for loops? Any suggestions? Thanks.
for(i=0;i<2000;i++)
for(j=0;j<100;j++)
for(k=0;k<4;k++)
A[i][j][k]= (i*i*i) + (j*j*j);
I hope I understood your question correctly. Or were you looking for something else?
It's not something hard to do:
int A[2000][100][4];
int i,j,k;
for (i=0;<2000;i++)
{
for (j=0;j<100;j++)
{
for (k=0;k<4;k++)
{
A[i][j][k] = i*i*i + j*j*j;
}
}
}
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.
...
main() {
char **src_ip[10];
char **dest_ip[10];
char **lat[10];
char *ip[20];
while{
//Read file
//fgets();
src_ip[j] = &data[0];
dest_ip[j] = &data[1];
lat[j] = &data[2];
int idx;
int addip;
for(idx=0; idx<20; idx++)
{
addip = 0;
//Check to see if the IP address is already in the array.
if ((strcmp(*(src_ip[j]), ip[idx]) == 0) ||
(strcmp(*(dest_ip[j]), ip[idx]) == 0))
{
addip=1;
break;
}
//If the IP address was already found then addip would equal 1.
if (!addip){
printf("new node: %s",*(ip[idx]));
}
else
printf("Exist");
}
}
j++;
}
I want to compare char string in two 1d array - array src_ip[j] and dest_ip[j] - and insert into one array ip[idx]. For example, when I look for ip[1], it will go to src_ip[1], meaning that it's only referring to one array - ip[idx] - instead of looking separately into src_ip and dest_ip.
There must be a problem in the code - maybe I left something out?
if((strcmp(*(src_ip[j],ip[idx]) == 0) && (strcmp(*(dest_ip[j]),ip[idx]) == 0))
^^
You can't have both *(src_ip[j]) and *(dest_ip[j]) equal to ip[idx]. What you meant to use was the || operator instead of &&. (By the way, there's a missing ) where I marked above)
Also your while(idx) doesn't make sense. Are you controlling the loop with the for above, or the while below? If with the while, then you'll get an infinite loop if the condition is true since you are never changing it inside the loop.
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 was asked this on a practice test, that has no answers posted. I have no way to test the code, but it has confused me. Can you please help me out with not only understanding the answer, but why.
int foo() {
int a = 1;
char b[] = "zapples";
a = *(b + 1);
if (a == 'a') return 1;
else return 0;
}
What does foo return? ____
Have they taught you how C pointers work?
I'm not going to give you a straight answer, but think about this:
b has the starting address of string "zapples". This means that b[0] points to "z". Another notation for this is *(b + 0), that is, "the value contained at address b, with an offset of 0). With this information, what is the value of *(b + 1)?
This should be enough to solve the exercise.
It will return 1.
The line a = *(b + 1) is the important one. It takes b as a pointer to the first element in the array and adds one so it points to the second. It is then dereferenced so that the value at that address 'a' is assigned to the variable a.
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.