EAS230: Programming for Engineers Lab 1 Fall 2004

Similar documents
Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

C Functions. 5.2 Program Modules in C

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Function Call Stack and Activation Records

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Functions and Recursion

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Fundamentals of Programming Session 25

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Programming Assignment #4 Arrays and Pointers

CSE123. Program Design and Modular Programming Functions 1-1

Chapter 3 - Functions

CS 150 Lab 10 Functions and Random Numbers

Chapter 3 - Functions

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

6.5 Function Prototypes and Argument Coercion

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

ECET 264 C Programming Language with Applications

Lecture 04 FUNCTIONS AND ARRAYS

C Functions Pearson Education, Inc. All rights reserved.

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

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

STUDENT LESSON A14 Boolean Algebra and Loop Boundaries

Using the Command Line

Lab 2.1: Fixing a C++ program

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

ECE15: Lab #3. Problem 1. University of California San Diego ( 1) + x4. + x8 + (1)

CIS 130 Exam #2 Review Suggestions

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

EE109 Lab Need for Speed

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

How to approach a computational problem

Upgrading BankLink Books

LAB: WHILE LOOPS IN C++

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

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

JAVASCRIPT LESSON 4: FUNCTIONS

CE221 Programming in C++ Part 1 Introduction

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Programming. C++ Basics

C++ PROGRAMMING SKILLS Part 4: Arrays

COMPUTER SCIENCE (083)

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

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

Chapter 5 - Functions

Unit 13. Linux Operating System Debugging Programs

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

CSci 1113 Lab Exercise 5 (Week 6): Reference Parameters and Basic File I/O

C++ Support Classes (Data and Variables)

Week 1: Hello World! Muhao Chen

COL100 Lab 9. I semester Week 9, To be able to write C programs involving functions and recursion.

C: How to Program. Week /Apr/23

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

LAB 4.1 Relational Operators and the if Statement

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Introduction to Programming using C++

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

Programming Language. Functions. Eng. Anis Nazer First Semester

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

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

AMCAT Automata Coding Sample Questions And Answers

CPSC 427: Object-Oriented Programming

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Why Is Repetition Needed?

ECE15: Lab #4. Problem 1. University of California San Diego

clicking on the on the New

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

BRAZOSPORT COLLEGE LAKE JACKSON, TEXAS SYLLABUS COSC 1320: INTRODUCTION TO C++ PROGRAMMING COMPUTER TECHNOLOGY & OFFICE ADMINISTRATION DEPARTMENT

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

From Pseudcode Algorithms directly to C++ programs

Companion C++ Examples

ECE 2036 Lab 1: Introduction to Software Objects

Why VC++ instead of Dev C++?

CPE Summer 2015 Exam I (150 pts) June 18, 2015

University of Cincinnati Department of Electrical and Computer Engineering EECE180 Software Methods I

CME 112- Programming Languages II. Week 1 Introduction, Scope Rules and Generating Random Numbers

1 Unit 8 'for' Loops

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Lab 9: Pointers and arrays

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

COMP322 - Introduction to C++ Lecture 01 - Introduction

6. Pointers, Structs, and Arrays. 1. Juli 2011

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

CS 103 Lab - Party Like A Char Star

CS 106B, Lecture 1 Introduction to C++

Computer Programming

COMP 110 Programming Exercise: Simulation of the Game of Craps

Transcription:

