当前位置:天才代写 > C++/C代写,c语言代写代考-100%安全,包过 > C++ Homework Lecture Lab1 代写编程程序

C++ Homework Lecture Lab1 代写编程程序

2018-02-17 08:00 星期六 所属: C++/C代写,c语言代写代考-100%安全,包过 浏览:526

FINM 326: Computing for Finance


 

Lecture 1: C++ Fundamentals

 

 



Programming Environment

 

 

The C++ Standard Library: A First Look

 

 

Data Types and Operators

 

 

Functions

 

 

Week 1 Summary


Programming Environment

 

We use Visual Studio on Windows as our primary programming environment:

 

Most students are familiar with Windows O/S

 

Windows can be installed easily on MacBook

 

Visual Studio is user friendly and feature rich programming environment

 

Free access

 

We will also use gcc on Linux as a secondary platform:

 

some employers value programming experience on Linux

 

another option for those who do not want to use Windows

 

You can use one or the other, or both.

 

I will use Windows and Visual Studio most of the time.

 

I will show how to use gcc and Linux.

 

Visual Studio is installed on lab computers. Report issues to Minsik Yu (minsik@uchicago.edu)


Obtaining Software

 

 

 

 

Installing Visual Studio (on Windows):

 

Instructions are posted on course site

Mac users:

 

