ThePantsExist


Joined 2 years ago
Homeworks submitted:
Homework comments:
4
0

About Me

No description provided.

Classes

Programming in C

Class status: Established
Role: Student
. 8% complete

Submitted Assignments

Programming in C: Lesson 1, HW 4

Heads up to other people attempting this lesson: I just spent more time than necessary tracking down why the code examples in the book don't seem to work (Ubuntu 10.10, GCC 4.4.3).

An explanation of why: http://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar

TLDR: Input is buffered, getchar() will see all of your characters at once. Likely more examples from the book won't work "out of the book", so don't get discouraged.

Will edit to post finished code later.


ThePantsExist 2 years ago
Programming in C: Lesson 1, HW 3

Simpler than Exercise 1.4. Made a small modification to add brackets for aesthetic reasons.

#Exercise 1.5
#include <stdio.h>

main()
{
        int fahr;

        for (fahr = 300; fahr >= 0; fahr = fahr - 20)
        { //seems icky to not use brackets for blocks
                printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
        }
}

#Output:
#300  148.9
#280  137.8
#260  126.7
#240  115.6
#220  104.4
#200   93.3
#180   82.2
#160   71.1
#140   60.0
#120   48.9
#100   37.8
# 80   26.7
# 60   15.6
# 40    4.4
# 20   -6.7
#  0  -17.8

ThePantsExist 2 years ago
Programming in C: Lesson 1, HW 2

We're still in simple C territory here; watch your floats and your order of operations.

#exercise 1.3
#include <stdio.h>

main()
{
        float fahr, celsius;
        float lower, upper, step;

        lower = 0;
        upper = 300;
        step = 20;

        fahr = lower;
        printf("Degrees F  |  Degrees C\n");
        while (fahr <= upper) {
                celsius = (5.0/9.0) * (fahr-32.0);
                printf("  %3.0f         %6.1f\n", fahr, celsius);
                fahr = fahr + step;
        }
}

#Output:
#Degrees F  |  Degrees C
#    0          -17.8
#   20           -6.7
#   40            4.4
#   60           15.6
#   80           26.7
#  100           37.8
#  120           48.9
#  140           60.0
#  160           71.1
#  180           82.2
#  200           93.3
#  220          104.4
#  240          115.6
#  260          126.7
#  280          137.8
#  300          148.9

#Exercise 1.4
#include <stdio.h>

main()
{
        float fahr, celsius;
        float lower, upper, step;

        lower = -20.0;
        upper = 160;
        step = 20;

        celsius = lower;
        printf("Degrees C  |  Degrees F\n");
        while (celsius <= upper) {
                fahr = (celsius * (9.0/5.0)) + 32;
                printf("  %6.0f         %3.1f\n", celsius, fahr);
                celsius = celsius + step;
        }
}

#Output:
#Degrees C  |  Degrees F
#     -20         -4.0
#       0         32.0
#      20         68.0
#      40         104.0
#      60         140.0
#      80         176.0
#     100         212.0
#     120         248.0
#     140         284.0
#     160         320.0

ThePantsExist 2 years ago