What will be the output for this logic? [closed] - c

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.

Related

C printing float arrays into columns [closed]

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 print out an array of 100 lf values into columns of ten. I was planning on inserting a new line whenever I hit the tenth data value in each line, but I'm having trouble with my if statement. "Left operand must be 1-value" is the error I keep running into.
Here's the code as is:
for (x=0; x<100; x++)
{
if (x % 10 = 0)
{
printf("\n");
}
printf("|%-6.2lf|", i[x]);
}
Is there a cleaner way to do this?
The problem is this line:
if (x % 10 = 0)
That should be the double-equals sign:
if (x % 10 == 0)
A common typo.

Nested whiles in C [closed]

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.

'strcmp' char array in C [closed]

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.

Getting string within a string c [closed]

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 want to get a string contained within a string where every time I see (xxx)
I will get only xxx.
I am trying to do it in a recursive manner with a function int find(char* str) for example.
The function will return 1 if there are no '(' or ')' left (all removed), and 0 otherwise.
the string can remain as is, just need to check the stated conditions.
OK input: (xxx)(zzz(yyy)zzz)
BAD input: xx(x(zzz)(y
How can this be done in C?
A simple stack is a good way to solve this. Every time you see a '(' push onto the stack. Every time you see a ')' pop off the stack. If you ever try to pop off an empty stack or have things left on the stack when you are done, then the input was bad.
Edit: you could also do the same thing with a counter. Increment and decrement respectively. If the counter ever goes negative, return false. Otherwise, when you are done return true if you are at 0, or false otherwise. So the counter just represents the size of the 'stack'.
I think this could be the answer, continuing the previous from abelenky:
int find(char *str)
{
int pars = 0, pos = 0;
while(str[pos] != 0) {
if (str[pos] == '(') pars++;
if (str[pos] == ')') pars--;
if (pars < 0) return 0; // closes before opens -> end here
pos++;
}
if (pars != 0) return 0; // not matched result
else return 1; // matched result
}

Can anyone explain this program? [closed]

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.

Resources