CSCI 102 - Fall 2026 Fundamentals of Computation

MT2 Spring 2021 Solutions

Reduce

reduce.cpp

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

// prototype of working GCD function
int gcd(int x, int y);

int main()
{
    int num, denom; // numerator and denominator, respectively 
    char fmt;       // character to store 'f' or 'm'
    // Declare other variables and write your code below
    // If you cannot figure out how to reduce the number, just
    // perform the rest of the task using the non-reduced fraction.
    cin >> num >> denom >> fmt;
    
    int g = gcd(num, denom);
    num /= g;
    denom /= g;

    cout << "Decimal: " << fixed << setprecision(3) <<  (double)num / denom << endl;

    // non whole number
    if(num % denom != 0){
        // fraction (or num < denom)
        if(fmt == 'f' || num < denom) {
            cout << "Reduced fraction: " << num << "/" << denom << endl;
        }
        // mixed number
        else  {
            cout << "Reduced mixed number: " << num/denom << " " << num%denom << "/" << denom << endl;
        }
    }
    return 0;
}
// working implementation to find and return the gcd of 2 numbers, x and y
int gcd(int x, int y)
{
    while(y != 0){
        int t = y;
        y = x % y;
        x = t;
    }
    return x;
}

Doses

doses.cpp

#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

int main()
{
    int np, nd;     // number of people wanting doses and number of doses on a day
    int day=1;      // variable to track what day it is
    int wp=0, wd=0; // carry-over of people waiting and doses available
    // Declare other variables and complete your code below (changing what you want)
    cout << "How many new people and doses arrived today: ";
    cin >> np >> nd;

    while(np >=0 && nd >= 0) 
    {
        wp += np;
        wd += nd;
        if(wp <= wd) {
            cout << "Day " << day << ": " << wp << " people got doses today" << endl;
            wd -= wp;
            wp = 0;
        }
        else {
            cout << "Day " << day << ": " << wd << " people got doses today" << endl;
            wp -= wd;
            wd = 0;
        }
        day++;
        cout << "How many new people and doses arrived today: ";
        cin >> np >> nd;
    }
    return 0;
}

Arrow

arrow.cpp

#include <iostream>
// You may not use the string type
using namespace std;

int main()
{
    // Declare your variables and write your code below.
    // You may not use the string type
    char letter, lower_or_upper;
    int len = 0;


    cin >> len >> lower_or_upper;
    if(len <= 0 || len > 26) {
        len = 20;
    }
    char start = 'a';
    if(lower_or_upper == 'u'){
        start = 'A';
    }
    for(int i=len; i <= 26; i++){
        letter = start;
        for(int k=0; k < i; k++){
            cout << letter;
            letter++;
        }
        cout << endl;
    }
    for(int i=25; i >= len; i--){
        letter = start;
        for(int k=0; k < i; k++){
            cout << letter;
            letter++;
        }
        cout << endl;
    }
    return 0;
}