Question about multidimensional arrays

This is a discussion on Question about multidimensional arrays within the C++ Programming forums, part of the General Programming Boards category; I have to read in data from a file. This file has 93 lines in it. I need to store ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    Question about multidimensional arrays

    I have to read in data from a file. This file has 93 lines in it. I need to store these numbers into a 3-dimensional array. The data file layout is like this:

    Low Hi <---- not in dat file....
    55 80
    56 78
    58 84
    50 81
    36 66
    46 76
    52 74
    50 65
    ... ...
    I would like to know why the code below does not print out 2 integers.
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    const int months=3;
    const int days=31;
    const int hiLo=2;
    
    ifstream infile;
    
    int main()
    {
    
    int selNum=0;
    int numbers[months][days][hiLo];
    
    infile.open("weather.dat", ios::nocreate);
    	if(!infile)
    	{
    		cout << "error" << endl;
    		exit(1);
    	}
    	for(int i=0;i<months;i++)
    		for(int j=0;j<days;j++)
    			for(int k=0;k<hiLo;k++)
    {
    	infile >> numbers[i][j][k];
    }
    			
    	cout << endl << endl << "check this out\n";//display low & high value temp
    	for(i=0;i<months;i++)
    	for(int j=0;j<days;j++)
    	for(int k=0;k<hiLo;k++)
    {
    	//cout << numbers[i][j][k]
    	//<< endl;
    }
    
    	selNum=numbers[3-1][2-1][hiLo];
    	cout << endl << selNum << endl;//             wrong	
    	return 0;
    }
    Any help would be greatly appreciated
    Last edited by kippwinger; 06-22-2003 at 08:11 AM.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Please look above for revised post

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Okay, I made the changes you suggested, but it only prints one number. I wanted to know how to print out two numbers. Here is the layout and data in the weather.dat file.

    55 71
    54 74
    52 74
    50 65
    32 66
    46 76
    52 74
    50 65
    32 66
    55 80
    56 78
    58 84
    50 81
    32 66
    46 76
    52 74
    .. ..

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    thanks, btw

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    Here is the last part of my program I am stuck on.

    I need to create a function that will calculate and display the highest temperature, month and day. But the thing is, is that in the file the highest temperature is 101 and there are several days that have that same temp. I need to display them all.
    Please help.

    Here is the code I have so far:
    Code:
    void case4(int numbers[][days][hiLo])//displays the highest temp
    {
    	int high=0,highestMon=0;
    	int highestDay=0;
    for(int i=0;i<months;i++)
    		for(int j=0;j<days;j++)
    		if(numbers[i][j][1]>high)	
    		{
    			high=numbers[i][j][1];
    			highestMon=i;
    			highestDay=j;
    			}
    		if(highestMon==0)
    		cout << "March";
    		if(highestMon==1)
    			cout << "July";
    		if(highestMon==2)
    			cout << "October";
    
    
    	cout << highestMon << " " << highestDay << " has the highest temperature.\n" << endl;
    }

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    hate to be a pest, but how might I do that?

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    declare maxTemperature = a very low number
    
    for each element in the array
    	if the current element's temperature is > maxTemperature
    		maxTemperature = current element's temperature
    	end if
    end for
    
    for each element in the array
    	if the current element's temperature is == maxTemperature
    		print out the element's data
    	end if
    end for
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Okay, here is what I have so far:

    Code:
    void case4(int numbers[][days][hiLo])//displays the highest temp
    {
    	int high=0,highestMon=0;
    	int highestDay=0;
    	
    
    for(int i=0;i<months;i++)
    		for(int j=0;j<days;j++)
    		if(numbers[i][j][1]>high)	
    		{
    			high=numbers[i][j][1];
    			highestMon=i;
    			highestDay=j;	
    		}
    		for(i=0;i<months;i++)
    			for(int j=0;j<days;j++)
    				if(numbers[i][j][1]==high)
    				//Not sure what to put here!
    
    		if(highestMon==0)
    		cout << "\nMarch "<< highestDay << " has the highest temperature.\n" << endl;
    		if(highestMon==1)
    			cout << "\nJuly "<< highestDay << " has the highest temperature.\n" << endl;
    		if(highestMon==2)
    			cout << "\nOctober "<< highestDay << " has the highest temperature.\n" << endl;
    
    	
    }
    I marked the area in which I am stuck. Should I store the values into a 2-D array? That seems to be the only thing I can think of.. Is there an easier way?

    thanks

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    I figured it out, thanks for our help.

    here is the code in case you are interested:

    Code:
    void case4(int numbers[][days][hiLo])//displays the highest temp
    {
    	int high=0,highestMon=0;
    	int highestDay=0;
    	
    
    for(int i=0;i<months;i++)
    		for(int j=0;j<days;j++)
    		if(numbers[i][j][1]>high)	
    		{
    			high=numbers[i][j][1];
    			highestMon=i;
    			highestDay=j+1;	
    		}
    		for(i=0;i<months;i++)
    			for(int j=0;j<days;j++)
    				if(numbers[i][j][1]==high)
    				
    				{
    					high=numbers[i][j][1];
    				highestMon=i;
    				highestDay=j;
    				
    		if(highestMon==0)
    			cout << "\nMarch "<< highestDay << " has the highest temperature.\n" << endl;
    		if(highestMon==1)
    			cout << "\nJuly "<< highestDay << " has the highest temperature.\n" << endl;
    		if(highestMon==2)
    			cout << "\nOctober "<< highestDay << " has the highest temperature.\n" << endl;
    				}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. stupid c question re arrays
    By C. Unger in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 08:38 PM

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