EECS 211 Lab 2. Getting Started. Getting the code. Windows. Mac/Linux

Similar documents
CMSC 201 Spring 2017 Lab 01 Hello World

CMSC 201 Spring 2018 Lab 01 Hello World

15-122: Principles of Imperative Computation

The Edit-Compile-Run Cycle. EECS 211 Winter 2017

COMP s1 Lecture 1

Using the Zoo Workstations

When you first log in, you will be placed in your home directory. To see what this directory is named, type:

Lab 1 Introduction to UNIX and C

1) Log on to the computer using your PU net ID and password.

CMSC 201 Fall 2018 Lab 04 While Loops

STA 303 / 1002 Using SAS on CQUEST

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Unit 13. Linux Operating System Debugging Programs

Linux File System and Basic Commands

Operating System Interaction via bash

Introduction to Unix - Lab Exercise 0

Laboratory 1 Semester 1 11/12

Lab 4. Out: Friday, February 25th, 2005

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Text Input and Conditionals

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Computer Science 62 Lab 8

Lab 1 Introduction to UNIX and C

Lab 1: Setup 12:00 PM, Sep 10, 2017

One of the hardest things you have to do is to keep track of three kinds of commands when writing and running computer programs. Those commands are:

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Lab 1: Getting Started with Linux The Extremely Illustrated Version. Graham Northup

No Food or Drink in this room. Logon to Windows machine

CS CS Tutorial 2 2 Winter 2018

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Section 1: Let s Shake Off the Rust!

Week 3. This is CS50. Harvard University. Fall Cheng Gong

Remote Access to Unix Machines

CS1600 Lab Assignment 1 Spring 2016 Due: Feb. 2, 2016 POINTS: 10

LAB #5 Intro to Linux and Python on ENGR

Using Ruby and irb in CSE 341 Winter 2015

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Unit 10. Linux Operating System

oit

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

Commands are in black

Programming Studio #1 ECE 190

Linux at the Command Line Don Johnson of BU IS&T

CSE115 Lab exercises for week 1 of recitations Spring 2011

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

oit Using HTPASSWD to Password Protect Pages on Campus Web Server Web sites UMass Offi ce of Information Technologies

CS 460 Linux Tutorial

CS50 Supersection (for those less comfortable)

Warm-up sheet: Programming in C

Laboratory 1: Eclipse and Karel the Robot

1 Installation (briefly)

Lab #2 Physics 91SI Spring 2013

CS : Computer Programming, Spring 2000 Programming Project #1: Thinking of a Number Due in class Thursday 27 January 2000

Instructions PLEASE READ (notice bold and underlined phrases)

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Physics REU Unix Tutorial

T H E I N T E R A C T I V E S H E L L

Chapter-3. Introduction to Unix: Fundamental Commands

Once you have installed MobaXterm, open MobaXterm. Go to Sessions -> New Session, and click on the SSH icon.

CS Operating Systems, Fall 2018 Project #0 Description

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lecture Transcript While and Do While Statements in C++

Tutorial 1: Unix Basics

Create a Login System in Visual Basic. Creating a login system. Start a new visual basic Windows Forms application project. Call it Login System

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Intermediate Programming, Spring Misha Kazhdan

sftp - secure file transfer program - how to transfer files to and from nrs-labs

The Command Shell. Fundamentals of Computer Science

Spring 2017 Gabriel Kuri

Introduction to the Linux Command Line

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

CMSC 201 Fall 2016 Lab 13 More Recursion

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Installing the WHI Virtual Private Network (VPN) for WHIX Users Updated 12/16/2016

Peer to Peer Instant Messaging

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Network Administration/System Administration (NTU CSIE, Spring 2018) Homework #1. Homework #1

Lab 0: Introduction to the FPGA Development Board

Users Manual. OP5 System 2.4. OP5 AB. Page 1 of 6

Computer Programming

Programming Studio #1 ECE 190

HTML/CSS Lesson Plans

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

CS Fundamentals of Programming II Fall Very Basic UNIX

Lab 4: Bash Scripting

C++ Programming on Linux

Computer Science II Lecture 1 Introduction and Background

UNIX Tutorial One

A Basic Guide to Using Matlab in Econ 201FS

9 and 11-Jan CSCI 4152/6509 Natural Language Processing Lab 1: FCS Computing Environment, SVN Tutorial. FCS Computing Environment, SVN Tutorial

Intro. Scheme Basics. scm> 5 5. scm>

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

Access Intermediate

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

EECS 211 Lab 6. Classes and Abstractation Winter Getting the code. Encapsulation and Abstraction. Classes

LAB 0: LINUX COMMAND LINE AND SVN

Linux Survival Guide

Lab 6 Vectors and functions

Transcription:

EECS 211 Lab 2 Control Statements, Functions and Structures Winter 2017 Today we are going to practice navigating in the shell and writing basic C++ code. Getting Started Let s get started by logging into a remote Northwestern server. We did this last week, but if you need help remembering the steps, they are included below. Windows The list of remote Northwestern servers can be found here: http://www.mccormick. northwestern.edu/eecs/documents/ current-students/student-lab-hostnames.pdf Open PuTTY. You ll need to enter a hostname to login to. The link on the right will take you to a list of student lab hostnames (such as tlab-03.eecs.northwestern.edu or batman.eecs.northwestern.edu). Ensure SSH is selected, then press Open. When prompted, enter your EECS username and password (not necessarily the same as your NetID password) and you re good to go. Mac/Linux Open up your terminal. At the prompt, use the ssh command of the form $ ssh [eecs-id]@[eecs-host].eecs.northwestern.edu where [eecs-id] is your EECS username (probably your NetID) and [eecs-host] is replaced by one of the EECS hostnames from the list of student lab hostnames (such as tlab-03.eecs.northwestern.edu or batman.eecs.northwestern.edu). When prompted, type in your EECS username and password (not necessarily your NetID password), press Enter again, and you should be logged in remotely! Getting the code Recall our basic Unix commands: cd, ls, mkdir, and pwd. What do they stand for and what do they do? Ask your TA if you don t remember. Use the following wget command to download the code into your directory of choice. It s easiest to just use your home directory, which is where you start out when you first log in to the server. Or ask Google. $ wget http://users.eecs.northwestern.edu/~jesse/course/eecs211/lab/eecs211-lab02.zip