Lab1: Introduction Visual C++ Objective The objective of this lab is to teach students: To work with the Microsoft Visual C++ 6.0 environment (referred to as VC++). C++ program structure and basic input and output statements. To type, edit, compile, link, and execute basic C++ programs. To identify errors (syntax, runtime and logic errors) in C++ and correct (debug) them. To add internal documentation (comments) o At the top of the code (a header) which contains student identification (name, person #, etc.) and information about the project (project name, purpose, etc.). A template for this information is given below. o Inside the code which explains the program statements. To bundle the programs and submit the lab work online. Description For this lab, you will type in, edit, compile, link, execute, and debug three (3) C++ programs in the VC++ environment. You will be given the code for each program. Each program will contain at least one error which you need to identify and correct. You will be graded on the identification of each error and the correct execution of the resulting code. Three types of errors have been introduced: 1. Syntax errors (e.g., typos, grammar mistakes) 2. Run-time errors (e.g. data errors, arithmetic errors) 3. Logic errors (e.g., algorithmic errors, design errors) There are three (3) parts to this lab: 1. Hello World Program (25%) 2. Percent Error Program (30%) 3. Craps Program (45%) Lab Setup If you are working in Furnas 211, you will have to log into the Ubiquity environment and start VC++ to work on the programs. The TAs will show you how to do this during your first lab session. You may also work on your own personal computer or other computers around the campus that support VC++. The software is free at UB Micro. For details on how to install it, visit UB Micro in the UB Commons. Visual C++ Environment A project in the VC++ environment is a program. A workspace is a folder in which all project-related information is stored. When you create a project you may create it in a new workspace or add it to an existing workspace. Refer to the handout Working with VC++ programs for more details. Do the following: 1. Create a workspace called lab1. 2. Create a project for each program and add it to the lab1 workspace. Create project names as follows: a. HelloWorld b. PercentError c. Craps Assigned: 9/8 Due: 9/22 Midnight Page 1 of 5

Template for Program Header Place the following code at the beginning of every source code file that you submit for this class. /************************************************************** * NAME : your name * * PERSON NUMBER : your person number * * PROGRAM : Lab name * * PURPOSE : 1-2 line summary of the purpose of the lab * * DATE : Date of last update * * PLATFORM : Microsoft Visual C++ 6.0 Pro * * Known bugs: * **************************************************************/ On-line submission of your code All source code (.cpp) will need to be submitted using the on-line command which will be given to you during the lecture and lab. The procedure for submission will be discussed in future lectures and labs. Part 1: Hello World Program 25% This program is the most basic program that can be written. All it does is print out a single line to the screen and exits. This program is worth 25% of the grade. The evaluation is as follows: 1. What were the errors? 5% 2. What kind of errors were they? 5% 3. What lines were they on? 5% code 10% Create a new project called HelloWorld and add it to the workspace lab1. Type it in exactly as it is given and save it as HelloWorld.cpp. The code for this program is as follows: cout << Hello World << endl Assigned: 9/8 Due: 9/22 Midnight Page 2 of 5

Program 2: Percent Error Program 30% This program is slightly more complex than the Hello World program as it uses variables and queries the user for input. It calculates the percent error between the expected and actual results and displays it to the user. Run the program with the following two data sets: 1. expectedresult = 100 and the actualresult = 90 2. expectedresult = 0 and the actualresult = 10 This program is worth 30% of the grade. The evaluation is as follows: 1. What was the error? 5% 2. What kind of error was it? 5% 3. What line was it on? 5% code 15% Create a new project called PercentError and add it to the workspace lab1. Type it in exactly as it is given and save it as PercentError.cpp. The code for this program is as follows: double expectedresult, actualresult; double percenterror; cout << "Enter the Expected Value:\t"; cin >> expectedresult; cout << "Enter the Actual Value:\t\t"; cin >> actualresult; cout << "The Percent Error is:\t\t" << ((expectedresult - actualresult) / expectedresult) * 100 << " % \n"; Assigned: 9/8 Due: 9/22 Midnight Page 3 of 5

Program 3: The Game of Craps Program 45% This program is more complex than the Percent Error program as it uses flow control structures. The program allows a user to play the game of Craps. This program is worth 45% of the grade. The evaluation is as follows: 1. What were the errors? 5% 2. What kinds of errors were they? 5% 3. What lines were they on? 5% code 20% 5. Demonstrate compiling, linking, and executing the program 10% Create a new project called Craps and add it to the workspace lab1. Type it in exactly as it is given and save it as Craps.cpp. The code for this program is as follows: #include <time.h> #include <math.h> #include <stdlib.h> int rolldice( void ); // function prototype enum Status CONTINUE, WON, LOST ; int sum, mypoint; Status gamestatus char ch; srand( time( 0 ) ); sum = rolldice(); // first roll of the dice switch ( sum ) case 7: case 11: gamestatus = LOST; case 2: case 3: case 12: gamestatus = WON; default: // win on first roll // lose on first roll // remember point gamestatus = CONTINUE; mypoint = sum; cout << "Point is " << mypoint << endl; // optional while ( gamestatus == CONTINUE ) // keep rolling cout << "Hit any character and Retrun to start a new roll \n"; cin >> ch; sum = rolldice(); Assigned: 9/8 Due: 9/22 Midnight Page 4 of 5

if ( sum == mypoint ) // win by making point gamestatus = WON; else if ( sum == 7 ) // lose by rolling 7 if ( gamestatus == WON ) cout << "Player wins" << endl; else cout << "Player loses" << endl; int rolldice( void ) int die1, die2, worksum; die1 = 1 + rand() gamestatus = LOST; % 6; die2 = 1 + rand() % 6; worksum = die1 - die2; cout << "Player rolled " << die1 << " + " << die2 << " = " << worksum << endl; return worksum; /********************************************************************** * (C) Copyright 2000 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these* * programs or to the documentation contained in these books. The * * authors and publisher shall not be liable in any event for * * incidental or consequential damages in connection with, or arising * * out of, the furnishing, performance, or use of these programs. * *********************************************************************/ Assigned: 9/8 Due: 9/22 Midnight Page 5 of 5