Thread: Need Help with pointers and arrays

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

    Exclamation Need Help with pointers and arrays

    Alright so I'm making this program and it has to do with inputting temperatures for the month. I am using a array (obviously) to store the values. The problem i run into is this:

    In main() I make all the values of the array equal to 0.
    Then I call the array using the function getTemps.
    This gives values to all the spots in the array.
    I have to use a separate function to sort them.
    The problem is that I when I call the array...I get the values from main() which are 0's.

    How can I ge the values from the getTemps function? I'm pretty sure I need to use pointers, but I'm not sure how to use them with arrays so that I can bring all the values at one time.

    Please give me an example...Thanks in advance,

    ...Dan

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Post your code. In your last thread, you didn't have any code setting values to 0 in main.

    If you pass an array to the GetTemps function to fill the data, then that array will be filled (arrays are effectively passed by reference, meaning any changes you make to the data in the array in a function will be reflected in the original array in main).

    Without seeing the code, my guess is that you didn't remove the local daily_temp array variable in GetTemps, and so you are modifying that instead of the one passed in from main.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    we'll need so see some code, but just a little nitpicking first:
    1. You're not really "calling" the array. That term is more used for methods. You're "accessing" the array.
    2. when you use an index in an array, you're already using pointers, you just dont' realize it
    3. when you send something to a function as an argument, you're again using pointers that you probably dont' realize.
    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

  4. #4
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks for the quick replies

    I'm pretty sure there is a way to set all the values of my daily_temp array to 0 without making that FOR loop...any help on that would also be appreciated

    Here's the code...not fully complete yet but enough:
    Code:
    #include <iostream>
    using namespace std;
    
    int getMonth();
    int getYear();
    int getTemps(int month, const int monthday[], double daily_temp[]);
    void BubbleSort(double[], int array_lengh, int passes, int day_num);
    void largestValues(double daily_temp[31], int days);
    void smallestValues(double daily_temp[31]);
    
    int main()
    {
    	char station[51];
    	int month = 0, year, day_num, passes;
    	const monthday[13] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, '\0'};
    	double daily_temp[31];
    	
    	for(int a = 0; a < 31; a++)
    	{
    		daily_temp[a] = 0;
    	}
    
    	cout << "Please enter station name: ";
    	cin.getline (station, 51, '\n');  
    	
    	day_num = monthday[(month-1)];
    
    	month = getMonth();
    	year = getYear();
    	passes = getTemps(month, monthday, daily_temp);
    	BubbleSort(daily_temp, 31, passes, day_num);
    	largestValues(daily_temp, day_num);
    	smallestValues(daily_temp);
    
    	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;
    }
    int getTemps(int month, const int monthday[], double daily_temp[])
    { 
    	int n, day_num = 1, pass_num = 0;
    	double total_temps = 0, avg_temp = 0, days;
    
    	days = monthday[(month-1)];
    
    	for ( n=0; n < 5; n++)
    	{
    		cout << "Please enter day " << day_num << "'s highest temperature: ";
    		cin >> daily_temp[n];
    		total_temps += daily_temp[n];	
    		
    		day_num++;
    		pass_num++;
    	}
    
    return pass_num;
    }
    void BubbleSort(double daily_temp[31], int, int, int)
    {
    	double temp_move;
    	int s = 35;
    
    	for(int b = 0; b < s; b++)
    	{
    		for(int u = 0; u < s-b; u++)
    		{
    			if (daily_temp[u] > daily_temp[u+1])
    			{
    				temp_move = daily_temp[u+1];
    				daily_temp[u+1] = daily_temp[u];
    				daily_temp[u] = temp_move;
    			}
    		}
    	}
    }
    void largestValues(double daily_temp[31], int day_num)
    {
    	cout << "Largest values are: " << daily_temp[day_num] << " and " << daily_temp[day_num-1];
    }
    void smallestValues(double daily_temp[31])
    {
    	cout << "Smallest values are: " << daily_temp[0] << " and " << daily_temp[1];
    }
    Thanks for the help,

    ...Dan
    Last edited by Dan17; 11-23-2005 at 01:05 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    daily_temp[31] = {0};

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The problem is that I when I call the array...I get the values from main() which are 0's.

    How can I ge the values from the getTemps function? I'm pretty sure I need to use pointers, but I'm not sure how to use them with arrays so that I can bring all the values at one time.
    Arrays are automatically passed-by-reference which means if you create an array in main() and pass it to a function, then the function can make changes to the original array back in main(). You do not have to use pointer notation with arrays to pass-by-reference. You can use array notation as you are doing here:
    Code:
    int getTemps(int month, const int monthday[], double daily_temp[])
    Your function changes the daily_temp array just fine. Here is your stripped down code:
    Code:
    #include<iostream>
    using namespace std;
    
    int getMonth()
    {
    	int month_num;
    
    	cout << "Please enter the month number: ";
    	cin >> month_num;
    
    	return month_num;
    }
    
    int getTemps(int month, const int monthday[], double daily_temp[])
    { 
    	int pass_num = 0;
    	int day_num = 1;
    
    	for (int n=0; n < 5; n++)
    	{
    		cout << "Please enter day " << day_num << "'s highest temperature: ";
    		cin >> daily_temp[n];
    				
    		day_num++;
    		pass_num++;
    	}
    
    	return pass_num;
    }
    
    int main()
    {
    	int month = 0;
    		
    	const monthday[13] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, '\0'};
    	double daily_temp[31] = {0};
    	
    	month = getMonth();
    	
    	int passes = getTemps(month, monthday, daily_temp);
    
    	//Did getTemps() change daily_temp[31]?
    	for(int i = 0; i<31; i++)
    	{
    		cout<<daily_temp[i]<<" ";
    	}
    
    	cout<<endl;
    	
    
    	return 0;
    }
    Note: generally you need to pass the size of the array to the function as well so the function knows where the end of the array is and does not try to access array indexes that are out of bounds.
    Last edited by 7stud; 11-23-2005 at 04:38 PM.

  7. #7
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks for your reply

    That really helps, thanks for reworking it.

    ...Dan
    Last edited by Dan17; 11-23-2005 at 04:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM