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 keep getting the warning that my function has an undefined reference and that doesn't really say much to me or how to fix it. Here are the errors
log_2.c: In function ‘main’: log_2.c:29: warning: implicit
declaration of function ‘logbase2’ /tmp/ccAXAmVb.o: In function
'main': log_2.c:(.text+0x5e): undefined reference to `logbase2'
collect2: ld returned 1 exit status
Heres my code:
int logbasetwo (int number)
{
int test;
for (int i = 0; i< number; i++){
test = 2 ^ i;
int result = i;
}
return result;
}
int main(){
printf("Enter a positive integer: ");
int number = get_int();
int logresult;
if (number > 0){
logresult=logbase2(number);
}
else (number < 0){
printf("Not a positive number. Re-enter: ");
number = get_int();
}
printf("Log base two of number is:%i", logresult);
}
return 0;
}
Well, in your code , both logbase2() and logbasetwo() are used, which are not the same !!!
You have defined a function named logbasetwo(), but you called logbase2().
Change either of them to match other one.
Also, you need to change the logic test = 2 ^ i;. As mentioned in earlier comment by Mr. #Bathsheba, ^ operator is for XOR, not exponentiation.
You need to use pow().
Related
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
Here is my C code. I am not getting any output. Please help. I also tried adding the initialization of inside the main function, then also I am not getting any output.
#include <stdio.h>
int x = 10;
int main()
{
if (x = 20)
{
x = -1;
}
else
{
printf("x not eqaul to 20\n");
}
if (x > 0)
{
printf("x not greather than 0\n");
}
else
{
/* notjing */
}
return 0;
}
So in the first if-statement you wrote if(x=20). This is not a conditunal argument, this is a mathematical operand.
So x will be set to 20; afterworths it will be set to -1. And no printf() will be called.
If think you wanted to use if(x==20).
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
So, I am supposed to print out the following series using a C program:
I decide to use a recursive approach and come up with this code:
#include<stdio.h>
float series(int n, float x)
{
float prod;
if(n==1)
return 1;
else
{
prod = (x*x)/((2*n-2)(2*n-3)); //line 10
return prod*series(n-1,x);
}
}
int main()
{
int n;
float x;
printf("\n Enter the values of n and x : ");
scanf("%d %f",&n,&x);
printf("\n The series is :");
for(int i=1;i<=n;i++)
printf(" %f,",series(i,x));
printf("\n\n");
return 0;
}
But this gives an error on line 10:
error: called object type 'int' is not a function or function pointer
I don't see any syntactical error on the line. It would be great if you could point it out.
Thank You!
prod = (x*x)/((2*n-2)(2*n-3)); //line 10
should be
prod = (x*x)/((2*n-2)*(2*n-3)); //line 10
The compiler sees this as a function call where 2*n-2 is the function pointer and 2*n-3 is the argument.
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 am currently doing CS50 introduction to computer science:
I started this code (written in C) where I have to code a pyramid based on what the user writes.
Here is my code :
#include <stdio.h>
#include <cs50.h>
int main(void);
int n;
do
{
n = get_int("Positive Number: \n");
}
while (n > 0 || n < 9);
Here is the error displayed by my terminal:
mario.c:6:1: error: expected identifier or '('
do
^
mario.c:10:1: error: expected identifier or '('
while (n > 0 || n < 9);
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
Can someone please help?
William
Look at your main(). You're not writing the definition; you're just declaring the main function prototype. Fix it by adding braces:
int main(void) {
.
.
.
return 0;
}
You placed a semicolon after the declaration of the function main
int main(void);
^^^^
Remove it and enclose the body of main in braces
int main(void)
{
//...
}
Also it seems the condition of the do-while statement
do
{
n = get_int("Positive Number: \n");
}
while (n > 0 || n < 9);
is incorrect. I suspect that you want to repeat the loop if the entered value of n is not positive or is greater than or equal to 9. In this case the condition should look like
do
{
n = get_int("Positive Number: \n");
} while ( !( n > 0 && n < 9 ) );
You have to code on function main()
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 5 years ago.
Improve this question
i have a function that counts arithmetic mean of even numbers in an array.
int func(int *x, int length)
{
float even_sum = 0;
int even_num;
int i;
float result;
for (i = 0; i<length; i++)
{
if (x[i] % 2 == 0)
{
even_sum = even_sum + x[i];
even_num++;
}
}
result = even_sum/even_num;
return result;
}
giving an array 1 2 2 1 i expect to receive 2 as mean, but i keep getting 0 as result. Where is a mistake in my code?
int func(int *x, int length)
{
float even_sum = 0;
int even_num; // <-- uninitialized, could be anything!
int i;
float result;
for (i = 0; i<length; i++)
{
if (x[i] % 2 == 0)
{
even_sum = even_sum + x[i];
even_num++; // <-- adding 1 to anything yields undefined behavior
}
}
result = even_sum/even_num; // <-- even more undefined behavior
return result;
}
You initialized some, but not all of your variables. It's best to get in the habit of setting your variables to sensible starting values, just as you do here with float even_sum = 0;
Initialize even_num=0
If you don't initialize variables, they can have garbage values.
In your case, even_num has a huge values, greater that even_sum causing your result to be 0.
#Pbd is correct. If you have the latest version of gcc installed and use the command line flags -Wall -Wextra -Wshadow when compiling, it will give you a warning for uninitialized values. 🤓
I think The only problem in your code is when you are passing int array into this function. Apart from that everything looks fine. I ran the code by initialling the 'x' array and it is working fine.
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 6 years ago.
Improve this question
I am working on CS50 PSET1. I have the following code so far:
#include <stdio.h>
#include <cs50.h>
int main(void) {
float change;
do {
printf("Change: ");
change = get_float();
} while(change < 0);
int coins;
for(int q = change; q < 25; q++) {
q = 25 / q;
coins += 1;
}
printf("%i", coins);
}
I am having an issue. When I try to compile my code with the make command I get an error saying this
greedy.c:17:9: error: variable 'coins' is uninitialized when used here [-> Werror,-Wuninitialized]
coins += 1;
The compiler is correct. You never assign anything to coins in the first place. All you do is increment its (uninitialized) value.
To assign an initial value, write
int coins = 0; /* or whatever the correct initial value is */
As an aside, I'm not quite sure what the intent is, but the following is highly unlikely to be what you want:
for(int q = change; q < 25; q++) {
q = 25 / q;
Note how the assignment modifies the loop variable. While this is permissible, in this context it looks unlikely to be intentional.