Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Examples of setw Function and Floating-Point Numbers Manipulation in C++, Schemes and Mind Maps of Engineering

Examples of using the setw function to manipulate the position and width of output for both integers and floating-point numbers in C++. It also covers the usage of fixed and setprecision to format floating-point numbers.

What you will learn

  • How to format floating-point numbers using fixed and setprecision in C++?
  • What is the purpose of using setw function in C++?
  • What is the difference between setw and setprecision in C++?

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 09/27/2022

mansi
mansi 🇺🇸

5

(2)

223 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Electrical and Computer Engineering 1of 52
UAH CPE 112
Formatting Output
By default, consecutive integer, floating-point, and
string values are output with no spaces between
them.
Manipulators are used to control the horizontal
spacing of the output. (endl is one we’ve already
seen)
Manipulators we’ll use now
endl first three defined in iostream
fixed
showpoint
setw last two defined in iomanip
setprecision
Electrical and Computer Engineering 2of 52
UAH CPE 112
setw
setw (set width) lets us control how many
character positions the next data item
should occupy when it is output. (used for
numbers and strings, not chars).s).
The argument to setwis an integer
fieldwidth specification, the data item is
right-justified within the fieldwidth.
If you don’t specify enough characters, the
minimum number of characters is used
anyway.
Electrical and Computer Engineering 3of 52
UAH CPE 112
setw Examples
Statement (ans = 33, num = 7132) Output (means blank)
cout << setw(4) << ans 33
Electrical and Computer Engineering 4of 52
UAH CPE 112
setw Examples
Statement (ans = 33, num = 7132) Output (means blank))
cout << setw(4) << ans << setw(5) 337132
<< num
Electrical and Computer Engineering 5of 52
UAH CPE 112
setw Examples
Statement (ans = 33, num = 7132) Output (means blank)
cout << setw(4) << ans << setw(5) 337132 Hi
<< num << setw(4) << “Hi”;
Electrical and Computer Engineering 6of 52
UAH CPE 112
setw Examples
Statement (ans = 33, num = 7132) Output (means blank)
cout << setw(4) << ans << setw(5) 337132 Hi
<< num << setw(4) << “Hi”;
cout << setw(2) << ans 33
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Examples of setw Function and Floating-Point Numbers Manipulation in C++ and more Schemes and Mind Maps Engineering in PDF only on Docsity!

Electrical and Computer Engineering

1 of 52

Formatting Output

  • By default, consecutive integer, floating-point, and

string values are output with no spaces between

them.

  • Manipulators are used to control the horizontal

spacing of the output. (endl is one we’ve already

seen)

  • Manipulators we’ll use now

endl first three defined in iostream

fixed

showpoint

setw last two defined in iomanip

setprecision

Electrical and Computer Engineering

2 of 52

setw

  • setw (set width) lets us control how many character positions the next data item should occupy when it is output. (used for numbers and strings, not chars).s).
  • The argument to setw is an integer fieldwidth specification, the data item is right-justified within the fieldwidth.
  • If you don’t specify enough characters, the minimum number of characters is used anyway.

Electrical and Computer Engineering

3 of 52

UAH CPE 112

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans 

Electrical and Computer Engineering

4 of 52

UAH CPE 112

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank)) cout << setw(4) << ans << setw(5) 33 << num

Electrical and Computer Engineering 5 of 52

UAH CPE 112

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”;

Electrical and Computer Engineering 6 of 52

UAH CPE 112

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans 33

Electrical and Computer Engineering

7 of 52

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132 << num

Electrical and Computer Engineering

8 of 52

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”;

Electrical and Computer Engineering

9 of 52

UAH CPE 112

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”; cout << setw(6) << ans 

Electrical and Computer Engineering

10 of 52

UAH CPE 112

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”; cout << setw(6) << ans << setw(3) 33Hi << “Hi”

Electrical and Computer Engineering 11 of 52

UAH CPE 112

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”; cout << setw(6) << ans << setw(3) 33Hi << “Hi” << setw(5) << num;

Electrical and Computer Engineering 12 of 52

UAH CPE 112

setw Examples

Statement (ans = 33, num = 7132) Output ( means blank) cout << setw(4) << ans << setw(5) 337132Hi << num << setw(4) << “Hi”; cout << setw(2) << ans << setw(4) 337132Hi << num << setw(2) << “Hi”; cout << setw(6) << ans << setw(3) 33Hi << num << setw(5) << “Hi”; cout << setw(7) << “Hi” << setw(4) Hi << num; cout << setw(1) << ans 33

