Thread: How to access different things in two dimensional arrays

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    34

    How to access different things in two dimensional arrays

    So I started taking a C++ class two weeks ago and we just got to arrays. I am confused on how to pull data from two dimensional arrays. The assignment for this topic reads as follows.
    Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:
    a. Function getData: This function reads and stores data in the two dimensional array.
    b. Function averageHigh: This function calculates and returns the average high temperature for the year.
    c. Function averageLow: This function calculates and returns the average low temperature for the year.
    d. Function indexHighTemp: this function returns the index of the highest high temperature in the array.
    e. Function indexLowTemp: this function returns the index of the lowest low temperature in the array.

    I made the getData function. I do know that i am supposed to more then likely use nested loops on each function to iterate through each column. I have been trying to figure this out. I first saw in my book that the array could be variables so naturally I tryed to make the array loop and increment up the column and row each time a item is stored to make it easier to loop in different functions but then i found out that they have to have a constant value and that doesn't work. Here is the code for index high temp that i have been working on. I honestly feel like i'm over thinking this and its easier then what i'm making it out to be. Here is all of the code. I don't necessarily want you guys to give me the answer. Just point me in the right direction so i'm actually learning and not copying.

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    void getData(int month);
    void averageHigh(int month);
    void averageLow(int month);
    void indexHighTemp(int month,int averagehigh);
    void indexLowtemp(int month);
    void monthData(int month);
    
    
    int main()
    {
        
        int month;
        getData(month);
        averageHigh(month);
        averageLow(month);
        indexHighTemp(month);
        indexLowtemp(month);
    
    
    }
    void getData(int month)
    {
        
        
        int month[12][2];
        
        for(int i; i < 13 && i!=0; i++)
        {
        cout << "Enter the high temperature for " << i << endl;
        cin >> month;
        cout << "Enter the low temparature for " << i << endl;
        cin >> month;
        
        switch (i)
        {
        case 1:
            cout << "January" << endl;
              break;
        case 2:
            cout << "February" << endl;
              break;
        case 3:
            cout << "March" << endl;
              break;
        case 4:
            cout << "April" << endl;
              break;
        case 5:
            cout << "May" << endl;
              break;
        case 6:
            cout << "June" << endl;
              break;
        case 7:
            cout << "July" << endl;
              break;
        case 8:
            cout << "August" << endl;
              break;
        case 9:
            cout << "September" << endl;
              break;
        case 10:
            cout << "October" << endl;
            break;
        case 11:
            cout << "November" << endl;
            break;
        case 12:
            cout << "December" << endl;
              break;
            
        }
        
        }
    
    
        
    }
    void indexHighTemp(int month)
    {
        int smallestValue = 100;
        double month_length = month.length;
        for(int i = 0; i < 24; i++)
        {
            if(month
        
       
    }
    
    
    
    
    
    
    
    
    void averageHigh()
    
    
    {
        averagehigh = 
    }

  2. #2
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    Okay here is a update of the GetDate
    Code:
    
    	for(row = 0; row < 12; row++)
    		for(col = 0; col < 2; col++)
    			  int month[row][col] = 0;
    	for(int i; i < 13 && i!=0; i++)
              switch statement
    I got this from the book and it is telling me row and col have to have a constant value. But i don't see how i can finish this program without making row and col a variable. Otherwise im going to be typing out 500 statements since i can't loop. Please help!

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should look at the difference between
    Code:
    int month[row][col] = 0;
    (which makes no sense on two different levels: one because you can't declare an array with variable amounts of indices, and second because you can't assign the value 0 to an array)
    and
    Code:
    month[row][col] = 0;
    which only deals with one single item in your array, specifically the one at (row, col).

    Also the fact that you have written this:
    Code:
    for(int i; i < 13 && i!=0; i++)
    suggests you didn't really get past for-loops yet either. (What values do you think i will take on during this loop? How will it get them?)

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    Quote Originally Posted by tabstop View Post
    You should look at the difference between
    Code:
    int month[row][col] = 0;
    (which makes no sense on two different levels: one because you can't declare an array with variable amounts of indices, and second because you can't assign the value 0 to an array)
    and
    Code:
    month[row][col] = 0;
    which only deals with one single item in your array, specifically the one at (row, col).

    Also the fact that you have written this:
    Code:
    for(int i; i < 13 && i!=0; i++)
    suggests you didn't really get past for-loops yet either. (What values do you think i will take on during this loop? How will it get them?)
    Thanks for the help! honestly i just scrapped the whole program and studied more about arrays and for loops. After i redid the program I got it functioning. after reading some on arrays i saw everything i did wrong. Here is the new complete code.
    Code:
    #include <iostream>
    using namespace std;
    
    
    const int months = 12;
    
    
    void getData( double A[][2], int B);
    double averageHigh(double A[][2], int B);
    double averageLow(double A[][2], int B);
    int indexHighTemp(double A[][2], int B);
    int indexLowTemp( double A[][2], int B);
    
    
    int main()
    {
        double temperatures [months][2];
    
    
        getData(temperatures, months);
    
    
        cout << "\n\n\tThe average high temperature for the year is "
              << averageHigh(temperatures, months);
        cout << "\n\n\tThe average low temperature for the year is "
             << averageLow(temperatures, months);
    
    
        cout << "\n\n\tIndex of the highest temperature for the year is "
             << indexHighTemp(temperatures, months);
        cout << "\n\n\tIndex of the lowest temperature for the year is "
             << indexLowTemp(temperatures, months);
        return 0;
    }
    
    
    void getData(double A[][2], int B)
    {
        for (int i = 0; i < B; i++)
        {
            cout << "\n\tEnter the highest temperature for the month"
                    << (i+1) << ": " ;
             cin >> A[i][1];
            cout << "\n\tEnter the lowest temperature for the month"
                << (i+1) << ": " ;
              cin >> A[i][0];
        
        }
    }
    
    
    double averageHigh(double A[][2], int B)
    {
        double sum = 0;
    
    
        for (int i = 0; i <B; i++)
            sum +=A[i][0];
        return (sum/B);
    }
        
    double averageLow(double A[][2], int B)
    {
        double sum = 0;
    
    
        for( int i = 0; i < B; i++)
            sum +=A[i][1];
        return (sum/B);
    }
    
    
    int indexHighTemp(double A[][2], int B)
    {    
        int index = 0;
    double highest = A[0][0];
    
    
    for(int i =1; i <B; i++)
        if (A[i][0] > highest)
        {
            highest = A[i][0];
            index =i;
        }
        return index;
    }
    
    
    int indexLowTemp(double A[][2], int B)
    {
        int index = 0;
        double lowest = A[0][1];
    
    
        for (int i = 1; i < B; i++)
            if (A[i][1] < lowest)
            {
                lowest = A[i][1];
                index = i;
            }
    
    
            return index;
    }

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    But i need to study more. I hastily read a chapter and me looking dumb ensued.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 dimensional arrays
    By naserian in forum C Programming
    Replies: 5
    Last Post: 03-22-2010, 04:40 PM
  2. Three ONE dimensional arrays
    By victory1 in forum C Programming
    Replies: 5
    Last Post: 11-16-2009, 07:14 AM
  3. two dimensional int array access problems ;/
    By krST in forum C Programming
    Replies: 6
    Last Post: 01-04-2006, 07:02 AM
  4. Replies: 2
    Last Post: 06-13-2002, 11:05 PM
  5. two-dimensional arrays
    By jblea in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2002, 06:30 AM