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 6 years ago.
Improve this question
#include <stdio.h>
#include <math.h>
main()
{
float i;
float x,N,sum;
printf("enter x and N respectively");
scanf ("%f %f", &x, &N);
sum = 0;
for (i=1;i<=N;i++){
sum = sum + ((pow(x,i))/(fact(i)));
}
printf ("%f", sum);
}
int fact(int n){
int i,temp;
temp = 1;
for (i=1;i<=n;i++){
temp = temp*i;
return temp;
}
}
this is to print the summation of the terms accordingly. I tried defining fact inside main but there was some control flow warning and I tried the same outside this time, yet wrong answer. Any help?
You cannot (you are forbidden by the C99 or C11 standard) define a function (like fact) inside another one (like main).
However, some C compilers, in particular GCC accept as an extension to have nested functions.
(I don't recommend using that extension, in particular if you are newbie in C)
Of course you'll better declare
int fact(int n);
before your main and leave its definition after.
Your code is wrong (in particular, better define main as int main(int argc, char**argv) then learn perhaps about getopt and use it). Compile it with all warnings & debug info (e.g. gcc -Wall -Wextra -g homework.c -o binaryprog...) then use a debugger (e.g. gdb ./binaryprog)
You need to add int fact(int n); before main() function to tell the compiler that a function called fact exists or you can add the whole function definition before.
You have to declare fact() above of main. There are 2 ways to do it.
First way: Add int fact(int n); above of main()
Second way: Copy the whole function above main(). So it looks like:
#include <stdio.h>
#include <math.h>
int fact(int n)
{
int i,temp;
temp = 1;
for (i=1;i<=n;i++){
temp = temp*i;
return temp;
}
}
main()
{
float i;
float x,N,sum;
printf("enter x and N respectively");
scanf ("%f %f", &x, &N);
sum = 0;
for (i=1;i<=N;i++){
sum = sum + ((pow(x,i))/(fact(i)));
}
printf ("%f", sum);
}
Related
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 3 years ago.
Improve this question
I just have studied programming and english. I'm a newbie.
My code is supposed to show a sum of 1 to entered number.
(ex : if you enter 5, answer is 15 and if you enter 10, answer is 55)
Bu this code is not working. I have tried many times to fix it, but i don't know why this code is not working.
#include <stdio.h>
int main(void){
int i = 0;
int sum;
int j;
scanf("%d\n", sum);
for(j = 1; j <= sum; j++){
i = i + j;}
printf("%d\n", i);
return 0;
}
The problem with your code is a simple one, and a decent compiler will warn you about it:
testprog.c: In function ‘main’:
testprog.c:9:11: warning: format ‘%d’ expects argument of type ‘int *’,
but argument 2 has type ‘int’ [-Wformat=]
scanf("%d\n", sum);
^
The scanf function requires the address of items you want populated since it needs to change them. Passing the actual item (since C is pass-by-value) to a function could only ever change the copy rather than the original.
You can see the correct way to do it in the standard:
d: Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the strtol function with the value 10 for the base argument. The corresponding argument shall be a pointer to signed integer.
As an aside, I've fixed that issue and added some more improvements, such as:
less confusing variable names (at no stage is your sum variable a sum of anything);
better (more logical) layout of code;
better scoping of temporary variables like i.
prompting the user for input so that they know what they're supposed to enter; and
checking for valid input by checking return value of scanf.
The code is:
#include <stdio.h>
int main(void){
int maxNum;
int sum = 0;
printf("Enter number to sum up to: ");
if (scanf("%d", &maxNum) != 1) {
fprintf(stderr, "Problem getting input\n");
return 1;
}
for (int i = 1; i <= maxNum; ++i) {
sum += i;
}
printf("Sum is %d\n", sum);
return 0;
}
Try this :
#include <stdio.h>
int main(void){
int i = 0;
int sum;
int j;
scanf("%d", &sum);
for(j = 1; j <= sum; j++){
i = i + j;}
printf("%d\n", i);
return 0;
}
Your mistake is just wrote scanf("%d\n", sum) instead of scanf("%d", &sum);
c language supports a type of call by value what transfers data copied.
So, you need to give the memory address that scanf function can write user's input to the sum variable memory.
scanf("%d", &sum)
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
#include <stdio.h>
int isPrime(int n){
int ndiv = 0;
int i;
for(i=1;i<=n;i=i+1){
if(n%i == 0){
ndiv = ndiv+1;
}
}
if(ndiv == 2){
return 1;
}
else{
return 0;
}
}
int nextPrime(int n){}
int main(){
int a = isPrime(7);
printf(a);
//printf(isPrime(4));
}
This code gives me a run time error, I think there's a problem here with the way I deal with data types while using a functions and the printf command, but I can't really figure it out. Help!
f in printf stands for "format". You need to supply a format string for printing: printf("%d\n", a)
Your isPrime is inefficient: you do not need to attempt dividing all the way up to the number itself. You could stop once you reach the square root of the number
Moreover, you could exit the loop early once you see that the number is not prime.
Once you fix these errors, your program would start running and producing the output that you expect.
Here is a small example of how to use printf. You can find more format specifiers here.
#include <stdio.h>
int main()
{
int a = 97;
int b = 98;
char hello[6] = "world";
printf("%d\n", a);
printf("%d\n", b);
printf("%s\n", hello);
return 0;
}
It is because your method of printing a variable is wrong. Here's the right one.
int main(){
int a = isPrime(7);
printf("%d",a);
}
I'm no C/C++ expert, but try
printf("%d", a);
%d is a format placeholder expecting an integer number, essentially.
That looks like an interesting isPrime function. Not very efficient at all, but different from what I have seen in the past. You can also loop over all the numbers between 1 and n, and just return false (or 0) if you find any that divise n. Or look up more efficient algorithms.
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 have written two similar programs for my programming class. I have shared the code for both programs due to them being similar in nature and both have compiler issues on the final line with return0; For the first string of code the error I receive is "expected a declaration" and "syntax error: 'return'". For the second code I receive the errors "return value type does not match the function type" and " 'display': 'void' function returning a value. I have deleted and added {} at different parts of both codes and tried to rearrange the code. Thanks for help in advance.
Code 1
#include "stdafx.h"
#define nums 7
void display(int *);
int main()
{
int channels[nums] = { 2,4,5,7,9,11,14 };
printf("Channels: ");
display(channels);
}
void display(int *channels)
{
int i;
for (i = 0; i < nums; i++);
{
printf("%d", *(channels + i));
}
}
return 0;
code 2
#include "stdafx.h"
#define nums 7
void display(int *);
int main()
{
int channels[nums] = { 2,4,5,7,9,11,13 };
printf("channels: ");
display(channels);
}
void display(int *channels)
{
int i;
for (i = 0; i < nums; i++)
{
printf("%d", *channels);
*channels++;
}
return 0;
}
Return value is always associated to functions. For example, if a function sums up 2 integers and returns the sum of the integers, it will look something like:
int sum( int a, int b ){
return a + b;
}
See here, the return type of the function is int, and what it returns is also an int, since addition of two integers gives you an integer. It's like 2 horses + 3 horses = 5 horses. You cannot do 2 horses + 3 lions = 5 mangoes. I hope you get the point of return value and type.
Now in code 1, the error is you put your return 0; statement out of the main() function. The program cannot detect which function is this return statement associated to. To correct this, move your return statement in the next line after you call the display() function, INSIDE main() function.
Similarly, in code 2, move the return statement from the display() function to the main() function, just as you did in the previous case. This is because void expects nothing to be returned, but you are returning an integer. This is the same like giving a millionaire a loaf of break when he says he doesn't need one, he will surely get frustrated!
Hope that helps.
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 6 years ago.
Improve this question
I am writing this code to return a value which I will pass through the parameter of the function and print that value.
here is my code:
#include<stdio.h>
int abs(int x)
{
if(x<0)
return x;
}
int main()
{
int x = -6;
printf("Value of abs is %d\n",abs(x));
return 0;
}
This code giving me the output:
Value of abs is 6
Why this code is not returning -6 ?
how to solve this ?
thanks in advance :)
Rename the function, the compiler is using the built-in abs() function instead of the one you defined.
#include <stdio.h>
int Abs(int x)
{
if (x < 0)
return x;
return -x;
}
int main()
{
int x = -6;
printf("Value of abs is %d\n", Abs(x));
return 0;
}
The gcc compiler has an option for this, -fno-builtin would respect every conflicting user defined function, if you just want to override 1 use -fno-builtin-abs in the case it's abs().
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 6 years ago.
Improve this question
the output numbers are wrong why ?
the program is used to get factorial of a number using recursion
and if you know sites to practice more examples I will be thankful for you
#include <stdio.h>
#include <stdlib.h>
int factorial(int a);
int main()
{
int n,x;
printf("enter ur number ");
scanf("%d",&x);
n=factorial(x);
printf("the factorial = %d",n);
return 0;
}
int factorial(int a)
{
int fac;
if(a<=1)
{
if (a<1)
{
fac=0;
}
return fac;
}
printf("the number = %d\n",a);
printf("the factorial = %d\n",fac);
fac = a * factorial(a-1);
This will always return 0, because the last fac will always be 0 and it multiplies all the other results. Change that to
if(a<1)
fac=1;
Also indent your code properly.