Passing variables to a subroutine

This is a discussion on Passing variables to a subroutine within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm trying to write a program that involves passing out some variables to a sub-routine to calculate some ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Manchester, UK
    Posts
    3

    Passing variables to a subroutine

    Hi everyone,

    I'm trying to write a program that involves passing out some variables to a sub-routine to calculate some other variables. Here is the code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (int argc, const char * argv[]) {
    
    	double T;
    	double r[7][4], k[7][4], A[7] = { 1, 2, 3, 4, 5, 6, 7}, Ea[7] = { 10, 20, 30, 40, 50, 60, 70}, K[2][4], Ad[2], Ead[2];
    
    	void rateConstants(double k[7][4], double A[7], double Ea[7], double T);
    
            T = 443.15;	
    
    	rateConstants(k, A, Ea, T);
    	
    	printf("\n %lf \n", k[1]);
    	
        return 0;
    }
    
    void rateConstants(double k[7][4], double A[7], double Ea[7], double T)
    {
    	int i, j;
    		  
    	j = 0;
    	
    	for (i=0; i<6; i++)
    	{
    		k[i][j] = A[i]*exp(-Ea[i]/(8.31*T));
    		
    		printf("\n %lf", k[i][j]);  
    			  
    	}
    
    }
    As you can see, the subroutine prints out each value of k[i][j] that it calculates, then I have the main routine print out one of the k values to check if it's stored.However, the readout I actually get is:

    0.997288
    1.989167
    2.975660
    3.956787
    4.932572
    5.903034
    -1.997117
    logout

    It doesn't matter what k value I choose, they all come out as -1.997117. Can anyone see what I'm doing wrong? I wrote a similar program for a much simpler case (as a test), and it all seemed to work.

    I should warn you that I am not a brilliant programmer, so you may have to explain things to me quite explicitly.

    Thanks in advance!

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    	printf("\n %lf \n", k[1]);
    k[1] is not a double value

    should be
    Code:
    	printf("\n %lf \n", k[1][0]);
    or
    Code:
    	printf("\n %lf \n", *k[1]);
    if you wish

    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Manchester, UK
    Posts
    3
    Thank you very much! It's the first option that I want - I really should have noticed that.

    Also, is there an easier way to operate on/print out an array than the loop I have in the subroutine?

    Sam

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by shaig View Post
    is there an easier way to operate on/print out an array than the loop I have in the subroutine?
    I'd say that any operation on an array involves some kind of loop.
    Kurt

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    Manchester, UK
    Posts
    3
    OK, thanks for the help!

    Sam

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    When printing out doubles with printf(), %f should be used, not %lf. In the older (and generally currently implemented) C standard, %lf in printf() is not defined. While a lot of C libraries support %lf to mean %f, there's no guarantee, and it's certainly possible that you might run across an implementation that doesn't know what to do with %lf.

    %lf should still be used for doubles with scanf(), though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  2. Passing multiple variables using function
    By m3rk in forum C++ Programming
    Replies: 12
    Last Post: 01-24-2009, 02:11 PM
  3. C# in ASP.Net - Passing complex variables
    By Llam4 in forum C# Programming
    Replies: 3
    Last Post: 01-19-2008, 01:53 AM
  4. Passing variables to system() function?
    By carter192 in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 08:44 PM
  5. question on passing variables
    By dirgni in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2002, 03:36 PM

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