0% found this document useful (0 votes)
91 views7 pages

C Programming Functions Assignment Guide

1. The document contains 12 programming problems of varying difficulty levels that involve writing functions to perform tasks like finding the length of a string, sorting arrays, determining prime numbers, and more. 2. Sample inputs and outputs are provided for testing each function. For some problems, additional constraints or requirements are listed, such as using a specific formula to calculate points in a cricket scoring problem. 3. The final problem involves creating a structure to store player data from a cricket match including runs, wickets, and points scored, then taking input for two players and determining the Man of the Match and Series based on points totals.

Uploaded by

tanvir ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Topics covered

  • data types,
  • mathematical operations,
  • LCM,
  • user input,
  • conditional statements,
  • prime numbers,
  • sports programming,
  • input handling,
  • algorithm complexity,
  • data encapsulation
0% found this document useful (0 votes)
91 views7 pages

C Programming Functions Assignment Guide

1. The document contains 12 programming problems of varying difficulty levels that involve writing functions to perform tasks like finding the length of a string, sorting arrays, determining prime numbers, and more. 2. Sample inputs and outputs are provided for testing each function. For some problems, additional constraints or requirements are listed, such as using a specific formula to calculate points in a cricket scoring problem. 3. The final problem involves creating a structure to store player data from a cricket match including runs, wickets, and points scored, then taking input for two players and determining the Man of the Match and Series based on points totals.

Uploaded by

tanvir ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Topics covered

  • data types,
  • mathematical operations,
  • LCM,
  • user input,
  • conditional statements,
  • prime numbers,
  • sports programming,
  • input handling,
  • algorithm complexity,
  • data encapsulation

Assignment - 3

*hint: if you have to return more than one value from a function, use
structure or an array within the structure

SL Problem statement Difficulty


levels

1 Function to take a string as input and find its length. *

Sample input Sample output

hello world 11

I love my country 17

2. Function to determine only even numbers in an array of input integers. *

Sample input Sample output

24 77 117 -512 1024 24 -512 1024

45 33 0 256 0 256

3 Function that finds and returns the minimum value in an array. **

Sample input Sample output


157 -28 -37 26 10 Minimum Value: -37

12 45 1 10 5 3 22 Minimum Value: 1

4 Function that multiplies the array elements by 2 and returns the array. *

Sample input Sample output

157 -28 -37 26 10 314 -56 -74 52 20

12 45 1 10 5 3 22 24 90 2 20 10 6 44

5 Function to sort and return an input array in ascending order. **

Sample input Sample output

10 22 -5 117 0 -5 0 10 22 117

6 Function “IsPrime()” to determine whether a number is prime or not. **

Sample input Sample output


1 Not prime

2 Prime

11 Prime

39 Not prime

101 Prime

7 Implement the following functions and calculate standard deviation of an array whose values ***
come from the terminal-

TakeInput()

CalcMean(array, num_of_elem)

Calc_Std_deviation(array, num_of_elem)

Formula:

Sample input Sample output

4 5 5 4 4 2 2 6 1.32

600 470 170 430 300 147.32

8 Function find_substr( ) that takes two string arrays (a, b) as parameters, returns 1 if string b **
is found anywhere in string a, or returns –1 if no match is found.
(Assuming, strlen(a)>strlen(b))

Sample input (a, b) Sample output

madam adam 1

telescope less 0

101010 101 1

9 Function find_substr( ) that takes two string arrays (a, b) as parameters, uses function ***
str_length() to determine the lengths of the strings, and then looks for the smaller string
anywhere in the bigger string. It returns 1 if the substring is found, or returns –1 if no match
is found.

[Restriction: str_length() cannot uses built-in strlen() function]

Sample input (a, b) Sample output

madam adam 1

telescope less 0

101010 101 1

10 Program that continuously takes two positive integers as inputs and uses two functions to **
find their GCD (greatest common divisor) and LCM (least common multiple). Both functions
take parameters and returns desired values.
[Hint: Use infinite loop to process inputs]

Sample input Sample output

5 7 GCD: 1

LCM: 35

12 12 GCD: 12

LCM: 12

12 32 GCD: 4

LCM: 96

11 Program to convert a positive integer to another base using the following functions- ****

I. Get_Number_And_Base () : Takes number to be converted (N) and base value (B)


from user. Base must be between 2 and 16.

II. Convert_Number () : Does the conversion

III. Show_Converted_Number() : Displays the converted value.


Sample input(N,B) Sample output

100 8 144

512 16 200

512 0 Base not within proper range!

12 The Tigers have clinched a stunning victory over their rivals recently. In that series of three
matches, some players put up some amazing performances. Now you have to create a
structure named player where you have to store the following information of each player:

1. Player’s name

2. Player’s country

3. Array(size 3) to store runs of 3 matches

4. Array(size 3) to store wickets of 3 matches

5. Array(size 3) to store points of 3 matches

Count points using the following formula:

1. Each wicket = 12 points

2. Runs <=25 in a match = 5 points

3. 25< Runs<=50 in a match = 10 points

4. 50< Runs<=75 in a match = 15 points

5. 75< Runs in a match = 20 points

Now, take input of two players and calculate the points for each player for all the three
matches. And also find man of the match(MOM) for each match based on their points and
find out the man of the series on more points overall.
Sample Input Sample Output

Shakib Al Hasan Match 1:

Shakib Al Hasan points: 17


Bangladesh
Tamim Iqbal points: 20
20 MOM : Tamim Iqbal

75 Match 2:

Shakib Al Hasan points: 27


103
Tamim Iqbal points: 20
1
MOM : Shakib Al Hasan
1

5 Match 3:

Shakib Al Hasan points: 80

Tamim Iqbal points: 5


Tamim Iqbal
MOM : Shakib Al Hasan

Bangladesh
Man of the Series: Shakib Al Hasan
100

109

17

Common questions

Powered by AI

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 .

You might also like