Electrical and Computer Engineering

19 of 52

Floating-Point

Output Examples

ValueValue of xof x StatementStatement Output (Output ( mmeans bl ank)eans bl ank)

ValueValue  cout << fixed; 310.0 cout << setw(10) << setprecision(2) << x; 310. 310.00 cout << setw(10) << setprecision(5) << x; 310. 310.0 cout << setw(7) << setprecision(5) << x; 310.

Electrical and Computer Engineering

21 of 52

Floating-Point

Output Examples

of xof x StatementStatement Output (Output ( means blank)means blank)

UAH CPE 112

ValueValue  cout << fixed; 310.0 cout << setw(10) << setprecision(2) << x; 310. 310.00 cout << setw(10) << setprecision(5) << x; 310. 310.0 cout << setw(7) << setprecision(5) << x; 310. 4.827 cout << setw(6) << setprecision(2) << x; 4. 4.827 cout << setw(6) << setprecision(1) << x; 4.

Electrical and Computer Engineering 23 of 52

Bad Style Example

Electrical and Computer Engineering 24 of 52

UAH CPE 112

Bad Style

Example Continued

const float PRICE = 150000.0; // Selling price less land int main(){ float grossFootage; // Total square footage float livingFootage; // Living area float costPerFoot; // Cost/foot of living area cout << fixed << showpoint; // Set up floating pt. // output format

grossFootage = LENGTH * WIDTH * STORIES;livingFootage = grossFootage - NON_LIVING_SPACE; costPerFoot = PRICE / livingFootage; cout << "Cost per square foot is “ << setw(6) << setprecision(2) << costPerFoot << endl; return 0; }

Electrical and Computer Engineering

of 52

UAH

Good Style Example

// HouseCost // This program computes the cost per square foot of

// the house, the number of stories, the size of the

//************************************************************** #include iostream> include < > // For () and setprecision using std;

float WIDTH = 30.0; const LENGTH = 40.0; // Length of the house const float STORIES = 2.5; // Number of full stories const float NON_LIVING_SPACE = 825.0; // Garage, closets, etc. const float PRICE = 150000.0; // Selling price less land

26

UA H CP E 11 2

Example Continued

mai n() { f loa t ; // Tot al sq u are fo ota ge f loa t ; // Liv ing a r ea c os t Per Foo t;

c o ut << << sho wp o int // Set up fl oa t ing pt.

g r oss Foo tag e = gr oss Foo ta g e NO N_ L IVI NG_ SPA CE; c o stP erF oot = PRI CE / ;

<< " C ost pe r s qua re foo t i s " << (6 ) < < s et p rec isi on cos tP e rFo ot << ; r e tur n 0 ; }

El e ct r ic a l a nd Co m put er En g ine er i ng

of 52

CPE 112

Addi tio na l

  • Now, we consider four functions that operate on strings: size, substr
  • Th e len gt h an d siz efun ct i on s b o th ret ur n a n un sig ne d i n teg er val ue equ al to th e n u mb e r of c h ar a ct e rs in th e s t rin g. T he sub st r in g

s ubs tri ng is fou nd, i t r e tur ns th e p o si t io n wh er e t h em at c hb egi ns, i f n o t f oun d, it re tur ns as pe c ial nu mb e r.

  • T he s ubs tr f unc tio nr et u rns a par tic ul a r subs tri ng of a st r in g. 28

UA H CP E 11 2

a nds ize

I f length looks li ke t hi s: .length()

  • len gt h fun cti on req ui r es no ar g um e nt s , bu t y o um us t s t ill us ep ar e nt h es e s. A lso , - ret ur n in g , s ot he cal l

Electrical and Computer Engineering 29 of 52

U AH CP E1 1 2

E xampl e Usi ng

a nd s ize

Exa mpl e:

stri ng fi r st N am e

fu l lN a me ;

f ir s tN a me

c ou t f i rs t Na m e. l en g th ( ) < < ; // Pr i nt s 9

= fir stN ame

< < f u ll N am e e n dl ;

Electrical and Computer Engineering 30 of 52

U AH CP E1 1 2

M ore About s tring

  • st ringi s a C++ cl ass, whi ch has dat a types and fun cti on s a s so c iat ed wi t hi t.
  • O the r c las ses m a yh av e a l eng th f unc tio n. To g et th e len gt h fun ct i on as s oci at e dw it h st r in g , we us e t h ed ot ope rat or wi t ha .
  • stringh as a dat a type associ at ed wi t h it str ing len gt h An o the r c las sc oul d .
  • string::size_types pecifies t hes ize_type as s oci at e dw it h str in g.

