Finding the mean of one row of a 2D array

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3436 Views - Last Post: 03 August 2012 - 05:41 AM Rate Topic: -----

#1 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Finding the mean of one row of a 2D array

Posted 01 August 2012 - 09:26 PM

Hello everyone. I am having another small problem. I feel as though I am really close to figuring it out though. I am trying to find the mean of a row the user chooses from a 2D array. Let m be the length of a row. Let n be the length of a column. Let array[i][j] be a matrix the user created with rows of length m and columns of length n. Let array[i][j] also have numbers populated in it. How would I go about finding the mean of one row if the user is asked to choose which one? Here is my attempt?

      float Mean = 0;
            float sum = 0;
            int j;
            int i = 0;

            cout << "Hello. Lets take the Mean,Median, and Range of a row. Which row would you like to use?\n\n";
            cin >> j;
            cout << "\n\n";
            for (int j = 0; j < m; j++)
            {
            sum += array[j][i];
            }
            Mean = sum / n;
            cout << "The Mean is " << Mean << ".\n\n";
            sum = 0;

This post has been edited by snesnerd: 01 August 2012 - 09:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding the mean of one row of a 2D array

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: Finding the mean of one row of a 2D array

Posted 01 August 2012 - 10:38 PM

You just got the row that you want to iterate over on line 7 into the j variable, and then on line 9, you overwrite that value of j and make j range over 0..m-1. Shouldn't i be the one ranging over 0..n?
Was This Post Helpful? 0
  • +
  • -

#3 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: Finding the mean of one row of a 2D array

Posted 01 August 2012 - 11:14 PM

What about something like this. Although the problem with this is that if row and column lengths are not the same I get a very bizarre answer.

float Mean = 0;
            float sum = 0;
            int j;

            cout << "Hello. Lets take the Mean,Median, and Range of a row. Which row would you like to use?\n\n";
            cin >> j;
            cout << "\n\n";

            for (int i = 0; i < n; i++)
            {
                sum += array[j-1][i];
            }

            Mean = sum / n;
            cout << "The Mean of row " << j << " = " << Mean << ".\n\n";
            sum = 0;

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: Finding the mean of one row of a 2D array

Posted 01 August 2012 - 11:50 PM

Why would you get very odd answers?

Mean = sum of numbers in the row / number of elements in the row.

It looks like you are implementing just that with lines 9-14.

Am I missing something?

If you had data that looked like:
3 4 5
6 7 8



And you got the average of the first row, aren't you getting 4?

This post has been edited by Skydiver: 01 August 2012 - 11:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: Finding the mean of one row of a 2D array

Posted 01 August 2012 - 11:57 PM

lol Here take a look for yourself. Copy and paste this code into a compiler. I use codeblocks. Actually another problem I found is that if you take the average of a line, then you choose to take the average of another line, the second average is off. If you get an error I may have missed a brace or something small when copying and pasting. Look familiar? I wanted to expand on this program.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int m,n; // Our variable for row length and column length.
    while (true) // The start of the infinite loop.
    {
        int array[26][26];
        cout << "1 - Create a new array.\n"; // Menu
        cout << "2 - Reset the array.\n";
        cout << "3 - Populate the array.\n";
        cout << "4 - Display array.\n";
        cout << "5 - Mean of a chosen row.\n\n";

        int option;
        cout << "Hello. We are going to create an array. Above you will see options to \nimplement for the array. To choose an option please type the number in \nnext to the number you would like to perform.\n\n";
        cin >> option;
        cout << "\n\n";

        if (option == 1) // Choice 1
        {
            cout << "Lets create a new array. The dimensions can be no more then 25 for a \ngiven row or column.\n\n";

            while (true) // We want the user to choose a number that is at most 25. We trap them in an infinite loop until they do so.
            {
                cout << "What would you like the length of the rows be?\n\n";
                cin >> m;
                cout << "\n\n"; // This will appear a lot in the program. It just spaces lines out.

                if (m > 25) // The case if the user tries to pick a number bigger then 25.
                {
                    cout << "That is to big. Please choose a length that is no more then 25.\n\n";
                }

                if (m <= 25) // The case if the user picks a number that is at most 25.
                {
                    break; // The infinite loop breaks when the user listens.
                }
            }

            while (true) // Another infinite loop to force the user to choose a number that is at most 25.
            {
                cout << "What would you like the length of the columns be?\n\n";
                cin >> n;
                cout << "\n\n";

                if (n > 25) // The case if the user tries to pick a number greater then 25.
                {
                    cout << "That is to big. Please choose a length that is no more then 25.\n\n";
                }

                if (n <= 25) // The case if the user listens and chooses a number that is at most 25.
                {
                    break; // The infinite loop breaks when the user listens.
                }
            }

            for (int i = 0; i < n; i++) // Builds up the column lengths.
            {
                for (int j = 0; j < m; j++) // Builds up the row lengths.
                {
                    spreadsheet[i][j] = 0; // Puts a zero in every slot of the matrix.
                    cout << array[i][j] << "   "; // Outputs the matrix with the users dimensions.
                }

                cout << endl;
                cout << "\n";
            }
                system("pause"); // This simply stops the program until the user hits a key.
        }

        if (option == 2) // Choice 1.
        {
            int choice; // The variable that will represent the choice of the user.
            cout << "Are you sure you want to reset the array?\n\n";
            cout << "Type 1 for yes and 2 for no.\n\n";
            cin >> choice;
            cout << "\n\n";

            if (choice == 1) // If the user is sure they want to reset the array this case will occur.
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)// The matrix is built up.
                    {
                        array[i][j] = 0; // Each slot in the matrix is replaced with a zero.
                        cout << array[i][j] << "   "; // Then the matrix is outputted.
                    }

                    cout << endl;
                    cout << "\n";
                }
                    cout << "The array has been resetted to all zeros.\n\n";
                    system("pause");
            }

            if (choice == 2) // If the user decideds not to reset the matrix.
            {
                system("pause"); // The program will do nothing and just wait until the user hits a key.
            }
        }

        if (option == 3) // Choice 3.
        {
                srand((unsigned)time(0)); // We need this for line 122 to work right.

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++) // The matrix is generated.
                    {
                        array[i][j] = rand()% 100; // The matrix is filled with random integers between 0 and 100.
                        cout << array[i][j] << "   "; // The matrix is then outputted.
                    }

                    cout << endl;
                    cout << "\n";
                }
                    system("pause");
        }


        if (option == 4) // Choice 4.
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++) // The matrix is generated.
                {
                    cout << array[i][j] << "   "; // The matrix is outputted.
                }

                cout << endl;
                cout << "\n";
            }
                system("pause");
        }

        if (option == 5)
        {
            float Mean = 0;
            float sum = 0;
            int j;

            cout << "Hello. Lets take the Mean of a row. Which row would you like to use?\n\n";
            cin >> j;
            cout << "\n\n";

            for (int i = 0; i < n; i++)
            {
                sum += array[j-1][i];
            }

            Mean = sum / n;
            cout << "The Mean of row " << j << " = " << Mean << ".\n";
            sum = 0;
        }
     }
return 0;
}


This post has been edited by snesnerd: 02 August 2012 - 12:00 AM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 12:21 AM

That's what is confusing... the convention is if you have variables m, and n associated with a an array, m corresponds to the number of rows, and n corresponds to the number of columns, so that you have an m by n array. A similar convention applies to i and j, where i typically is for rows, and j is for columns. The way it's typically accessed as an array is to match up with math conventions: array[row][column]

From you original post it wasn't obvious that you had defied convention and have n rows, and m columns. But that would explain why you also originally ranged from 0..m.

This post has been edited by Skydiver: 02 August 2012 - 12:28 AM

Was This Post Helpful? 0
  • +
  • -

#7 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 06:04 AM

Well I did have rows be m and columns be n. Now i and j are reversed where in my case i is associated with the column. Is that really the problem as to why the mean comes out weird sometimes? Where am I making my mistake at then?
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,861
  • Joined: 25-December 09

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 06:14 AM

Why are you calling row and col by different names in different locations. Pick one variable name to use as your index variable and use it everywhere when you refer to these indexes. Also you should try to break your one large function into several smaller functions. Try to avoid using "magic numbers" (int array[26][26]).

Also I see srand() is inside your loop, srand() should be called once and only once.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 08:55 AM

Thanks for the help everyone especially skydiver. My variables were mixed up and after I fixed them I was able to get the Mean of a single row. I hate to keep asking questions but your knowledge is just so helpful. Now I just want to calculate the Median and Range of a single row the user chooses. I have been messing around with the Median and have obtained what you see below. It does give me a number but the wrong one. And then as for Range, I have no clue how to pick out the biggest and smallest number within a row the user chooses to find the range.

