CEN 109 – Introduction to Algorithms and Programming Assignment 2 – SWE Group B and D January 2024 Student’s full name: KEJDI MUÇI Problem 1 - The promotional season #include <stdio.h> float checkAmount(int amount) { if (amount == 1) { return amount; } else return amount; } float secondItem(int price) { return price * 0.75; } float getItems(int n) { float total = 0; int amount, price; for (int i = 0; i < n; i++) { scanf("%d %d", &amount, &price); if (checkAmount(amount) == 2) total += checkAmount(amount) * secondItem(price); else total += price; } return total; } int main() { int n; scanf("%d", &n); float result = getItems(n); printf("%.2f\n", result); return 0; } It works by taking in the number of items “n” and going through each one, where it reads the quantity and unit price. It uses the “checkAmount” function to figure out the right multiplier for the unit price based on whether the quantity is 1 or 2. The “secondItem” function is responsible for calculating the price for the second item, considering the half-price deal. The total price for all items is then computed by utilizing these functions. In the end, the program prints out the total price with two-digit precision. Problem 2 - Population Bar. #include <stdio.h> int roundToThousand(int n) { return (n + 500) / 1000 * 1000; } void printBar(int year, int population) { printf("%d ", year); for (int i = 0; i < population / 1000; i++) { printf("*"); } printf("\n"); } int main() { int y1900, y1950, y2000; scanf("%d %d %d", &y1900, &y1950, &y2000); y1900 = roundToThousand(y1900); y1950 = roundToThousand(y1950); y2000 = roundToThousand(y2000); printBar(1900, y1900); printBar(1950, y1950); printBar(2000, y2000); return 0; } It creates a bar chart representing the population growth of a small town at 50-year intervals over the past century. The user providse population data for 1900, 1950, and 2000. The program rounds each population number to the nearest 1,000 and displays the corresponding year along with a bar of asterisks, where each asterisk signifies 1,000 people. I have used to functions “roundToThousand” and “printBar”. Exercise 3 - Public Transportation (USE AT LEAST 1 FUNCTION) #include <stdio.h> #include <string.h> int main() { int n, i, passenger; float fuel_price, total_profit = 0.0, max_profit = 0.0; char bus_name[100], max_bus[100]; scanf("%d %f", &n, &fuel_price); for (i = 0; i < n; i++) { float profit = 0.0; int fuel; scanf("%s %d", bus_name, &fuel); while (scanf("%d", &passenger) == 1) { if (passenger == 1) profit += 30.0; else if (passenger == 2) profit += 25.5; else if (passenger == 3) profit += 22.1; else profit+=0.0; } profit -= fuel * fuel_price; total_profit += profit; if (profit > max_profit) { max_profit = profit; strcpy(max_bus, bus_name); } } printf("%s %.2f\n", max_bus, max_profit); printf("%.2f\n", total_profit); return 0; } Problem 4 - Pattern. #include <stdio.h> int main() { int n; char letter = 'A'; scanf("%d", &n); for (int i = n; i >= 1; i--) { for (int j = 1; j <= n - i; j++) { printf(" "); } for (int k = 1; k <= i; k++) { printf("%d%c ", i, letter); } printf("\n"); letter++; } return 0; } Int n 3 3 3 3 3 3 Int i 3 3 3 2 2 1 Int j 1 1 1 1 2 1 Int k 1 2 3 1 2 1 letter ‘A’ (65) ‘A’ (65) ‘A’ (65) ‘B’ (66) ‘B’ (66) ‘C’ (67) Output 3A 3A 3A 3A 3A 3A 2B 2B 2B 1C Problem 5 - Unique elements. #include <stdio.h> int main(){ int a[5]; int b[5]; for(int i=0;i<5;i++){ scanf("%d",&a[i]); //the user puts 5 values in array a } for(int j=0;j<5;j++){ scanf("%d",&b[j]); //the user puts 5 values in array b } for(int k=0;k<5;k++){ //it changes the value of array b according to k place int p=0; for(int u=0;u<5;u++){ //it changes the value of array a according to u place if(b[k]!=a[u]) { //it checks which value of arrab b are not in array a p++; //each time a value is not presented when its checked, the int p increases by 1 } } if(p==5){ //if int p will be increased 5 times it prints the current checked number printf("%d ",b[k]); } } return 0; }