may download and install Windows as a second O/S using bootcamp (https://support.apple.com/en-us/HT201468)

 

use VMWare (instructions posted on course site)

 

Linux access:

 

Request a CS account at: https: //account-request.cs.uchicago.edu/account/requests

 

Need help? Contact the TAs.


 

 

 

Getting Started with Visual Studio Integrated Development


 

Environment (IDE)


Getting Started

Goal is to learn how how to use Visual Studio IDE, to:

 

1. create a project (application).

 

2. use the editor to write code.

 

3. build the application.

 

4. run the application.


Getting Started: Hello World Program

 

 

 

We will create a very simple project to illustrate how to use Visual Studio IDE.

 

The program shown below writes a message, "Hello, World!", to console (screen):

 

 

#include

 

int main()

{

std::cout << "Hello, World!" << std::endl;

 

return 0;

}


Our First C++ Project

 

Let's code, build and run this program in Visual Studio.

 

First, we will learn the steps we need to follow to create a project.

 

After that, we will explain this program step by step and learn some C++.


Step by Step

 

 

 

Step by step instructions used to create an application are illustrated next.

 

Start Visual Studio. Select new project.

 


Select "Visual C++" -> "Windows Desktop"

 


Select "Windows Desktop Wizard"

 


Enter:

 

1. (project) name

 

2. directory location

 

3. solution name for the project.

 


I Change Additional Options as follows:


 

uncheck Precompiled Header box.

 

check Empty Project box.

 


We have a new project!

 


Now we can to add source  les to our project.

 

Go to Source Files folder on the Solutions Explorer.

 

Right click, and select, Add -> New Item

 


Select C++ File

 

Name the  le appropriately.

 

Now you have a blank cpp  le.

 


Type code.

 

Build the project.

 


Make sure the project builds successfully.

 


Run it.

 


You'll see the greeting message on the screen.

 


Exercise

 

 

1. Use the steps shown above to create an application/project.

 

2. Code Hello World example. NOTE: C++ is case sensitive

 

3. Build the application.

 

4. Run it.

 

5. Change the program to print a di erent message on the screen.


Hello, World Program: Line by Line

 

 

int main()

 

main is a special function.

 

It is the program entry point.

 

Every console application we create should have a main function.

 

This function returns an integer (int) value.

 

We do not pass any arguments to this function.

 

Note: We can have two di erent function signatures for main function:

 

1. int main()

 

2. int main(int argc, char* argv[]). We will discuss the use of the second function signature later.


 

The line below shows the body of our main function.

 

{

std::cout << "Hello, World!" << std::endl;

 

return 0;

}

 

A function body starts with { and ends with }.

 

Body has 2 statements; each statement ends with a semicolon (";").

 

First line writes a message, "Hello, World!", to console.

 

Here we use some functionality from the C++ Standard Library.

 

After that, the function returns 0 (an integer value)1.

 

 

 

 

1We don't have to return a value in main function. Return 0 (zero) is implied.


 

 

 

The C++ Standard Library: A First Look


The C++ Standard Library

 

 

Here, we brie y look at the feature (i.e. to write a message to console) used in the Hello World example.

 

The C++ Standard Library implements some very useful features we need.

 

The Standard Library is divided into several sections (more precisely headers), based on functionality.

 

Each header has a name.

 

We need to include the appropriate header in order to have access to its components.

 

The functionality to handle output to console is de ned in the iostream header.

 

To include it: #include


The Standard Input and Output

 

 

 

Terminology:

 

input: a sequence of byte ow coming into the computer memory from a device (e.g. keyboard, le).

 

output: a sequence of byte ow from the memory to an output device (e.g. console, le).

 

stream: a sequence of characters (bytes) read from an input device or written to an output device.

 

The iostream section of de nes objects to read and write a stream to Standard I/O channels:

 

cin

 

cout

 

and more..


Standard Output

 

 

 

 

 

cout (pronounced as see-out):

 

known as the standard output

 

attached to the standard output device, which usually is the console

 

uses the stream insertion operator, << cout << "Hello, World";


Standard Input

 

 

 

 

cin (we pronounce it as see-in):

 

known as the standard input

 

attached to the standard input device, which usually is the keyboard

 

uses the stream extraction operator ( >>), to read a stream from the standard input

 

int x; cin >> x;


Namespace

 

 

 

Namespaces allow us to group classes and functions using a name.

 

Functions and classes in the C++ Standard Library are de ned in the std namespace.

 

To use them (classes/functions), we have to use the fully quali ed name.

 

Example: We saw the use of std::cout.

 

std is the namespace.

 

:: is the scope resolution operator in C++.

 

cout is the object in this case.


 

We will see the use of namespaces later, when we discuss classes.


Using Namespaces

 

Having to write the namespace every time we use a function is a bit cumbersome.

 

There are di erent ways to overcome this problem.

 

Technique 1:

 

using std::cout;

using std::endl;

 

int main()

{

cout << "Hello, World!" << endl;

}

 

Technique 2:

 

using namespace std;

 

int main()

{

cout << "Hello, World!" << endl;

}


If we're not careful, the second technique can lead to subtle problems in large programs.

 

I encourage you to use the  rst technique.

 

I may not show every namespace in subsequent slides after they are introduced.

 

E.g. I may show cout without showing using std::cout; but you should assume it is there.


 

 


Data Types

 

 

We write programs to handle/manipulate data.

 

We use variables to store data.

 

Programs need to store di erent kinds/types of data:

 

a letter or sequence of letters: e.g. your name

 

numbers: e.g. your age

 

In C++ every variable has to have a type.

 

The type of the variable has to be declared before it is used.

 

Once a type is assigned to a variable, it cannot be changed.

 

Data types in C++ belong to two main categories:

 

1. fundamental data types

 

2. user de ned data types


 

Fundamental data types are de ned in the C++ language.

 

C++ also allows the user to de ne types (classes).


Fundamental Data Types: Numerical Types

 

 

 

 

Numerical values can be whole or real numbers: e.g. 1 or 1.2

 

For real numbers we have two types (in C++):

 

oat : uses 4 bytes

 

double : uses 8 bytes

 

I As a result, max and min values each can store are di erent.

 

Type

Size (Bytes)

Value Range

oat

4

3.4

E 38

double

8

1.7

E 308

 

I We can use them to store both negative and positive real numbers.


For whole numbers we have several types:

 

short: uses 2 bytes

 

int: uses 4 bytes

 

long: at least 4 bytes

 

They have signed and unsigned versions:

 

unsigned version can store positive numbers only

 

signed version can store both positive and negative numbers

 

Type

Size (Bytes)

Value Range

unsigned short

2

0 to 65,535

short

2

-32,768 to 32,767

unsigned int

4

0 to 4,294,967,295

int

4

-2,147,483,648 to 2,147,483,647


More Types

 

 

 

Type to hold a  ag (true/false)

 

I bool

 

Type to hold a single character

 

I char

 

Reference: https: //msdn.microsoft.com/en-us/library/cc953fe1.aspx

 

Type to hold a string of characters

 

std::string

 

std::string is NOT a fundamental data type

 

de ned in the C++ Standard Library

 

de ned in the string header  le


 

 

 

C++ Operators


Operators for Fundamental Data Types

 

Listed below are some operators de ned for fundamental types:

 

Assignment operator (=) :

 

a = 5;

//a is an int

b = 1.5f;

//b is a float

c = 1.4;

//c is a

double

d = 'a';

//d s a char

e = true;

//e is a

bool

 

Arithmetic operators (+;  ; ; =) :

 

= a + 1; b = b – 1.2f; c = c * 2.1 ; c = c / 2.2;

 

Comparison operators ():

 

a > 3; a < 3; a >= 3; a <= 3;


 

Comparison operators return a bool (true/false) value.


Equality (==): a == 3;

 

Non Equality (! =) : a != 3;

 

Logical AND (&&) : returns true if both operands are true; returns false otherwise

 

a == 3 && d == 'a';

 

Logical OR (jj) : return true if either or both operands is true; returns false otherwise

 

a == 3 || d == 'a';

 

Modulo % (to nd the remainder from integer division): int x = 7 % 3;


Operators: Short-cuts

 

 

We have some short-cuts; e.g. For an int value i:

 

Increment:

 

i++; // same as, i = i+1;

 

i += 17; // same as, i = i+17;

 

Decrement:

 

i–;  // same as, i = i – 1;

 

i -= 21; // same as, i = i – 21;

 

Multiplication:

 

i *= 13; // same as, i = i*13;

 

Division:

 

i /= 42; // same as, i = i / 42;


Pre x and Post x

 

 

 

Post x:

 

increment x and return old x:

 

int x = 3; int y = x++;

 

now, y == 3 and x == 4

 

Pre x:

 

increment x and return new value

 

int x = 3; int y = ++x;

 

now, y == 4 and x == 4


 

 

 

Functions


C++ Functions

 

We saw the main() function { every application has to have a main().

 

We do NOT write the entire program in the main function.

 

We use functions and classes.

 

It is easier to solve a complex problem by breaking it up into smaller parts.

 

And, combine them to solve the problem.

 

We write a function to do a certain task or a sub task.

 

Combine one/more functions to solve a problem.

 

Using functions have several advantages.

 

First, let's learn how to write functions in C++.

 

Example: we can write a function to display a greeting message on the screen:

 

void DisplayGreeting()

{

std::cout << "Hello World!" << std::endl;

}


 

A C++ function:

 

has a name2

takes zero or more arguments

 

usually does something in the function body

 

returns some value of a certain type

 

if a function does not return anything, its return type is void

 

 

2We can also write a function without name


Functions: Example 1

 

 

Our rst function writes the greeting, "Hello World!" to the screen:

 

#include

 

void DisplayGreeting()

{

std::cout << "Hello World!" << std::endl;

}

 

We can now call this function from the main function:

 

int main()

{

DisplayGreeting();

 

return 0;

}


Functions: Example 2

 

 

 

How would you write a function to add two integers?

 

int Add(int a, int b)

{

return a+b;

}

 

int main()

{

int result = Add(2, 3);

 

cout << " Result :" << result << endl;

 

}


Functions: Advantages

 

 

 

 

1. Better/simpler solution: divide the problem into smaller tasks.

 

2. Better code structure: improve readability/clarity.

 

3. Reuse: once a function is written, we can use it any number of times.

 

4. Maintainability: we write a function once, if we want to make a chance, x a bug, we can do it just in one place.

 

5. Type safety:

 

arguments passed to a function should match the types in the function declaration

 

program will not compile if there's a mismatch


Parameter Overloading

 

 

Overloading allows 2 or more functions to have the same name:

 

they must have di erent input argument types

 

they cannot di er by the return type only

 

Allows us to write more than one method to do some task in di erent ways, using di erent inputs.

 

For example, we can write another Add() function as:

 

 

double Add(double a, double b)

 

{

 

return a+b;

 

}


In-Class Exercises

 

 

 

 

 

 

 

Write functions to add:

 

1. two  oat values

 

2. 3 integer values

 

3. 3 double values


Implicit Type Conversions

 

 

Suppose we have the following function:

 

double Add(double a, double b)

 

{

 

return a+b;

 

}

 

What happens if we use it to add two integer values:

 

int main()

 

{

 

int a = 2; int b = 3;

 

cout << Add(a, b) << endl;

 

}

 

Question: does this program build?


Yes, this program builds just  ne:

 

arguments are passed to the function as doubles

 

size of int is 4 bytes; double is 8 bytes

 

we can safely store any int value in a double

 

C++ (compiler) converts the int to double (implicitly) in this case.

 

This is a conversion from a smaller (4 byte) to a larger (8 byte) type.

 

It is called a widening, or, promotion

 

Promotions are safe implicit conversions.


Suppose we have the following function:

 

int Add(int a, int b)

 

{

 

return a+b;

 

}

 

What happens if we use it to add two double values:

 

int main()

 

{

 

double a = 2.2; double b = 3.3;

 

cout << Add(a, b) << endl;

 

}

 

Will this program build?


Yes, this program also builds:

 

arguments are passed as ints

 

size of double is 8 bytes; int is 4 bytes

 

we cannot safely convert every double value to an int

 

C++ (compiler) converts the double do int values implicitly in this case.

 

Results in a conversion from a larger (8 byte) to a smaller (4 byte) type.

 

It is called a narrowing.

 

Narrowing can be dangerous, and results in a build Warning.

 

Why is this not an error?

 

C++ expects us to know what we're doing

 

C++ will let you shoot yourself in the foot if we're not careful


 

Lesson: Pay Attention to Build Warnings!


Casting

 

What's the result from integer divsion below:

 

int a = 3, b = 2;

 

cout << a / b << endl;

 

To get the correct (real) number, we must convert at least one operand to real.

 

Explicit type conversions are known as casting.

 

Casts are not encouraged, sometimes they are necessary.

 

C++ supports 4 types of casts (known as named casts).

 

We'll look at static_cast now:

static_cast:

 

Used to force conversions, e.g. int->double

 

int a = 3; int b = 2;

 

cout << static_cast

 

static_cast returns a double value in this case.


 

Demos

 

 

 

 

 

Implicit type conversions:

 

widening

 

narrowing

 

static cast

 

Handling syntax errors:

 

1. build logs: errors and warnings

 

2. IntelliSense3: makes coding covenient

 

Debugging

 

https://msdn.microsoft.com/en-us/library/dn957937.aspx


C++ and Header Files

 

 

 

 

We wrote all code in the same  le.

 

In practice we never write all code in the same  le:

 

di cult to read a very large le (with thousands of lines of code)

 

doesn't promote reusability

 

di  cult for multiple programmers to work on the same project

 

makes build process longer

 

Solution is to use separate  les for functions/classes etc.

 

This requires using:

 

C++  les

 

Header  les


Let's illustrate how to implement a function(or related functions) in a separate C++ le.

 

Let's use the add functions as an example:

 

We will implement the Add() functions in a separate Add.cpp le.

 

When we move the add functions to a new le our program won't build.

 

The main() function doesn't know about the Add() functions.

 

We need to declare a function before we can use it in a separate le.

 

We could add the following two declarations before we use them in main:

 

int Add(int, int);

 

double Add(double, double);

 

This solution is not ideal: it requires us to have these declarations in every le we use them.


Proper C++ way:

 

Write the function declarations in a header  le (e.g. Add.h):

 

int Add(int, int);

 

double Add(double, double);

 

Include it in source les, using #include preprocessor directive:

 

#include "Add.h"


In-Class Exercise

 

 

Using one (or more) Add() function/functions from the previous exercise, write:

 

1. function de nition in the header  le.

 

2. function implementation in the cpp source  le.

 

3. call the Add() function from main().


C++ Build Process

 

C++ build process uses several steps:

 

1. Preprocessing:

 

uses the preprocessor

 

the preprocessor handles the preprocessor directives (e.g. #include)

 

the output of this step is a C++ le without preprocessor directives

 

1. Compiling:

 

uses the compiler

 

checks syntax

 

makes sure the data types are compatible with the operations

 

each .cpp  le is compiled to create an object  le (.obj)

 

2. Linking :


 

uses the linker

 

creates an executable (.exe)  le using the object  les


Demo: Build Errors

 

 

We can have build errors in each stage.

 

We need to have an idea about each stage to x build problems.


Type Alias

 

 

Type aliasing can help make code:

 

1. more readable

 

2. less error prone

 

Consider the function declaration below which calculates the

 

Black Scholes price of an Option.

 

double BSPrice(double, double, double, double, double);

 

Error prone: it is not clear what the arguments are

 

We can use typedefs here:

 

typedef double OptionPrice;

typedef double StockPrice;

typedef double Strike;

typedef double Expiration;

typedef double Rate;

typedef double Volatility;


 

OptionPrice BSPrice(StockPrice, Strike, Expiration, Rate, Volatility);


The const keyword

 

 

 

 

The const keyword is used to de ne a constant value.

 

If you try to change the value of a const member, the compiler catches it.

 

const int MaxValue = 10;

 

MaxValue = 20; // <= This is an error

 

This is another example of the bene ts of type safety in C++.

 

We will discuss other uses of the const keyword later in the course.


Comments

 

 

 

 

We can write comments using C style or C++ style.

 

C style:

 

/* This is a C style comment */

 

C++ style:

 

// This is a C++ style comment


Assignment 0 (Not Graded)

 

 

1. Write a function to add two integers.

 

2. Read two integer values from the keyboard.

 

3. Add them using the function you wrote.

 

4. Write the result to console.

 

Individual Assignment.

 

Due: January 13, 2018 by midnight (CST).

 

Total points: 0 (Zero)

 

Submission (Optional):

 

Clean the solution using Clean Solution under Build tab in Visual Studio.

 

Compress (zip) the folder containing the project.

 

Submit on Canvas: follow Assignments -> Assignment 0 link and attach the zip le.


 

 

 

Week 1 Summary


Visual Studio IDE

 

 

 

 

We created our  rst application:

 

we create a project

 

solution is a placeholder for one or more projects

 

Tools in Visual Studio IDE:

 

editor to write code

 

intellisense support

 

build tools

 

debugger

 

Build uses 3 steps:

 

preprocessing, compiling and linking

 

brie y discussed each step


Data Types

 

 

 

 

 

Categories:

 

1. fundamental

 

2. user de ned

 

We discussed fundamental types and operators

 

Compiler can do implicit type conversions:

 

3. widening/promotion (e.g. int to double)

 

4. narrowing (e.g. double to int)

 

We can force type conversions via casting


Functions

 

 

 

 

main(): program entry point

 

Write a function to do a certain task or a sub task.

 

Combine them to write a large program.

 

Functions promote:

 

readability

 

reusability

 

maintainability

 

type safety

 

You will understand these advantages when we write more code.


C++ Standard Library

 

 

 

 

 

 

Collection of useful classes/functions.

 

Brie y looked at some features in the iostream header:

 

streams

 

cout

 

cin

Introduced the std::string type in std::string header.

代写计算机编程类/金融/高数/论文/英文


  u=199783060,2774173244&fm=58&s=188FA15AB1206D1108400056000040F6&bpow=121&bpoh=75.jpgalipay_pay_96px_533896_easyicon.net.pngpaypal_96px_533937_easyicon.net.pngchina_union_pay_96px_533911_easyicon.net.pngmastercard_pay_96px_533931_easyicon.net.pngasia_pay_96px_533902_easyicon.net.png

本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝或者Upwork交易!

E-mail:850190831@qq.com   微信:BadGeniuscs  工作时间:无休息工作日-早上8点到凌晨3点


如果您用的手机请先保存二维码到手机里面,识别图中二维码。如果用电脑,直接掏出手机果断扫描。

qr.png

 

    关键字:

天才代写-代写联系方式