-
Help swapping 2 arrays
This whole pointer thing is really baffling me =(
I have to create a function that swaps 2 arrays. I know how to create the function, I know how to swap the arrays inside the function, but I can't figure out how to get the main() to recognize it =(
Please help, I have to turn this assignment in really soon.
-
Post your code so people can help you out, the members here are programmers not clairvoyants :), so by posting your code they will point your to the right direction.
-
what the main() has to recoganise. the sentence is incomplete =( ??
post the you code or tell us whats the actual problem with the code
s.s.harish
-
This code is really long, so I really can't post all of it
Code:
int print_Array(int x[]);
int exchange(int x[], int y[]);
int sort_Array(int x[], int flag);
int counter (int x[], int flag);
int get_menu_choice (void);
main()
{
int array_A[100], array_B[100];
srand(997);
Large amounts of code here allowing for manipulation of the 2
arrays. Lots of misc stuff like counting the number of even/odd
values, sorting the arrays, ect.
exchange(array_A[] , array_B[])'
return (0); /* indicate program ended successfully */
}
Lots of functions here that perform the actions mentioned aboe
int exchange(int num1[], int num2[])
{
int forflag = 0;
int temp[100];
for( forflag = 0, forflag <100, forflag++)
{
temp[forflag] = num1[forflag];
num1[forflag] = num2[forflag];
num2[forflag] = temp[forflag];
}
return(0);
}
Ehh, I really hope this is readable. Posting the whole code would have been too much so i cut a lot of it out. Hope i didn't cut too much.
-
Code:
int exchange(int num1[], int num2[])
{
int forflag = 0;
int temp[100];
for( forflag = 0, forflag <100, forflag++)
{
temp[forflag] = num1[forflag];
num1[forflag] = num2[forflag];
num2[forflag] = temp[forflag];
}
return(0);
}
That should work, but you're making temp unncessarily large. You're only ever swapping 1 variable at a time so you only need 1 temp variable, not an array. Something like:
Code:
int exchange(int num1[], int num2[])
{
int forflag = 0;
int temp;
for( forflag = 0, forflag <100, forflag++)
{
temp = num1[forflag];
num1[forflag] = num2[forflag];
num2[forflag] = temp;
}
return(0);
}
Also, you only ever return 0 from that function which makes a return value pretty useless. I'd get rid of the return statement and define exchange() with a void return value instead of int.
As far as making main() recognize the swapped arrays, it looks like it should work to me. What's your code for printing the arrays for verification look like?
-
*sigh* I'm a freaken idiot.
Thank you for all your help so far :)
I kept trying to do stuff like array_A[] = num1[] and just messing around with * and & makes.
So umm, why don't I have to have array_A[] = num1[] inside the function?
Sorry for such n3wb question, this is our professors first time with an intro course, and he doesn't follow the book at all. Makes things really hard.
-
You can't copy an array by doing something like array_A[] = num1[] in C. If you want to copy all the contents of an array in one statement you can use the memcpy() function, but I think your current exchange() function is doing a fine job.
-
Code:
void SwapArrays(int *Array1, int *Array2,int Size)
{
int *temp;
temp=malloc(Size*sizeof(int));
if (temp)
{
memcpy((int *)temp,(int *)Array1,size*sizeof(int));
memcpy((int *)Array1,(int *)Array2,size*sizeof(int));
memcpy((int *)Array2,(int *)temp,size*sizeof(int));
free(temp);
}
}
I think that should swap correctly using memcpy. I'm a C++ fanatic so using just pure C was extremely difficult.
-
We don't have to typecast malloc in C. ;)
Quzah.