Knowee
Questions
Features
Study Tools

Write a C function, which returns the smallest odd number in a given array, and 0 if no such number is found.int smallest_odd(int a[] , int len);

Question

Write a C function, which returns the smallest odd number in a given array, and 0 if no such number is found.int smallest_odd(int a[] , int len);

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

Solution

Sure, here is a simple C function that does what you asked for:

#include <limits.h>

int smallest_odd(int a[], int len) {
    int smallest = INT_MAX;
    int found = 0;
    for(int i = 0; i < len; i++) {
        if(a[i] % 2 != 0 && a[i] < smallest) {
            smallest = a[i];
            found = 1;
        }
    }
    if(found) {
        return smallest;
    } else {
        return 0;
    }
}

This function works by iterating over each element in the array. If the element is odd (a[i] % 2 != 0) and it is smaller than the current smallest odd number found, it updates the smallest number. If no odd number is found after checking all elements, it returns 0.

This problem has been solved

Similar Questions

Complete the function count_odd_even() , which receives number of elements and array as input. Function should count number of even numbers and odd numbers in the array and print it.Input Format:No Need to read any input, as it is predefined.Output Format:Print the count of odd and even as: Odd = ODD_COUNTEven = EVEN_COUNTConstraints:1<=size<=10^9 1<=arr[ind]<=10^15Sample Input 1:1047 41 87 88 95 46 35 66 63 93Sample Output 1:Odd = 7Even = 3Sample Input 2:81 2 3 4 5 6 7 8Sample Output 2:Odd = 4Even = 4

Given an integer array of size 10. Replace the odd number in numpy array with -1 ?Note: Generate the following arrayarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])Print the Numpy array.

Write a function:class Solution { public int solution(int[] A); }that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.Given A = [1, 2, 3], the function should return 4.Given A = [−1, −3], the function should return 1.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..100,000];each element of array A is an integer within the range [−1,000,000..1,000,000].

Given an array of integers, your task is to find the smallest and second smallest element in the array. If smallest and second smallest do not exist, print -1.

Define a function odd_numbers(n) with one argument integer n. The function find all odd numbers less than and equal to n and returns a list with all the odd numbers you found.Hintlist.append() is an inbuilt method in the Python to store an odd number into a list.

1/2

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.