Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy an array using a function

Status
Not open for further replies.

solardude

Programmer
Apr 7, 2007
6
AU
I need to copy one array into a different array using a function.

However, if the source array has less elements than the destination array,the remaining elements in the destination array should be set to 0.

If the source array is bigger than the destination array, I need to copy only as much as the destination array can fit.

Any help would be appreciated
 
I thought use:

void Arraycopy (int sourceArray[], int sourceSize, int destinationArray[], int destinationSize)
 
Well the function prototype looks good.

What have you tried to do so far?

Post an attempt, and please use the [code][/code] tags.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
My attempt is a screw up. I dont know where to set the size of the arrays. I dont know how to use functions properly either.

Code:
#include <stdio.h>

void copyArray (int srcArray[], int srcSize, int dstArray[], int dstSize);

int main(int argc, char **argv) {

	int i = 0;
	int srcSize;
	int srcArray[];
	int dstSize;
	int dstArray[];
	
	
	//scan values into source array
	i = 0;
	while (i < srcSize) {
	   scanf("%d", srcArray[i]);
	   i++;
	}
	
	//print what you scanned in
	i = 0;
	printf("\nsource array is: ");
	while (i < srcSize) {
		printf("%d", srcArray[i]);
    }
	i = 0;
	
	//print the empty destination array
	printf("\ndestination array is: ");
	while (i < dstSize) {
		printf("%d", dstArray[i]);
    }
	
	//call function
	void copyArray (int srcArray[], int srcSize, int dstArray[], int dstSize) 
	
	//print both arrays
	i = 0;
	printf("\nNew source array is: ");
	while (i < srcSize) {
		printf("%d", srcArray[i]);
    }
	i = 0;
	printf("\nNew destination array is: ");
	while (i < dstSize) {
		printf("%d", dstArray[i]);
    }

   return 0;
}

