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

Similar documents
Computer Programming : C++

CHAPTER 3 BASIC INSTRUCTION OF C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

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

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Fundamentals of Programming CS-110. Lecture 2

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Getting started with C++ (Part 2)

Chapter 1 INTRODUCTION

Objectives. In this chapter, you will:

Creating a C++ Program

The C++ Language. Arizona State University 1

C++ Support Classes (Data and Variables)

C++ Programming: From Problem Analysis to Program Design, Third Edition

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Understanding main() function Input/Output Streams

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

BITG 1233: Introduction to C++

Introduction to Programming EC-105. Lecture 2

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

LECTURE 02 INTRODUCTION TO C++

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

2 nd Week Lecture Notes

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Program Organization and Comments

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.

UNIT- 3 Introduction to C++

In this lab, you will learn more about selection statements. You will get familiar to

Computer Science II Lecture 1 Introduction and Background

Introduction to Programming using C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

REPETITION CONTROL STRUCTURE LOGO

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSc Introduction to Computing

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

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

REVIEW. The C++ Programming Language. CS 151 Review #2

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS242 COMPUTER PROGRAMMING

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

Chapter 2: Overview of C++

Software and Programming 1

DEPARTMENT OF MATHS, MJ COLLEGE

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Introduction to the C++ Programming Language

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

4. Structure of a C++ program

6.096 Introduction to C++ January (IAP) 2009

A First Program - Greeting.cpp

download instant at Introduction to C++

Tutorial 2: Compiling and Running C++ Source Code

Chapter 1 - What s in a program?

Lab Instructor : Jean Lai

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Course Outline. Introduction to java

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

Lecture 3 Tao Wang 1

Chapter 1 Introduction to Computers and C++ Programming

Lecture 2 Tao Wang 1

Introduction to Programming

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 2 C++ Fundamentals

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Input And Output of C++

BTE2313. Chapter 2: Introduction to C++ Programming

Chapter 2 Basic Elements of C++

Programming. C++ Basics

COMP322 - Introduction to C++ Lecture 01 - Introduction

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

A Freshman C++ Programming Course

Eng. Mohammed S. Abdualal

Chapter 4 - Notes Control Structures I (Selection)

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

INTRODUCTION TO PROGRAMMING

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

Variables. Data Types.

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

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

VARIABLES & ASSIGNMENTS

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

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

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

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

Introduction to Programming

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

CSCI 1061U Programming Workshop 2. C++ Basics

CSCE 110 PROGRAMMING FUNDAMENTALS

Software and Programming 1

Transcription:

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ 0.1 Introduction This is a session to familiarize working with the Visual Studio development environment. It can be used to create various applications such as simple store apps, games for mobile, and complex systems that power enterprises and data centers. Visual Studio by default supports C#, C and C++, JavaScript, F#, and Visual Basic. Here we are going to work with C++. Also this session is going to give you an understanding of the following C++ Concepts: Variables, Data Types and Variables Declaration Reserved Words Special Characters Operators Arithmetic Assignment Comparison Logical Algorithmic operations: o Conditional operation 0.2 Preliminaries Make sure you have access to your home directory. Visual Studio should create a place for all of your workspaces in c:\documents\visual studio\projects. Check that in this case. 0.3 Starting and Ending Visual Studio You can find out which edition of Visual Studio is right for you at Visual Studio Editions. You can install Visual Studio 2015 by downloading it from Visual Studio Downloads. If you need to know more about the installation process, see Installing Visual Studio 2015. Note that any version you download from the Visual Studio website might be slightly different to the versions that we are running on the department machines, but all of the concepts should be the same. On department machines, Visual Studio can be found through the following menus: Start Button-All Apps Visual Studio This program works similar to any other software product, i.e. you open up the program, and you then either create a new program or load an existing program. When you have finished, you exit Visual Studio by selecting File-Exit. Advanced Structured Programming Page 1 of 10

0.4 Visual Studio IDE When you create an application in Visual Studio, you first create a project. To create a console application deploy the following steps 1. On the menu bar, choose File, New, and Project. 2. In the Visual C++ category, choose the Win32 Console Application template, then name the project and Press Ok. 3. When the Win32 Application Wizard appears, choose the Finish button. The project with the basic files for a Win32 console application is created as shown below. Advanced Structured Programming Page 2 of 10

The components of the Visual Studio IDE that we will be working with are as follows: a) Code Editor Window for editing source code manually. b) Error List Window for displaying information about a specific error message. Next, you'll add code in the Code Editor Window. To display Welcome to Lab 0 in the console window 1. In the code editor window enter a blank line before the line return 0. 2. Enter the following code cout << "Welcome to Lab 0\n"; A red squiggly line appears under cout. An error message appears if you point to it. This error message cout is included in the <iostream> header file also appears in the Error List window. You can display the window by going to the menu bar, choosing View, then Error List. Advanced Structured Programming Page 3 of 10

3. To include the iostream header, enter the following code after this header #include "stdafx.h". You probably noticed that a box appeared as you entered code, providing suggestions for the characters that you entered. This box is part of C++ IntelliSense, which provides coding prompts, including listing class or interface members and parameter information. You can also use code snippets, which are pre-defined blocks of code. For more information, see Using IntelliSense and Code Snippets. Note that: The red squiggly line under cout disappears when you fix the error. 4. Save the changes to the file. You can debug your application now to see whether the sentence Welcome to Lab 0 appears in console window. To debug the application Start the debugger by choosing Debug from menu bar, and Start without Debugging The debugger starts, and a console window appears showing as below. Advanced Structured Programming Page 4 of 10

Note: You can press SHIFT + F5 to stop debugging. The components of this simple program are as follows: stdafx.h describes both standard system and project specific include files that are used frequently but hardly ever change. iostream provides basic C++ standard library input and output services. using namespace std is used to replace std:: in each sentence Without when you write for example cout <<; you'd have to put std::cout <<;. Program Statements, Every program statement must end with a semi-colon ";". main is a section where any code within it will be executed when we run our program. The Code needs to be placed within the braces that follow the definition of main as below: 0.5 Data Types, Variables Declaration, and Statements Types Variable is a symbol whose value can change during the execution of a program. Variables must: have a name and a type. contain upper or lower case letters, numbers & underscore character _. Names must begin with an alphabetical character. - Valid names Fred, X, y, Salary, abc12, Z_1 - Invalid names 54p, @Hello, John*, Zero#, Temp, 12 Advanced Structured Programming Page 5 of 10

0.5.1 Data Types 0.5.2 Variables Declaration and Initialization Syntax: <Type> <Name> = <Expression>; Example: bool b1 = true; char c; int a =10; float b =2.5; string text; byte z = 22; 0.5.3 Statements Types: - Declaration statements string name; int y, x= 100; x+=y; - Executable statements cin >> name; cout << Hello << name <<endl; 0.6 Reserved Words cannot be used for other purpose Example: bool, break, byte, case, catch, char, default, double, do, else, false, float, for, if, import, int, long, new, null, public, return, short, static, switch, true, try, void, while, using 0.7 Special Characters Character Name Description // Double slash Marks single line comment. # Pound sign (hash) Marks the beginning of a preprocessor directive < > Opening / closing brackets Encloses a filename when used in #include directive ( ) Opening / closing parentheses Used in naming a function e.g. int main ( ) { } Opening / closing braces Encloses a group of statements Opening / closing quotations Encloses a string of characters, ; Semicolon Marks the end of a complete statement 0.8 Operators 0.8.1 Arithmetic 0.8.2 Assignment Advanced Structured Programming Page 6 of 10

0.8.3 Comparison 0.8.4 Logical Note: Be careful of the Order of Operator Precedence 0.9 Visual Deployment Average computation of three numbers a, b, and c #include "stdafx.h" int main() { float a, b, c, avg; cout << "Enter the value of A \n"; cin >> a; cout << "Enter the value of B \n"; cin >> b; cout << "Enter the value of C \n"; cin >> c; avg = (a+b+c)/3; cout << "Average is " << avg << endl; return 0; } Exchange X and Y integer values #include "stdafx.h" int main() { int X,Y,Temp; cout << "Enter the value of X \n"; cin >> X; cout << "Enter the value of Y \n"; cin >> Y; Temp = X; X=Y; Y=Temp; cout <<"X= "<<X<<" Y= "<<Y<<endl; return 0; } 0.10 Conditional Operation Types of conditional operators are : if-else, if-elseif- else, switch, nested conditions If-else If-elseif-else Switch Nested conditions Syntax If( boolean expression) else If( boolean expression) elseif else Switch(expression) { case constant exp.: statement(s); break; default: statement(s); break; } If( boolean expression) { If( boolean expression) else } else Advanced Structured Programming Page 7 of 10

