The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
A task for school.
Discuss A task for school. in the C Programming forum on Dev Shed. A task for school. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 10th, 2013, 09:41 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 1
Time spent in forums: 23 m 17 sec
Reputation Power: 0
|
|
|
A task for school.
Hello, dear users, i would ask, could you please help me with this issue, ihave accoured during my class work.
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");
printf("Ievadi x: ");
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
|

February 10th, 2013, 11:40 AM
|
 |
Contributing User
|
|
|
|
|
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 {
printf("Ievadi x: ");
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!
|

February 10th, 2013, 02:04 PM
|
 |
Contributed User
|
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|