Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 10th, 2013, 10:41 AM
 SacredD
Registered User

Join Date: Feb 2013
Posts: 1
Time spent in forums: 23 m 17 sec
Reputation Power: 0

The issue: Every time i enter an x value every task in the code completes exept it allways shows that y:=0 maybe i have written down the equations wrong to c. Could you please help me?

Source code.
#include <stdio.h>
#include <windows.h>
#include <math.h>

int main()
{
double x, a, b, y;

do
{
system("cls");
scanf("%lf", &x);
a =fabs((7*x-5)/(2*pow(x,2)+3*x));
b =fabs(pow((x+1)/(x-1),x)-18*x);
if (x == 0,x == 1)
{
printf("x nedrikst but 1 un 0!!!\n"); // pārbauda vai vērtība nav 0
system("pause");
}
else
{
if (a>10 && b>10)
{

y =fabs(1/(2*a*b)*((sqrt(a*a-b*b)*tan(x)+2)/(sqrt(a*a-b*b))*tan(a)-2)) ;
printf("1. zars\n");

}
else
{
if (a<10 && b<10)
y=fabs(1/(b*b)*(log(b/a)+a/b));
printf("2. zars\n");
}
printf("x = %.2lf, a = %.2lf, b = %.2lf, y = %.2lf\n", x, a, b, y);

system("pause");
}
}
while(x == 0 || x == 1);
return 0;
}// main beigas

#2
February 10th, 2013, 12:40 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
comma operator may have been typographical oversight.

Code:
```#include <stdio.h>
#include <math.h>

int main() {
char buffer[128];		/* buffer for fgets */
double x, a, b, y;
do {
if (NULL == fgets(buffer,sizeof(buffer),stdin)) /* test for end of file */
break;
if (1 != sscanf(buffer,"%lf",&x)) /* validate numeric entry */
continue;
if ((x == 0) || (x == 1)) {	      /* You had comma operator  (x == 0, x == 1) */
puts("x nedrikst but 1 un 0!"); // pārbauda vai vērtība nav 0
} else {
a =fabs((7*x-5)/(2*pow(x,2)+3*x)); /* compute a and b only when needed */
b =fabs(pow((x+1)/(x-1),x)-18*x);
if (a>10 && b>10) {
y =fabs(1/(2*a*b)*((sqrt(a*a-b*b)*tan(x)+2)/(sqrt(a*a-b*b))*tan(a)-2)) ;
printf("1. zars\n");
} else {
if (a<10 && b<10)
y=fabs(1/(b*b)*(log(b/a)+a/b));
printf("2. zars\n");
}
printf("x = %.2lf, a = %.2lf, b = %.2lf, y = %.2lf\n", x, a, b, y);
}
} while(x == 0 || x == 1);
puts("Press enter to finish.  Next time adjust your command window to remain open.");
getchar();
return 0;
}```
__________________
[code]Code tags[/code] are essential for python code!

#3
February 10th, 2013, 03:04 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 19 h 10 m 45 sec
Reputation Power: 1827
Does it really help?
Code:
```    if (1 != sscanf(buffer,"%lf",&x)) /* validate numeric entry */
continue;
if ((x == 0) || (x == 1)) {	      /* You had comma operator  (x == 0, x == 1) */```

There is nothing quite so annoying as operand reversal applied when it doesn't matter, and not applied when it might actually make a difference.

1. The 'rule' (such as it is) only makes any sense at all when the operator in question is ==. Generally speaking, people manage all the other operators without incident, although there is a minor edge case of writing =! in place of !=, which would be another kind of assignment.
Something like if ( sscanf(buffer,"%lf",&x) = 1 ) would have been a compiler error anyway, since the scanf call isn't an l-value to begin with. Of course, if you had if ( sscanf(buffer,"%lf",&x) = variable ), you would now be sitting at the bottom of the very hole you were trying to avoid if you ended up doing this if ( variable = sscanf(buffer,"%lf",&x) ).

2. The second point is that code should in the first instance be as readable as possible. while ( 5 >= i ) is how Yoda the Coda would write it, not someone proficient in English. In a complicated expression, it might take minutes to grok what should only take seconds (and this is time wasted EVERY time the code is read). ((Yoda doesn't do C++, he says "do or do not, there is no try")).

3. Unlike if ( a == b ), when simply swapping the operands doesn't change the outcome, changing if ( a < b ) involves rather more editing to turn it into if ( b > a ). I've seen in the past well-meaning enthusiasts mess this up and introduce bugs into previously working code.

4. If you're in the unfortunate position of having if ( var1 == var2 ), then no amount of rearranging the deck-chairs on the Titanic is going to save you. You should have spent the time forcing yourself to remember = vs ==, not silly syntax tricks which only 'solve' a subset of some problem.

5. Most (all?) modern compilers will, with sufficiently high warning level, tell you that you've used = in a context where == is more usual. Yes, even for the problematic var1 == var2 case where the operand reversal fails so miserably at the most critical moment.

The idea was born in the 1980's, when compilers only emitted syntax errors. Anything that was suspect (no matter how stupid) was just compiled with the assumption that the programmer knew what they were doing.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

 Viewing: Dev Shed Forums > Programming Languages > C Programming > A task for school.