C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 10th, 2013, 09:41 AM
SacredD SacredD is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 SacredD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 10th, 2013, 11:40 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

Reply With Quote
  #3  
Old February 10th, 2013, 02:04 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > A task for school.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap