moving an array from function to function

This is a discussion on moving an array from function to function within the C++ Programming forums, part of the General Programming Boards category; Alright so here's my problem. I am doing this assignment where we read in the temperatures from every day of ...

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Exclamation moving an array from function to function

    Alright so here's my problem. I am doing this assignment where we read in the temperatures from every day of the month. I have sorted out most of the stuff we have to do but one part we have to make a function that will sort the values from smallest to largest. I somehow need to transfer my array from one function into this other function.

    Here is my code so far...Sorry about there not being comments..haven't gotten around to them yet.

    What I want to do is transfer the daily_temp array to the BubbleSort Function (which has not been created yet):

    Code:
    #include <iostream>
    using namespace std;
    
    int getMonth();
    int getYear();
    void getTemps(int month, const int monthday[]);
    
    int main()
    {
    	char station[51];
    	int month, year;
    	const monthday[13] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, '\0'};
    
    	cout << "Please enter station name: ";
    	cin.getline (station, 51, '\n');  
    	
    	month = getMonth();
    	year = getYear();
    	getTemps(month, monthday);
    
    	cout << station;
    	cout << month;
    	cout << year;
    
    	return 0;
    }	
    int getMonth()
    {
    	int month_num;
    
    	cout << "Please enter the month number: ";
    	cin >> month_num;
    
    	return month_num;
    }
    int getYear()
    {
    	int year_num;
    
    	cout << "Please enter the year number: ";
    	cin >> year_num;
    
    	return year_num;
    }
    void getTemps(int month, const int monthday[])
    { 
    	int n, day_num = 1, pass_num = 0;
    	double daily_temp[31];
    	double total_temps = 0, avg_temp = 0, days;
    
    	days = monthday[(month-1)];
    
    	for ( n=0; n < days; n++)
    	{
    		cout << "Please enter day " << day_num << "'s highest temperature: ";
    		cin >> daily_temp[n];
    		total_temps += daily_temp[n];	
    		
    		day_num++;
    	}
    }
    Thanks for any help you can give,

    ...Dan
    Last edited by Dan17; 11-23-2005 at 06:48 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    The simplest method is to copy the daily_temp declaration and use it as a function argument. This should work fine because the size of the array is always 31. Notice that you already have an example of passing an array to a function: monthday.

    You might want to beware of months with less than 31 days. Since your array has 31 slots, your sort will need to know whether the last entry is valid (for a 31 day month) or invalid (for a 30 day month or February). Same is true for the 29th and 30th slots if the month is February. In your current code, you leave unused slots uninitialized, meaning they can have any value. One solution is to pass the number of valid days to the sort function, another is to use a sentinel to indicate the end of valid data (in this case that might be difficult since there are many valid temperatures, including 0 and -1).

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What I want to do is transfer the daily_temp array to the BubbleSort Function (which has not been created yet)
    The problem is that you are creating the daily_temp[] array inside your GetTemps function. All local variable names declared in a function get destroyed and no longer exist when the function finishes executing. So after GetTemps() executes, there is no daily_temp[] array anymore.

    You can do two things:

    1) Create the array in main() and pass the array and its size to your GetTemps() function. That way GetTemps() can fill up the array. There's no need to return the array back to main() because arrays are automatically passed-by-reference.

    2) Inside GetTemps(), dynamically create the array with new. When you dynamically allocate memory, it stays in existence until you use delete. The memory will not be affected by a function ending.
    Last edited by 7stud; 11-23-2005 at 02:59 PM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by 7stud
    2) Inside GetTemps(), dynamically create the array with new. When you dynamically allocate memory, it stays in existence until you use delete. The memory will not be affected by a function ending.
    if you do this, make sure you keep a pointer to the memory...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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