Creating a Fibonacci sequence in C - c

Consider a Fibonacci sequence whose values do not exceed four million. Find the sum of the even-valued terms. Your answer must be 4613732.
#include <stdio.h>
int main()
{
int a =0,b=1,c=0,sum=0;
while (c<=10){
int c=a+b;
a=b;
b=c;
if(c%2==0){
sum=sum+c;
}
}
printf("%d",sum);
return 0;
}
The output comes out to be blank. I tried 10 for smaller calculation time and the answer should be 2+8=10. I don't know what to do.

int c=a+b;
This doesn't modify the variable c that was declared outside the loop; it creates a new variable, whose scope is only the body of the loop. You just want c=a+b; here.
You have another bug in that on the iteration of the loop which increases c beyond its limit value, that value may still be added to the sum, even though it should not. For instance, if you replace the loop test with while (c <= 7), you will still get 10 as the answer, instead of the correct 2. Think about how to restructure the loop body to fix this.

The main problem is here:
int c = a + b;
You have redefined the variable c that shadows the original variable on each iteration. Remove the int declaration from there.
Also, note that you want to iterate up to 4 million to obtain your desired results. So, change the iteration limit from 10 to 4000000.*
* It is recommended declaring a macro constant on the top of the program to avoid magical numbers

int a = b+c is the main problem in your code. That line of code re defines the entire variable declaration. So instead u want to put c = a+b
#include <stdio.h>
int main()
{
int a =0,b=1,c=0,sum=0;
while (c<=4000000){
c=a+b; // just remove the `int` and this should fix it
a=b;
b=c;
if(c%2==0){
sum=sum+c;
}
}
printf("%d",sum);
return 0;
}
outputs: 4613732

In this question
int c=a+b;
You are not using the value declared outside of the method Insted you are defining a new variable that only works on inside of your method. Try removing the int decleration inside the while loop and It should work

Related

Variable Initialization in Function

I'm fairly new to coding and am currently learning C. In my C programming class, my instructor gave us the assignment of writing a program that uses a function which inputs five integers and prints the largest. The program is fairly simple even for me, but I'm facing some problems and was hoping to get some advice.
#include <stdio.h>
int largest(int x);
int main(void) {
int integer1;
largest(integer1);
return 0;
}
int largest(int x) {
int i;
for (i = 0; i < 5; i++) {
printf("Enter an integer: ");
scanf_s("%d", &x);
}
return x;
}
This is the code that I have written. The main problem that I am having is that in my main method, the IDE tells me to initialize the value of integer1. However, I'm not really sure how to do that because I'm supposed to input the value within the largest() method via the scanf_s function. How may I solve this?
The problem is here, the warning message is to warn you about the potential pitfall of using the value of an uninitialized automatic local variable. You made the call like
largest(integer1);
but you ignore the return value, so the integer1 remains uninitialized.
Remember, in view of largest(), x is a local copy of the actual argument passed to that function, any changes made to x won't be reflecting to the caller.
That said, your code is nowhere near your requirement, sorry to say. A brief idea to get there would be
Create a function.
Create a variable (say, result) and initialize with minimum possible integer value, INT_MIN
Loop over 5 times, take user input, compare to the result value, if entered value found greater, store that into result, continue otherwise.
return result.
I know that normally help for assignments shouldn't be given but I have to say that you might need to rethink what you want to do.
You are inputting an integer to the function named largest. But why are you only inputting a single integer to a function that should return the largest value. You can't do much with a single number in that case.
You should instead be inputting say an array of 5 values(as said in your assignment) to the function and let it return the largest.
The order would then be:
Read 5 values and save to an array
Call the function largest with the array as input
Let the function do it's work and return the largest value
Do what ever you want with the largest value
But if you only want to remove the warning simply type
int integer1 = 0;

Using It's Own Call as One The Parameters in a Function call statement

Here Is my code:
#include <stdio.h>
int mul(int,int);
int main()
{
int sum,m,n;
scanf("%d%d",&m,&n);
sum=mul(10,mul(m,n));
printf("%d",sum);
}
int mul(int x,int y)
{
int sum;
sum=x+y;
return(sum);
}
Input
10
5
Output
25
Can someone tell me why I get 25 as output? Was the function called 2 times?
One during parameters and other time during sum?
It's perfectly simple:
sum=mul(10,mul(m,n));
You're calling mul() with 10 as the first argument, and the return value of mul(m, n) as the second argument.
m and n are 10 and 5, so mul(10, 5) returns 15. The statement in your main function then evaluates to this:
sum = mul(10, 15);
Which is 25.
TL;DR: yes, mul() is called twice. Once with m and n as arguments. The second time with the sum of m and n, adding 10
Using a debugger, or even looking at the assembler output generated by the compiler would've told you there were 2 successive calls to mul.
And yes, as others have rightfully pointed out: reading the help section (in particular how to ask) would be a good idea. It explains that you're expected to do the sensible debugging/diagnostic steps yourself. Only if that didn't solve the problem should you post a question here:
Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself.
You merely state that, given input X, you get output Y, and you don't know why.