Using If condition Subtracting two numbers #include "stdafx.h" int main() { int a, b, c; cout << "Enter the 1st Value\n"; cin >> a; cout << "Enter the 2nd Value\n"; cin >> b; if (a > b) { c = a - b; } else { c = b - a; } cout << " the result is " << c << endl; return 0; } Using Switch Printing number of days per month #include "stdafx.h" int main() { int month_num, daysinmonth = 0; cin >> month_num; switch (month_num) {case 1: case 3: case 5: case 7: case 8: case 10: case 12: daysinmonth = 31; break; case 2: daysinmonth = 28; break; case 4: case 6: case 9: case 11: daysinmonth = 30; break; default: daysinmonth = -1; break;} cout<<"days in month # "<<month_num<<" = "<< daysinmonth<<endl; return 0; } 0.11 Exercise The following is a short list of tasks aimed at demonstrating some of the features of Visual Studio and C++. 0.11.1 Write C++ statements to display on the screen the following patterns 1) ************* ********* ***** *** * 2) ****** ***** **** *** ** * 3) @@@@@@@@ Good Morning! @@@@@@@@ Advanced Structured Programming Page 8 of 10

0.11.2 Write a C++ program to print your Name, Level, and Faculty each in different line. Run the program to see the results. Remove the << after cout Keyword. Try running it again, what happens? Then Remove the semi-colon ";. Try running it again, what happens? And Finally Remove \n. Try running it again as well, what happens? 0.11.3 State whether each of the following statements are valid or invalid then state statement type a) int a, b, c; b) cout < Hello to CS World\n ; c) y = 100.3; d) x = (a +b)/(d+c); e) bol h=true; f) cinn> k; g) cout<< A ; h) cout<< endl; 0.11.4 Find any errors in the following C++ programs a) void main() { cout<<'c++ Programming questions and answers'; return 0; } b) using namespace sd; void main(){ integer a; float b; return 0;} c) #include <iostrem> usng namespace std; int main(){ a,b,c int; d float; cout<< The end of the program"; return 0; } d) #nclude <iostream> int main(){ int a,b,sum,pro; a=100; 20=b; sum=a+b; pro=a*b; cout<<left; cout<<"a+b";<< \n <<"a-b" cout<sum<< \t <<pro; return 0; } 0.11.5 Create a new project and call it Lab00. Write the following code: #include "stdafx.h" int main() { int x; x = 100; cout << x << endl; x = x + 10; //Line A cout << "Line A " << x << endl; x = x + 20; //Line B cout << "Line B " << x << endl; return 0; } Then run the program to see the results. What could you replace the code commented with Line A and Line B in order to make the program produce the same results? Now modify the code. 0.11.6 Write a new program that declares the following variables: a = 100, b = 2.3, c = -52.2, d = true, e = "I am ", f = "a student", g = 0, h = '!'. 0.11.7 Declare the following constants, pi = 3.142 and name = <your name>. Make sure you use the correct types. Declare three more variables, x as a long, y as a double and z as a String. 0.11.8 Write a C++ Program to implement basic Calculator Operations (+,-, /,*, %) on two numbers. 0.11.9 Write a C++ Program to converts 125 seconds to an equivalent number of minutes, & seconds. 0.11.10 Given y = ax 3 + 7, which of the following, if any, are correct C++ statements for this equation? Advanced Structured Programming Page 9 of 10

a) y = ( a * x ) * x * x + 7; d) y = a * x * x * x + 7; b) y = a * ( x * x * x ) + 7; e) y = a * x * x * ( x + 7 ); c) y = a * x * ( x * x + 7 ); f) y = ( a * x ) * x * ( x + 7 ); 0.11.11State the order of evaluation of the operators in each of the following C statements and show the value of x after each statement is performed. a) x = 7 + 3 * 6 / 2-1; b) x = 2 % 2 + 2 * 2-2 / 2; c) x = ( 3 * 9 * (3 + ( 9*3 / ( 3 ) ) ) ); 0.11.12 Write C++ Program to calculate the square root of a given number. If the number is negative print the error message Square root does not exist, otherwise print the number. 0.11.13Write C++ Program to read two numbers m and n and decide if n is bigger than m. 0.11.14 Write C++ Program to read a number and decide if it is odd or even. 0.11.15 Write C++ Program to take the letter grade from the user then using switch print its equivalent E.g. input: A Output: Excellent 0.11.16Write C++ Program to compute a simple calculator using switch statement. Advanced Structured Programming Page 10 of 10