Lab 2.1: Fixing a C++ program

Similar documents
VARIABLES & ASSIGNMENTS

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Lab01: C++ Expressions ES036a: Programming Fundamentals Fall 2007

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Installing and Using Dev-C++

Getting Started with Visual Studio

AP Computer Science Unit 1. Writing Programs Using BlueJ

Pointers II. Class 31

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.

Introduction to Java Unit 1. Using BlueJ to Write Programs

AP Computer Science Unit 1. Writing Programs Using BlueJ

Your First C++ Program. September 1, 2010

Homework 11 Program Setup (with some IMPORTANT NEW STEPS!)

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 2 C++ Fundamentals

Fundamentals of Programming CS-110. Lecture 2

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Add Subtract Multiply Divide

7/21/10 MOTIVATION. Did you see Inception this weekend? COMP 10 EXPLORING COMPUTER SCIENCE. Lecture 6 Arrays

Week 1: Hello World! Muhao Chen

Homework #3 CS2255 Fall 2012

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

CS 150 Lab 10 Functions and Random Numbers

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Constructors.

Spring CS Homework 12 p. 1. CS Homework 12

1 Anatomy of a Program 4

DELHI PUBLIC SCHOOL TAPI

CS242 COMPUTER PROGRAMMING

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

Security Coding Module Integer Error You Can t Count That High CS1

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

EAS230: Programming for Engineers Lab 1 Fall 2004

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

IDE: Integrated Development Environment

CS 150 Lab 3 Arithmetic and the Debugger. Lab 3.0 We are going to begin using the Visual Studio 2017 debugger to aid with debugging programs.

CS101 PLEDGED SPRING 2001

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Lab # 02. Basic Elements of C++ _ Part1

CS Homework 10 p. 1. CS Homework 10

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

Introduction to Programming EC-105. Lecture 2

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Scientific Computing

CS Week 14 Lab Exercise

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

CSci 1113 Midterm 1. Name: Student ID:

CSCE Practice Midterm. Data Types

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

clicking on the on the New

Binomial pricer (1.1)

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

Teaching Math thru Big Ideas Focusing on Differentiation. Marian Small April 2017 San Diego, CA

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

Exercise 1.1 Hello world

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Types of files Command line arguments File input and output functions Binary files Random access

Chapter 7 - Notes User-Defined Functions II

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Chapter 1 - What s in a program?

CS2141 Software Development using C/C++ C++ Basics

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Increment and the While. Class 15

Reading from files File errors Writing to files

Programming Language. Functions. Eng. Anis Nazer First Semester

Engineering Problem Solving with C++, Etter/Ingber

CIS 130 Exam #2 Review Suggestions

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Project Identity Shampoo-Conditioner Identification Device/ Medicine Reminder Week 2: 1/28/07-2/3/07 Karla Sittnick

CHAPTER 1: PROGRAM DEVELOPMENT LIFE CYCLE. Prepared for: CSC 128 Fundamentals of Computer Problem Solving

Chapter 1: An Overview of Computers and Programming Languages. Objectives. Objectives (cont d.) Introduction

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Computer Programming

do { statements } while (condition);

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

pointers + memory double x; string a; int x; main overhead int y; main overhead

Program Organization and Comments

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

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Lab: Supplying Inputs to Programs

Sequential Program Execution

Lab 9: Creating a Reusable Class

Chapter 1: Why Program? Computers and Programming. Why Program?

Functions, Arrays & Structs

Security Coding Module - Buffer Overflow Data Gone Wild CS1

Transcription:

CS 150 Lab 2 Introduction to Compiler Errors, Variables, Assignments and Output The purpose of today s lab session is to allow you to gain experience using primitive data types, constants, assignment statements and output statements. Each activity today will consist of a small number of computations and a set of output to display the results, but first you will correct the syntax errors in a C++ program. Lab 2.1: Fixing a C++ program A programmer was assigned the task of writing a C++ program that allows the user the ability to enter a name and a number. Then half of the number entered is displayed. The programmer is also charged with making the program user friendly. The programmer produced the following program: //********* // File: // Author: // Date: // Class: // Assignment: // Purpose: // //********* #include <iostream> #include <string> int main() { int number; ints total; string name; // get the user's name cout << "What is your first name? "; cin >> name; cout << name << ", today is a great day for lab " << "!" // get a number from the user cout << endl << "Let's start off by typing a number of your choice." << endl; cin >> number; // halve the number and store the result in total total = number / 0; // display the value that is stored in total above. cout >> total >> " is half the number you typed." >> endl; } return EXIT_SUCCESS; 1

Lab 2.1: Syntax & Runtime errors Create a project: The following instructions are for creating a C++ project called 02_1_FixErrors in your solution PUNetIdLabs. 1. Open up your solutions folder PUNetIDLabs that currently contains two projects 01_1_HelloWorld and 01_2_PayCalculator created from the last lab. 2. Create a new project called 02_1_FixErrors 3. Add a new source file main.cpp to the Source Files folder in the project 02_1_FixErrors. 4. Copy the program from fixerrors.cpp in the CS150-01 Public folder into main.cpp. You can open the file fixerrors.cpp using Visual Studio. 5. Set 02_1_FixErrors as the Startup Project and then build the project. The built program will give you errors, which are examples of a syntax error. You are likely to encounter many of these errors as you progress through this course, and it s important for you to learn to recognize & fix syntax errors. Each error message shows a line number in the file that contains the error. You can double click on the error message and Visual Studio will mark that line in the editor with a blue bar on the left of the line numbers. Take a look at the marked lines and fix the errors. Q. How many total errors did the compiler list? A. Q. Which line numbers contained an error? A. Q. Explain what caused the first error: Q. Explain what caused the very last error: Eliminate ALL syntax errors. Do not fix anything that is not a syntax error. 2

Build your program which should produce a warning. You need to scroll up in the Output window to find the warning message. Q. What is the warning message? Note: When you turn in a working program, there are to be no syntax errors AND no warning messages. Q. What happens if you run your program without eliminating the warning message? This error is called a runtime error, meaning the error occurs when you run the software. The program builds but one or more of the operations the programmer asked the computer to perform is incorrect. Think about the error that you see and then look in the code to try to find the line of code that contains the runtime error. Fix the error, rebuild the software, and test it again. Run your program and test your program with the following test cases: Testcase #1: John 10 Testcase #2: Sara 9 Q. Do you feel that you are getting valid output? Why or why not? 3

Lab 2.2 For this lab you will need to write a new program! Creat your fourth project called 02_2_NumberCruncher in your solutions folder PUNetIDLabs. What does this program need to do? This program must ask the user for two integers. The program must then display the sum of the integers and the difference of the integers. Example input and output is shown below. Your program's output should look exactly like the output below. The large, bold text is data input by the user. Number Cruncher 1.0 Please enter an integer: 9 Please enter an integer: 100 Sum: 9 + 100 = 109 Difference: 9 100 = -91 BEFORE YOU START PROGRAMMING answer these questions: Q: What data does your program need to get from the user? Q: What declaration statements do you need for these data? Write a completely documented program to produce the above output. 4

Once both projects are completed, place your solution PUNetIDLabs into the CS150-01 Drop folder on Turing. Your solution is to contain all four projects completely working and correct. Optional Challenge: You do not need to submit the optional challenge! For this lab you will create a new Visual Studio Project named 02_3_GeometryHelper in your solutions folder PUNetIDLabs. The goal of this program is to calculate the area and perimeter of a rectangle and then display both of these values on the screen. When displaying each value to the screen, display the formula used to calculate that value as well. You may use the previous program as a guide. Ask the user for the length and width of the rectangle as well as the unit of measurement used. Allow the user to provide a number with a decimal point. Make sure your program works with a length of 4.1 and width of 1.9. Example input and output follows. Geometry Helper 1.0 What is the length and width of the rectangle? 4.1 1.9 What is the unit of measurement? meters Perimeter: 4.1 + 4.1 + 1.9 + 1.9 = 12 meters Area 4.1 * 1.9 = 7.79 square meters Q: What data does your program need to get from the user? Q: What declaration statements do you need for these data? Be sure to add comments to your code to detail how each variable is used and the purpose of each calculation. 5