Function that's calculating a log - Unused variable error [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm writing a function that tries to get the square of a root.
I guess it's easier with an example:
I want to give a number to that function, say 1024 and the function should tell me 12. So always looking for the x here: 1024 = 2^x.
If I gave 255 to the function it should return 7.
Now I guess my maths is pretty okay, but I get an Error saying I didn't use a Variable in Line 6. Could you have a look?
int log_base2(int num)
{
int x = 2;
int count = 0;
for(; x <= num; x * 2 )
{
count++;
}
return count;
}
Error is in line 6 ( for(....))

for(; x <= num; x * 2 )
Here x * 2 calculates its value, and then throws the result out. What you want is probably:
for(; x <= num; x *= 2 )
The error message is perhaps because the compiler optimizes the variable x away as it's useless.

You are not modifying x anywhere. If you want x to become 2 * x in the next iteration you have to change this
for(; x <= num; x * 2 )
to
for(; x <= num; x = 2 * x )

Related

why n mod 10 in loop doesn't show output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have solve a basic problem in c that is count the digits in integer and I have written -
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i;
while(n!=0)
{
n %= 10;
++i;
}
printf("%d",i);
}
I already know that above code is wrong, I should write n/=10; instead of n%=10; but I wants to know why it is not printing even value of i i.e 0.
If I have written any wrong so please ignore it ,I am new here..
If the number n is not divisible by 10 then the value of this expression (the remainder of the division)
n %= 10;
will never be equal to 0.
Also the variable i is not initialized.
int i;
You should write
int i = 0;
do
{
++i;
} while ( n /= 10 );
printf( "%d\n", i );

Why different behaviour of same code by different compilers? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is code written by me where I have to print a single integer denoting the minimum possible capacity of a tram (0 is allowed). It's a problem from codeforces. The answer in CodeBlocks is showing 6 (the right answer) but in codeforces compiler I'm getting another output.
Why is this happening?
#include <stdio.h>
int main() {
int n, i, j, max = 0, sum = 0;
int pssnger_left;
scanf("%d", &n);
int a[n][2];
for (i = 0; i < n; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &a[i][j]); // declaring the value of array
}
}
pssnger_left = a[0][0] + a[0][1];
for (i = 1; i < n; i++) {
sum = pssnger_left - a[i][0];
sum = sum + a[i][i];
pssnger_left = sum;
if (max < sum)
max = sum;
}
printf("%d", max);
}
Input:
4
0 3
2 5
4 2
4 0
Output:
4221555
Answer:
6
Checker Log
wrong answer expected 6, found 4221555
Here is the link of the problem: https://codeforces.com/problemset/problem/116/A
Different compiler, different answer, 99% of the cases is explained by Undefined Behaviour, UB.
In the shown code here it is a[i][i];.
For any i > 1 that is not what you want it to be
and for most high i it illegally accesses beyond a.
-> Undefined Behaviour.
A hint on how I spotted this:
Whenever I see [i][i], actually whenever I see [same][same],
I think "diagonal in a square". And in your code I immediatly thought "What square?", because when seeing int a[n][2]; I thought "Long narrow table." and got a conflict of shapes there.
The problem is not basically with the compiler.
You are doing
sum = sum + a[i][i];
but you have declared a 2D array of n x 2 i.e. int a[n][2]
see in some compilers it shows out of bound because you are accessing an element which is not there in the array
and in compilers it just loops in the already existing array for ex if your array has 5 elements and you are trying to access 6th element then it will go back to 1st index,
so this might be whats happening here.

How to fix "1 value required as left operand of assignment"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to build a project, where a counter counts up, every time a measurement is under 800 and resets if a measurement is over 800. I tried lots of things, but many didn't work, and some were way to complicated. Please help me... This down there is my original code, which doesn't work.
const int sensor = A0;
int x;
void setup(){
x = 0;
}
void loop(){
int Val = analogRead(sensor);
if(Val =< 800){
x + 1;
}
else{
x = 0;
}
delay(250);
}
Inside your if condition, you are not setting the value of variable. You are just giving an expression.
if(Val =< 800){
x + 1;
}
The correct code would be assigning the new value to the variable again, and also your relational operator is wrong.. it should be <=
if(Val <= 800){
x = x + 1;
}

Why does my summation program behaves weird? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Yesterday, I had to do a math introduction test, where I had to calculate the cardinality of a summation. I was lazy to write it out and I thought I'd just write a small C program to solve it. I felt confident that'll work, since I did much more complex programs, but I just can't get it to work.
#include <stdio.h>
int main(){
int i = 1;
int n = 2 * i + 1;
while(n <= 36){
printf("%d\n", n);
i++;
}
return 0;
}
In theory, there should have been the sequence "3, 5, 7, 9, ...", but all I get is "3, 3, 3, 3, ...". It's only not working if I'm using the variable n, if I replace it with i within while everything works as I would expect.
What am I missing?
This:
int n = 2 * i + 1;
Is not a formula for n which gets calculated every time n is used. It sets n to the value of 2 * i + 1 == 2 * 1 + 1 == 2 + 1 == 3 at the time the statement is encountered and that's it. So n never changes inside of the loop and you end up with an infinite loop.
Move the assignment to inside the loop:
while(n <= 36){
printf("%d\n", n);
i++;
n = 2 * i + 1;
}
There is no change of value n in the while loop and so it never fail the condition n <= 36. Maybe you want to do this way
while(n <= 36){
printf("%d\n", n);
i++;
n = 2 * i + 1;
}
It always gives 3 as a result because you do not update n at each iteration. n is calculated once at the beginning. You should calculate n again each time i is incremented.

calculate average of squared values of an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have use following code to calculate average of squared values of array. but it returns error "143 expression must have pointer to object type".
Int16 mono_input;
Int16 delayed_input;
double sum_sq;
double power;
int j;
int n;
delayed_input = delay_1(mono_input); //delay_1 returns sample of sound (eg 1024 samples)
for ( n = 0 ; n < 1024 ; n++)
{
sum_sq += delayed_input [n] * delayed_inputA [n] ; // to get the squared values of n th sample and add that to the previous value. but here it returns error 143.
}
power = sum_sq/ 1024; // to get the average of squared values
You have typo'd the second delayed_input here:
sum_sq += delayed_input [n] * delayed_inputA [n] ;
Should be (I assume)
sum_sq += delayed_input [n] * delayed_input [n] ;

Resources