0% found this document useful (0 votes)
60 views3 pages

CSC211 Programming Exam Solutions

The document outlines a programming assignment for CSC 211, requiring students to answer four questions related to data analysis, candidate score processing, calculator functionality, and combination formulas. Each question includes a Python solution demonstrating the required functionality, such as calculating average sales, identifying top performers, implementing a calculator class, and computing combinations. The assignment emphasizes practical coding skills and understanding of basic programming concepts.

Uploaded by

Johnpraise
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
0% found this document useful (0 votes)
60 views3 pages

CSC211 Programming Exam Solutions

The document outlines a programming assignment for CSC 211, requiring students to answer four questions related to data analysis, candidate score processing, calculator functionality, and combination formulas. Each question includes a Python solution demonstrating the required functionality, such as calculating average sales, identifying top performers, implementing a calculator class, and computing combinations. The assignment emphasizes practical coding skills and understanding of basic programming concepts.

Uploaded by

Johnpraise
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

CSC 211 – Computer Programming II

Date: 04-06-2024
Instruction: Answer any 4 questions
Duration: 2 Hours
Course Title: Computer Programming II

Question 1: Phone Sales Data Analysis


a. Show total sales per phone type in 2024 (assume average)
b. Determine phone brand with highest 4-year total
c. Total number of phones sold across all brands

Python Solution:

sales = {
"Samsung": [30, 23, 34, 28],
"IPhone": [20, 26, 31, 30],
"Tecno": [22, 28, 26, 29],
"Nokia": [18, 15, 20, 23],
"Huawei": [19, 17, 22, 25]
}

# a. Predict sales for 2024 (average)


print("Predicted 2024 Sales (Average of 2020–2023):")
for brand, values in sales.items():
avg_sales = sum(values) / len(values)
print(f"{brand}: {round(avg_sales)} units")

# b. Phone brand with highest total sales


max_brand = max(sales, key=lambda b: sum(sales[b]))
max_total = sum(sales[max_brand])
print(f"\nPhone with highest 4-year total: {max_brand} ({max_total} units)")

# c. Total phones sold


total_all = sum([sum(v) for v in sales.values()])
print(f"\nTotal phones sold from 2020–2023: {total_all} units")

Question 2: Candidate Score Processor


Python Solution:

candidates = {
"Taiwo": [56, 43, 57, 60],
"Kenny": [76, 56, 67, 60],
"Usman": [47, 36, 75, 59],
"Nkechi": [37, 49, 64, 58]
}

print("Average Scores for Each Candidate:")


for name, scores in candidates.items():
average = sum(scores) / len(scores)
print(f"{name}: {average:.2f}")

# Identify best-performing student


best = max(candidates, key=lambda name: sum(candidates[name]))
print(f"\nTop performer: {best} with total {sum(candidates[best])}")

Question 3: Simple Calculator Class


Python Solution:

class Calculator:
def calculate(self, a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
elif op == '/':
return a / b if b != 0 else "Error: Division by zero"
else:
return a + b # Default to sum if unknown operator

# Test Cases
calc = Calculator()
print("10 + 5 =", calc.calculate(10, 5, '+'))
print("10 - 5 =", calc.calculate(10, 5, '-'))
print("10 * 5 =", calc.calculate(10, 5, '*'))
print("10 / 5 =", calc.calculate(10, 5, '/'))
print("10 $ 5 =", calc.calculate(10, 5, '$'))

Question 4: Combination Formula – nCr Class


Python Solution:
class Combination:
def factorial(self, n):
result = 1
for i in range(2, n + 1):
result *= i
return result

def nCr(self, n, r):


if r > n:
return 0
return self.factorial(n) // (self.factorial(n - r) * self.factorial(r))

# Example Usage
combo = Combination()
print("5C2 =", combo.nCr(5, 2))
print("6C3 =", combo.nCr(6, 3))
print("10C0 =", combo.nCr(10, 0))

You might also like