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 } ?
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:
-
The function
funcis called witharrand4as arguments.arris an array of integers{2, 4, 8, 10}and4is the size of the array. -
The variable
valis initialized to0. -
A
forloop runs from0tosize - 1(i.e.,3in this case). In each iteration, theith element ofarris added toval. -
After the loop,
valis divided bysizeto calculate the average of the elements inarr. -
The
ifstatement checks if the average is an integer by checking if the remainder ofvaldivided by1is0. If it is, the function returns1, otherwise it returns0.
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.
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;}
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.