Thread: Double

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    15

    Double

    I am trying to make a program where I need to have some doubles in the read function, that needs to be used in the Tovn1 function, like ... It is double prewatt, double tovn and double maalt.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    double Tovn1(double Tovn, double prewatt, double maalt)
    {
    	double P_k;
    	double P_s;
    	double R_th = 30;
    	double C_th = 22.5;
    	double A = 0.0025;
    	FILE *out1;				
    	out1=fopen("1output.txt","w");
    	//double Tovn;
    	//double prewatt = 10018.50/1000;
    
    		P_k=(1/R_th)*(maalt-23);
    		P_s=A*0.0000000567*(pow((maalt)+273,4.0)-pow(23+273,4.0));
    		Tovn=maalt+(1/C_th)*(prewatt-P_k-P_s);
    		printf("Tovn: %lf \t prewatt: %lf \t maalt: %lf \n", Tovn, prewatt, maalt);
    		//return 0;
    }
    
    void read(int b)
    {  
    	int j=0;
    	double i=0;
    	double maalt=23;
    	double prewatt=0;
    	double Tovn;
    	char buff[80];
    	double num1, num2, num3;
    	char watt[50];
    	char tempmaalt[50];
    	char taelle[10];
    	FILE *in,*out;				
    	out=fopen("output.txt","w");
    	if((in=fopen("Temp.txt", "r")) == NULL) 
    	{
    		printf("\nError opening file");
    	}
    	while(fgets(buff, sizeof(buff), in))
    	{
    	    if (3 == sscanf(buff, "%lf %lf %lf", &num1, &num2, &num3)) 
    	    {
    	    	sprintf(tempmaalt, "%.1f", num2*100);
    	    	sprintf(watt, "%.1f", num3*b);
    	    	sprintf(taelle, "%.1f", i*0.5);
    	    	j=0;
    	    	while(taelle[j]!='\0')
    			{
    				if(taelle[j]=='.')
    					taelle[j]=',';
    				++j;
    			}	    
    	    	j=0;
    	    	while(tempmaalt[j]!='\0')
    	    	{
    				if(tempmaalt[j]=='.')
    					tempmaalt[j]=',';
    				++j;
    			}	    
    	    	j=0;
    	    	while(watt[j]!='\0')
    			{
    				if(watt[j]=='.')
    					watt[j]=',';
    				++j;
    			}
    	    	
    	    	printf("Tidsstempel[S]: %s \t Power[W]: %s \t Measured[C]: %s \t ", taelle, watt, tempmaalt);
    	        fprintf(out, "Tidsstempel[S]: %s \t Power[W]: %s \t Measured[C]: %s \t ", taelle, watt, tempmaalt);
    	    	Tovn1(Tovn, prewatt, maalt);
    		printf("Simulated[C]: %lf \n", Tovn);
    		fprintf(out, "Simulated[C]: %lf \n", Tovn);
    	    	maalt = Tovn;
    	    	prewatt = num3; 
    	        i++;
    	    }
    	}
    	fclose(in);
    	fclose(out);
    }
    
    void main()
    {
    	int a, b;
    	printf("Skriv hvad du vil \n 1: Køre program \n 2: Hvis du vil ændre Volt konstanten \n");
    	fflush(stdout);
    	scanf("%d", &a);
    	if(a==1){
    		b=15;
    		read(b);
    	}
    	else{
    		printf("Skriv en Volt konstant: ");
    		fflush(stdout);
    		scanf("%d", &b);
    		read(b);
    	}
    }
    But I can't figure out how to make the doubles usable in other functions. Can anyone help me?
    Last edited by trillykins; 11-24-2010 at 11:55 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > //return 0;
    Instead of this, how about

    return Tovn;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You should also read this: Why void main() is bad
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    Quote Originally Posted by Salem View Post
    > //return 0;
    Instead of this, how about

    return Tovn;
    But is the
    Code:
     double Tovn1(double Tovn, double prewatt, double maalt)
    .
    .
    .
    .
    Tovn1(Tovn, prewatt, maalt); and
    correct? and can you only return one array? Cause I was trying to use three of the doubles from Tovn1 and use in the read function.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you want to return multiple values from a function you need to pass in the parameters as pointers. But it seems like the Tovn1() function only changes the variable Tovn, so maybe that's the only thing you need to return. Simple example:
    Code:
    double sum(double x, double y, double z) {
        x += y;
        y += z;
        return x;
    }
    
    /* ... */
    double q = sum(4, 5, 6);
    /* ... */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    Quote Originally Posted by dwks View Post
    If you want to return multiple values from a function you need to pass in the parameters as pointers. But it seems like the Tovn1() function only changes the variable Tovn, so maybe that's the only thing you need to return. Simple example:
    Code:
    double sum(double x, double y, double z) {
        x += y;
        y += z;
        return x;
    }
    
    /* ... */
    double q = sum(4, 5, 6);
    /* ... */
    Would I then be able to get the new x,y,z variables to use in the other function? And what does the q and sum do? Sorry, a little confused :b

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Well to expand a little
    Code:
    	    	Tovn = Tovn1(Tovn, prewatt, maalt);
    		printf("Simulated[C]: %lf \n", Tovn);
    		fprintf(out, "Simulated[C]: %lf \n", Tovn);
    	    	maalt = Tovn;

    You should also remove these lines as well
    FILE *out1;
    out1=fopen("1output.txt","w");
    You neither use the file, nor close it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    Quote Originally Posted by Salem View Post
    Well to expand a little
    Code:
    	    	Tovn = Tovn1(Tovn, prewatt, maalt);
    		printf("Simulated[C]: %lf \n", Tovn);
    		fprintf(out, "Simulated[C]: %lf \n", Tovn);
    	    	maalt = Tovn;

    You should also remove these lines as well
    FILE *out1;
    out1=fopen("1output.txt","w");
    You neither use the file, nor close it.
    Yeah, I know. This was just a "prototype" of what I was trying to do, but I just removed the math function and integrated into the read function, which made everything easier, and now it works!

    Thanks everyone and have switched to int main(void) hehe, I just went with the standard.
    Last edited by trillykins; 11-25-2010 at 03:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in printf line,,,what's wrong?
    By mft_ika in forum C Programming
    Replies: 9
    Last Post: 03-19-2010, 08:46 PM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM