calculate average of squared values of an array [closed] - c

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] ;

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 );

Taking power with loop(with-out pow() function) [closed]

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 4 years ago.
Improve this question
Im trying the powers of an integer. For example if my integers is 2 first 10 power is 2^1,2^2...2^10. Im using
while (expnt < 10)
{
preExpnt = expnt;
while (preExpnt)
{
preExpnt *= num;
printf("%lld\n", preExpnt);
}
expnt++;
}
but it doesn't work.`
Here is a way you could achieve your purpose.
int num = 2; // for example
int out = 1;
for (int exp = 1; exp <= 10; exp++)
{
out *= num;
printf("%d\n", out);
}
Remarks about your code:
Your inner while loop is infinite if num and expnt are both different from 0.
Assigning preExpnt to the value of expnt at each step and multiplying by num would display a something like: 1*n 2*n 3*n 4*n ... if expnt starts at 1.

Summing elements of a 3d array in C [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 7 years ago.
Improve this question
Im trying to sum the elements of a 3d array in C. The code recognizes that position check[1][1][0]=4 and adds 4 to sum when the loop reaches this position. However for the rest of the array it continues to add on this value again, and then it adds the total sum again for the rest of the positions of the array. Can anyone see why?
#include <stdio.h>
main() {
int check[3][3][3]={ 0 };
int size=2;
int i,j,k,sum=0;
check[1][1][0]=12;
for(k=0;k<size;k++) {
for(j=0;j<size;j++) {
for(i=0;i<size;i++) {
printf("i=%d, j=%d,k=%d, checkijk=%d ",i,j,k,check[i][j][k]);
sum+=sum+check[i][j][k];
printf("sum=%d\n", sum);
}
}
}
printf("The sum is %d\n",sum);
}
sum+=sum+check[i][j][k];
should be
sum+=check[k][j][i];
And if you want to sum all the values int size = 2; must be int size = 3;

Function that's calculating a log - Unused variable error [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 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 )

Too many arguments to function 'rand'? [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 8 years ago.
Improve this question
I have only used the function twice and it displays the aforementioned error. Can someone explain as to why the compiler does that?
void printrandom()
{
int x = (rand(5)+1);
int y = (rand(5)+1);
printf("%d and %d - a total of %d", x, y, (x+y));
}
It is actually rand(void), which is why you are getting that error.
Try int x = (rand() % 5) + 1;
EDIT as Daniel points out, using % will actually affect the probability. See his link for how to address this issue.
Declaration for rand() function is
int rand(void);
This means that it takes no arguments. Remove 5 from rand. If you want to generate random numbers from 1 to 5, the you can do this as
int x = rand()%5 + 1;

Resources