Electrical and Computer Engineering

37 of 52

Examples Using find

str1 = “Programming and Problem Solving”;

str2 = “gram”;

Function CallFunction Call Value Returned by FunctionValue Returned by Function

str1.find(“and”) 12

str1.find(“Programming”) 0

str2.find(“and”) string::

str1.find(“Pro”) 0

str1.find(“ro” + str2) 1

Electrical and Computer Engineering

38 of 52

E xampl es Usi ngfind

str1 = “Programming and Problem Solving”;

F uF unction Callnction Call Value Returned by Function

str1.find(“and”) 12

str1.find(“Programming”) 0

s tr2.find(“and”) string::

s tr1.find(“ro” + str2) 1

string::npos

El ectrical and Computer Engineering

39 of 52

UAH CPE 112

Examples Using find

str1 = “Programming and Problem Solving”;

str2 = “gram”;

Function CallFunction Call Value Returned by FunctionValue Returned by Function

str1.find(“and”) 12

str1.find(“Programming”) 0

str2.find(“and”) string::npos

str1.find(“Pro”) 0

str1.find(“ro” + str2) 1

str1.find(“Pr” + str2) string::npos

str1.find(‘ ’) 11

Electrical and Computer Engineering

40 of 52

UAH CPE 112

The substr Function

  • Sample function call:

myString.substr(5, 20)

  • Substr returns a string, the arguments to it are of type string::size_type.

Electrical and Computer Engineering 41 of 52

UAH CPE 112

Examples Using substr

myString = “Programming and Problem Solving”;

Funct iFunct io n C a lo n C a lll S tri ng Re t urn ed by Fu n cti on

myString.substr(0, 7) “Program”

Electrical and Computer Engineering 42 of 52

UAH CPE 112

Examples Using substr

myString = “Programming and Problem Solving”;

Function CallFunction Call String Returned by FunctionString Returned by Function

myString.substr(0, 7) “Program” myString.substr(7, 8) “ming and”

Electrical and Computer Engineering

43 of 52

Examples Using substr

myString = “Programming and Problem Solving”;

Function CallFunction Call String Returned by FunctionString Returned by Function

myString.substr(0, 7) “Program” myString.substr(7, 8) “ming and” myString.substr(10, 0) “”

Electrical and Computer Engineering

44 of 52

Examples Using substr

myString = “Programming and Problem Solving”;

Function CallFunction Call String Returned by FunctionString Returned by Function

myString.substr(0, 7) “Program” myString.substr(7, 8) “ming and” myString.substr(10, 0) “” myString.substr(24, 40) “Solving”

Electrical and Computer Engineering

45 of 52

UAH CPE 112

Examples Using substr

myString = “Programming and Problem Solving”;

Function CallFunction Call String Returned by FunctionString Returned by Function

myString.substr(0, 7) “Program” myString.substr(7, 8) “ming and” myString.substr(10, 0 “” myString.substr(24, 40) “Solving” myString.substr(40, 24) Error

Electrical and Computer Engineering

46 of 52

UAH CPE 112

StringOps Program

// This program demonstrates several string operations //**************************************************************** #include #include // For string type using namespace std; int main() { string fullName; string name; string::size_type startPos; fullName = "Jonathan Alexander Peterson"; startPos = fullName.find("Peterson"); name = "Mr. " + fullName.substr(startPos, 8); cout << name << endl; return 0; }

Electrical and Computer Engineering 47 of 52

UAH CPE 112

Problem-Solving Case Study

  • Problem: You are asked to calculate the total cost of

painting traffic cones in three different colors. The

cone company uses the area painted to estimate the

total cost.

  • Output: The surface area of the cone in square feet,

and the costs of painting the cone in the three

different colors, all displayed in floating point form to

three decimal places.

  • Discussion: Cones are measured in inches. A typical

cone is 30 inches high and 8 inches in diameter. Red,

blue, and green paint cost 10, 15, and 18 cents per

square foot, respectively. The non-base surface area

of a cone is πr(r^2 + h^2 )1/2, where r is the radius of the

cone and h is its height.

Electrical and Computer Engineering 48 of 52

UAH CPE 112

High-Level Algorithm

Cone Painting Program

Print

Results

Compute Surface Area of the Cone

Convert

Dimensions

to Feet

Define

Constants

Compute Cost for Each Color