void copyArray (int srcArray[], int srcSize, int dstArray[], int dstSize) {
	int i = 0;
	if (srcSize < dstSize) {
		while (srcSize + i <= dstSize) {
		dstArray[srcSize + i] = 0;
		i++;
	    }

	}
	if (srcSize >= dstSize) {
		while (i <= dstSize) {
			srcArray[i] = dstArray[i];
			i++;
		}
	}
 
> int srcArray[];
You can't declare arrays like this, you need to specify a size.

Eg.
#define MAX_SIZE 100
int srcArray[MAX_SIZE];

If you really want variable length arrays, then that's a step up from what you have at the moment.

> //scan values into source array
Are you intending to fill the whole array, or stopping when the user has input the data they want?

> scanf("%d", srcArray);
This needs an &, like
[tt]scanf("%d", &srcArray);[/tt]

The call to the function would be
[tt]copyArray (srcArray, srcSize, dstArray, dstSize);[/tt]

All your loops printing things out are infinite loops. The index doesn't change from one iteration to the next.
Using a for loop is a more natural loop construct for accessing arrays.





--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
I fixed up the loops thanks for the help. If I wanted to stop when the user has inputed the data they want how would I do that? I tried to do this code but for some reason my c is automatically equaling new line.

Code:
while (c != '\n') {
   c = getchar();
   if (i < srcSize) {
      srcArray[i] = c - '0';   
      i++;
   }
}
 
To copy excluding the newline
Code:
while ( (c=getchar()) != '\n' ) {
}
Watch the ( ), omitting them gets you these implied [red]( )[/red]
[tt]c = [red]([/red]getchar() != '\n'[red])[/red][/tt]
which just makes c into a boolean true/false result.


You can bring the if statement into it as well
Code:
/* while room in the array, and input isn't newline */
while ( i < srcSize && (c=getchar()) != '\n' ) {
      srcArray[i] = c - '0';   
      i++;
}


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
The program doesnt allow me to input anything into the array.
It goes straight to printing the source array and skips the while loop entirely.
I dont get what im doing wrong.
 
Post your latest code then.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Code:
#include <stdio.h>

void copyArray (int srcArray[], int srcSize, int dstArray[], int dstSize);

int main(int argc, char **argv) {

   int i = 0;
   int srcSize;
   int srcArray[15];
   int dstSize;
   int dstArray[15];
   int c=0;

   //Enter array sizes
   printf("Enter the source size: ");
   scanf("%d",&srcSize);
   printf("Enter the destination size: ");
   scanf("%d",&dstSize);
   printf("Enter source array Values: \n");

   //scan values into source array
   i = 0;
   while (i < srcSize && (c=getchar()) != '\n') {
          srcArray[i] = c -'0';
          i++;
   }

   //print what you scanned in
   i = 0;
   printf("\nsource array is: ");
   while (i < srcSize) {
      printf("%d ", srcArray[i]);
      i++;
   }

   //print the empty destination array
   i = 0;
   printf("\ndestination array is: ");
   while (i < dstSize) {
      printf("%d ", dstArray[i]);
      i++;
   }

   //call function
   copyArray(srcArray, srcSize, dstArray, dstSize);

   //print both arrays
   i = 0;
   printf("\nNew source array is: ");
      while (i < srcSize) {
      printf("%d ", srcArray[i]);
      i++;
   }
   i = 0;
   printf("\nNew destination array is: ");
   while (i < dstSize) {
      printf("%d ", dstArray[i]);
      i++;
   }

   return 0;
}

void copyArray (int srcArray[], int srcSize, int dstArray[], int dstSize) {

   int i = 0;

   if (srcSize < dstSize) {
      while (i < dstSize - 1) {
         dstArray[i] = srcArray[i];
		 i++;
	  }
      while (i >= dstSize - 1) {
         dstArray[i] = 0;
         i++;
      }
   }

   if (srcSize >= dstSize) {
      while (i < dstSize) {
         dstArray[i] = srcArray[i];
         i++;
      }
   }
}
 
Try this
Code:
#include <stdio.h>

void copyArray(int srcArray[], int srcSize, int dstArray[], int dstSize);

int main(int argc, char **argv)
{
    int i = 0;
    int srcSize;
    int srcArray[15];
    int dstSize;
    int dstArray[15];
    int c = 0;

    //Enter array sizes
    printf("Enter the source size (<15): ");
    scanf("%d", &srcSize);
    printf("Enter the destination size (<15): ");
    scanf("%d", &dstSize);
    printf("Enter source array Values: \n");

    //scan values into source array
    for (i = 0; i < srcSize; i++) {
        if ( scanf("%d",&srcArray[i]) != 1 ) break;
    }

    //print what you scanned in
    printf("source array is:\n");
    for (i = 0; i < srcSize; i++) {
        printf("%d:%d\n",i, srcArray[i]);
    }

    //print the empty destination array (will be junk, maybe zeros)
    printf("destination array is:\n");
    for (i = 0; i < dstSize; i++) {
        printf("%d:%d\n",i, dstArray[i]);
    }

    //call function
    copyArray(srcArray, srcSize, dstArray, dstSize);

    printf("New source array is:\n");
    for (i = 0; i < srcSize; i++) {
        printf("%d:%d\n",i, srcArray[i]);
    }
    printf("New destination array is:\n");
    for (i = 0; i < dstSize; i++) {
        printf("%d:%d\n",i, dstArray[i]);
    }

    return 0;
}

void copyArray(int srcArray[], int srcSize, int dstArray[], int dstSize)
{
    int i = 0;
    for ( i = 0 ; i < srcSize && i < dstSize ; i++ ) {
        dstArray[i] = srcArray[i];
    }
    for ( ; i < dstSize ; i++ ) {
        dstArray[i] = 0;
    }
}

My results
[tt]
$ ./a.exe
Enter the source size (<15): 3
Enter the destination size (<15): 7
Enter source array Values:
11 22 33
source array is:
0:11
1:22
2:33
destination array is:
0:1
1:3475100
2:24
3:1996539226
4:2359296
5:2280012
6:2280648
New source array is:
0:11
1:22
2:33
New destination array is:
0:11
1:22
2:33
3:0
4:0
5:0
6:0[/tt]

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top