• Portfolio
  • Archive
    • Grad Students Halloween Party 2016
    • Praxis Showcase 2010 Highlights
    • 2015 Canada Blooms Near North Hardscapes
    • 2014 IEEE Toronto AGM Highlights
    • 2014 GSU Halloween Party
    • 2014 GSU Halloween Party 2
    • IEEE Day: Wine and Cheese
    • 2014 Akido Club Photoshoot
    • Canonball 1T5
    • 2014 Toronto Christmas Market
    • 2015 Nocturne
    • Cute-Baby-TBP
  • Stuff.
  • Contact Info

kmingk

Just Me.

  • Portfolio
  • Archive
    • Grad Students Halloween Party 2016
    • Praxis Showcase 2010 Highlights
    • 2015 Canada Blooms Near North Hardscapes
    • 2014 IEEE Toronto AGM Highlights
    • 2014 GSU Halloween Party
    • 2014 GSU Halloween Party 2
    • IEEE Day: Wine and Cheese
    • 2014 Akido Club Photoshoot
    • Canonball 1T5
    • 2014 Toronto Christmas Market
    • 2015 Nocturne
    • Cute-Baby-TBP
  • Stuff.
  • Contact Info

APS105 Tutorial 4 - Midterm Solutions

Official Midterm Solutions are online, but here is the files I will use to show how the solution works

Q14

/* 
 * File:   main.c
 * Author: kmingk
 *
 * Created on October 18, 2015, 12:46 PM
 */

#include <stdio.h>
#include <stdlib.h>

/* Develop a program that would continuously ask the users for characters. 
 * When a user enters NAN, it should print out pattern found, and should be
 * able to recognize NANAN as two separate instances.  
 * 
 * Program should end when F is entered
 */
int main(int argc, char** argv) {
    // Declare Variables
    char c1, c2; // c1 - previous input, c2 - the input before c1
    char currentInput;
    
    // Initialize variables to non-important characters
    c1 = 'C';
    c2 = 'C';
    currentInput = 'C';
    
    // Keep looping for user input of characters
    do {
        printf ("Please enter a character: ");
        scanf (" %c", &currentInput);
        // Proper way to do this.  Keeps a correct running history
        if ((currentInput == 'N') && (c1 == 'A') && (c2 == 'N')) {
            printf ("Pattern NAN Found\n");
        }
        
        // Keep a running history of characters
        c2 = c1;
        c1 = currentInput;
        
        // Incorrect way other people are doing this - This misses NANAN or NNAN
//        if (currentInput == 'N') {
//            printf ("Please enter a character: ");
//            scanf (" %c", &c1);
//            if (c1 == 'A') {
//                printf ("Please enter a character: ");
//                scanf (" %c", &c2); 
//                if (c2 == 'N') {
//                    printf ("Pattern NAN Found\n");
//                }
//            }
//        }
    } while (currentInput != 'F');

    printf ("Done!\n");
    return (EXIT_SUCCESS);
}

Q11

/* 
 * File:   main.c
 * Author: kmingk
 *
 * Created on October 18, 2015, 1:21 PM
 */

#include <stdio.h>
#include <stdlib.h>

/* Create a function that takes in a user inputted integer and reverse the digits
 * must use a function
 * 
 */

// 12345 --> 54321
// 21452 --> 25412
// 12340 --> 04321
int reverseDigit (int input) {
    // We want to start with output = 0 since we use it later on, so
    // make sure its initialized properly
    int output = 0;
    // we want to stop once we are done with the "1's" digit.
    // Extra: Try changing loop so it is >= 10, 100, what happens?
    while (input >= 1) {
        int remainder; // temp variable to store the remainder
        remainder = input%10;
        input = input/10; // our loop relies on us to continues divide input
        
        output = output*10 + remainder;
    }
    // Example of how this works:
    // 12345 --> input
    // 12345%10 --> 5 + output*10 (0*10) = 5 = output
    // 12345/10 --> 1234 --> input
    // 1234%10 --> 4 + output*10 (5*10) = 54 = output
    // 1234/10 --> 123 --> input
    // 123%10 --> 3 + output*10 (54*10) = 543 = output
    // 123/10 --> 12
    return output;
}

int main(int argc, char** argv) {
    int userInput;
    int output;
    
    // Get User Input
    printf ("Enter Number: ");
    scanf("%d", &userInput);
    
    output = reverseDigit (userInput);
    
    printf ("Output: %d\n", output);
    return (EXIT_SUCCESS);
}

Q13

/* 
 * File:   main.c
 * Author: Kmingk
 *
 * Created on October 18, 2015, 1:03 PM
 */

#include <stdio.h>
#include <stdlib.h>

/* Program to accept a number input from the user (0-9)
 * The program will then output a pattern that is ten rows in total and ten
 * digits.  
 * The pattern will shift to the left
 * 
 * Example: 
 * Input: 0
 * 0123456789
 * 1234567890
 * 2345678901
 * 3456789012
 * 4567890123
 * 5678901234
 * 6789012345
 * 7890123456
 * 8901234567
 * 9012345678
 * 
 * Input: 3
 * 3456789012
 * 4567890123
 * 5678901234
 * 6789012345
 * 7890123456
 * 8901234567
 * 9012345678
 * 0123456789
 * 1234567890
 * 2345678901
 * 
 */
int main(int argc, char** argv) {

    // Declare Variables
    int userInput = 0;
    int row, col;
    // Get User Input
    printf ("Please enter index (0-9): ");
    scanf ("%d", &userInput);
    
    // Go through the 10 rows
    for (row = 0; row < 10; row++) {
        // Go through all the columns
        for (col = 0; col < 10; col++) {
            // Uncomment each of the lines (1-4) one by one to see the effect
            // 1: Printing out col will repeat me 0123456789 for all rows
//            printf ("%d", col); 
            
            // 2: Printing out col+row --> starting number increases by 1 each
            //    row, spacing added for visibility
//            printf ("%d ", (col+row));
            
            // 3: Printing out col+row%10 --> Gets me the shifted pattern
//            printf ("%d", (col+row)%10);
            
            // 4: Adding in the effect of userInput
//            printf ("%d", (col+row+userInput)%10);
            
            // Extra: What happens if i print newline here?
            //        A: Adds newline after every digit! --> not good
//            printf ("\n");
        }
        // end of columns, print newline
        printf ("\n");
    }
    return (EXIT_SUCCESS);
}

 


Monday 10.19.15
Posted by Kei-Ming Kwong
 

APS105 Tutorial 3

Mainly covers a basic loop and checks and conditions to get out of the loop.  

  • Can use "continue" to get out of the loop, but usually there are ways to do it without in a cleaner way. 
/* 
 * File: main.c
 * Author: user
 *
 * Created on October 3, 2015, 2:03 AM
 */

#include <stdio.h>
#include <stdlib.h>

/*
 * Create a program which repeatedly asks a user for a grade between 0 and 100 
 * (inclusive) and computes the minimum grade, the maximum grade and the average
 * grade.The grades may be entered as floating point numbers
 * and the program should ask for another grade when presented with on greater
 * than 100.Any grade less than 0 should case the program to terminate.Prior
 * to termination, the program should print the min, max, avg grade. The program
 * cannot ask the user for the number of grades directly.
 *
 * Inputs
 *inputGrade - double (0-100) >0 is invalid/less than 0 ends program 
 * Outputs
 *minGrade - double smallest grade
 *maxGrade - double largest grade
 *avgGrade - double average grade
 */
int main(int argc, char** argv) {
// Declare and Initialize variables
double inputGrade, minGrade, maxGrade, avgGrade, sumGrades;
int numGrades;
minGrade = 100;
maxGrade = 0;
numGrades = 0;
sumGrades = 0;

// Loop through and repeatedly ask user for grades
// determine running min/max/avg while we loop
// Exit conditions: inputGrade Less than 0
printf ("Enter a grade: ");
scanf ("%lf", &inputGrade);
while (inputGrade >= 0) {
// can be another way to check for valid input
//if (inputGrade > 100) {
//// ask user for input
//continue;
//}

// Check Valid Input
if (inputGrade <= 100) {
// Calculate running statistics 
if (inputGrade > maxGrade) {
maxGrade = inputGrade; // calculate running Max
} 
if (inputGrade < minGrade) {
minGrade = inputGrade; // calculate running Min
}
// Running total of grades and number of grades --> get average at the end
numGrades += 1;
sumGrades += inputGrade;
}
printf ("Enter a grade: ");
scanf ("%lf", &inputGrade);
}

// compute average
avgGrade = sumGrades / numGrades;

// Output results
if (numGrades > 0) {
printf ("Number of grades: %d\n", numGrades);
printf ("Minimum Grade: %lf\n", minGrade);
printf ("Maximum Grade: %lf\n", maxGrade);
printf ("Average Grade: %lf\n", avgGrade);
} else {
printf ("Didn't enter grades\n")
}
return (EXIT_SUCCESS);
}
Monday 10.05.15
Posted by Kei-Ming Kwong
 

