Overclock.net banner

1 - 12 of 12 Posts

·
Registered
Joined
·
933 Posts
Discussion Starter #1
Hey guys, we're just finishing up a chapter on arrays, and our teacher gave us this program (pre-coded for us) today as an example. Well, I don't get it. I know what it's doing, but I don't understand how it's doing it. If someone could explain what's going on in the program, that'd really be great.

Ok, what the program does is take the input values, put them in order, and count how many copies of each value there is. It then outputs those results. By looking at the code, I just don't understand what the heck is going on. I'm kinda having trouble grasping the whole multi-dimensional array thing too.

Code:
Code:
#include <iostream>
#include <iomanip>

using namespace std;

const int ARRAY_SIZE=100;

void processData(int twoDim[][2], int& len, int size);
void sortTwoDim(int twoDim[][2], int len);
void print(int twoDim[][2], int len);

int main()
{
    int numList[ARRAY_SIZE][2];
    int length;

    processData(numList, length, ARRAY_SIZE);
    sortTwoDim(numList, length);
    print(numList, length);

    return 0;
}

void processData(int twoDim[][2], int& len, int size)
{
    int num;
    bool found;
    int i;

    len=0;

system("cls");

    cout << "Enter a maximun of 100 positive integers ending with -999" <<endl;
    cin  >> num;

    while(num != -999 && len < size)
    {
        found = false;

        for (i= 0; i < len; i++)
            if(num == twoDim[i][0])
            {
                found= true;
                break;
            }

            if(!found)
            {
                twoDim[len][0]=num;
                twoDim[len][1]=1;
                len++;
            }
            else
                twoDim[i][1]++;

            cin >> num;
    }
}

void sortTwoDim(int twoDim[][2], int len)
{
    int ind;
    int mInd;
    int smallInd;

    int temp;

    for(ind= 0; ind < len-1; ind++)
    {
        smallInd=ind;

        for(mInd=ind+1; mInd < len; mInd++)
            if(twoDim[smallInd][0] > twoDim[mInd][0])
                smallInd=mInd;

        temp=twoDim[smallInd][0];
        twoDim[smallInd][0]=twoDim[ind][0];
        twoDim[ind][0]=temp;

        temp=twoDim[smallInd][1];
        twoDim[smallInd][1]=twoDim[ind][1];
        twoDim[ind][1]=temp;
    }
}

void print(int twoDim[][2], int len)
{
    int i, j;

system("cls");

    cout << setw(7) << "Test Score" << " " << setw(6) << "Count" <<endl;

    for(i= 0; i < len; i++)
    {
        for(j= 0; j < 2; j++)
            cout << setw(6) << twoDim[i][j] << "    ";
        cout <<endl;
    }
}
Again, thanks for any help on this!
 

·
Registered
Joined
·
745 Posts
The first function, processData, takes input from the user (a test grade in this case) and either adds in to the first column in the array, or increments the respective row in the second column if the value already exists. It uses the while loop to take up to 100 numbers (or until the user inputs -999) with a for loop inside of that to check if the value already exists. If the value is not there: if(!found) then it sets the next open spot in that array to the input number. If it does find the value, then it increments value in the respective row in the second column by 1. The second column is used purely as a counter.

The next function, sortTwoDim, for each value in the array (except the last one), checks the next value, and switches them (and their counts, in the second column of the array) if the first one is greater than the one next to it. This is then looped again until the last value is reached. It's a pretty elementary way of sorting a list of numbers in ascending order, used commonly with small lists.

The last function, print, is pretty obvious, it just prints the first number in the row (the value), then the second number (the count) and then a new line. This is done until the length of the list is reached.

The finished product is a sorted (ascending) list of the values you inputted, with their frequency to their right.

If you have any more questions I'd be glad to go into more detail, I don't know what level you stand at though.
 

