• C

# 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 !?

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!/
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

progCommented:
puts("Enter initial values of independent variable x and dependent variable y: ");
scanf("%s %lf", &q[0], &y);
0
Commented:
scanf("%s %lf", q, &y);
will work too.
0
Author 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

0
Commented:
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
progCommented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author 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!