[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Just another simple c program

In this program , i'm suppose to use the runge-kutta method to compute a math function, i won't run into details now, i'll just post my code and ask my question:

This is my code:

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>

double yprime(double x, double y);
double runge(double h, double x, double y, double (*prime)(double x, double y));

main(){

      double x, y, max, h, clibrary;
      double (*prime)(double x, double y);
      char q[10];
      
      
      
      puts("Enter initial values of independent variable x and dependent variable y: ");
      scanf("%s %lf", &q, &y);
 
      if(!isdigit(q[0])) {
            puts("Program terminated: non-numerical entry");
            exit(1);
      }else
            x=atof(q);


      puts("Enter the maximum value of x: ");
      scanf("%lf", &max);


      puts("Enter the independent variable increment h: ");
      scanf("%lf", &h);


      for(x; x<max; x=x+h){

            clibrary=sin(x);
            printf("x = %.3lf   y = %.7lf   c library sin(x) = %.7lf\n", x, y, clibrary);
            y=runge(h, x, y, yprime);            

}}



double runge(double h, double x, double y, double (*prime)(double x, double y)){

      double s1, s2, s3;
      
      s1=(*prime)(x, y);
      s2=(*prime)(x+h, y+h*s1);
      s3=(*prime)(x+h/2.0, y+h*(s1+s2)/4.0);
      y=y+h*(s1+4*s3+s2)/6.0;
      return y;

}

double yprime(double x, double y){

      y=sqrt(1-(y*y));
      return y;

}

this is my questions:

the program should run indefinitely in a loop, until the user enters a letter of the alphabet instead of a floating point number which is requested, then the program should terminate:

How can i do that?, my program teminates after one turn(please run it if you want)

and is my numerical entry test correct !?
any other comments

I appreciate any help !

phew! out of order....

/Reminder for imladris: if you read this question, please  continue on the previous one, wanna keep my promise!/  
0
saoud
Asked:
saoud
1 Solution
 
deightonCommented:
puts("Enter initial values of independent variable x and dependent variable y: ");
     scanf("%s %lf", &q[0], &y);
0
 
rbrCommented:
scanf("%s %lf", q, &y);
will work too.
0
 
saoudAuthor Commented:
it just didn't work ! I copied your scanf(...) and pasted it, itdidn't work , my program still terminates after one turn, i'm not able to enter initial values again....remember, my program shouldn't terminate until a letter is entered

check my test for numerical entries and see if it's working right

please help as soon as possible.....thanks
0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
imladrisCommented:
Given his grading record (D's and C's), and the fact that he has asked 6 questions in the C forum in the last 2 and half months but graded none (despite getting solid responses), it is my considered opinion that saoud should be ignored.
0
 
deightonCommented:
You need a repeating loop to get bck to your puts and scanf, otherwise you just drop out of the bottom of the code.

main(){

                      double x, y, max, h, clibrary;
                      double (*prime)(double x, double y);
                      char q[10];

                      do{

                      puts("Enter initial values of independent variable x and dependent variable y: ");
                      scanf("%s %lf", q, &y);
                       
                      if(!isdigit(q[0])) {
                      puts("Program terminated: non-numerical entry");
                      exit(1);
                      }else
                      x=atof(q);


                      puts("Enter the maximum value of x: ");
                      scanf("%lf", &max);


                      puts("Enter the independent variable increment h: ");
                      scanf("%lf", &h);


                      for(x; x<max; x=x+h){

                      clibrary=sin(x);
                      printf("x = %.3lf   y = %.7lf   c library sin(x) = %.7lf\n", x, y, clibrary);
                      y=runge(h, x, y, yprime);

                      }}



                      double runge(double h, double x, double y, double (*prime)(double x, double
                      y)){

                      double s1, s2, s3;

                      s1=(*prime)(x, y);
                      s2=(*prime)(x+h, y+h*s1);
                      s3=(*prime)(x+h/2.0, y+h*(s1+s2)/4.0);
                      y=y+h*(s1+4*s3+s2)/6.0;
                      return y;

                      }

                      double yprime(double x, double y){

                      y=sqrt(1-(y*y));
                      return y;

} while (1);
0
 
saoudAuthor Commented:
Thanks deighton, my program is running! I appreciate your help!

imladris: I'm so angry at what you said! do you want me to grade an answer that i'm not satisfied with?
    I do have 6 questions, one is just to check whether this place is appropriate for asking for help, and one is ACCidentally duplicated, and finally this one will soon be graded after clicking the submit bottom below!
    Sometimes, you guys provide answers/comments that includes things we didn't get into it yet, so i shouldn't use them.
   I have invited you to continue with the previous program, i was so busy to spend some time on something have already been turned, I just wanted to keep my promise, and i reminded you here, and now you SAY that to people!!!!......tears
you hurt my feeling!

I'm angry, sad, and .........!

see you guys next program
Thanks again
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now