C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2009, 05:16 PM   #1
Registered User
 
Join Date: Nov 2009
Posts: 15
Question Help with exponentials

hi

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;
}
i am an absolute newbie and have been reading the articles on this website and i find them helpful

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   Reply With Quote
Old 11-06-2009, 06:08 PM   #2
subminimalist
 
MK27's Avatar
 
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;
You should treat that as a "long float" in your template:
Code:
scanf("%lf", &x);
printf("%lf",x);
Otherwise, as you probably have noticed by now, strange things can result.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 11-06-2009, 06:14 PM   #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   Reply With Quote
Old 11-06-2009, 06:36 PM   #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   Reply With Quote
Old 11-06-2009, 06:41 PM   #5
Registered User
 
Join Date: Nov 2009
Posts: 15
Quote:
Originally Posted by Adak View Post
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;
}
thanks, i'll work on those changes immediately...

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   Reply With Quote
Old 11-06-2009, 06:51 PM   #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   Reply With Quote
Old 11-06-2009, 06:54 PM   #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++);
you are overwriting information stored in the variable x before you use it or make a copy of it.
Brain_Child is offline   Reply With Quote
Old 11-06-2009, 07:02 PM   #8
Registered User
 
Join Date: Nov 2009
Posts: 15
Quote:
Originally Posted by Brain_Child View Post
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++);
you are overwriting information stored in the variable x before you use it or make a copy of it.
that is probably something that i've done wrong
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   Reply With Quote
Old 11-06-2009, 07:29 PM   #9
Registered User
 
Join Date: Sep 2006
Posts: 2,512
Quote:
thanks, i'll work on those changes immediately...

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
Neat table alignment is a detail - put it off for now. What you want now is accurate output, with the proper range of data, and clear logic and code.

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
That way the input loop just keeps repeating until it's in the proper range.

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   Reply With Quote
Old 11-07-2009, 09:19 AM   #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;
}
i cant run it so i dont know what the progam will output... also how do i change variable s to an int?
any help
abdi_84 is offline   Reply With Quote
Old 11-07-2009, 01:53 PM   #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:
how do i change variable s to an int?
Simply change the type to "int"? Instead of including it as a "double" with the list of your other variables, put it on a different line, declared as an "int". You shouldnt have to change any other code for "s".

Your doing
Code:
for         (x=1; x<=s; x++); 
Hopefully its clear enough. You do not want a "semi colon" here. If you do, then the for loop basically does nothing, (s-x) times.

Code:
(x*x);
This line does "nothing". Well, it multiplies x*x, but it does nothing to the result. You probably want to save its value, like
Code:
double result = x*x;
Code:
printf("\n%1.3lf ",x);
"x" will always take on integer values (though stored as floats, since your using "double"). So theres not really any point of making it a double, you could just use a long (integer). Same thing for "result" in my previous example, if "x" is an integer, then "x*x" is an integer. So "double result" could be "long int result"

Code:
pow(exp,x);
Similar to "x*x", this line does "nothing". Well, of course it does: pow returns "exp"^"x", but your not saving the result. You probably want something like "double result = pow(exp,x);", or whatever return type--based on the types of the arguments.

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   Reply With Quote
Old 11-07-2009, 02:15 PM   #12
Registered User
 
Join Date: Nov 2009
Posts: 15
Quote:
Originally Posted by nadroj View Post
I havent followed this thread, but I can try and help with your last post.

Simply change the type to "int"? Instead of including it as a "double" with the list of your other variables, put it on a different line, declared as an "int". You shouldnt have to change any other code for "s".

Your doing
Code:
for         (x=1; x<=s; x++); 
Hopefully its clear enough. You do not want a "semi colon" here. If you do, then the for loop basically does nothing, (s-x) times.

Code:
(x*x);
This line does "nothing". Well, it multiplies x*x, but it does nothing to the result. You probably want to save its value, like
Code:
double result = x*x;
Code:
printf("\n%1.3lf ",x);
"x" will always take on integer values (though stored as floats, since your using "double"). So theres not really any point of making it a double, you could just use a long (integer). Same thing for "result" in my previous example, if "x" is an integer, then "x*x" is an integer. So "double result" could be "long int result"

Code:
pow(exp,x);
Similar to "x*x", this line does "nothing". Well, of course it does: pow returns "exp"^"x", but your not saving the result. You probably want something like "double result = pow(exp,x);", or whatever return type--based on the types of the arguments.

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.
---i dont get this part

EDIT: Are you allowed to use the actual exponential function? exp - C++ Referenceyes i'm allowed thanks for the link


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;
}
i have an error in the code, which i have made bold.

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   Reply With Quote
Old 11-07-2009, 02:23 PM   #13
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 848
Quote:
i have an error in the code, which i have made bold.
I dont see anything in bold.

Quote:
the compiler needs to print the two values of x and exp(x) alongside each other in a neat table,
The compiler doesnt need to do anything--you have to. Note that the compiler isnt the one that "runs" your program either.

Code:
    while (x<=0; x<=spacing;) {
Should probably be
Code:
    while (x<=0 && x<=spacing) {
This has many errors:
Code:
   do
   double result = float exp(      float x);
   printf("%d ","%d ",x      exp(x)  );
Code:
   printf("%d\n", spacing);
   {
       printf("\n%1.3lf ",x);
   }
Does the same thing as
Code:
   printf("%d\n", spacing);
  printf("\n%1.3lf ",x);
I think you should start this over from scratch, working on one small thing at a time. Or read over your notes or a C tutorial to understand the basic syntax (i.e. "format") of the language. It will only be very confusing for you to continue to add more and more things to the program, if you dont have a solid foundation of what the basics are.
nadroj is offline   Reply With Quote
Old 11-07-2009, 02:28 PM   #14
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 848
Quote:
i have an error in the code, which i have made bold.
Code:
   do
   double result = float exp(      float x);   
       printf("%d ","%d ",x      exp(x)  );

   }
edited by abdi_84; Today at 03:17 PM. Reason: made a typing mistake
Ah, theres the bold text now. As I said in my previous post, there are a number of things wrong with this...and I dont even know if I could figure out what your trying to do. As I suggested, I would refresh your memory, or learn for the first time, what the basic syntax of the C language is. Go over some basic tutorials of function and loop structures. Then come back to this and you will see the (basic) errors immediately.
nadroj is offline   Reply With Quote
Old 11-07-2009, 03:05 PM   #15
Registered User
 
Join Date: Nov 2009
Posts: 15
Quote:
Originally Posted by nadroj View Post
Ah, theres the bold text now. As I said in my previous post, there are a number of things wrong with this...and I dont even know if I could figure out what your trying to do. As I suggested, I would refresh your memory, or learn for the first time, what the basic syntax of the C language is. Go over some basic tutorials of function and loop structures. Then come back to this and you will see the (basic) errors immediately.
i read some notes, started again and i've got a error which i cant remove
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;
}
once again, i've made it bold
thanks

Last edited by abdi_84; 11-07-2009 at 03:06 PM. Reason: typing mistake
abdi_84 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 09:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22