Need Help With Array Function

This is a discussion on Need Help With Array Function within the C++ Programming forums, part of the General Programming Boards category; I've been trying to complete a program to the following specs. Although a lot of it is stumping me, and ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    28

    Need Help With Array Function

    I've been trying to complete a program to the following specs. Although a lot of it is stumping me, and im wondering what im doing that is incorrect.

    Below is basically what needs to be done.







    Code:
    Design and write a program that reads it's data from an input file whos external filename can be entered at run time. All data from the input file should be stored in five parallel arrays. Each line of the input file contains the following weather information:
    
    Month Name
    Total Rainfall
    High Temperature
    Low Temperature
    Average Temperature
    
    Once the file has been opened and the data has been stored in the internal array, the program should print out the data stored in the five parallel arrays. It should also calculate and display the following summary information:
    
    Total Rainfall for the year
    Average monthly rainfall
    Highest and lowest temperatures for the year(and the months they occurred in)
    Average of all monthly temperatures.
    
    What it should look like.
    ------------------------------------------------------------------------------------------------------------------------------------
    
    Program Weather
    ------------------------------------------------------------------------------------------------------------------------------------
    This program will read a data file and store its contents in five parallel arrays. It will calculate and report the average monthly rainfall, total rainfall for the year , average of the monthly temperatures and the highest and lowest temperatures for the year.
    -------------------------------------------------------------------------------------------------------------------------------------
    
    Please enter the name of the input file: weather.dat
    
                         Total                     High                   Low                  Average
    Month           Rainfall                 Temp                 Temp                    Temp
    ---------------------------------------------------------------------------------------------------------------------------------------
    January           2.14                      34                      8                         20.4
    February         1.59                      39                       6                        23.2
    March             2.58                      51                      23                       38.7
    .
    .
    .
    .
    -----------------------------------------------------------------------------------------------------------------------------------------
    Average monthly rainfall :                         XXX.XX
    Total rainfall for year:                               XXX.XX
    Average of monthly temp averages            XXX.XX
    Highest Temperature                               XXX   (month_name)
    Lowest Temperature                                XXX   (month_name)

    This next portion is actually what I have accomplished.

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    void screenHeader();
    void resultsOutput();
    void inputFile();
    
    
    
    int main()
    {
     system("clear");
     float monRain[11], totRain[11], avgmonthTemp[11];
     int hiTemp[100], loTemp[100];
     string monthName[11];
     int lowTemp[11], highTemp[11];
    
     screenHeader();
     inputFile();
     resultsOutput();
    
    
    return 0;
    }
    
    
            void screenHeader()
            {
             cout << "Program Weather" << endl;
             cout << "------------------------------------------------------------" << endl;
             cout << "This program will read a data file and stores its contents"   << endl;
             cout << "in five parallel arrays. It will calculate and report the"    << endl;
             cout << "average monthly rainfall, total rainfall for the year,"       << endl;
             cout << "average of the monthly temperatures, and the highest and"     << endl;
             cout << "lowest temperatures for the year."                            << endl;
             cout << "------------------------------------------------------------" << endl;
            }
    
    
    
            void resultsOutput()
            {
             float monRain[11], totRain[11], avgmonthTemp[11];
             int hiTemp[100], loTemp[100];
             string monthName[11];
    
             cout << "------------------------------------------------------------"       << endl;
             cout << "Average monthly rainfall:          " << monRain                     << endl;
             cout << "Total rainfall for year:           " << totRain                     << endl;
             cout << "Average of monthly temp.averages:  " << avgmonthTemp                << endl;
             cout << "Highest temperature:               " << hiTemp << "  " << monthName << endl;
    cout << "Lowest temperature:                " << loTemp << "  " << monthName << endl;
            }
    
    
    
            void inputFile()
            {
    
             float monRain[11], totRain[11], avgmonthTemp[11];
             int hiTemp[100], loTemp[100];
             string monthName[11];
    
             int i;
             int lowTemp[11], highTemp[11];
             string highTempMo, lowTempMo;
             string fileName[50];
             cout << "Please enter the name of the input file:  " ;
             cin >> fileName;
    
             ifstream infile;
             infile.open(fileName.c_str());
    
             for(i = 0 , i <12 , i++)
              {
                infile      >> monthName[i]
                            >> monRain[i]
                            >> hiTemp[i]
                            >> loTemp[i]
                            >> avgmonthTemp[i]
    
                if(hiTemp[i] > highTemp) {
                    highTemp = hiTemp[i];
                    highTempMo = monthName[i];
                    }
    
                if(loTemp[i] < lowTemp)  {
                    lowTemp = loTemp[i];
                    lowTempMo = monthName[i];
                    }
    
              }
            }


    Any help or insight on how to solve this, or any comments to lead me in the right direction would be great.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    A few things. All of your arrays you make for the months are 11 elements [0 - 10]. You need to make these 12 elements. Also, you use local arrays in your functions. For example, in your output results function you declare the arrays locally and then try to read them. Well they will be filled with garbage. You need to either decalre them in main and then pass them to the functions for use or you could make them global and have the functions use the global arrays. Try to work with this and then write back.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Yeah, I was having troubles with errors. Was not sure If I could pass by reference on arrays or what have you.

    The eleven is just a mistake on my part ... Thinking 0- 11 = 12. However I guess 0 - 11 would be declared as [12]. Im still get a few compile errors.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Any ideas on what exactly is causing my compile errors, Or what should be done to fix this issue.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well, I would work on getting certain parts of the program to work completly, such as File I/O. Just comment out everything unrelated to file i/o until it's working. For instance you have the following three lines:

    Code:
    string fileName[50];
    cout << "Please enter the name of the input file:  " ;
    cin >> fileName;
    Why do you have an array of 50 strings? There is no need for this. Maybe you should re-read the section on arrays in your C/C++ reference because I think some of the concepts are confusing you. I'm not trying to be a jerk here, just a suggestion.

    In your for loop in the file i/o you use , instead of ;

    Your comparisons are wrong, you are trying to compare a pointer to an integer in the before-mentioned for loop.

    Those are your major errors. Work those out and then post what you've changed.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    There is a lot you have to do there. But it's doable.


    1. Store 5x12 numerical pieces of information so each can be accessed
    separately.
    2. The file is probably read in using char, so the numerical data has
    to be converted using 'atoi'.
    3. Print out the tables so each one is tabulated correctly.
    4. Calculate the highest, lowest and average temps or rainfall and
    identify their appropriate month names. Therefore you will need an
    algo which reads in an array and displays the highest or lowest.
    Therefore you need to know how to use functions and pass back
    the values into main.
    5. What would make it slightly harder is if the months in the data file
    were given in random order as opposed to chronological order, i.e
    December=>info, February=>ifo...etc.

    Good Luck!

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Don't know about you but I got this far:

    The rest should be equally as easy and you can make the table
    look better by setting the tabs.

    Code:
    /******************************
    
      A program to demonstrate
      how much free time I have
      
    ********************************/
    
    #include<iostream>
    #include<fstream>
    #include<string.h>
    #include<ctype.h>
    
    int print_table(float[][81]);
    int print_stats(float[][81]);
    
    
    using namespace std;
    
    
    
    int main()
    {
        fstream file_pointer;
        file_pointer.open("forcast.txt",ios::in);
        
        char array[81];
        float hold[81][81];
        
        
        int counter=0;
        int x_counter=0;
        int y_counter=0;
        do
        {
            counter++;
            int check=counter%3;
            file_pointer>>array;
            if(check==0)
            {
              
               if(isdigit(array[0])!=0)
               {   
                   
                  
                   float i;
                   i=atof(array);
                   
                   //cout<<"xcounter "<<x_counter<<endl;
                   //cout<<"ycounter "<<y_counter<<endl;
                   hold[x_counter][y_counter]=i;
                   
                   y_counter++;
                    if(y_counter==4)
                   {
                       y_counter=0;
                       x_counter++;
                   }      
                   
                   
               }  
               
                
                    
               
                
            }
        }while(file_pointer.peek()!=EOF);
        file_pointer.close();
        /*
        for (int a=0; a<12; a++)
        {
            for(int b=0; b<4; b++)
            {
                cout<<hold[a][b]<<endl;
            }
        } 
        */
        
        print_table(hold);
        
               
        
        
        int stop;
        cin>>stop;
    }            
            
    
    int print_table(float hold[81][81])
    {
          cout<<"MONTH    RAINFALL  HIGHTEMP  LOWTEMP  AVETEMP"<<endl;
          for (int a=0; a<12; a++)
        {
            if(a==0)
            {
                cout<<"January ";
            } 
            if(a==1)
            {
                cout<<"February ";
            } 
            if(a==2)
            {
                cout<<"March    ";
            } 
            if(a==3)
            {
                cout<<"April    ";
            } 
            if(a==4)
            {
                cout<<"May      ";
            } 
            if(a==5)
            {
                cout<<"June     ";
            } 
            if(a==6)
            {
                cout<<"July     ";
            } 
            if(a==7)
            {
                cout<<"August   ";
            } 
            if(a==8)
            {
                cout<<"September";
            }  
            if(a==9)
            {
                cout<<"October  ";
            }   
            if(a==10)
            {
                cout<<"November ";
            } 
            if(a==11)
            {
                cout<<"December ";
            } 
            
            for(int b=0; b<4; b++)
            {
                
                cout<<hold[a][b]<<"          ";
            }cout<<""<<endl;
        } 
    }
    Attached Files Attached Files

  8. #8
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Erm here is the completed version.

    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Hmm, Wow thanks a ton.

    I guess the only thing im really confused on thus far, is how can i get it to ask the user for what file input they want?

    Instead of having it already in the program.

  10. #10
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    It's right there:
    Code:
    infile.open(fileName.c_str());
    where fileName is a string and infile is an ifstream.

    The following, however, is wrong:
    Code:
    string fileName[50];
    You don't have to reserve space for strings. They grow as they need to.

  11. #11
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I should probably stop doing ppl's h/w for them as it doesn't help anyone. Lesson learnt. I definitely won't do that again. Sorry.

    Btw, although I can't take my original post back, I hope you at least try to understand what I've done if you intend to use it.

    Sorry again.


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, 05:23 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 01:19 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM
  5. Replies: 3
    Last Post: 03-23-2002, 04:20 PM

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