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++;
}
}