COMP1917 in a Day

WARNING!
This guide contains strong language, hipster-bashing, blatant sexism, and other acts which may be perceived as inappropriate.
If you can not tolerate pure awesomeness, have a weak stomach, or lack balls of steel, then don't say we didn't warn you.
Introduction
Choice of Operating System
The Linux Command Line
Chapter 1: Hello, World!
Chapter 2: Variables
Chapter 3: Input
Chapter 4: If Statements
Exercise: Leap Years
Chapter 5: Loops
Chapter 6: Functions
Chapter 7: Arrays
Recap 1
Chapter 8: Pointers
Chapter 9: Strings
Chapter 10: Structures

For the love of code...

           #########           #########          
       #################   #################      
     #########################################    
    ###########################################   
   #############################################  
   #############################################  
   #############################################  
   #############################################  
    ###########################################   
    ###########################################   
     #########################################    
       #####################################      
        ###################################       
          ###############################         
             #########################            
               #####################              
                  ###############                 
                      #######                     
                         #                        
Download: heart.c
Toggle line numbers
01: #include <stdio.h>
02: 
03: #define HT 25
04: #define WD 50
05: 
06: float power(float base, int index);
07: 
08: int main(int argc, char *argv[]) {
09: 
10:     int grid[WD][HT]; //the grid that will store the shape
11: 
12:     int i, j; //counters
13: 
14:     float x, y; //for storing x and y values of points
15: 
16: 
17:     //store the heart shape as a grid of 1s and 0s
18:     for (i = 0;i<HT;i++) {
19:         
20:         y = (i - HT/2)/(HT/3.0);
21: 
22:         for (j = 0;j<WD;j++) {
23:             
24:             x = (j - WD/2)/(WD/2.5);
25:             
26:             //check if a the point lies in the heart shape
27:             if (power((power(x, 2) + power(y, 2) - 1), 3) - 
28:                 (power(x, 2)*power(y, 3)) <= 0) {
29: 
30:                 //if so store a 1
31:                 grid[j][HT-i-1] = 1;
32:             } else {
33:                 //otherwise store a 0
34:                 grid[j][HT-i-1] = 0;
35:             }
36:         }
37:     }
38: 
39:     //print a heart of hashes
40:     for (i = 0;i<HT;i++) {
41:         for (j = 0;j<WD;j++) {
42:             if (grid[j][i] == 1) {
43:                 printf("#");
44:             } else {
45:                 printf(" ");
46:             }
47:         }
48:         printf("\n");
49:     }
50: 
51: 
52:     return 0;
53: }
54: 
55: float power(float base, int index) {
56:     float answer = 1;
57:     int i;
58: 
59:     for (i = 0;i<index;i++) {
60:         answer *= base;
61:     }
62: 
63:     return answer;
64: }
DISCLAIMER: This is an unofficial, unendorsed guide.
This guide is written by previous students based on their own experience. Everything in this guide should be taken as advice only.
When in doubt, consult your tutor and/or lecturer.
index Table of Contents