accessing a two dimensional array

This is a discussion on accessing a two dimensional array within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { float array[12]; char months[12] [10] = {"January", "February", "March", "April", "May", ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    accessing a two dimensional array

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	float array[12];
    	
    	char  months[12] [10] = {"January", 
    		                     "February", 
    		                     "March", 
    		                     "April",
    		                     "May",
    		                     "June", 
    		                     "July", 
    		                     "August",
    		                     "September", 
    		                     "October", 
    		                     "November", 
    		                     "December"};			 
    		              
    		
    	int   sum = 0;
    
    	int   largest = 0;
    	int   smallest = INT_MAX;
    
    	cout << "Enter the total rainfall for each month" << endl;
    
    	for (int i = 0; i < 12; i++)
    	{
    		cout << months[i] << " : "; 
    		cin  >> array[i];
    
    		while (cin.fail() || array[i] < 0)
    		{
    
    			cin.clear();		
    			cin.ignore(INT_MAX, '\n');
    			
    			cout << "\nOnly enter positive numbers" << endl;
    
    			cout << months[i] << " : ";
    			cin  >> array[i];
    		}
    		
    		if (array[i] > largest)
    			largest = array[i];
    
    
    		if (array[i] < smallest)
    			smallest = array[i];
    
    		sum += array[i];
    
    	}
    
    	cout << "\nThe total rainfall for the year is " << sum << endl;
    	cout << "\nThe average monthly rainfall is " << sum/12 << endl;
    	
    	cout << "\nThe month with the highest rainfall is "
    	cout << "\nThe month with the smallest rainfall is " 
    
    	return 0;
    }
    I'm having trouble with the last two cout statements. I don't know what to put at the end of those statements.
    Last edited by volk; 04-05-2003 at 04:36 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    According to your topic, I assume that your problem is to access the right element rather than find the highest/smallest rainfall.
    Well, you simply type months[i] where i is the month (0 - 11) (*surprise surprise*).
    Code:
    int max = FindHighestRainfall();
    int min = FindSmallestRainfall();
    
    cout << "\nThe month with the highest rainfall is " << months[max] << endl;
    cout << "\nThe month with the smallest rainfall is " << months[min] << endl;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Magos

    According to your topic, I assume that your problem is to access the right element rather than find the highest/smallest rainfall.
    Well, you simply type months[i] where i is the month (0 - 11) (*surprise surprise*).
    It's really the same thing: to find the highest/smallest rainfall is to access the right element.

    Anyway, I didn't understand your explanation on how to find the right element. You stored functions to find the highest/smallest rainfall in variables.

    How would the definition of those functions look? Isn't there an easier way to do this without functions?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, finding the largest element in an array is piece of cake. First have a variable keeping the currently highest value. Loop through all elements and if a new highest value is found update the variable. To save some speed (who will notice a few ns anyway? ) you can implement this in the same loop when you read the values, but I leave that to you.
    Code:
    int FindHighestRainfall()
    {
       int CurrentMax = Array[0];
    
       for(int i=1; i<12; i++)
       {
          if(Array[i] > CurrentMax) CurrentMax = Array[i];
       }
    
       return CurrentMax;
    }
    Almost the same with FindSmallestRainfall(), just one modification. Guess what .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Unhappy

    I wanted to find the largest and smallest values so I can find out which months contain those values.

    I know how to find the largest and smallest values, but those functions are confusing me. Isn't there a way to do this without functions?

    I just want those last two cout statemtents to print out the month with the largest value and the month with the smallest value (not the actual values).

  6. #6
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Look at this:
    Code:
    string months[12] = a bunch of crap
    float rain[12];
    
    int i;
    
    float highestRain = 0;
    int highSpot;
    float lowestRain = 0;
    int lowSpot;
    
    for(i = 0; i < 12; i++)
    {
      cout << "Enter rain for month " << months[i];
    
    cin >> rain[i];
    
    if(rain[i] > highestRain)
    {
     highestRain = rain[i];
     highSpot = i;
    }
    
    if(rain[i] < lowestRain)
    {
     lowestRain = rain[i];
     lowSpot = i;
    }
    
    // other crap
    The rain spot will be the address of the array, so to display the highest:

    Code:
    int highSpot;
    
    ...
    
    cout << rain[highSpot];
    Hope that helps, show your effort at least!
    If you ever need a hug, just ask.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by volk
    I wanted to find the largest and smallest values so I can find out which months contain those values.
    And that is what that code do...

    Originally posted by volk
    I know how to find the largest and smallest values, but those functions are confusing me. Isn't there a way to do this without functions?
    Not using functions you say? Why wouldn't you want to use them? They make the code clearer, better structured, easier to read, easier to maintain, easier to get a nice overlook, prevents you from writing the same code over and over etc... etc... I don't see any reason why you shouldn't use them.
    If the reason is that you're not familiar with them, then this is a nice oportunity (sp?) to be.

    Confused by that code? It's as simple as it gets.

    Originally posted by volk
    I just want those last two cout statemtents to print out the month with the largest value and the month with the smallest value (not the actual values).
    They do print the months. Haven't you tested the code?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Two Dimensional Array Input from Formatted Data
    By teedoff087 in forum C Programming
    Replies: 14
    Last Post: 04-29-2007, 01:46 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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