Knowee
Questions
Features
Study Tools

Predict the output of the following code snippetint func(int arr[], int size) {        float val = 0;        int i ;        for(i = 0; i < size; i++)            val += arr[i];        val /= size;       if (val % 1 == 0)            return 1;        else            return 0;      }What will be the output for func(arr, 4) where arr = {2, 4, 8, 10 }  ?

Question

Predict the output of the following code snippetint func(int arr[], int size) {        float val = 0;        int i ;        for(i = 0; i < size; i++)            val += arr[i];        val /= size;       if (val % 1 == 0)            return 1;        else            return 0;      }What will be the output for func(arr, 4) where arr = {2, 4, 8, 10 }  ?

🧐 Not the exact question you are looking for?Go ask a question

Solution

The function func calculates the average of the elements in the array arr and checks if the average is an integer or not. If the average is an integer, it returns 1, otherwise it returns 0.

Here are the steps:

  1. The function func is called with arr and 4 as arguments. arr is an array of integers {2, 4, 8, 10} and 4 is the size of the array.

  2. The variable val is initialized to 0.

  3. A for loop runs from 0 to size - 1 (i.e., 3 in this case). In each iteration, the ith element of arr is added to val.

  4. After the loop, val is divided by size to calculate the average of the elements in arr.

  5. The if statement checks if the average is an integer by checking if the remainder of val divided by 1 is 0. If it is, the function returns 1, otherwise it returns 0.

In this case, the sum of the elements in arr is 2 + 4 + 8 + 10 = 24, and the average is 24 / 4 = 6. Since 6 is an integer, the function returns 1.

This problem has been solved

Similar Questions

Predict the output for the given code for func(8, 2) ?int func(int a, int b){    int c;    c = a << b;    return c;}Select one:a. 16b. 8c. 4d. 32

What will be the output of the given program?int fun(int i){if(i==1 || i==0){return 1;}return fun(i-2)+fun(i-1);}void main(){int x=8;printf("%d", fun(x));}Options32303436

Predict the output?       int arr[] = {10, 20, 30, 40, 50};       for(int i=0; i < arr.length; i++)       {             System.out.print(" " + arr[i]);                     }

What is the output of the following Java code:Code:class hacker{ public static void main(String[] arg) { int[] a = { 128, 132, 98, 116, 100 }; int x = func(a); System.out.println(x); } public static int func(int[] no) { int b = no[0]; for (int i: no) { if (i > b) b = i; } return b; }}

Predict the output of the following code snippet.123456789#include <stdio.h>int main() { int x = 5, y = 3; int *ptr1 = &x, *ptr2 = &y; int a = *ptr1 * *ptr2; printf("%d\n", x); return 0;}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.