Hello All.
Let me start by saying this is for a school assignment and I do not feel comfortable putting all of my code up so I will do my best to post a shortened version and explain what I am trying to do. In addition to this, I am not looking for someone to do my assignment for me. My question relates to a helper function that will essentially make the assignment easier for my implementation and not asking a directly "how do I do my homework" type question.
Ok, with that disclaimer out of the way here is what I am trying to do.
Essentially I am trying to receive back an array of char's in a certain order via a function. The reason I am calling a separate function in the first place is because I need to order the chars in a specific order depending on two other variables I am sending with the function.
Here is a brief implementation:
Code:
/*helper function declaration*/
void helpDist(int A, int B, char order[4]);
int main()
{
char order[4] = { 'a', 'a', 'a', 'a' }; //*** do I need to initialize
// the array sent???
...
...
helpDist(4, 3, order [4]); // arbitrary numbers used
// I now want the array order [4] filled in the certain
// order as specified in helpDist
}
void helpDist(int A, int B, char order[4])
{
int w,x,y,z, temp, ord[4], i, j;
w = A+B;
x = A*B;
y = A/B;
z = A-B;
// sort the list in ascending order
for (i = 0; i < 4; i++)
{
for (j = 0; j < (3 - i); j++)
{
if (ord[j] > ord[j + 1])
{
temp = ord[j];
ord[j] = ord[j + 1];
ord[j + 1] = temp;
}
}
}
// Here is the part I question****
// I want to fill the array of chars in a specific order
// and "send the information" back
for (i = 0; i < 4; i++)
{
if (ord[i] == w)
{
order[i] = 'w';
}
else if (ord[i] == x)
{
order[i] = 'x';
}
else if (ord[i] == y)
{
order[i] = 'y';
}
else
{
order[i] = 'z';
}
}
}
Do I need to this operation by reference? I have a feeling these values that are changed in the helper function are not reflected back in main where I have the calling function. Any insight on the matter would be greatly appreciated. I suppose I'm still not clear as to if the contents of an array need to be 'by ref' vs 'by val' since an array is essentially a pointer?
Thanks ahead of time.