• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本 扫码下载译学馆APP

#### 《卷轴教程之C++入门》#6 设计一个算法2

Devising an Algorithm C++ Program - Part 2

So in these next couple of lines of code that I’m going to write,

I’ll maybe make some mistakes on purpose,

and then we’ll come back and look at it.

But often times, in terms of learning something,

if you exhibit the problem first,

and then come back and correct it,

it’s more impactful in terms of getting things right later.

Ok, so how do we go about calculating the average?

So the average is just going to be,

the summation of some values,

and then dividing by the number of values that we have.

So a pretty basic calculation.

We’ll need to have a variable to actually store the result of our calculation.

So,

we can think about just the basic expression,

what are we wanting you to do?

We’re wanting you to take the, the Metascore,

and the Tomatometer score,

and then divide by 2,

since there’s 2 scores there.

So we can think of, maybe an expression like this,
metascore + tomato
‘metascore + tomato’,
tomatometer
‘tomatometer’,

and then we want to divide by 2.

Now we have to be careful here with this division here,

we have to remember our order of operations.

So we know that we know that we do anything in parentheses first,

then we do exponents, then multiplication/division,

working from left to right.

So,

they ways it’s, it’s specified here,

would actually do this division operation before we did the addition.

We actually want to sum these value first,

so we should probably put parentheses here.

So that we construct parentheses around some particular operation,

that we want to do before some other operation,

to override the operator precedence of multiplication before,

or excuse me, division before addition here.

So we do what’s in parentheses first.

And what we need to do,

is assign this particular calculation here, to a variable.

So I’m going to just assign it to,

an end variable called average, avg.

And make sure that our statement ends with a semicolon.

So we have a valid statement here in C++,

to calculate the average.

Ok.

The other thing that I’m going to point out here is that,
Metascore和Tomatometer
Metascore and Tomatometer,

they both offer values that are on 100 point range,

as oppose to a 10 point range.

So this is where we need to rescale the average that we come up with,

from 100 point range to a 10 point range.

So basically we’re taking whatever value we calculated here,

and we’re going to divide it by 10.

So, down here under ‘Rescale average’,

I’m going to declare a new variable,

and we’ll call it ‘rescale’,

maybe ‘rescaledScore’,

maybe that’s a good variable name.

And we’ll take the average that we previously calculated,

and divide it by 10,

so that rescales it onto a 10 point scale.

Then for displaying the result,

it’s just going to be a ‘cout’,

and then we’ll say,
Our calculated
‘Our calculated’,

Oops, just going to say, ‘calculated’,
average
‘average’,
on a 10 point scale
‘on a 10 point scale’,
for 在这里要输出movie的名字
‘for’, and we’ll put in the name of the movie here,

and,
is
we’ll say ‘is’,

space, let’s see,

and then put in the variable ‘rescaledScore’.

alright, and then we can put a period here at the end.

If you prefer, you can put this on multiple lines.

So anytime you see this insertion operator here,

you can hit ‘return’ before or after the insertion operator.

And I’ll just put it on the next line, so that way our line is not too long.

That’s our last bit of code that we need to have.

One thing else that we probably should do,

is have this ‘return 0’.

If we don’t put that ‘return 0’ in there,

it will be ok by default main,

if you in C++, if you don’t have a return statement in there.

It will default to a ‘return 0’ statement.

You may end up with some sort of warning from whatever editor,

or whatever integrated development environment that you’re using.
OK 看起来一切就绪了 可以保存并建造项目了
Ok, so it pretty much looks like we’re ready to, to save and build our programme.

But before we do that,

let’s go ahead and calculate what the scaled average would be,

on a 10 point scale,

for a particular movie,

if we were to use the Metascore and the Tomatometer score.

So we want to be able to come up with a particular test case,

see what the value would be,

and then when we run our programme,

we can test our, our programme against the value that we expect to receive.

So, I’ve already pulled up a movie here,
《超人特工队》
The Incredibles,

on both Rotton Tomatoes and metacritic,

which has the Metascore.

And so Metascore has a score of 90,

and then on Rotton Tomatoes,
Tomatometer是97
it has a Tomatometer score of 97.

So let’s just pull up our calculator here,

and say ’90 + 97′,

and then that would give us the summation that we’re going to do,

and then we’ll divide that by 2,

which is ‘93.5’,

and then we’ll divide that by 10 to get our scaled score.

So we would expect to get a result of ‘9.35’ from our programme,

if it’s working correctly.

So let’s go ahead and, and go back in here to Eclipse,

and run this programme.

So first we need to save it,

and then we will build it.

So during the build phase,

we’re doing the compiling and the linking operations.

Doesn’t look like we have any problems,
OK 现在继续 运行
so let’s go ahead and run this.
OK 现在向下看 看这里的控制台
Ok, so if we look down here at the console,

we’ll see the prompt of ‘What is the name of the movie: ‘,

so the name of our movie is ‘The Incredibles’.

so type that in.

Hit ‘Enter’.

And then say, it says,
What is the metascore rating for the Incredibbles?
‘What is the metascore rating for The Incredibles?’,
metascore评分是90
the metascore rating was 90,

