Question

Given an array of integer, please complete a function multiply(). The function accepts the first memory...

Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array.

Pseudo code example:

  multiply([ 1, 2, 3], 3) → 6
  multiply([1, 1, 4 ,2], 4) → 8
  multiply([7, 0, 0, 7, 0, 7], 6 ) → 0

C program:

#include<stdio.h>

int multiply(int * arr, int array_size);

int main()

{

       int arr[] = { 1, 2, 3, 4, 5 }; // input example

       int result = multiply(arr, 5);

       printf("result %d ", result);

       return 0;

}

int multiply(int * arr, int array_size)

{

       // assuming that your code will be integrated into this part.

}

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

int multiply(int * arr, int array_size);

int main() {

  int arr[] = { 1, 2, 3, 4, 5 }; // input example

  int result = multiply(arr, 5);

  printf("result %d ", result);

  return 0;

}


int multiply(int * arr, int array_size) {

  int i;

  // initialize the product variable to 1
  int array_product = 1;

  // looping through the array
  for(i = 0; i < array_size; i++) {
    
    // calculating the product
    array_product = array_product * arr[i];

  }

  // return product of array
  return array_product;

}

For help please comment.

Thank You

Add a comment
Know the answer?
Add Answer to:
Given an array of integer, please complete a function multiply(). The function accepts the first memory...
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
  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array...

    public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] TESTING: collatz(7,3) should return {7, 22, 11, 34} collatz(6,0) should return {6} collatz(6, 5) should return {6, 3, 10, 5, 16,...

  • Must be written in C 89 Mode Array Insertion Your task is to complete the implementation...

    Must be written in C 89 Mode Array Insertion Your task is to complete the implementation of the insertion function, insert_into_array: int* insert_into_array(int arr[], size_t arr_len, int value, size_t pos); The insertion function inserts a value into an array at a specified position. All elements to the right of the inserted element are shifted over a position (upon inserting, all elements at the insertion position are shifted up an index, and the last element in the array is overwritten by...

  • You are given an integer array in the console program which declares and initializes it as...

    You are given an integer array in the console program which declares and initializes it as follows, for example: int int_array[] = {1, -3, 23, 4, 9, 2, 29, -3, 0, 2, 48, 7, 6, -1}; In that array there are n elements, where 0 < n < INT_MAX. Your program is to find the largest sum of any consecutive elements in that array. In the above example, the largest sum is obtained by adding 23, 4, 9, 2, 29,...

  • Consider the following code. Assume that the array begins at memory address Ox200. What is printed...

    Consider the following code. Assume that the array begins at memory address Ox200. What is printed by the following code. Assume sizeof(int) is 4 bytes and sizeof(char) is 1 byte. Do not include the Ox or leading zeros in your answer. Enter -1 if the answer is indeterminate, or -2 if the code generates a compile error, or -3 if the code generates a runtime error. #include <stdio.h> int main() { int a[] {1,2,3}; int *iptr a; printf("%p\n", iptr+1); return...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • I must call multiply, divide and remainder functions inside of SUBTRACT function.. I am stuck at...

    I must call multiply, divide and remainder functions inside of SUBTRACT function.. I am stuck at this point. Thank you #include<stdio.h> int getDifference(int a, int b); int main() { int a, b, result, multiply, divide, rem; printf("Enter the two integer numbers : "); scanf("%d %d", &a, &b); //Call Function With Two Parameters result = getDifference(a, b);    printf("Difference of two numbers is : %d\n" , result);    return (0); } int getDifference(int a, int b) { int c, multiply; float...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

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