·
Registered
Joined
·
933 Posts
Discussion Starter #3
Ok, I kinda get it now. I think its just mostly the multidimensional arrays that I'm having a hard time with right now. I'm just trying to get this totally understood, because Wednesday we move on to Classes (and from the sounds of it, that's gonna be tons of fun... /sarcasm)
 

·
Registered
Joined
·
45 Posts
For multi-dimensional arrays, it's easiest to imagine them as if they were a table, like something you would see in excel. Consider this 3x2 table:

|2|1|
|3|1|
|4|1|

In it we have two columns and three rows. In C++ when you declare a multi-dimensional array, you can consider it as a table like this. In this case this is a two-dimensional array. To access it you do the same as you would with an array, using the brackets ([]) to indicate the position by row and column.

For example, the number 3 is in position [0][1] (not sure if columns or rows comes first, it's been awhile since working with multi-dimensional arrays for me but with some simple testing you could easily figure it out).

If you want a three dimensional array, envision multiple of these tables stacked one on top of each other coming out towards you (like a 3d box divided up into different cells). You again access it with three brackets, the third indicating how many deep into the box the cell you are looking for is.

Four dimensional arrays are harder to actually visualize in this way, as this has been drawing from our own abilities to perceive and we can't truly perceive the fourth dimension. The best way to do it is to imagine a long row of these 3d boxes and use the fourth bracket to determine which of the boxes to search for the cell in.

You can probably figure out the pattern from here on out. Multi-dimensional arrays can be very useful, although I doubt you'll ever work with more than a 2d array in an intro level CS course.

Classes are actually pretty cool, but they'll likely seem dry and pointless at first (kind of like pointers, if you've learned those yet). Especially in C++, as it has many different forms of class inheritance (you'll learn it soon after you learn classes) which just makes things that much more complicated. Java is probably a better language to learn how to use classes simply in, but that's neither here nor there as you're in a C++ class. Just learn them by rote and use them and eventually their actual use will become more apparent.
 

·
Registered
Joined
·
933 Posts
Discussion Starter #5
Ok, that makes a bit more sense. That's sounds essentially the same as populating a matrix in math if I understand that correctly. (for 2d arrays anyway)
Hmm, in our book pointers come later on, after classes. Well, I think this should at least get me to where I'm on par with where we're supposed to be at before starting the next chapter, thanks.
 

·
Registered
Joined
·
45 Posts
Exactly the same as a matrix (for 2d arrays). In fact if you want to represent a matrix in a program a 2d array is likely the best way to go!

No problem. If you have any difficulties with any of the later topics let us know!
 

·
Premium Member
Joined
·
13,477 Posts
Multidimensional arrays are EASY.

A 2d array is like a Sudoku puzzle.

A 3d array is like a Rubik's cube.
 

·
Registered
Joined
·
745 Posts
Yeah, what everyone's said so far has been valid.

Classes are definitely good to know (they are a little scary at first, but you'll learn to use them powerfully) and pointers are basically used ALL the time. I'm actually surprised you weren't at least introduced to pointers before, just remember to go into that class with an open mind, if you ever want to go on to learning about data structures and storage, pointers are ubiquitous.

If you want any more help (on anything C++ related) just shoot me a PM and I'll be more than happy to help.
 

·
Registered
Joined
·
425 Posts
Quote:

Originally Posted by pastadiablo View Post
For multi-dimensional arrays, it's easiest to imagine them as if they were a table, like something you would see in excel. Consider this 3x2 table:

|2|1|
|3|1|
|4|1|

Quote:

Originally Posted by error10 View Post
Multidimensional arrays are EASY.

A 2d array is like a Sudoku puzzle.

A 3d array is like a Rubik's cube.

What about four of five dimensional arrays?
 

·
Premium Member
Joined
·
13,477 Posts
Quote:

Originally Posted by Snipe07 View Post
What about four of five dimensional arrays?
Look up the hypercube.

 

·
Registered
Joined
·
425 Posts
Quote:

Originally Posted by error10 View Post
Look up the hypercube.


wooowwwww... sooo mesmerizing....
 

·
Registered
Joined
·
45 Posts
Quote:


Originally Posted by Snipe07
View Post

What about four of five dimensional arrays?

Four dimensions is a typical three physical dimensional object considered along with the time dimension. For envisioning this as a programming array, just imagine a long row of three dimensional arrays. This could be representative of the three dimensional array changing over time.

The fifth dimension is a largely unknown dimension within relativity and is essentially hypothetical, but is considered to be an object with 4 physical dimensions plus the time dimension. To imagine it in array format, however, you can probably just imagine a two dimensional array of three dimensional arrays.

For the sixth dimension on not much is postulated by anyone other than String Theorists. Again, you can just imagine a three dimensional array whose elements are each three dimensional arrays.

Mathematically one doesn't need to be able to imagine n different dimensions to utilize them. You can use it to store important information that has many different facets to it.

For example, if you have some sort of data that can be described by four different characteristics (represented by numbers) then you can store that data in a four dimensional array.

More specifically, lets consider using a four dimensional array to hold the state of an object (alive, dead, or didn't exist at that position and time) that exists in three dimensional space and time. As time goes on in our program we edit this array to reflect the changing state of the object. We may hold an array such as this for the purposes of keeping track of data over time. You could define the array with four dimensions and consider these variables to represent each dimension:

x: this is the x-coordinate position of the object
y: this is the y-coordinate position of the object
z: this is the z-coordinate position of the object
t: this is the current time position of the object.

Now, let's say we want to know if the object existed at a specific position at a specific time. We would check to see if the value in the array at that position and time were something other than the non-existant state:

Code:
Code:
//to check at position 0, 3, 2 at time 10
if(array[0][3][2][10] != "non-existant")
{
     cout << "The object exists at this time and position!" << endl;
}
Please note that this is a horribly contrived example, but may help understanding that when handling multiple dimension arrays in computer science (c/c++, java, what-have-you) one doesn't necessarily need to envision the actual physical array, but merely needs to realize that each dimension can be used as a layer of description about the information being stored in the array.
 
1 - 12 of 12 Posts
Top