float Median;

            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < j; k++)
                {
                    if (array[i][j] < array[j][k])
                    {
                        int temp = array[i][j];
                        array[i][j] = array[j][k];
                        array[j][k] = temp;
                    }
                }
            }

                if (n % 2 == 0)
                {
                    for (int j = 0; j < n; j++)
                    Median = ((array[i-1][j]/2) + (array[i-1][j]/2)-1)/2;
                    cout << "The Median of row " << i << " = " << Median << ".\n";

                }

                if (n % 2 != 0)
                {
                    for (int j = 0; j < n; j++)
                    Median = array[i-1][j]/n;
                    cout << "The Median of row " << i << " = " << Median << ".\n";
                }

This post has been edited by snesnerd: 02 August 2012 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,861
  • Joined: 25-December 09

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 09:18 AM

Quote

Now I just want to calculate the Median and Range of a single row the user chooses. I have been messing around with the Median and have obtained what you see below. It does give me a number but the wrong one.

Please show the contents of your "row", what you think the median should be, and what your program produces as the median.

Quote

And then as for Range, I have no clue how to pick out the biggest and smallest number within a row the user chooses to find the range.

What exactly don't you understand? Do you know how to find the minimum and maximum of the entire array?

Jim
Was This Post Helpful? 0
  • +
  • -

#11 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 09:33 AM

No I want to find the Median and the Range of a single row the user chooses from a 2D array. My attempt at finding the Median was above. And I have no clue how to find the Range of a row the user chooses in a 2D array.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,861
  • Joined: 25-December 09

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 09:41 AM

I can't begin to compile the code you provided that you think finds the median so you must show me the values held in your array, what your program produces for the median and what you think the median should be.

I know you want to find the minimum and maximum for a given range, but if you don't know how to find the minimum and maximum for the entire array then that will be your starting point, figuring out how to determine the minimum and maximum.

While I reasking questions you may want to tell me exactly what you think is the purpose of the following snippet:
for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < j; k++)
                {
                    if (array[i][j] < array[j][k])
                    {
                        int temp = array[i][j];
                        array[i][j] = array[j][k];
                        array[j][k] = temp;
                    }
                }
            }

And show me the contents of your array before this snippet and after this snippet.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 09:58 AM

Okay. Let me start with the snippet of code you posted. You asked me what the purpose was of that snippet. The point of that snippet was to bubble sort the row the user chose because the Median of a given set of numbers is the middle number, but the numbers have to be in order. The values in my array are randomly generated numbers between 1 and 100 so I cant exactly tell you what the exact numbers will be. Now even though this is a 2D array the user chooses one row they would like to find the Median of. This row can be thought of as being 1D now. In 1D the Median is like I said earlier. I hope I was able to answer all your questions.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,861
  • Joined: 25-December 09

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 10:13 AM

Quote

The values in my array are randomly generated numbers between 1 and 100 so I cant exactly tell you what the exact numbers will be.

So either use a "fixed" array until you have your basic program working, or print the array values before and after your sort routine. If you don't know the values of the array then there is no way you will know if your median is correct or not.

Quote

I hope I was able to answer all your questions.

No you did not!

Do you know how to find the minimum and maximum of the entire array?

Did you show me the contents of your array before your "sort"? No you didn't.

Did you show me the contents of your array after your "sort"? No you didn't.

Did you show me what you thought the median should be? No you didn't.

Did you show me what your program computes the median to be? No you didn't.

Also don't forget to tell me which "row" you are trying to work with.

For now I recommend that you comment out the call to srand() so your array will always contain the same elements. This will make troubleshotting your program much easier. Once you have it working you can then re-enable the srand() call to finish the problem.

By answering the above questions, and showing the data being used you can confirm several things. First that your sort is working properly, two by inspecting the actual data you should be able to "see" the minimum, maximum and be able to properly determine the median using pen and pencil to tell if your program is producing the correct results.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 snesnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 14-June 12

Re: Finding the mean of one row of a 2D array

Posted 02 August 2012 - 10:47 AM

Do you know how to find the minimum and maximum of the entire array?
In a 1D array yes, but not a 2D array.

Did you show me the contents of your array before your "sort"? No you didn't.
The contents of the array is always different since random numbers are always being generated.

Did you show me the contents of your array after your "sort"? No you didn't.
Again same as question 2.

Did you show me what you thought the median should be? No you didn't.
I explained what the median is. I cant really tell you the exact median if the numbers are random.

Did you show me what your program computes the median to be? No you didn't.
Its always a different number.

I mean if you look at my earlier posts with the whole program, putting the median with the mean is pretty much what I am going after here. I am shooting for the program telling me the mean,median, and range of one row the user chooses.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2