Can anyone explain why this code prints garbage values?
int main(){
int a, b, sum;
sum = a+b;
scanf("%d%d",&a,&b);
printf("addition is %d", sum);
return 0;
}
But if I write code like this it prints the correct value?
int main(){
int a, b, sum;
scanf("%d%d",&a,&b);
sum = a+b;
printf("addition is %d", sum);
return 0;
}
That's because C is an imperative language and each line is like a command, executed from top to bottom. So in this case:
int main(){
int a, b, sum; // create 3 variables: a, b and sum. Garbage on start
sum = a+b; // add 'a' and 'b' and save it in 'sum'. Still garbage
scanf("%d%d",&a,&b); // read 2 numbers and save them in 'a' and 'b'. Nowe they're valid, but 'sum' is still garbage
printf("addition is %d", sum); // print 'sum'. That's garbage
return 0;
}
By simply reordering the operations (adding a and b after reading their values) we get valid result
sum = a+b;
is not a definition or a formula, it's a statement - it's executed as soon as it is encountered, with whatever values are in a and b (which are indeterminate).
C just isn't that high-level.
Related
That is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int min=0, med=0, max=0, n=0, t=2,
k=2, j=2, flag=0, counter=0,
a=0, b=0, c=0;
scanf("%d%d", &min, &max);
for(; min<=max; min++){
while(t <= min/2){
if(min%t==0){
flag=1;
break;
}
t++;
}
if(flag==0){
counter++;
}
t=2;
flag=0;
}
printf("%d", counter);
return 0;
}
This code prints all the prime numbers of a given range.
I want to store the first 3 prime numbers into different variables, and then check if the minimum and the maximum divided by two equals the mid one.
Also, I want to keep doing it until the program reaches the end of the range.
I have no idea how to store the first three in different variables inside the loop, or even if have I cannot do it.
I'd appreciate any hints.
I am still wondering how could I implement that inside the loop, because it only gives me one value which is "min".
You could store the current found prime in another variable (c) after you stored that variable in another variable (b) after you stored that variable in another variable (a); this way, you have the last seen three primes at hand.
a = b, b = c, c = min;
if (b-a == c-b) printf("%d %d %d\n", a, b, c);
I have a question in C where I need to insert coefficients of a quadratic equation into a function and return the number of solutions and result.
Write a program that accepts a series of 3 real numbers, which are the
coefficients of a quadratic equation, and the program will print out
some solutions to the equation and the solutions themselves.
Guidelines:
Functions must be worked with one of the functions that
returns the number of solutions as a returned value, and returns the
solutions themselves through output parameters.
3 numbers must be
received each time. The input will be from a file (will end in EOF)
In the meantime I built the function without reading from a file just to see that it works for me, I built the function that returns the number of solutions but I got entangled in how to return the result as output parameter
here is my code for now:
int main ()
{
double a, b, c, root1,root2,rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=(rootnumber(a,b,c);
printf("the number of roots for this equation is %d ",rootnum);
}
int rootnumber (double a,double b, double c)
{
formula=b*b - 4*a*c;
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
In C, providing an "output parameter" usually amounts to providing an argument that is a pointer. The function dereferences that pointer and writes the result. For example;
int some_func(double x, double *y)
{
*y = 2*x;
return 1;
}
The caller must generally provide an address (e.g. of a variable) that will receive the result. For example;
int main()
{
double result;
if (some_func(2.0, &result) == 1)
printf("%lf\n", result);
else
printf("Uh oh!\n");
return 0;
}
I've deliberately provided an example that illustrates what an "output parameter" is, but has not relationship to the code you actually need to write. For your problem, you will need to provide two (i.e. a total of five arguments, three that you are providing already, and another two pointers that are used to return values to the caller).
Since this is a homework exercise, I won't explain WHAT values your function needs to return via output parameters. After all, that is part of the exercise, and the purpose is for you to learn by working that out.
Apart from a wayward parenthesis in the call and some other syntax errors, what you have so far looks fine. To print out the number of roots, you need to put a format specifier and an argument in your printf statement:
printf("the number of roots for this equation is %d\n", rootNum);
The %d is the format specifier for an int.
Here is your working code:
#include <stdio.h>
int rootnumber (double a,double b, double c)
{
double formula = (b*b) - (4*(a)*(c));
if (formula > 0) {
return 2;
}
else if (formula < 0) {
return 0;
}
else {
return 1;
}
}
int main (void)
{
double a, b, c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
return 0;
}
It just need some sanity checking, its working now:
#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{
double a, b, c, root1,root2;
int rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=rootnumber(a,b,c);
printf("the number of roots for this equation is %d", rootnum);
return 0;
}
int rootnumber(double a, double b, double c)
{
int formula= (b*b) - (4*a*c);
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
I want to calculate sum of arithmetic progression in which we have to take 3 variables from user. a=first number, b= step size/increment, c=length of sequence.
If there are more than 1 test case , say three, then I have to scan a,b,c three time. How to do this?
E.g scanf (" %d %d %d", a,b,c); 3 times without affect initial values in first test case.
If you know no of test cases read it first and store it in a variable.
int calculate_ap(int a, int b, int c)
{
//Implement function to calculate Arithmetic progression and return the result
}
int main()
{
int test_cases = 0;
int a, b, c;
scanf("%d", &test_cases); //Reads no of test cases
while(test_cases--)
{
scanf("%d, %d, %d", &a, &b, &c); //read A, B, C
printf("%d\n", calculate_ap(a, b, c));
}
}
Hope this helps.
So, I've been trying to get two integers from the user and the program should return the value of the larger one. Here is my sample program:
#include<stdio.h>
int larger(int a, int b);
int main()
{
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("\n%d is larger than the other", larger(num1, num2) );
}
int larger(int a, int b)
{
if (a>b)
{
printf("%d", a);
}
else if(b>a){
printf("%d", b);
}
}
But the problem here is that whenever I compile then run the program an integer value of 1 is placed beside the sentence "is larger than the other" ,while the largest integer (the integer that should be beside the 'is larger than the other') is placed above the integer 1.
SAMPLE OUTPUT:
Enter first number: 5
Enter second number: 3
5
1 is larger than the other
What should I do to place the larger value placed where it should be?
int larger(int a, int b)
Your function doesn't have a return statement . If you want largest value between two you need to return it from function . Like this -
int larger(int a, int b)
{
if (a>b)
{
//printf("%d", a); unnecessary as you print value in main
return a;
}
else if(b>a){
//printf("%d", b);
return b;
}
else
return a; //in this case take care of output message as both variables will be equal
}
larger needs to actually return the larger value: write return a;, and return b; in the appropriate places. And the output will look odd if you retain the printf calls in that function.
Currently your program behaviour is undefined as the return value is missing: the output is currently arbitrary.
You're also not dealing with all possibilities. What should happen if a and b are equal? You must return something on all control paths.
Your function isn't returning anything, you need to enable compiler warnings or alternatively, get a new compiler.
That being said, in case you aren't afraid of boolean logic, the function can simply be written as
static inline bool larger (int a, int b)
{
return a > b;
}
Naturally, you'll have to rewrite main() to check the result:
if(larger (x, y))
{
printf("x larger than y");
}
In int function larger(int a, int b) you should just compare and return the value rather than printing it.
#include<stdio.h>
int larger(int a, int b);
int main()
{
int num1, num2, largeOftheTwo= 0;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
if(num1 != num2)
printf("\n%d is larger than the other", larger(num1, num2));
else
printf("\n%d is equal to %d", num1, num2);
return 0;
}
int larger(int a, int b)
{
if (a>b)
return a;
else
return b;
}
I am learning recursion and i encountered a conceptual doubt while solving the problem of calculation of remainder when a positive integer is a is divided by a positive integer b.
My code is:
#include<stdio.h>
#include<stdlib.h>
int x;
int rem(int a,int b)
{
x=a;
if(x>=b)
{
x=x-b;
rem(x,b);
}
printf("%d\n",x);
return x;
}
int main()
{
int a,b;
printf("Enter a & b\n");
scanf("%d %d",&a,&b);
int y =rem(a,b);
printf("rem is :%d",y);
return 0;
}
Its working fine. I have learned that for every call a new set of formal parameters and local variables are created.
So i experimented it by printing x on return of every recursive call!
But it is printing 1 1 1 1. Why is the value of x corresponding to a particular call not printed. ?
Why only the last modified value printed?.. Is that because i declared 'x' as global?
In this case perhaps you need only to move your print up
int rem(int a,int b)
{
x=a;
printf("%d\n",x);
if(x>=b)
{
x=x-b;
rem(x,b);
}
return x;
}
But I think you should avoid the use of global variables in a recursive alrotithm. It could make the algorithm very difficult to reason about. Recursive functions are better to be 'pure functions'.
It is because while x >= b, rem() is repeatedly called before printf()s are called. Only after x < b will the printf()s are called as each call on rem() unwinds.
You might want to make x local to rem() to get the desired result.
Ignoring issues with checking the return value from scanf() and that the two entered values are both positive, etc, I think you can and should avoid x altogether. You could use:
#include <stdio.h>
static int rem(int a, int b)
{
if (a >= b)
a = rem(a-b, b);
printf("%d\n", a);
return a;
}
int main(void)
{
int a, b;
printf("Enter a & b\n");
scanf("%d %d", &a, &b);
int y = rem(a, b);
printf("rem(%d, %d) is: %d\n", a, b, y);
return 0;
}
This code captures the return value from rem() at each level of recursion. In this case, because the returned value doesn't change as the recursion unwinds, you could use the global variable x, but there is no need for it, and you should avoid global variables whenever you can.
#include<stdio.h>
#include<conio.h>
int fun(int,int);
int main()
{
int a,b;
printf("enter two numbers");
scanf("%d %d",&a,&b);
fun(a,b);
//printf("%d",fun(a,b));
}
int fun(int a,int b)
{
if(a<b)
printf("%d",a);
if(a>=b)
a=fun(a-b,b);
return a;
}