radius of a star help writing program, need corrections?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Daleio
    New Member
    • Feb 2007
    • 15

    radius of a star help writing program, need corrections?

    hey im suppose to write a program using pelles C for windows to calculate the radius of the star using the formulas:

    r = 1/2T^2 *sqrt( L/pi*σ)

    L= 3.36192 *10^(140 - 2M/5)

    σ = 5.67051*10^ -8

    you are given and suppose to input the values for T and M to solve for r, so far I have this but im stuck and I dont even know if this is right:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    {
    	double r, T, M;
    	// Read input data
    	printf("\nr = ");
    	scanf("%1f", &r);
    	printf("T= ");
    	scanf("&1f", &T);
    	printf("M=");
    	scanf("%1f", &M);
    
    	//Perform calculation
    	r = ((1/2T^2)*(sqrt(3.36192*10^(140-2M/5)/(pi*5.67051*10^-8));
    			T=.................;
    			M=................;
    
    			//Display output
    			printf("..............");
    			return 0;
    		}
    any help would be greatly appreciated, thanks.
    Last edited by sicarie; Feb 13 '07, 01:02 AM. Reason: Added code tags.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Comments in code:
    Code:
    #include <stdio.h>
    #include <math.h>
    // i would recommend declaring pi here as a global (as pi's value is not going to change), you don't declare it anywhere, you just use it
    
    int main (void)
    {
    	double r, T, M; 
    	// Read input data
    	printf("\nr = ");
    	scanf("%1f", &r);
    	printf("T= ");
    	scanf("&1f", &T);
    	printf("M=");
    	scanf("%1f", &M);
    
    	//Perform calculation
    	r = ((1/2T^2)*(sqrt(3.36192*10^(140-2M/5)/(pi*5.67051*10^-8));
        // at 2T you need to change this to (1/2)*T and figure out if you want all of that squared, or just T
        // and it is the same thing with 'M'
       // and, of course, declare pi
       // and you're missing one more pair of parentheses at the end
    //			T=.................;
    //			M=................;
    
    			//Display output
    			printf("..............");
    			return 0;
    		}
    Try making those changes and see if it compiles, then re-post your code, and we can work on the rest!

    Comment

    • Daleio
      New Member
      • Feb 2007
      • 15

      #3
      Originally posted by sicarie
      Comments in code:
      Code:
      #include <stdio.h>
      #include <math.h>
      // i would recommend declaring pi here as a global (as pi's value is not going to change), you don't declare it anywhere, you just use it
      
      int main (void)
      {
      	double r, T, M; 
      	// Read input data
      	printf("\nr = ");
      	scanf("%1f", &r);
      	printf("T= ");
      	scanf("&1f", &T);
      	printf("M=");
      	scanf("%1f", &M);
      
      	//Perform calculation
      	r = ((1/2T^2)*(sqrt(3.36192*10^(140-2M/5)/(pi*5.67051*10^-8));
          // at 2T you need to change this to (1/2)*T and figure out if you want all of that squared, or just T
          // and it is the same thing with 'M'
         // and, of course, declare pi
         // and you're missing one more pair of parentheses at the end
      //			T=.................;
      //			M=................;
      
      			//Display output
      			printf("..............");
      			return 0;
      		}
      Try making those changes and see if it compiles, then re-post your code, and we can work on the rest!




      I was kind of wondering how to put unknown variables into the program. I am given values for T and M and somehow im suppose to put them into the program to solve for r. Im not quite sure how to put the equations in the program. I am suppose to put in the values of T and M to solve for r. This is my main problem. Please Help.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by Daleio
        I was kind of wondering how to put unknown variables into the program. I am given values for T and M and somehow im suppose to put them into the program to solve for r. Im not quite sure how to put the equations in the program. I am suppose to put in the values of T and M to solve for r. This is my main problem. Please Help.
        If I understand you correctly, you either have a user inputting the variables, or you have a file with those variables, correct? (and which one?)

        This is a page that details many different options you can use to get input from both the user and from a file (though you might have to do a little more digging if it is in a file - you can google "c read input from file" and you should get something, or you can post back here if you can't find anything).

        Do those look like what you're trying to do, or did I miss what you were saying?

        Comment

        • Daleio
          New Member
          • Feb 2007
          • 15

          #5
          these are the errors i get after i think i made all the corrections....

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

          int main (void)
          {
          double r, T, M,pi;
          // Read input data
          printf("\nr = ");
          scanf("%1f", &r);
          printf("T= ");
          scanf("&1f", &T);
          printf("M=");
          scanf("%1f", &M);

          //Perform calculation
          r = ((1/2*T^2)*(sqrt(3. 36192*10^(140-2*M/5)/(pi*5.67051*10^-8)));
          T=............. ....;
          M=............. ...;

          //Display output
          printf("....... .......");
          return 0;
          }




          C:\Documents and Settings\...... \Assign 2\radius.c(16): error #2168: Operands of ^ have incompatible types 'double' and 'int'.
          C:\Documents and Settings\.....\ Assign 2\radius.c(16): error #2168: Operands of ^ have incompatible types 'double' and 'int'.
          C:\Documents and Settings\...... \Assign 2\radius.c(16): error #2168: Operands of ^ have incompatible types 'double' and 'double'.
          C:\Documents and Settings\...... \Assign 2\radius.c(16): error #2001: Syntax error: found ';' - expecting ')'.
          C:\Documents and Settings\...... ..\Assign 2\radius.c(17): error #2039: Illegal expression.
          C:\Documents and Settings\...... \Assign 2\radius.c(17): error #2001: Syntax error: found '...' - expecting ';'.
          C:\Documents and Settings\...... .\Assign 2\radius.c(17): error #2061: Illegal statement termination.
          C:\Documents and Settings\...... \Assign 2\radius.c(18): error #2039: Illegal expression.
          C:\Documents and Settings\...... ..\Assign 2\radius.c(18): error #2001: Syntax error: found '...' - expecting ';'.
          C:\Documents and Settings\...... ...\Assign 2\radius.c(18): error #2061: Illegal statement termination.
          *** Error code: 1 ***

          need help with these errors please. sorry im a newbie to programming

          Comment

          • hirak1984
            Contributor
            • Jan 2007
            • 316

            #6
            make the return type of main to void.

            Code:
             void main();
            and not
            Code:
            int main(void)
            may be this will help
            Originally posted by Daleio
            these are the errors i get after i think i made all the corrections....

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

            int main (void)
            {
            double r, T, M,pi;
            // Read input data
            printf("\nr = ");
            scanf("%1f", &r);
            printf("T= ");
            scanf("&1f", &T);
            printf("M=");
            scanf("%1f", &M);

            //Perform calculation
            r = ((1/2*T^2)*(sqrt(3. 36192*10^(140-2*M/5)/(pi*5.67051*10^-8)));
            T=............. ....;
            M=............. ...;

            //Display output
            printf("....... .......");
            return 0;
            }




            C:\Documents and Settings\...... \Assign 2\radius.c(16): error #2168: Operands of ^ have incompatible types 'double' and 'int'.
            C:\Documents and Settings\.....\ Assign 2\radius.c(16): error #2168: Operands of ^ have incompatible types 'double' and 'int'.
            C:\Documents and Settings\...... \Assign 2\radius.c(16): error #2168: Operands of ^ have incompatible types 'double' and 'double'.
            C:\Documents and Settings\...... \Assign 2\radius.c(16): error #2001: Syntax error: found ';' - expecting ')'.
            C:\Documents and Settings\...... ..\Assign 2\radius.c(17): error #2039: Illegal expression.
            C:\Documents and Settings\...... \Assign 2\radius.c(17): error #2001: Syntax error: found '...' - expecting ';'.
            C:\Documents and Settings\...... .\Assign 2\radius.c(17): error #2061: Illegal statement termination.
            C:\Documents and Settings\...... \Assign 2\radius.c(18): error #2039: Illegal expression.
            C:\Documents and Settings\...... ..\Assign 2\radius.c(18): error #2001: Syntax error: found '...' - expecting ';'.
            C:\Documents and Settings\...... ...\Assign 2\radius.c(18): error #2061: Illegal statement termination.
            *** Error code: 1 ***

            need help with these errors please. sorry im a newbie to programming

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              If you are going to do that, you need to remove the 'return 0;' at the bottom, otherwise you will get an error with main().

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                If you look at the error messages, everything your compiler is complaining about is on lines 16, 17, and 18.

                You can delete 17 and 18, as far as I can tell, you don't need them.

                Then on 16, you need to be careful on what you are multiplying to what and how:

                r = (( 1/2 * T^2) * (sqrt(3.36192 * 10^(140 - 2 * M/5) / (pi * 5.67051 * 10^-8)));
                I think you should break that part up into several different sections - it will be easier for you and the compiler.

                Comment

                • Daleio
                  New Member
                  • Feb 2007
                  • 15

                  #9
                  yeah tahnks i got most of the problem worked out the only error i get now after th enew code....

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

                  int main (void)
                  {
                  double r, T, M,pi, z,a,b;
                  // Read input data
                  printf("\nr = ");
                  scanf("%1f", &r);
                  printf("T= ");
                  scanf("&1f", &T);
                  printf("M=");
                  scanf("%1f", &M);

                  //Perform calculation
                  r = ((1/2*z)*(sqrt(3.36 192*a)/(pi*5.67051*b)) );
                  z= pow(T,2);
                  a= pow(10,(140-2*M/5);
                  b=pow(10,-8);


                  //Display output
                  printf("....... .......");
                  return 0;
                  }


                  C:\Documents and Settings\.....\ Assign 2\radius.c(18): error #2001: Syntax error: found ';' - expecting ')'.

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Actually, it looks pretty correct except that you need a ')' at the end of the statement, and you should change T^2 to pow(T,2) - that will return a double (while using the carat returns an int - which was what your compiler was saying).

                    (I'd still break it up, but that's just me - it looks very crammed to have all that in there on one line - I'm betting there will be a logic error in there somewhere...)

                    Comment

                    • Daleio
                      New Member
                      • Feb 2007
                      • 15

                      #11
                      nevermind i fix taht but get new error...

                      Code:
                      #include <stdio.h>
                      #include <math.h>
                      
                      int main (void)
                      {
                      	double r, T, M,pi, z,a,b;
                      	// Read input data
                      	printf("\nr = ");
                      	scanf("%1f", &r);
                      	printf("T= ");
                      	scanf("&1f", &T);
                      	printf("M=");
                      	scanf("%1f", &M);
                      
                      	//Perform calculation
                      	r = ((1/2*z)*(sqrt(3.36192*a)/(pi*5.67051*b)));
                      		z= pow(T,2);
                      		a= pow(10,(140-2*M/5));
                      		b=pow(10,-8);
                      
                      
                      	//Display output
                      			printf("..............");
                      			return 0;
                      		}
                      C:\Documents and Settings\...... ..\Assign 2\radius.c(6): warning #2115: Local 'z' is initialized but never used.
                      C:\Documents and Settings\...... ..\Assign 2\radius.c(6): warning #2116: Local 'pi' is used but never assigned a value.
                      Last edited by sicarie; Feb 14 '07, 05:55 AM. Reason: Added code tags.

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        Originally posted by Daleio
                        nevermind i fix taht but get new error...

                        Code:
                        #include <stdio.h>
                        #include <math.h>
                        
                        int main (void)
                        {
                        	double r, T, M,pi, z,a,b;
                        	// Read input data
                        	printf("\nr = ");
                        	scanf("%1f", &r);
                        	printf("T= ");
                        	scanf("&1f", &T);
                        	printf("M=");
                        	scanf("%1f", &M);
                        
                        	//Perform calculation
                        	r = ((1/2*z)*(sqrt(3.36192*a)/(pi*5.67051*b)));
                        		z= pow(T,2);
                        		a= pow(10,(140-2*M/5));
                        		b=pow(10,-8);
                        
                        
                        	//Display output
                        			printf("..............");
                        			return 0;
                        		}
                        C:\Documents and Settings\...... ..\Assign 2\radius.c(6): warning #2115: Local 'z' is initialized but never used.
                        C:\Documents and Settings\...... ..\Assign 2\radius.c(6): warning #2116: Local 'pi' is used but never assigned a value.
                        So the first is telling you that you declared z, assigned a value to it, but then didn't use it again. I'm betting that you wanted T-squared in your answer somewhere there ;).

                        The second is saying that you declared pi, but you never assigned it the value of pi. My suggestion was for a global, so that would be like this:

                        Code:
                        #include //...
                        #define PI    3.1415926
                        int main() 
                        // ...
                        Then whenever you use PI, it will have the value of 3.1415926.
                        Last edited by sicarie; Feb 14 '07, 06:02 AM. Reason: Editing my own code tags... :(

                        Comment

                        • sicarie
                          Recognized Expert Specialist
                          • Nov 2006
                          • 4677

                          #13
                          A side note that would otherwise lead to a logic error:

                          You declare the variables, but then go on to define them after you use them in a function. In C (and C++), when you define variables and do not give them values, they are initalized with whatever leftover values were in the memory allocated to them before they got there. This also known as 'junk' (was going to use a less-nice word there, but decided not to. You get the picture). Junk variables are hard to catch because they actually have a value, just not the one you think they do!

                          So I made some slight modifications below.

                          Code:
                          #include <stdio.h>
                          #include <math.h>
                          #define PI = 3.141592653
                          
                          int main ( )
                          {
                          	double r, T, M, z,a,b;
                          	// Read input data
                          	printf("\nr = ");
                          	scanf("%1f", &r);
                          	printf("T= ");
                          	scanf("&1f", &T);
                          	printf("M=");
                          	scanf("%1f", &M);
                          
                          	//this change in order should help properly perform the calculation
                                  z= pow(T,2);
                          	a= pow(10,(140-2*M/5));
                          	b=pow(10,-8);
                          	r = ((1/2*z)*(sqrt(3.36192*a)/(pi*5.67051*b)));
                          
                          	//Display output
                          	printf("%d",r);
                          	return 0;
                          }

                          Comment

                          • Daleio
                            New Member
                            • Feb 2007
                            • 15

                            #14
                            i got the pi to work but i did use z in my r = equation right after the 1/2 i dont know why it says i dont use it the b and a work fine though

                            Comment

                            • Daleio
                              New Member
                              • Feb 2007
                              • 15

                              #15
                              #include <stdio.h>
                              #include <math.h>
                              #define pi 3.1415926

                              int main (void)
                              {
                              double r, T, M, z,a,b;
                              // Read input data
                              printf("\nr = ");
                              scanf("%1f", &r);
                              printf("T= ");
                              scanf("&1f", &T);
                              printf("M=");
                              scanf("%1f", &M);

                              //Perform calculation

                              z= pow(T,2);
                              a= pow(10,(140-2*M/5));
                              b=pow(10,-8);
                              r = ((1/2*z)*(sqrt(3.36 192*a)/(pi*5.67051*b)) );




                              //Display output
                              printf("The Radius is:");
                              return 0;
                              }

                              Comment

                              Working...