What is wrong with my square root code?

What is wrong with this code? I am learning both Python and C at the same time. Similar code on Python works fine but I am confused why this does not work here?
#include <stdio.h>
float a, b, c,min_value, max_value;
int main(){
printf("Enter a number here:");
scanf("%f",&a);
b=(max_value+min_value)/2;
while(abs(b*b-a)>0.1){
if (b*b>a){
max_value=b;
b=(max_value+min_value)/2;
}
else if(b*b<a){
min_value=a;
b=(max_value+min_value)/2;
}
printf("the square root of the number is %f",b);
}
}
In C, abs is an integer function - passing float values to it will result in truncation, so small values < +/-1.0 will just become 0. You need to use fabs for floating point values. Change:
while(abs(b*b-a)>0.1){
to:
while(fabs(b*b-a)>0.1){
and add:
#include <math.h>
near the top of your source file.
A lot of things are wrong:
float a, b, c,min_value, max_value;
You shouldn't declare these variables outside the main function.
b=(max_value+min_value)/2;
max_value and min_value are not defined, what are you expecting here?
if (b*b>a){
max_value=b;
b=(max_value+min_value)/2;
}
else if(b*b<a){
min_value=a;
b=(max_value+min_value)/2;
}
Once again, max_value and min_value may not be defined. I could continue.
If you are trying to implement a sqrt function, perhaps you should take a look at someone else's code, as it seems obvious you lack knowledge in C.
For future references, please state what your program is trying to accomplish and what errors are occuring. Not trying to be mean or condescending; just giving you a heads up on Stack Overflow policy on question phrasing.
That said, on a brief glance I did notice that you also did not define "max_value" or "min_value" before using them in line 6 (where you set a value to "b"). In addition, declaring your variables outside the main function makes them global variables, which I would suggest avoiding if your variables are not going to be used in another program (via "#include ").

Test result of assignment against original value without introducing extra variable in ANSI C

I wonder if I can at the same time assign a value and check if it changed in a C conditional expression without introducing new examples. Consider the function test as fixed in the following example (I don't want to change its parameters or return values). I search for a variation of the conditional in the main routine which prints "works" because the value of n is incremented by 1 by the test routine. I.e. I want a comparison with the old value of nsing. At the same time it should print "works not" if n would not be incremented by test. I wonder if this could be possible exploiting rules for the order of evaluation or something, i.e. without introducing new variables which store the value of n.
#include <stdlib.h>
#include <stdio.h>
int test(int n)
{
return n + 1;
}
int main()
{
int n;
if ((n = test(n)) == n) {
printf("works not\n");
} else {
printf("works\n");
}
return 0;
}
short answer: no you cannot. For a longer explanation have a look at sequence points
No, you can't do that and that's undefined behaviour, because there's no sequence point between the assignment and the comparison.

Is there a syntactic error that I'm overlooking in this loop

Hey guys could you please spot the semantic error that's in the code below, it seems OK to me but my instructor claims that there still is an "syntactic" error.
This is a simple program that prints a simple series starting from 256.
The series depends on the value of the variable a which is 256 in this case.
Hence in this case the series looks like 256,16,4,2,1. */
#include <stdio.h>
#include <math.h>
int main()
{
int a = 256;
int square_root_a;
printf("%d\n", a);
repeat:
square_root_a = sqrt(a);
if (square_root_a >= 2)
{
printf("%d\n", square_root_a);
a = square_root_a;
goto repeat;
}else{
printf("%d\n", 1);
} return 0;
}
You declare a as an integer, which will round the result of sqrt() to the nearest integer.
I guess you're supposed to use double.
Well, if it's not about the goto, or the int-ness of the variables, then my guess is your teacher means something subtle, like the printfs in both branches of the if. They have exactly the same purpose! Why write two statements that do the same, if it's possible to use only one? Just move the first one to above the if, and delete the second one.

Resources