eecs 211 lab 2 2 Once we have our ZIP file, we will need to turn it in file directory using the unzip command. $ unzip eecs211-lab02.zip You should now have a directory called eecs211-lab02. Setting up the build system Change your directory to eecs211-lab02. Make a new subdirectory called build and navigate into it. Then, the first time you set up the project, use the command cmake in order to setup the CMake build system. $ cmake.. Note the two periods, which tell cmake to run on the parent of the current working directory. Writing the code Navigate to your eecs211-lab02 directory, and open up lab2.cpp in Emacs using $ emacs -nw lab2.cpp Notice that there is already some skeletons of functions and some code in main() here. Iteration First, find the function called sumnumbers.we are going to use this function to sum up all of the numbers from 1 to num. If you remember from class, we have a few ways of iterating through numbers, most notably for and while. We will be using both, but first we will be using while. Notice that sumnumbers is going to return an int. While loops As we learned in class, a while loop has the following syntax: while (<expr>) { Note that in while loops we usually will use a boolean expression for <expr> (an expression which returns True or False) // Looping through code here // Until <expr> is false Use a while loop inside our sumnumbers in order to add the numbers from 1 to num together. Make sure to use a return statement Remember that we have the ++ and += to return the sum that we aggregated! functions to help us. Once you think that your function works as intended, save and exit emacs, then navigate to the build directory. If you remember C-x C-s to save and C-x C-c to exit

eecs 211 lab 2 3 from last week, we used the make command in order to turn our C++ file into machine code. Making sure you are in the build directory, type in $ make lab2 If everything works, if we list our files, we should now see a file called lab2. Enter the command $./lab2 See if your value looks right! If it doesn t, don t worry, Rome wasn t built in a day. Go back to your lab2 directory and try and see what went wrong. Play around with the value of numbound and see how it affects the result. Error messages may look scary, but in reality, they re there to help you! Not intimidate you! For loops Once we have everything working with our while loop, let s work on using a for loop. To help you remember the syntax, here is an example of a for loop to print out the values from 1 to 5 for (int i = 1; i <= 5; ++i) { // Note that the code inside the curly braces is the code // That is ran each iteration of the for loop cout << i << \n ; Go back to our sumnumbers function, and try to replace your while loop expression with a for loop to accomplish the same task. Once you are done, again, navigate into your build directory, then make and run your file. See if everything looks the same! If not, no worries, go back and try again! Structs Structs are an important tool in C++ for grouping data together. In your lab2.cpp you will notice that we created an Apple structure for you. This is so that we can organize attributes of Apples together in a convenient way. If you look inside our Apple struct, we decided that we will want to know the weight, the variety, and the color of our apples. In our main(), we created an example of a Red Delicious Apple. Now, create your own type of apple (you ll need to define it as a type Apple), and give it those three attributes. Add in a print statement (we gave you an example one) to print out your new Apple. Navigate back to your build directory, and then make and run your lab2 file! Hopefully everything works as expected! If not, don t fret or get upset, go back and make changes! How bout dem apples?

eecs 211 lab 2 4 Now that we know all about Apples, let s create our own structure. Define a structure of your favorite animal, and give your animal three attributes, with one of them being age. Don t forget to give your attributes types. You can create a new struct that looks very similar to the Apple we created. Once you have created your animal, go into main() and create an instance of your animal, assign it those three attributes, and then create a print statement to print out information about your animal. These print statements are getting annoying; we ll tackle that soon. Navigate back to your build directory, make and run lab2, and see if your new animal shows up the way that you intended. Hopefully everything works! If not, as usual, go back and try and find what went wrong and update your code. Don t forget the semi-colon after the closing brace. Creating your own function So far, we ve been filling in skeleton functions that were provided for you. Now it s time to write your own function from scratch. Remember how annoying it was to type out the cout lines each time you wanted to print out your animal? We re going to abstract that out and replace it with a simple call to a function! Write a function called printanimal that uses cout to print out your animal s three attributes. Note that this should take in one argument(of the same type as your animal struct). Think about what type your function should be! Once you wrote your function, go to your main function and replace your print statement with a call to printanimal(). Go back to your build directory, make and run lab2, and see if everything still works! Note that the void return type signifies that nothing is returned remember to pass your animal instance to the function Control statements Now that we have gotten the hang of structs and functions, let s practice our control statements. Go back to our printanimal function. Remember from hw1 and class that if statements have the basic following syntax: if (<test-expression>) { // run if <test-expression> evaluates to true else { // run if <test-expression> evaluates to false // note that you don t necessarily need an else case Using an if and else statement, check your animals age and add to your printanimal function a line to print out This animal is old! if

the animal is at least 10 and print out This animal is not that old if the animal is younger than 10. Go back to the build directory, make and run lab2, and see if this feature is working! eecs 211 lab 2 5