CIS2500 Lab One
Introductions
About Me
- Name: Jessy Williams
- Email: cis2500@socs.uoguelph.ca
About The Lab
- What: CIS2500 Intermediate Programming
- Who: Sections 5/6
- When: Thursdays from 3:30PM to 5:20PM
- Where: Thornbrough Building Room 2418 (upstairs labs)
- Outline: Here. (requires moodle login)
Lab Exam
- Labs are not graded, however the lab exam is (10% of final grade)
- Our lab exam is on Thursday, February 4th
- You must go to your own section (unless you have instructor approval)
RPI Setup
- You need a Raspberry Pi for this course
- A model B+ or newer is recommended
- Its recommended that you install SoCS OS
- If you don’t have a Raspberry Pi, Justin is selling kits throughout the week
- Make sure to bring an HDMI cord, SD card, micro USB cord, and power dongle
Expected Coding Conventions
- camelCase
- Use 4 spaces for indentation; no tabs
main
must be first function if present within a .c file- Compile with -std=c99
- Don’t have crazy long lines (80 or 100 chars max)
- Be consistent (bracket style, naming schemes, etc.)
- Reference available here (requires Moodle login)
Task One: Hello
Your first task is to write a program which prints “Hello, World!” to the stdout.
The Rules:
- You need to have a function named
hello
which takes a string, wraps it with “Hello, {str}!, and prints it to stdout - You need to separate your source files into the file hierarchy specified below
So something like this…
int main(int argc, char** argv){
hello("World");
hello("is it me you're looking for?");
}
Would give you this..
Hello, World!
Hello, is it me you're looking for?!
Setting Up Your File Hierarchy
- Put your .c files in ‘src’
- Put your .h files in ‘include’
- Put documentation in ‘docs’ (when warrented)
- Put sourced files in ‘assets’ (when warrented)
Project
├── assets
├── bin
├── docs
├── include
├── src
└── README
Compilation
- When you’re including headers outside of the working directory and system path you need to tell your compiler where they are
- We can do this with the
-I
flag (I for include) - Follow the flag immediately with the directory containing your headers without a space between the two
- ie.
gcc src/hello.c -Iinclude
- the ‘include’ part is just refering to the ‘include’ directory, its not some kind of special command; ie. if you wanted to put .h files in a directory called appleSauce, the command would be
gcc src/hello.c -IappleSauce
- the ‘include’ part is just refering to the ‘include’ directory, its not some kind of special command; ie. if you wanted to put .h files in a directory called appleSauce, the command would be
Extra: Include Guards aka Macro Guards
- Avoids double incursion; C and C++ have a ‘one definition rule’, if you define a function more than once you will have a bad time
- Use
ifndef
, which stands for ‘if not defined’ to skip on subsequent calls - Use caps and snake case when defining macros; use a unique name
At the top of the header file put:
#ifndef __USERNAME__MODULE_NAME__
#define __USERNAME__MODULE_NAME__
At the bottom of the header file put:
#endif
Solution
Task Two: A Bit of Fun With File I/O
Your second task is to write a program that takes a plaintext file as an argument from the command line and prints it out as upper case.
The Rules:
- You need to pass a plaintext file to your program using a command line argument
- You must read up to 500 chars from it
- You need to use
fgets
from<stdio.h>
- You need to convert characters to uppercase without using
string.h
,ctype.h
, or any other imported library - Lastly, you must print the results to stdout
ie. running ‘bin/uppercase heartOfDarkness.txt’, containing…
One ship is very much like another, and the sea is always the same. In the immutability of their surroundings the foreign shores, the foreign faces, the changing immensity of life, glide past, veiled not by a sense of mystery but by a slightly disdainful ignorance; for there is nothing mysterious to a seaman unless it be the sea itself, which is the mistress of his existence and as inscrutable as Destiny.
Outputs This:
ONE SHIP IS VERY MUCH LIKE ANOTHER, AND THE SEA IS ALWAYS THE SAME. IN THE IMMUTABILITY OF THEIR SURROUNDINGS THE FOREIGN SHORES, THE FOREIGN FACES, THE CHANGING IMMENSITY OF LIFE, GLIDE PAST, VEILED NOT BY A SENSE OF MYSTERY BUT BY A SLIGHTLY DISDAINFUL IGNORANCE; FOR THERE IS NOTHING MYSTERIOUS TO A SEAMAN UNLESS IT BE THE SEA ITSELF, WHICH IS THE MISTRESS OF HIS EXISTENCE AND AS INSCRUTABLE AS DESTINY.
Using fgets
- The expected way to read files in this course
- Part of
stdio.h
, the standard C Library fgets(char* str, int size, FILE* stream);
- Stores text in the first parameter,
str
size
specifies the maximum number of characters to be read; will also stop at a newline or end of file (EOF)stream
is aFILE*
, a variable which is set usingfopen
- Stores text in the first parameter,
Using fopen
- Also part of
stdio.h
, the standard C Library FILE* fopen(char* path, char* mode);
- Returns a FILE pointer
path
is either theabsolute
orrelative
path to the file you are trying to openmode
is way you want to open a file"r"
is used to open a file for reading"w"
is used to open a file for writing"a"
is used for appending to a file"+"
is used in conjunction with'r'
or'a'
to also allow for writing.
- Keep in mind that when you use a FILE type, your program stores a pointer of where you left off, so each subsequent call to
fgets
orfread
will advance this location
What’s in a char?
- C is referred to as ‘strongly typed but weakly enforced’
- Every type you declare a variable you need to specify its type, ie. int, char, float, etc.
- That being said there is nothing preventing you from treating type like another
- ie. you can use type casting
- …this can get you into trouble sometimes
- chars map to ints using the ASCII table
- This provides a way to compare chars and ints with each other intuitively
Pseudocode
main(passedTxtFile) {
/**
* Open text file
*/
textFile = openFile(passedTxtFile, read mode)
/**
* Print text before transformation
*/
print("Text Before:")
print(text)
/**
* Transform text, print results
*/
uppercase(text);
print("Text After:")
print(text)
}
/**
* uppercase
* transformes passed char array to uppercase and prints the result to stdout
* Arguments
* text
* the char array to transform
* Return Values
* None
*/
uppercase(text) {
index = 0
do while text at index exists:
currentChar = text at index
if currentChar >= 'a' and currentChar <= 'z':
text at index = text at index - 32
increment index
}