APS105 Tutorial 2

Mainly covers conditional statements, but once again going from code to program.  There are several key things to note here:

  • Constants are used to instead of "magic" numbers. 
  • Math library is used here
    • Netbeans --> Project properties --> Linker --> Libraries --> Standard libraries
/* 
 * File: main.c
 * Author: Kmingk
 *
 * Created on September 27, 2015
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Tutorial 2 - Advanced output
 * 
 * Program Description:
 * Build a program that takes a dollar (and cents) value less than 20
 * and rounds the value to the nearest nickel.It then computes the
 * smallest number of total bills and coins that sum to that rounded value
 * 
 * Inputs:
 *rawAmount - Double
 * Outputs:
 *roundedAmount - Double
 *numTens - int
 *numFives- int
 *numOnes 
 *numQuarters
 *numDimes
 *numNickels
 */
int main(int argc, char** argv) {
// Variable and Constants Declaration
const double TEN= 10;
const double FIVE = 5;
const double ONE= 1;
const double QRTR = 0.25;
const double DIME = 0.1;
const double NICKEL = 0.05;

double rawAmount, roundedAmount, savedRoundedAmount;
int numTens, numFives, numOnes, numQuarters, numDimes, numNickels;
 
// Get monetary value from user
printf ("Enter Value: ");
scanf ("%lf", &rawAmount);
if (rawAmount < 0) {
exit()
}

// Round value to nearest nickel
// round the normalized amount rawAmount/Nickels --> then multiply
// by the value of a nickel to get the rounded to nickel value
roundedAmount = rint(rawAmount / NICKEL) * NICKEL;
savedRoundedAmount = roundedAmount;
//printf ("%lf", roundedAmount);

// Calculating the number of each denomination, roundedAmount keeps a 
// rolling total of the remainder
numTens = roundedAmount/TEN; 
roundedAmount = roundedAmount - TEN*numTens; 
numFives = roundedAmount/FIVE; 
roundedAmount = roundedAmount - FIVE*numFives;
numOnes = roundedAmount/ONE;
roundedAmount = roundedAmount - ONE*numOnes;
numQuarters = roundedAmount/QRTR;
roundedAmount = roundedAmount - QRTR*numQuarters;
numDimes = roundedAmount/DIME;
roundedAmount = roundedAmount - DIME*numDimes;
numNickels = roundedAmount/NICKEL;
roundedAmount = roundedAmount - NICKEL*numNickels;

// Output Raw and Rounded amount with change needed
printf ("Raw Value: $%0.2lf\n", rawAmount);
printf ("Rounded Value $%0.2lf\n", savedRoundedAmount);
printf ("# Tens: %d\n", numTens);
printf ("# Fives: %d\n",numFives);
printf ("# Dollars: %d\n",numOnes);
printf ("# Quarters: %d\n", numQuarters);
printf ("# Dimes: %d\n",numDimes);
printf ("# Nickels: %d\n",numNickels);
}
Monday 10.05.15
Posted by Kei-Ming Kwong
 

APS105 Tutorial 1

The code is meant to take a program description (in comments) and convert that to code. Mainly uses boundary checks and conditions and input/output

  • If statement order matters --> Try for yourself and change the ordering of the if statement
  • The & in front of the variable means that we are passing an address. This is needed for scanf.
/* 
 * File: main.c
 * Author: user
 *
 * Created on September 21, 2015, 9:11 AM
 */

#include <stdio.h>
#include <stdlib.h>

/*
 * Build a program that computes a person's Body Mass Index. BMI is computed as 
 * BMI = Person's Weight (KG)/ Person's Height (m) ^2 and tell if a person is
 * overweight.Overweight BMI is 25.
 * 
 */

int main(int argc, char** argv) {
// Get user's height and weight
double height, weight, BMI;
printf("Please enter the height(m): ");
scanf("%lf", &height);
printf("Please enter the weight(kg): ");
scanf("%lf", &weight);

// Compute the BMI: BMI = w/h^2
BMI = weight / (height * height);

// Output BMI
printf("Your BMI \tis: %20.4lf\n", BMI);

// Tell if the user is overweight or not
if (BMI >= 40) {
printf("See a doctor?\n");
} else if (BMI >= 25) {
printf("Overweight\n");
} else {
printf("Normal\n");
}

return (EXIT_SUCCESS);
}
Monday 10.05.15
Posted by Kei-Ming Kwong