so type that in and hit ‘Enter’.

And then we have ‘What is the Tomatometer rating for The Incredibles?’.
97 然后我们敲击回车
So that was 97, and then we’ll hit ‘Enter’.

So, looks like, for our programme it says,

‘Our calculated average on a 10 point scale for The Incredibles is 9’.

And then it has a dot there,

and that’s actually the period,

it’s not even representative of a decimal point.

It’s just the period that ended our, our sentence here.

So, it’s not exactly what we were expecting.

We were expecting to get 9.35.

So let’s go back and, and look at our programme,

where we were actually calculating the average and rescaling the average.

So what you’ll notice here, if we look at this closely,

we have a datatype over here,

on the left hand side of the assignment operation,

of an ‘int’.

So we’re saying that the average is an ‘int’ datatype,

and what do we know about ‘int’ datatypes?

Well, those are representative of integers,

so they do not store fractional parts.

So if we calculate an average here,

and we know that the average can have a fractional part,

if we were to take some set of numbers,

and divide them by some other number,

we can end up with a fractional part.

We’re losing that, that’s being, you know, truncated or lost,

whenever we store it to this variable over here,

cause it’s not capable of storing,

a, a fractional part.

So what we can do,

is change this particular datatype,

to being a datatype that can preserve or save a fractional part.

So one of the types that we looked at that can do that is the double,

so let’s change this to ‘double’.

And let’s also do the same thing here for ‘Rescale average’.

So we need to be able to preserve it there,

cause if you preserve it here, and then you go and divide by 10 on this next line,

and then store it back into an integer value,

you’re going to lose whatever fractional part you had, again.

Alright, so lets go ahead and save this.

And we’ll build it again,

and then we’ll run it again.

Alright, so let’s go back through,

and we’ll type in the name, being ‘The Incredibles’,

the metascore rating again, was 90,
Incredibles的Tomatometer评分是97
and the Tomatometer for The Incredibles was 97.

So this time it says,

‘Our calculated average on a 10 point scale for The Incredibles is 9.3’.

So still not the 9.35 that we were expecting,

so something else is going on.

And what going on, is if we look at this line of code here,

we have ‘(metascore + tomatometer) / 2’,

we’ll that looks like, you know,

it should give us an average that has a fractional part.

But something subtle is going on behind the scenes,

or maybe not so subtle if we actually understand it.
metascore是一个整型数值
So, ‘metascore’ is an integer value,

cause we typed in ’90’;
tomatometer也是一个整型数值
‘tomatometer’ was also an integer value,

it’s just we specify that both of those were integer values.
tomatometer是97
It was 97,

and whenever we add those together,

it’s also going to be resulting in an integer value,

and this ‘2’ here is an integer literal.

So whenever we preform this division here,

what’s going on is it’s performing what we call, integer division.

since we have an integer both in the dividend and the divisor.

So the result is also going to be an integer,

that’s just what it is.

So the way that we need to change this,

is that we can make either the dividend a, a double,

or the divisor a double,

it really doesn’t matter which one.

Because if one’s a double,

then the operation that it will do will actually preserve the fractional part.

So what we can do,

is actually cast, maybe the, the value here,

‘(metascore + tomatometer)’ to be a double value.

And the way we can go about casting,

is just doing an ‘(double)’.

So what this effectively is saying is,

for this particular operation,

make this stuff here,

to be of type double.

So whenever we have a double here,
double 噢 不好意思 是int
and then double, or excuse me, int here,

it will automatically do a division operation,

so that it preserves the fractional part.

So we can see that, that works.

I’ll save this,

click on the ‘Build’ button,

and then we’ll run our programme again.

And this time the name of the movie, once again,

is going to be ‘The Incredibles’,

and then we have the metascore rating, which was 90,

and the Tomatometer rating, which was 97.

So ‘Our calculated average in a 10 point scale for the Incredibles is 9.35.’.

So we get the expected result this time.

Now I did say that we could have also made this ‘2’ value a double.

And one of the easiest ways to make this ‘2’ value a double,

is to just do,

‘.0’.

So the decimal point, zero.

So this makes this a double literal.

Anytime in our programme, where we have a value,

and it has decimal point,

it’s automatically seen as a double value.

So if you wanted to, you could get rid of this,

it doesn’t matter that it’s there.

But we could get rid of that,

and so you have a double here,

and you have an int here.

And since you have and int and a double,

it’s going to automatically promote this to being a double,

and perform double division,

so that we have a fractional part.

So let’s go ahead and save that, build it,

and then run it.

Alright, so again, the name is ‘The Incredibles’,
metascore评分是90
the metascore rating is 90,
Tomatometer得分是97
and the Tomatometer score is 97.

And so again, we get the same exact result.

We get 9.35, so, that was our expected result.

Alright, so we’ve covered a lot of things in this programme here.

We’ve looked at devising an algorithm, writing pseudocode,

type conversion, also integer division

casting, so being able to cast a particular value to another value.

And we’ll probably talk more about casting in another programme.

So we’ve, we’ve seen a lot.

And we’ll do more in our next programme.

##### 译制信息

double int double you

positiveme

[B]Azrael