I'd like for the program to solve my equation yet sadly it doesn't. Additionally, I'd want for it to print an answer depending on the value of x that I input in the equation. Please let me know how I would be able to print the answer or how I can program it so that the equation gives me an answer that I can then print.
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
/* Main program */
void main ()
{
/*
variable declaration section comments
l: length value
q: value of q
ei: value of ei
s: l devided by 2 since 0 < x < l/2
b: the length l (thus, 20)
z: 0
first_equation: The first equation pertaining to 0 < x < l/2
second_equation:The second equation pertaining to l/2 < x < l
*/
double x, first_equation, second_equation, l, q, ei, s, b, z;
l = 20.0;
q = 4000.0;
ei = 1.2 * (pow(10.0, 8.0));
s = l / 2.0;
b = l;
z = 0.0;
printf ("please enter the x-value\n");
scanf ("%lf", &x);
/* Deflection equations */
first_equation = ((q * x) / (384.0 * ei)) * ((9 * (pow(l, 3.0))) - (24.0 * l * (pow(x, 2.0))) + (16 * (pow(x, 3.0))));
second_equation = ((q * l) / (384.0 * ei)) * ((8 * (pow(x, 3.0))) - (24.0 * l * (pow(x, 2.0))) + (17 * (pow(l, 2.0)) * x) - (pow(l, 3.0)));
/* Determining what equation to use */
if (x >= z && x <= s)
printf ("\n first_equation\n\n");
else if (x > s && x <= b)
printf ("\n second_equation\n\n", second_equation);
else if (x < 0 || x > b)
printf ("\n invalid location\n\n");
return;
}
This...
printf ("\n second_equation\n\n", second_equation);
... does not print the second_equation variable: it provides it as an argument to printf, but printf only uses extra arguments as directed by %f or other conversion instructions embedded in the text provided as the first argument. You could write:
printf ("\n second_equation %f\n\n", second_equation);
You may want to do something similar for first_equation.
Alternatively [when I answered the question was tagged C++] you could use C++ I/O routines (scanf and printf are from the C library, and have a number of disadvantages, the most obvious here being that you have to remember funny letter codes like "lf" matching your data types)...
#include <iostream>
...at the very top of your file, then in your function write...
std::cout << "\n second_equation " << second_equation << "\n\n";
You could also use C++ I/O for input, replacing scanf with...
if (!(std::cin >> x))
{
std::cerr << "you didn't enter a valid number\n";
exit(1);
}
Your code is really unclear; but going by your question, you seem to want to be able to print your answer. In that case, here is the proper syntax
printf ("Answer: %d \n", yourAnswer); //if 'yourAnswer' is decimal or number
To use one of your code snippets, you'll have this:
printf ("\n second_equation: %d\n", second_equation);
Related
I am learning c. So, I was practicing in a online judge. I have got a logic behind the problem and submitted ans got wrong answer. What is the problem?
Problem:
Area
100 Points · Limits 1s, 512 MB
In this problem, you will be given a square which has a length of n. Co-ordinates of the square are (0,0), (n,0),(n,n),(0,n) . You need to draw 4 straight lines:
Line from (0,1) to (n,n-1)
Line from (1,0) to (n-1,n)
Line from (0,n-1) to (n,1)
Line from (1,n) to (n-1,0)
These four lines will intersect in a point (x,y) like the figure shown below.
Calculate the total area of A+B+C+D (except the four corner unit square).
Input
Input will start with an integer T. Then there will be T cases. Each case will contain one integer N. 1 <= T <= 100000
3 <= n <= 1018
Output
For each test case, print “Case x: y” without quotation marks where x is the case number and y is the required answer.
It is guaranteed that y is always an integer.
Sample
Input Output
1
6
Case 1: 8
My code:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int test, i;
scanf("%d", &test);
for(i=0; i<test; i++)
{
double n, area, a,x, b1, b, s, tri, area1, area_t;
scanf("%lf", &n);
area= n*n;
a=n-2;
x=n/2;
b1= (x-1)*(x-1) + x*x;
b= sqrt(b1);
s= (a+b+b)/2;
area1= s*(s-a)*(s-b)*(s-b);
area_t = (4* sqrt(area1));
printf("Case %d: %.0lf\n",i+1, (area-(area_t + 4)));
}
return 0;
}
Please help me to improve the code. Thank you.
I believe you have a compatability problem and your online judge is on a C89 implementation where "%lf" does not exist, making your program output
Case 1: %.0lf
Case 2: %.0lf
...
Try using the C89 specifier
printf("Case %d: %.0f\n", i + 1, area - (area_t + 4));
/* ^^^^ C89, not %.0lf */
Note: double x; scanf("&lf", &x) has been valid since C89.
Let Area be the requested area, which can be calculated as:
Area = OuterSquareArea - 4 * IsoscelesTringleArea - 4 * SmallSqareArea
where:
OuterSqareArea = n * n
IsoscelesTriangleArea = base * height / 2
= (n - 2) * (n / 2) / 2
= (n - 2) * n / 4
SmallSquareArea = 1 * 1
= 1
The computation of Area can be summed up to:
Area = (n * n) - 4 * ((n - 2) * n / 4) - 4 * (1)
= n * n - (n - 2) * n - 4
= (n - (n - 2)) * n - 4
= 2 * n - 4
The trace guarantees that t, n and Area are integers. The code we need is then:
#include <stdio.h>
int main() {
int i, t, n;
scanf("%d", &t); /* read t */
for (i = 1; i <= t; i++) { /* for any i in [1,t] */
scanf("%d", &n); /* read n */
printf("Case %d: %d\n", i, 2 * n - 4); /* solve */
}
}
The code could simply be extented to check t and n to be in the given ranges.
I am writing a basic program to calculate an arithmetic-geometric mean using command line arguments using C. However the program doesn't seem to be recognizing anything I type in. Here is my code:
/*
*This program will calculate an arithmetic-geometric mean
*provided two numbers (x,y) and an epsilon (e) are entered.
*/
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main (int argc, char **argv) {
//Check for command line argument.
if (argc != 4) {
printf ("Please enter two numbers (x,y) and an epsilon (e)\n");
printf ("as command line arguments for an AGM calculation\n");
exit(1);
}
double e,x,y,an,gn;
x = atof (argv[1]); //First number x.
y = atof (argv[2]); //Second number y.
e = atof (argv[3]); //Number of repetitions e.
double absoluteAnMinusGn; //Continuation condition.
double a = (x + y) / 2; //Normal arithmetic mean.
double g = sqrt (x * y); //Normal geometric mean.
an = (a + g) / 2; //Iteration 1 for calculation arithmetic mean.
gn = sqrt (a * g); //Iteration 1 for calculation geometric mean.
absoluteAnMinusGn = an - gn; //Calculates continuation condition.
if (absoluteAnMinusGn < 0) {
absoluteAnMinusGn = absoluteAnMinusGn * (-1); //Ensures absolute value of continuation condition.
}
printf ("DEBUG IN: x%d, y%d, e%d, absoulteAnMinusGn%d, a%d, g%d, an%d, gn%d\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE
while (absoluteAnMinusGn > e) {
an = (a + g) / 2;
gn = sqrt (a * g);
a = an;
g = gn;
absoluteAnMinusGn = an - gn;
if (absoluteAnMinusGn < 0) {
absoluteAnMinusGn = absoluteAnMinusGn * (-1);
}
}
//printf ("The arithmetric-geometric mean is (%d,%d) for %d\n", a,g,e);
printf ("DEBUG OUT: x%d, y%d, e%d, absoulteAnMinusGn%d, a%d, g%d, an%d, gn%d\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE
return 0;
}
I enter the following on the command line: agm.exe 3 4 5
I get the following output:
DEBUG IN: x0, y10742661112, e0, absoluteANMinusGn1074790400, a0, g1075052544, an-171951648, gn1057505593
DEBUG OUT: x0, y10742661112, e0, absoluteANMinusGn1074790400, a0, g1075052544, an-171951648, gn1057505593
I made a similar program yesterday for calculating an integral using command line inputs that works exactly as intended. The code for that is here:
/*
*This program will calculate a Riemann sum using the
*left hand rule for sin(x)/x.
*/
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main (int argc, char **argv) {
//Check for command line argument.
if (argc != 4) {
printf ("Please enter integral bounds (a,b) and number of intervals (n)\n");
printf ("as command line arguments for a Riemann sum calculation \n");
exit(1);
}
double a,b,i,n,h,riemann,rectangles,answer;
a = atof (argv[1]); //Lower bound of integral.
b = atof (argv[2]); //Upper bound of integral.
n = atof (argv[3]); //Number of intervals.
h = (b - a) / n; //Delta X.
i = 0; //Counts intervals.
//Calculation of Left Hand Riemann Sum.
while (i <= (n - 1)) {
if (a == 0 && i == 0) { //Stops from dividing by zero.
rectangles = 1;
i++;
}
riemann = (sin(a + (i * h))) / (a + (i * h));
rectangles += riemann;
i++;
}
//Finalize answer.
answer = rectangles * h;
printf ("Sin(x)/x for bounds (%f , %f) with %f intervals is approximately %f \n", a,b,n,answer);
return 0;
}
The above program for a left hand Riemann sum outputs correctly, and is almost the same as the code I have for the AGM. Can someone please help me figure out what is going wrong here? I've searched all over and I can't find a solution. I'm aware that the AGM code is probably set up to output an incorrect answer, but my primary concern is fixing my command line argument recognition. I can redo my math later.
The format specifier for printing double is %f. In cases where you didn't provide the right format specifier to process the double, and passed double to %d format specifier - it lead to undefined behavior.(%d expect integral argument not double) (in your case wrong output).
From §7.21.6.3p9 N1570 (c11 standard)
If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
I did not test your algorithm, but I found at least three problems in your code. I now list them as follows:
Forgot init your variable, that's why you got a Large Numbers
which is memory undefined data!
No have covert your argv to a integer type.
Wrong format for printf.
I have put my fixed code here, you can test it . if it can run as your expect!
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main (int argc, char *argv[]) {
if (argc != 4) {
printf ("Please enter two numbers (x,y) and an epsilon (e)\n");
printf ("as command line arguments for an AGM calculation\n");
exit(1);
}
// argv is pointer which pointer to pointer
// and argv[1] is a pointer to a string.
// so we need to covert string to int
char *p=NULL;
int x = strtol(argv[1], &p, 10);
int y = strtol(argv[2], &p, 10);
int e = strtol(argv[3], &p, 10);
//Check for command line argument.
double an = 0.0,gn =0.0;
double absoluteAnMinusGn=0.0; //Continuation condition.
double a = (x + y) / 2; //Normal arithmetic mean.
double g = sqrt (x * y); //Normal geometric mean.
an = (a + g) / 2; //Iteration 1 for calculation arithmetic mean.
gn = sqrt (a * g); //Iteration 1 for calculation geometric mean.
absoluteAnMinusGn = an - gn; //Calculates continuation condition.
if (absoluteAnMinusGn < 0) {
absoluteAnMinusGn = absoluteAnMinusGn * (-1); //Ensures absolute value of continuation condition.
}
printf ("DEBUG IN: x%d, y%d, e%d, absoulteAnMinusGn=%f, a=%f, g=%f, an=%f, gn=%f\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE
while (absoluteAnMinusGn > e) {
an = (a + g) / 2;
gn = sqrt (a * g);
a = an;
g = gn;
absoluteAnMinusGn = an - gn;
if (absoluteAnMinusGn < 0) {
absoluteAnMinusGn = absoluteAnMinusGn * (-1);
}
}
printf ("DEBUG OUT: x=%d, y=%d, e=%d, absoulteAnMinusGn=%f, a=%f, g=%f, an=%f, gn=%f\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE
return 0;
}
and then when i run it with some sample number like below show, i got the result .
Just replace the %d(which is a format specifier for integers) with %f(which is the format specifier for double) because you are using a variable of type double, not an integer .
#include <stdio.h>
int main() {
float a,c,d;
int b;
printf("Enter the float number: "); scanf("%f", &a);
a * 100 == b ;
b % 100 == c ;
c + a == d ;
printf("%f", d);
}
It prints 0.00.
Why is it doing this?
These statements:
a * 100 == b ;
b % 100 == c ;
c + a == d ;
are a series of comparisons. == is the equality comparison operator, while = is the assignment operator. Also, % is the modulus operator, and can only be used for integral operands. Perhaps you meant something more like
b = (float) ((int) (a * 100) % 100); /* fraction */
c = (float) ((int) a % 100) / 100.0; /* mantissa */
d = b + c;
Note that this is not necessarily good style, but should work.
To start with == is a comparison operator. To assign a value, you need to use =.
Assignment syntax in C is written as:
b = a * 100;
Try reading some tutorials on assignment operations and you should see where you are going wrong.
Here's one to get you started.
I'm trying to draw a line in C language using Bresenham's algorithm.I'm using turbo C++ in dosbox for windows 7 to implement this code.While compiling i'm not getting any error but when i run the code the programs terminates after obtaining the 2 co-ordinates.Please Help..
the message on compiling is as follows..
the directories path is as follows
My code..
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
OP should post input that was used.
The posted sample code does not work is x1 > x2 nor y1 > y2. This is one set of input that would stop the routine abruptly. To fix, the dx and dy should be based on the absolute value and the incremental x & y steps need to be independently +1 or -1.
An input of 3,4 instead of 3 4 (comma vs. whitespace) will also mess up the routine.
In the while loop, recommend if(p <= 0).
OP's "... code the programs terminates after obtaining the 2 co-ordinates." is not detailed enough, for of course the code should terminate sometime after obtaining the 2 co-ordinates. But OP does not detail where it terminates too early.
This is a typical perfect point to start the debugger and go through the code step-by-step, watching any variables. If a debugger is unavailable, printf debugging to the console is a backup alternative.
A first tip is to check that these lines do not generate an error/exception:
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);
Nice program. But, you haven't initialized any loop as well as lines coded in while-loop were partially incorrect. Here is my try:-
i = 1; // loop initialization
do {
putpixel(x, y, 15);
while(p >= 0) {
y = y + 1;
p = p - (2 * dx);
}
x = x + 1;
p = p + (2 * dy);
i = i + 1;
}
while(i <= dx);
A way to rectify the problem is by changing the path in the initgraph function according to the address mentioned in the screenshot.
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TURBOC3\\bgi");
putpixel(x,y,WHITE);
I've been trying to look up on Google how to put in an equation in my program but wasn't able to find any. How do you include:
x = ( -b + √b2 - 4ac ) / 2a
in the program?
Here's my code:
{
int a, b, c;
float x;
//statements
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
//computeforX
x = ( -b + √b2 - 4ac ) / 2a
printf("The value of x is %.1f", x);
return 0;
}
Assuming we're talking about C (or C++) here, you will need to investigate the sqrt function, and maybe also the pow function as well (although that's unnecessary because b-squared can be computed as b*b).
Note that you will need to convert all of your input values to float or double before you start the calculation, otherwise you will not get the intended result.
You need a table to allow you to translate:
a+b -> a+b
a-b -> a-b
a/b -> a/b
ab -> a*b
√x -> sqrt(x)
x² -> x*x (If you want to square something more complicated it might be best to use a temporary variable for the value to be squared, breaking your equation up into pieces.)
Note that if you divide an int by an int in C you get an int. So better convert those ints to doubles before dividing.
If we are dealing with C++ it would be something like
#include <iostream.h>
#include <cmath>
int main ()
{
//Declare Variables
double x,x1,x2,a,b,c;
cout << "Input values of a, b, and c." ;
cin >>a >>b >>c;
if ((b * b - 4 * a * c) > 0)
cout << "x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)" &&
cout << "x2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)";
if else ((b * b - 4 * a * c) = 0)
cout << "x = ((-b + sqrt(b * b - 4 * a * c)) / (2 * a)"
if else ((b * b - 4 * a * c) < 0)
cout << "x1 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a) &&
cout << "x2 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a);
return (0);
}
Now why do i have this wierd feeling I just did someone's first semester programming class' homework?
Granted its been years and I don't even know if that will compile but you should get the idea.
I am really depressed looking the quality of above answers and help, which has been given.
I hope to improve the content of this thread.
One can compile the C file below with the command line gcc file.c -o file -lm.
Herewith a possible solution in C:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(){
int da, db, dc;
double x, a,b,c;
//statements
printf("Enter three integers: ");
scanf("%d %d %d", &da, &db, &dc);
a = (double)da;
b = (double)db;
c = (double)dc;
//computeforX
x = (double) ( -b + sqrt(b * b) - 4 * a * c ) / ( 2 * a ) ;
printf("The value of x is %g \n", x);
return 0;
}