EE 355 Lab 3 - Algorithms & Control Structures

Similar documents
CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

CSE 374 Programming Concepts & Tools

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

EE 355 Lab 4 - Party Like A Char Star

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

CSE 410: Systems Programming

EE355 Lab 5 - The Files Are *In* the Computer

Debugging. John Lockman Texas Advanced Computing Center

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

1. Allowed you to see the value of one or more variables, or 2. Indicated where you were in the execution of a program

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Programming Studio #9 ECE 190

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CS 103 Lab - Party Like A Char Star

Exercise Session 6 Computer Architecture and Systems Programming

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSE 351. GDB Introduction

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 5

IDE: Integrated Development Environment

Lab 03 - x86-64: atoi

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

CS354 gdb Tutorial Written by Chris Feilbach

CS/COE 0449 term 2174 Lab 5: gdb

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Lab 8 - Vectors, and Debugging. Directions

CS 103 Lab 6 - Party Like A Char Star

Using gdb to find the point of failure

Using a debugger. Segmentation fault? GDB to the rescue!

Reviewing gcc, make, gdb, and Linux Editors 1

Control Flow: Loop Statements

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

To become familiar with array manipulation, searching, and sorting.

Scientific Programming in C IX. Debugging

Programming Tips for CS758/858

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Using the Command Line

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Debug for GDB Users. Action Description Debug GDB $debug <program> <args> >create <program> <args>

Introduction. Overview and Getting Started. CS 161 Computer Security Lab 1 Buffer Overflows v.01 Due Date: September 17, 2012 by 11:59pm

All about flow control

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Repetition CSC 121 Fall 2014 Howard Rosenthal

Computer Science II Lab 3 Testing and Debugging

Name Section: M/W T/TH Number Definition Matching (8 Points)

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

We first learn one useful option of gcc. Copy the following C source file to your

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Loops and Files. of do-while loop

CS 103 Lecture 3 Slides

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Chapter 2 Basic Elements of C++

GDB Linux GNU Linux Distribution. gdb gcc g++ -g gdb EB_01.cpp

Other Loop Options EXAMPLE

The First Real Bug. gdb. Computer Organization I McQuain

Data and File Structures Laboratory

EE 109 Lab 8a Conversion Experience

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

EP241 Computing Programming

Lab 1: Space Invaders. The Introduction

COSC 6374 Parallel Computation. Debugging MPI applications. Edgar Gabriel. Spring 2008

The CS-220 Development Environment

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

Section 1: Tools. Contents CS162. January 19, Make More details about Make Git Commands to know... 3

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Chapter Four: Loops II

Problem Set 1: Unix Commands 1

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

gdbtui - Linux Command

Lab 5 - Repetition. September 26, 2018

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Programming Assignment 1 (PA1) - Display Bowtie

Name Section: M/W T/TH Number Definition Matching (6 Points)

Code::Blocks Student Manual

Lecture 07 Debugging Programs with GDB

A Tutorial for ECE 175

COSC 4397 Parallel Computation. Debugging and Performance Analysis of Parallel MPI Applications

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such as GitHub.

Software Development With Emacs: The Edit-Compile-Debug Cycle

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Learning Objectives. A Meta Comment. Exercise 1. Contents. From CS61Wiki

Lab 4 - Lazy Deletion in BSTs

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #28. Functions: Examples 2

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

In either case, remember to delete each array that you allocate.

Basic functions of a debugger

Transcription:

1 Introduction In this lab you will gain experience writing C/C++ programs that utilize loops and conditional structures. This assignment should be performed INDIVIDUALLY. This is a peer evaluated lab submitted on Vocarium. Your grade will be determined by 2 peer evaluations. Please see the guidelines for peer evaluated labs provided by the instructor. 2 What you will learn After completing this lab you should be able: Find repeated structure/actions in an algorithm and use loops to represent them Use for and while loops to control iterations Use conditional statements ('if' statements) Use the 'gdb' debugger to step through code and 3 Background Information and Notes GNU Debugger (gdb) 3.1 Starting up GDB To compile a program with debugger symbols, use the -g flag when you compile. For programs with a Makefile, we'll usually include it for you. Once that is done you can debug the program. The command to run gdb is gdb EXECUTABLE_NAME Here you can run the executable using by entering run or r. If you need to add command line arguments, you can add them when you run the executable. For example: run ARG_1 ARG_2... 3.2 Breakpoints & Printing Info If your program has a segmentation fault, memory error, or other program-terminating problem, gdb will halt and let you know that you have hit an error. It is useful to stop the execution of your program at an arbitrary point. This can be accomplished by setting breakpoints. To set a breakpoint, use the break command. The break requires a line number to be specified, and if there are multiple source files, you must specify that too. For example: break LINE_NUMBER Last Revised: 1/29/2016 1

break sourcefile.cpp:line_number Once you hit a breakpoint, you can use the print and backtrace commands. When the program stops, there are a couple commands that might be helpful. print VARIABLE_NAME will print the value of a given variable name. This can be used to see what value is incorrect and is usually a good point to start looking for bugs. backtrace or bt will print the stack trace; this will show you the functions that have been called up to the point of the crash. To proceed with program execution, there are two commands you can use. continue or c to run the program until it encounters the next breakpoint or the end of execution. step or s to step through the program line by line. next or n to step through the program, but skip over function calls (i.e. treat the function call as one line of code rather than stepping through each subsequent line of the function call) To delete a breakpoint: delete BREAKPOINT_NUMBER To list all breakpoints: info breakpoints To quit gdb: quit 3.3 Summary gdb is a powerful tool, and we've only outlined the basics. A quick Google search can really help if you want to find some more commands to play with. 4 Procedure 4.1 Prime Time Write a program (must be named prime23.cpp )to determine if a natural number ( greater than 1) has only 2 and/or 3 as prime factors (but no other prime factors) and how many of each factor (2 and 3) it does have. Write your program from scratch (you can reference other examples to get started with the basic structure of a program) and name it prime23.cpp. The program should meet the following requirements: a. Prompt (print a message to) the user to enter a natural number. [i.e. use cout]. The prompt should read Enter a positive integer: b. Receive the integer input from the user. [i.e. use cin ] c. Implement your algorithm (using while loops and if statements). 2 Last Revised: 1/29/2016

d. Print either on one line and a count of 2 factors and a count of 3 factors (i.e. an input of 24 would print: Twos=3, Threes=1) on the next or just No if the number has factors other than 2 or 3. Example 1: Input is 30 No Example 2: Input is 18 Twos=1 Threes=2 Example 3: Input is 8 Twos=3 Threes=0 Example 4: Input is 9 Twos=0 Threes=2 Example 5: Input is 10 No Your output messages must be EXACLTY in the same format as shown above to get credit. Take care you create such an output. Compile and test your program. Whether your program works or not, we want you to practice using a debugger like gdb. To practice do the following: Start GDB Set a breakpoint at whatever line you wrote your first 'while' loop in your code and run the program to that breakpoint. Then type 'run'. Step through one iteration (line by line) of the code in the while loop for a few steps Print out the current value of some variable while stepping through your code (likely you should print the current value of the number your finding the factors of). In a file: gdbanswers.txt copy down the commands you would use to accomplish each of the bullet points above and submit it with your code files. 4.2 ASCII Art i The final product of this assignment will be built up in 3 parts. Name your file 'tri.cpp'. [Step 1] A right triangle could be drawn using the ASCII character and the cout function by using two for loops (one nested inside the other with the outer loop counting lines and the inner loop counting characters per line). Write a program to print out the s to form an isosceles right triangle 31 rows high (i.e. 1 on row 0, 2 s on row 1, etc.). You should get a triangle similar to the one shown below. Hint: You do not have to put a Last Revised: 1/29/2016 3

'\n' or endl character in every string that you print via cout but can print a newline only when you have printed "enough" asterisks Y axis... X axis 0 [Step 2] Except for distortions in the proportion of a Θ characters height/width (i.e. the font that Linux uses), the triangle above should be an isosceles triangle (i.e. 45 degrees for each of the non-right angles). We could generalize and pick an arbitrary number of degrees for the angle, Θ, shown below. Assume 15 Θ 75 and that the height of the triangle will be 31 text lines 30 running from a y-axis value of 0 to 30 (inclusive). [Step 2] Modify your program from step 1 to now query the user for a value of Θ between 15 and 75 (you can assume they will always comply and not give you a bad value) then print a triangle given a particular value of Θ. To solve this problem, try to derive a mathematical expression for the length of the x-axis (i.e. # of 's) as a function of Θ and the y-coordinates. Use the C++ constant M_PI in place of π. Then iterate through every line (y-coordinate) and calculate the appropriate x-coordinate (round down to get an integral value using the floor() function in math.h / cmath, if desired). The x- coordinate you compute will directly determine how many s are printed. Your program should prompt the user for the value of theta they would like to use. [Step 3] Modify the program in step 2 so that if the x-coordinate (the number of 's you print on a line) for any line falls within the range 20 x 30 then only print a line with exactly 20 s rather than the calculated value of x. This will make a sharp vertical cliff in the triangle for some number of lines (maybe none if theta is small) and then resume along the original diagonal line. Compile and test your program. 0 30 Θ for part c Note: DO NOT print SPACES between the s. 4 Last Revised: 1/29/2016

Enter the angle theta in degrees: 45 Enter the angle theta in degrees: 60 45-degree Sample Output (Note the 2 blank lines at the top of the triangle) Submit your final (step 3) version of tri.cpp on Vocareum. 60-degree Sample Output (Note the 1 blank line at the top of the triangle) 5 Evaluation Criteria 1. [1 pt.] Your prime number program produces the correct output for a number with only 2 s as prime factors. 2. [1 pt.] Your prime number program produces the correct output for a number with only 3 s as prime factors. 3. [2 pt.] Your prime number program produces the correct output for a number with at least one factor of 2, 3, and some other prime number (e.g. 5, 7, etc. where the answer is No) 4. [1 pt.] Your prime number program produces the correct output for a number with no 2 s and 3 s as prime factors. 5. [1 pt.] GDB commands were submitted and are reasonable 6. [1 pts.] Your triangle program produces the correct output with a square cutout for the appropriate lines for 45 degrees. 7. [1 pts.] Your triangle program produces the correct output with a square cutout for the appropriate lines for 30 degrees. 8. [1 pts.] Your triangle program produces the correct output with a square cutout for the appropriate lines for 60 degrees. 9. [1 pt.] Both programs were appropriately indented when opened in gedit on the Linux VM. i Adapted from Michael Locasto while he was at Cornell University Last Revised: 1/29/2016 5