C Programming Functions Assignment Guide
Topics covered
C Programming Functions Assignment Guide
Topics covered
The function that finds the minimum value in an array iterates through the elements, comparing each to a stored minimum value. Initially, the first element is set as the minimum. For each subsequent element, if a lower value is found, it becomes the new minimum. In the example array [-28, -37, 26, 10], the minimum value identified by the function is -37 .
Player performances in cricket are evaluated by accumulating points based on specific criteria: 12 points per wicket, runs scoring 5 to 20 points depending on the range. For each match, points are tallied, and the player with higher points is the 'Man of the Match'. The 'Man of the Series' is identified by summing total points across matches, with Shakib Al Hasan determined as the series winner based on his superior aggregate score compared to Tamim Iqbal .
The 'IsPrime()' function determines if a number is prime by checking divisibility from 2 to the square root of the number. A number is prime if it is greater than 1 and not divisible by any of these. Expected outcomes for inputs include 'Not prime' for 1 and 39, and 'Prime' for 2, 11, and 101 .
A function would need to convert a positive integer to another base when dealing with systems requiring non-decimal representations, such as computer science (binary, hexadecimal). Pre-conditions include ensuring the base falls within 2 to 16. For an input like (100, 8), the output is 144 in octal; for (512, 16), it's 200 in hexadecimal. If the base is out of range, an error message 'Base not within proper range!' occurs .
To filter only even integers from an input array, a function iterates over the array and checks each element using the modulo operator (%). Numbers yielding zero when divided by two are even and added to a separate output array. These steps are necessary to differentiate even numbers from odd ones, ensuring correct filtering. For example, given inputs [24, 77, 117, -512, 1024], the even numbers identified are [24, -512, 1024].
To calculate the standard deviation of a numerical array, use the following steps: First, take input using 'TakeInput()' to populate the array. Then, calculate the mean using 'CalcMean(array, num_of_elem)'. Finally, use 'Calc_Std_deviation(array, num_of_elem)' to compute the standard deviation based on the formula that involves the mean and the differences of array elements from this mean. The process involves computing variance first, then taking its square root to get the standard deviation .
One way to ensure a function returns multiple values in a structured format is to use a structure or an array within a structure. This is useful when functions need to return more than one value, like a string's length or filtered even integers from an array. By using a structure, programmers can encapsulate multiple return values efficiently .
The 'find_substr()' function identifies substrings by iterating over the larger string and checking if the smaller string matches at any position. It uses 'str_length()' to determine string lengths, ensuring it does not rely on built-in functions like strlen(). The function returns 1 if the substring is found, or -1 if not. A constraint is that 'str_length()' must manually compute lengths without external help .
To sort and return an input array in ascending order, implement a sorting algorithm like quicksort or mergesort to reorder elements. For the array [10, 22, -5, 117, 0], the sorted output would be [-5, 0, 10, 22, 117]. The method organizes numbers by comparing and swapping based on their values in such a way that after sorting, all elements are arranged from the smallest to the largest .
The GCD (Greatest Common Divisor) and LCM (Least Common Multiple) of two integers are calculated using specific functions. GCD can be computed using the Euclidean algorithm, while LCM is derived from the relationship LCM(a, b) = (a * b) / GCD(a, b). For inputs like (5, 7), the outputs are GCD: 1 and LCM: 35; for (12, 12), both GCD and LCM are 12; for (12, 32), GCD is 4 and LCM is 96 .