Question

Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /*...

Consider the syntactically correct C code below, which is missing a function copy_positive.


#include <stdio.h>

 /* copy_positive(A, Aout, size)
  * Given an array A, which will have the provided size, copy all positive
  * (non-negative/non-zero) elements of A into the provided output array Aout.
  * Return the size of the resulting array. */
 /* (your code from below would be placed here) */

 void print_array(int arr[], int n){
     for( int i = 0; i < n; i++ )
     printf("%d ", arr[i]);
 printf("\n");
 }
 int main(){
     int A1[] = {0, 9, -1, 0, 6, 10, 17};
     int A2[] = {11, 1, 0, -5};
     int B[100]; int b_size;
     b_size = copy_positive(A1,B,7);
     print_array(B, b_size);
     b_size = copy_positive(A2,B,4);
     print_array(B, b_size);
     return 0;
 }

Write a definition of the function copy_positive (including the function signature). For full marks, your implementation should work correctly on all input values, not just the ones above.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

 /* copy_positive(A, Aout, size)
  * Given an array A, which will have the provided size, copy all positive
  * (non-negative/non-zero) elements of A into the provided output array Aout.
  * Return the size of the resulting array. */
 /* (your code from below would be placed here) */
int copy_positive(int A[], int Aout[], int size){
    int count = 0;
    for(int i = 0;i<size;i++){
        if(A[i]>0){
            Aout[count] = A[i];
            count += 1;
        }
    }   
    return count;
}

void print_array(int arr[], int n){
     for( int i = 0; i < n; i++ )
         printf("%d ", arr[i]);
    printf("\n");
 }
 
 int main(){
     int A1[] = {0, 9, -1, 0, 6, 10, 17};
     int A2[] = {11, 1, 0, -5};
     int B[100]; int b_size;
     b_size = copy_positive(A1,B,7);
     print_array(B, b_size);
     b_size = copy_positive(A2,B,4);
     print_array(B, b_size);
     return 0;
 }

Add a comment
Know the answer?
Add Answer to:
Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /*...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized...

    4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized to functions, allowing easy reuse. Recall that arrays in C are essentially represented by pointers, so when an array is passed into a function, the function is given access to the original array data (not a copy). This means that arrays are effectively passed by reference in C, and therefore that functions must be careful not to modify the contents of arrays they receive...

  • Please explain answer :D Question 5 (5 marks] Consider the syntactically correct C code below, which...

    Please explain answer :D Question 5 (5 marks] Consider the syntactically correct C code below, which is missing a function print_diagonal include <stdio.h> • typedef int Table(100) (100); */• print diagonal (T, n) . Given a Table T, which will have n rows and n columns, print all of the 1 entries on the main diagonal of T (that is, entries whose row number and column number are equal). Remember to print a newline at the end. / ► /...

  • C program-- the output is not right please help me to correct it. #include <stdio.h> int...

    C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...

  • Modify the following C code to the changes listed below. Please format code correctly. #include <stdio.h>...

    Modify the following C code to the changes listed below. Please format code correctly. #include <stdio.h> #include <stdlib.h> #define SIZE 5 void displayData(int [], int s); void getData(int array[], int size) {     for(int i=0; i<SIZE; i++)     {         scanf("%d", &array[i]);     } } int main() {     int grades[SIZE];     for(int i=0; i<SIZE; i++)     {         grades[i] = 0;     }     getData(grades,SIZE);     displayData(grades, SIZE); } void displayData(int grades[], int size) {     for(int i=0; i<size;...

  • I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){...

    I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){ int i; for(i = 0; i < asize;i++) printf("%d", array[i]); printf("\n"); } int sum(int array[], int asize){ int result = 0; int i = 0; for(i=0;i < asize;i++){ result = result + array[i]; } return result; } int swap( int* pA,int*pB){ int result = 0; if(*pA > pB){ int tamp = *pA; *pA = *pB; *pB = tamp; result = 1; } return result;...

  • Whats wrong with my code? Please help! #include <stdio.h> int main() { //variables int m1, m2,...

    Whats wrong with my code? Please help! #include <stdio.h> int main() { //variables int m1, m2, m3; int a1, a2, a3; int f1, f2, f3; //input acceleration printf("Enter value for a1: "); printf("Enter value for a2: "); printf("Enter value for a3: "); scanf("%d %d %d",&a1,&a2,&a3); //input mass printf("Enter value for m1: "); printf("Enter value for m2: "); printf("Enter value for m3: "); scanf("%d %d %d",&m1,&m2,&m3); //functions f1=m1*a1; m1=f1/a1; a1=f1/m1; f2=m2*a2; m2=f2/a2; a2=f2/m2; f3=m3*a3; m3=f3/a3; a3=f3/m3; //command: printf("f1=%d\n, m1=%d\n, a1=%d\n"); printf("f2=%d\n,...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements....

    #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • Could you do that in C language? Here is the code which we got #include <stdio.h>...

    Could you do that in C language? Here is the code which we got #include <stdio.h> #define MAX_SIZE 20 // function definitions void displaySpiral(int matrix[][MAX_SIZE], int size); void displayMatrix(int matrix[][MAX_SIZE], int size); int takeInput(int inputMatrix[][MAX_SIZE]); int main() { int matrix[MAX_SIZE][MAX_SIZE]; int matrixSize = takeInput(matrix); printf("Displaying the whole matrix:\n"); fflush(stdout); displayMatrix(matrix, matrixSize); printf("Now, displaying the matrix in a spiral way:\n"); fflush(stdout); displaySpiral(matrix, matrixSize); return 0; } // already implemented for you int takeInput(int inputMatrix[][MAX_SIZE]) { int size; printf("What is the size...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT