A multi-dimension array question

This is a discussion on A multi-dimension array question within the C++ Programming forums, part of the General Programming Boards category; Hi, I did the following code. It can be run except one point that I do not know how to ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    A multi-dimension array question

    Hi,

    I did the following code. It can be run except one point that I do not know how to handle it.

    How can I get the total of temperature for EACH day? I got the figure to be 30 for these 5 days.

    Thanks for help.

    gogo

    #include <iostream>
    #include <string>
    #include <cctype>
    #include <iomanip>

    using namespace std;

    int main()
    {
    const int x = 5;
    const int y = 3;
    const string week[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

    float day[x][y] = {0};
    float average[y] = {0};

    float sum = 0;
    float sum1 =0;
    int count = 0;

    for (int i=0; i<x; i++)
    {
    cout << endl;
    for (int j=0; j<y; j++)
    {
    cout << "Pls enter " << week[i] << "\'s temperature ";
    cout << "with reading #" << (j+1) << " :";
    cin >> day[i][j];
    }
    }
    cout << endl;

    for (int a=0; a<y; a++)
    for (int b=0; b<x; b++)

    average[a] += day[b][a];

    cout << "Average morning temp is " << average[0]/x << "." << endl;
    cout << "Average afternoon temp is " << average[1]/x << "." << endl;
    cout << "Average evening temp is " << average[2]/x << "." << endl;

    cout << endl;

    for (int e=0; e<x; e++)
    {
    cout << week[e] << endl;
    for (int f=0; f<y; f++)
    {
    cout << "reading # " << (f+1) << "is : " << day[e][f] << endl;
    count = count++;
    sum += day[e][f];
    sum1 += day[0][f]; //Don't understand??
    }
    cout << endl;
    }

    cout << "Total 1-5 = " << sum << endl << endl;

    for (int g=0; g<x; g++)
    cout << week[g] << "\'s total is " << sum1 << endl << endl;

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Hi,

    I fixed the problem. Thanks.

    gogo

    #include <iostream>
    #include <string>
    #include <cctype>
    #include <iomanip>

    using namespace std;

    int main()
    {
    const int x = 5;
    const int y = 3;
    const string week[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

    float day[x][y] = {0};
    float average[y] = {0};

    float sum = 0;
    float sum1[x] = {0};
    int count = 0;

    for (int i=0; i<x; i++)
    {
    cout << endl;
    for (int j=0; j<y; j++)
    {
    cout << "Pls enter " << week[i] << "\'s temperature ";
    cout << "with reading #" << (j+1) << " :";
    cin >> day[i][j];
    }
    }
    cout << endl;

    for (int a=0; a<y; a++)
    for (int b=0; b<x; b++)

    average[a] += day[b][a];

    cout << "Average morning temp is " << average[0]/x << "." << endl;
    cout << "Average morning temp is " << average[1]/x << "." << endl;
    cout << "Average morning temp is " << average[2]/x << "." << endl;

    cout << endl;

    for (int e=0; e<x; e++)
    {
    cout << week[e] << endl;
    for (int f=0; f<y; f++)
    {
    cout << "reading # " << (f+1) << "is : " << day[e][f] << endl;
    count = count++;
    sum += day[e][f];
    //sum1 += day[0][f];
    }
    //sum1 += day[e][f];
    cout << endl;
    }

    cout << "Total Mon-Fri = " << sum << endl << endl;

    cout << "Average temperature is " << sum / count << endl << endl;

    for (int h=0; h<x; h++)
    {
    for (int v=0; v<y; v++)
    {
    sum1[h] += day[h][v];
    }
    cout << week[h] << "\'s total per day temp is : " << sum1[h] << endl;
    }

    cout << endl;

    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 03:58 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  4. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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