A couple things to mention:
1.) This program uses system("PAUSE"); in the main function, if you are not using Windows, you might have to pull it.

2.) There are a couple lines in there that are not needed, I'm just using them to test the size of the array.

My question is this, when I run this program, it does it's intended job, which is:
Create a 2D char Array (regionName[5][20]) to hold Region Names
Create an int Array (regionAccident[5]) to hold accidents for related region
Get user input (by function) for each regions accidents and store it in related int arrray.
regionName[0] is a one to one with regionAccident[0], etc.
Sort regionAccident array by way of a bubblesort and whatever happens to regionAccident["Element"] happens to regionName["Element"]
================
HERE IS THE PROBLEM
I like to use (sizeof("Array To Be Used")/sizeof("Array Type")) to determine my Outer Loop limits. When I check the Arrays sizes right after I populate the data in main, everything is good. When I pass them to the function, they go down to 1 Element.

So when I am in the function findLowest and I try to get (sizeof(accidents)/sizeof(int)), it returns 1....

I'm wondering what I'm losing when I pass my int array through a function that only see's the array as one element (Although when I force it to show me each element it will, the sizeof( ) just isn't seeing the big picture....does that make sense?

#include <iostream>
#include <string>

using namespace std;

//Function Declarations
void intro(void);
int getNumAccidents(char region[]);
void findLowest(char region[5][20], int accidents[]);

int main()
{
    //2D Character Array to store region names
    char regionName[5][20] = {
                               {"North Region"},
                               {"South Region"},
                               {"East Region"},
                               {"West Region"},
                               {"Central Region"}
                              };

     //Integer array to store accidents
     int regionAccident[5];
    
    intro();
    
    regionAccident[0] = getNumAccidents(regionName[0]);
    regionAccident[1] = getNumAccidents(regionName[1]);
    regionAccident[2] = getNumAccidents(regionName[2]);
    regionAccident[3] = getNumAccidents(regionName[3]);
    regionAccident[4] = getNumAccidents(regionName[4]);
    
    cout << sizeof(regionAccident) << endl;   //Just to see the size of the array
    cout << sizeof(regionName) << endl;       //Just to see the size of the array
    
    findLowest(regionName, regionAccident);
    
    cout << "\n\n";
    system("PAUSE"); //I know this is a sin, but these are all rinky dink
    return 0;               //programs not are not going anywhere
}

void intro(void)
{
     cout << "This program will determine which of five pre-entered regions \n"
          << "have the fewest reported automobiles accidents last year.  \n\n"
          << "The number of accidents for each region will be supplied by you, the user. \n\n";
}

int getNumAccidents(char region[])
{
    int numAccidents = 0;
    cout << "Please enter the number of accidents for the " << region << ": ";
    cin >> numAccidents;
    
    while(numAccidents < 0)
    {
        cout << "I'm sorry, you must enter a number of accidents greater than 0. " << endl;
        cout << "Please enter the number of accidents for the " << region << ": ";
        cin >> numAccidents;
    }
    return numAccidents;
}
 
void findLowest(char region[5][20], int accidents[])
{
     char tempName[20];
     int tempNum;
     
     int a = 0;
     int b = 0;

     cout << sizeof(accidents) << endl;
     
     for(a = 0; a < 5; a++)
     {
           for(b = 0; b < a; b++)
           {
                 if(accidents[a] < accidents[b])
                 {
                     tempNum = accidents[b];
                     accidents[b] = accidents[a];
                     accidents[a] = tempNum;            
                                 
                     strcpy(tempName, region[b]);
                     strcpy(region[b], region[a]);
                     strcpy(region[a], tempName);                 
                 }
           }
     }            
                     
     a = 0;
     while(a < 5)
     {
     cout << region[a] << endl;
     a++;
     }
     
     a = 0;
     while(a < 5)
     {
     cout << accidents[a] << endl;
     a++;
     }
}
WaltP commented: First post and nicely formatted with CODE tags -- Excellent! +9

Here is an in-depth discussion I googled concerning the dangers of using sizeof(array)/sizeof(arraytype) in order to get current length of an array:

http://www.gamedev.net/community/forums/topic.asp?topic_id=345898

Some alternatives to consider:

  • Dedicate a 'size' variable to hold number of entries made into an array.
  • Use null-terminated c-strings; get array length using strlen().
  • Use an STL container such as String or Vector, use a size() member function to return string length.

A couple things to mention:
1.) This program uses system("PAUSE"); in the main function, if you are not using Windows, you might have to pull it.

Then you shouldn't use it at all. Keep your programs Standard so you don't have problems when you change compilers. See this.

HERE IS THE PROBLEM
I like to use (sizeof("Array To Be Used")/sizeof("Array Type")) to determine my Outer Loop limits.

Why? If you set up the array to be 5, use 5. Just make it a constant at the top of the program so that when you want to change the size you only need to do it in one place.

When I check the Arrays sizes right after I populate the data in main, everything is good. When I pass them to the function, they go down to 1 Element.

So when I am in the function findLowest and I try to get (sizeof(accidents)/sizeof(int)), it returns 1....

I'm wondering what I'm losing when I pass my int array through a function that only see's the array as one element (Although when I force it to show me each element it will, the sizeof( ) just isn't seeing the big picture....does that make sense?

You are losing the fact that there is an array. All the function knows is a pointer was passed in. It has no idea how many locations are associated with the pointer. You need to also pass the size into the function.

Then you shouldn't use it at all. Keep your programs Standard so you don't have problems when you change compilers. See this.

My Teacher wants us to use system("PAUSE") and system("CLS") for our programs, I'm sure he has probably worked on nothing but Microsoft, but he gives the grades and those are his demands, so that is why I do it.

Why? If you set up the array to be 5, use 5. Just make it a constant at the top of the program so that when you want to change the size you only need to do it in one place.

This defeats the whole purpose of challenging myself in programming. I could also put all the Arrays and Variables as globals and not have to worry as much either because it is so small, but that's bad practice. If I just set the outer loop as a static '5' then I have learned nothing new. I want the flexibility of saying, "Hey, I'll let the user decide how many fields to enter" and the array dynamically adjusts. One is automated, one is easy.

A million ways to skin a cat, I just like to learn different ways to go about it.

You are losing the fact that there is an array. All the function knows is a pointer was passed in. It has no idea how many locations are associated with the pointer. You need to also pass the size into the function.

I didn't use a pointer, is that what you are saying I need? If I pass it a pointer and the array size then that should do the trick?

You are losing the fact that there is an array. All the function knows is a pointer was passed in. It has no idea how many locations are associated with the pointer. You need to also pass the size into the function.

void myfunction(int array[], int array_size)
{
    //stuff
}

Here is an in-depth discussion I googled concerning the dangers of using sizeof(array)/sizeof(arraytype) in order to get current length of an array:

http://www.gamedev.net/community/forums/topic.asp?topic_id=345898

Some alternatives to consider:

  • Dedicate a 'size' variable to hold number of entries made into an array.
  • Use null-terminated c-strings; get array length using strlen().
  • Use an STL container such as String or Vector, use a size() member function to return string length.

I read through and it seemed that as long as you could separate the Pointer from the Array as they are two different things, that you should be okay, but everyone seemed to be pointing to vectors.

Here is where I stand, I'm in College and I love to program, but I'm a pretty "thorough" kind of worker, so before I jump to new topics I have a tendency to really beat a dead horse. So it creates this branching out effect rather slowly. Right now how deep and how many types of sorts and arrays and what you are capable of with them, I have really been trying to grasp that whole branch, but it is pretty big (For someone like me), so when I don't understand, I don't have a college professor that seems to be able to field these questions sometimes...so I find myself in these sticking points. Thanks for the response, I appreciate it.

void myfunction(int array[], int array_size)
{
    //stuff
}

Gotcha, thanks. I kept trying

void myFunction(int array[5]) and driving myself crazy

the problem is when you pass an array to a function the actual thing that gets passed is a pointer to the first element of the array. the function doesn't know its an array so it doesn't know that he address after the pointer are part of the array. there are to ways to go about this dilemma. first you could pass the size of the array to the function so it knows hat the upper bound is.

int size = 0;
cout << "please enter the number of employees you want to enter: ";
cin >> size;
char * employees = new char[size]; // create a dynamic array
sort(employees, size);
//...

or you could use a sentinel value at the end of the array and check for it while parsing through the array.

//in function for sorting
void sort(char temp[]);
{
       int i = 0;
       while (temp[i] != '/0')
       {
              // your code here
       }
}

i prefer to use the first case becuase it is much easier for int arrays then using a sentinel.

Yeah, thanks a bunch Clinton Portis. A really simple solution for something I just didn't realize. Declaring a new integer

int sizeAccidentArray = (sizeof(regionAccident)/sizeof(int));

and then passing sizeAccidentArray as the outerLoop that way I can eventually mold this into letting the user choose the regions and everything.

I can do things statically very easily, I am trying to break into more levels of automation, so starting with something very unflexible and bending it to become flexible is definitely the direction I wanted to go with this.

Thanks for the help!

Sidenote: I defintely need to move on to learning about pointers, I was completely unaware that passing an array into a function made it a pointer....

Does that go for all things passed to functions?

That they become pointers whilst in the function?

You guys rock, I appreciate explaining that to me, I feel I understand a lot better about passing arrays into functions. I can admit after the brief Moderator scolding that I got, I was about to assume this was another one of those "snub this dumb question from my pedestal without helping" sites but you guys really did go above and beyond helping me understand it. I appreciate it.

"If you know it, you can teach it. If you don't, you can't."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.