Thread: Copy function

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    Copy function

    I am struggling at the following task:

    Write a function called copy which takes the following parameters:

    -Two integer arrays
    -The size of the two arrays as an integer
    -A threshold value as an integer

    Your function should copy the values of the first array into the second array but ignore values above the given threshold.

    I always get only 0 0 0 0 0 as output with my function. Does anybody now how I can fix this?


    Code:
    int copy(int* arr1, int* arr2, int length, int d) {
       for (int m=0; m< length;m++){
           
         int min= arr1[m];
         for(  int  i=m+1; i< length; i++){
           if (min>arr1[i]){
               arr1[i]=arr1[i-1];
           }
           else
               min=min;
       }
        arr2[m]  = min;
    }
    for (int n=0; n< length;n++){
        if (arr2[n] > d)
            arr2[n]=0;
        else
            arr2[n]=arr2[n];
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    72
    You need to copy the values from arr1 to arr2 only if they are greater than threshold. There is no need to find the min.

    Maybe sth. like this:
    Code:
    for (int i = 0; i < length; ++i)  {
        if (arr1[i] > d)
           // copy arr1[i] over to arr2

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    Thank you, it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this function to copy a file ok?
    By SillyStudent in forum C Programming
    Replies: 8
    Last Post: 04-23-2018, 03:11 PM
  2. Replies: 2
    Last Post: 03-11-2009, 07:52 AM
  3. a copy function
    By hallo007 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2006, 09:27 AM
  4. string copy function
    By rahulsk1947 in forum C Programming
    Replies: 8
    Last Post: 02-14-2006, 07:13 PM
  5. function to copy one string to another one
    By robstr12 in forum C Programming
    Replies: 15
    Last Post: 01-28-2005, 11:46 PM

Tags for this Thread