![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 15
| i've been working on this program and i need help with it, it needs to print a neat table of values for x and e^x along side each other. each value needs to be given to three decimal places. i also need to make the minimum value of x 0. The program should ask the user for the maximum value of x and x-spacing. eg if the maximum is 5 then the spacing is 1 then something like the following should appear x exp(x) 0.000 1.000 1.000 2.718 2.000 7.389 3.000 20.086 4.000 54.598 5.000 148.413 so far i have come up with the following code, insert Code: #include <stdio.h>
#include <math.h>
int main(void)
{
double x,s,xmin,exp;
printf("Write the maximum value for x : ");
scanf(" %d ",&x);
printf("Write a Value for the spacing : ");
scanf(" %d ",&s);
for
for (x=1; x<=n; x++);
{
(x*x);
printf("\n%d ",x);
pow(exp,x);
}
scanf("% d,%d8.3 ",x, exp);
return 0;
}
if anyone can lend a helping hand, i would be grateful. Last edited by abdi_84; 11-06-2009 at 05:23 PM. Reason: changing the wording of the questions |
| abdi_84 is offline | |
| | #2 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| One mistake you are making is that the scanf/printf type functions are specific about data types within the templates. So if you have a variable: Code: double x; Code: scanf("%lf", &x);
printf("%lf",x);
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #3 |
| Registered User Join Date: Nov 2009
Posts: 15
| i see i've realised that my code goes on a forever loop.. im not sure how to tackle the spacing issue with the program, and where i put the colons for the output to print as a neat table thanks |
| abdi_84 is offline | |
| | #4 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| Code: #include <stdio.h>
#include <math.h>
int main(void)
{
double x,s,xmin,exp; //change s to an int
printf("Write the maximum value for x : ");
scanf(" %d ",&x); //use %lf instead of %d
printf("Write a Value for the spacing : ");
scanf(" %d ",&s); //s should be an int, since no spacing will be a partial number
for //remove this "for"
for (x=1; x<=n; x++);
{
(x*x);
printf("\n%d ",x); //change %d to %.3lf add a field width before the dot, as needed
pow(exp,x);
}
//This is out of place - it does nothing. What do you want to do here?
scanf("% d,%d8.3 ",x, exp);//change %d to %lf add a space after it
return 0;
}
|
| Adak is offline | |
| | #5 | |
| Registered User Join Date: Nov 2009
Posts: 15
| Quote:
will the program do the task after i made those changes, neat table, calculation etc i'm currently installing ubuntu on windows and dev c++ at the moment so that i can write programs and run them. Cheers | |
| abdi_84 is offline | |
| | #6 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,202
| If you want a table, one way is to print it row by row, separating the columns with tab characters. It won't necessarily be perfect but close enough.
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
| | #7 |
| Registered User Join Date: Feb 2009
Posts: 35
| what is the purpose of the variable x?? Code: printf("Write the maximum value for x : ");
scanf(" %d ",&x);
...
for (x=1; x<=n; x++);
|
| Brain_Child is offline | |
| | #8 | |
| Registered User Join Date: Nov 2009
Posts: 15
| Quote:
i want the program to use the x value the user inputs as long as it is >=0, and then work out its e^x and print that value next to it in another row, the program also needs to repeat this untill the number of spaces has been reached, at which point the loop terminates my logic on how to code this program is flawed thats why the code is confused. i'll work on it as soon as the installation of the compiler is complete.. any help would be greatly appreciated thanks | |
| abdi_84 is offline | |
| | #9 | |
| Registered User Join Date: Sep 2006
Posts: 2,512
| Quote:
You can put a do while loop around your scanf() to get x, like so: Code: do {
//get your x value from the user here
}while(x <= 0); //or whatever you want to be rejected for a range of x
I believe a few do and for loops, will have your program up and running in short order. And don't worry about the print out alignment - that won't be a problem. | |
| Adak is offline | |
| | #10 |
| Registered User Join Date: Nov 2009
Posts: 15
| i've changed my code to the following Code: #include <stdio.h>
#include <math.h>
int main(void)
{
double x,s,xmin,exp; //change s to an int
do {
printf("Write the maximum value for x : ");
scanf(" %lf ",&x)
}while(x <= 0);
printf("Write a Value for the spacing : ");
scanf(" %d ",&s); //s should be an int, since no spacing will be a partial number
for (x=1; x<=s; x++);
{
(x*x);
printf("\n%1.3lf ",x);
pow(exp,x);
}
scanf("% d,%lf 8.3 ",x, exp);
return 0;
}
any help |
| abdi_84 is offline | |
| | #11 | |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 848
| I havent followed this thread, but I can try and help with your last post. Quote:
Your doing Code: for (x=1; x<=s; x++); Code: (x*x); Code: double result = x*x; Code: printf("\n%1.3lf ",x);
Code: pow(exp,x); Also, "pow(exp,x)", as I mentioned, will return "exp^x". The first argument is the base, the second argument is the exponent. From your variable names, you probably want "pow(x,exp)" instead. Also, your using "exp" without having initialized it--so who knows what the result will be. And finally, I dont know if it has been mentioned in this thread already, but since your using "math.h", make sure to link to it when you compile, i.e. pass "-lm" when you compile it. EDIT: Are you allowed to use the actual exponential function? http://www.cplusplus.com/reference/clibrary/cmath/exp/ Last edited by nadroj; 11-07-2009 at 02:00 PM. | |
| nadroj is offline | |
| | #12 | |
| Registered User Join Date: Nov 2009
Posts: 15
| Quote:
i've removed the x*x part completely because it wasnt what i wanted the comiler to do. and also made some changes Code: #include <stdio.h>
#include <math.h>
int main(void)
{
double x,xmin,ex;
int spacing;
printf("Write the maximum value for x : ");
scanf("%lf", &x);
printf("Write a Value for the spacing : ");
scanf("%d\n", &spacing);
spacing += 1;
while (x<=0; x<=spacing;) {
do
double result = float exp( float x);
printf("%d ","%d ",x exp(x) );
}
printf("%d\n", spacing);
{
printf("\n%1.3lf ",x);
}
return 0;
}
also... the compiler needs to print the two values of x and exp(x) alongside each other in a neat table, i dont know how to do that, i am a beginner Last edited by abdi_84; 11-07-2009 at 02:17 PM. Reason: made a typing mistake | |
| abdi_84 is offline | |
| | #13 | ||
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 848
| Quote:
Quote:
Code: while (x<=0; x<=spacing;) {
Code: while (x<=0 && x<=spacing) {
Code: do
double result = float exp( float x);
printf("%d ","%d ",x exp(x) );
Code: printf("%d\n", spacing);
{
printf("\n%1.3lf ",x);
}
Code: printf("%d\n", spacing);
printf("\n%1.3lf ",x);
| ||
| nadroj is offline | |
| | #14 | |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 848
| Quote:
| |
| nadroj is offline | |
| | #15 | |
| Registered User Join Date: Nov 2009
Posts: 15
| Quote:
Code: #include <stdio.h>
#include <math.h>
int main(void)
{
double x, ex;
char x;
int s;
printf("Write the maximum value for x : ");
scanf("%c ", &x);
printf("Write a Value for the spacing : ");
scanf("%c ", &s);
while ( s >= 0; s < 10; s++ ){ //somewhere before or on this line.....expected ';'before ')' token
printf("%int\n", s);
}
(exp(x));
printf("%8.3lf ", x);
printf("%8.3lf ", ex);
return 0;
}
thanks Last edited by abdi_84; 11-07-2009 at 03:06 PM. Reason: typing mistake | |
| abdi_84 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|