no answer from simple subtraction code in C [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
i wrote code, and it gives me answers like 0.00 and 1.00, not actual math answer. Where i made mistake? (im begginer, dont scream on me :) )
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
float x;// score
// Request 1
printf("Zadanie 1(12)\n");
// Calculate the square root of the given interval
printf("Oblicz pierwiastek rownania w podanym przedziale\n");
// Give me a number a
printf("Podaj liczbe a:\n");
scanf("%d",&a);
// Give me a number b
printf("Podaj liczbe b:\n");
scanf("%d", &b);
x=&a-&b;
//answer
printf("%f", x);
system("PAUSE");
return 0;
}

x = a - b
not
x = &a - &b
Explanation: The & operator gives you the memory address of a, which you need to give to scanf, so that it can place data there. But you do math on the actual value of a.. which is just a.

because of pointer arithmetic:
x = &a - &b;
computes the distance between the addresses of a and b, which are probably close since they're auto variables of the same type declared close-by. My guess is that you could get 1 or -1 (or any other integer value but not 0 since a and b are located in 2 separate addresses) then put in a float.
(the difference of addresses of 2 consecutive integers is 1, independently of the size of the integer)
You need of course to do:
x = a - b;
You've been misled by the fact that scanf needs a pointer on a or b to be able to write a value when reading the input.

Related

variable inside int in c giving exceptional output [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 1 year ago.
Improve this question
In below given program if I put n=0, then the program is giving right answer but if I write n instead of n=0, the program is giving me wrong answer. If I put k=10 then output is 94 but the correct answer is 55. Why it is adding additional 39?
int main(){
// program to calculate the sum of 'n' numbers
int i=1,k,n;
printf("Enter number: ");
scanf("%d",&k);
do{
n+=i;
i++;
}while(i<=k);
printf("The sum is: %d",n);
return 0;
}
Your loop is adding a value to the current value of n, but you never set the initial value of n. That means its value is indeterminate, and reading an indeterminate value (in this case when you add to it), when the variable in question never had its address taken triggers undefined behavior.
Initialize n to 0 so you have a valid starting point.
int i=1, k, n=0;

Output coming unreasonably large in C for the equation x+ y^2 + z^3 [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 2 years ago.
Improve this question
//Experiment 3 Task 4 b.)
#include<stdio.h>
void main(){
int x,y,z,ans;
printf("\nEnter the value of x. ");
scanf("%i",&x);
printf("\nEnter the value of y. ");
scanf("%i",&y);
printf("\nEnter the value of z. ");
scanf("%i",&z);
ans = (x + (y^2) + (z^3));
printf("\nAnswer = %i",&ans);
}
OUTPUT:
Enter the value of x. 1
Enter the value of y. 2
Enter the value of z. 4
Answer = -483189484
There are two issues with your code:
With regards to large value, you are printing address rather than actual answer. So remove the & when you print
The ^ operator in C represents the bitwise XOR. So you can either use the pow function, (dont forget to put #include <math.h>) or write your own version of it to get correct results.

Calculating n! and x^n and proceeding the calculation [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 3 years ago.
Improve this question
Write a C program to calculate (x^n)/(n!) where x is a floating point number and n is an integer greater than or equal to zero.
I coded the following:
#include<stdio.h>
#include<math.h>
void main()
{
float x,p;
int i,n,f=1;
printf("Enter the value of x,n\n");
scanf("%d %d",&x,&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
f=f*i;
}
p=(float)pow(x,n)/f;
printf("The value of p is %.3f",p);
}
if(n==0)
{
p=(float)pow(x,n)/1;
printf("The value of p is %d",p);
}
getch();
}
But this is not running well. Where have I gone wrong?
PS: Edit
In your question I have recognize 3 problems.
main problem is scanf("%d %d",&x,&n); should be change into scanf("%f %d",&x,&n);
because x is `float type #dragosht has mentioned it.
printf("The value of p is %d",p); should be correct as printf("The value of p is %f",p); beacause p is also float type.
It is better to set p = 0; at the beginning because you did not assign value to p using keyboard. There for some times you will get corrupted values because of this.

Program with array to find a sum 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 4 years ago.
Improve this question
I was thinking of a code to find the sum of numbers.
So i wrote bellow code :
#include <stdio.h>
#include <math.h>
int main()
{
int n,i,s=0,a[100];
printf("enter number of numbers");
scanf("%d",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
s=0;
for (i=1;i<=n;i++)
{s=s+a[i];}
printf("sum is%d\n",s);
}
But in the output it shows segmentation error . I.e.
So whats the mistake?
So this line:
scanf("%d",n);
should be replaced by
scanf("%d",&n);
Explanation:
scanf() reads data from stdin according to format and store data in the location pointed by next additional argument. this case, format is %d means we want to read an integer number and this integer number will be stored in the location of n. The & operator is to get the location of a variable in C.
Use this :
scanf("%d",&n);
The reason :
You MUST put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.
Examples :
scanf("%d %d", &a, &b);
printf("%d %d", a, b);
As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf(). So in order to reflect changes in the variable a and b of the main function, we need to pass addresses of them. We cannot simply pass them by value.
But in case of printf function as we are only going to print the values of the variables in output console, there are no changes going to be made in variable a and b’s values. So it is not required to send their addresses.

Unable to understand the meaning of errors in C language using Turbo C. I've coded this code [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 6 years ago.
Improve this question
This is what I've coded - the exchange of 2 numbers in c language using turbo c. I've tried real hard to google the solution but the explanation is not understandable as I'm a mere beginner. I'm having my internals on 20th and this is what they might ask along with 40 other programs.....
You should write printf("please enter"); instead of printf{"please enter"};
The same with scanf.
When you see Statement missing ; error, you definitely have an language error.
Functions take arguments between () and {} are used for blocks of code. For example:
if (1) {
printf("Printing\n");
printf("Printing again\n");
}
Another thing is, that we assign value to the left side of =.
a = a-b
a = a+b
Your code should be:
#include<stdio.h>
void main() {
int a,b;
printf("Enter two (2) numbers, please\n");
scanf("%d%d", &a, &b);
a = a+b; //assign a to the sum of current a and b
b = a-b;
a = a-b;
printf("The numbers a, b after some calculations are: a=%d and b=%d\n", a, b);
}
Use parenthesis () instead of braces {} in your functions printf() , scanf().
printf("please enter 2 nos");
scanf("%d %d" &a,&b);
printf("our exchanged numbers are a = %d" and b=%d" a,b);
And you can't assign value to a expression, it will give lvalue error. So to remove this error change,
a-b = a change it to a=a-b and change all the expressions.

Resources