change. For example, at the beginning of a program, we can write:
int f = 1;
During execution of the program, the value retained in the variable f can be
changed:
f = 12;
The main types of data are:
- Integer (int) is an integer number with a sign
- Character (char) can retain characters
- Real, meaning decimal number (float)
Real, number with decimal but precision twice as much bigger than the float type
(double)
Constant
Sometimes in programs, we need some values ??on which we use
repeatedly. And we don't want to write this value every time. In this case, we can
use constant.
Example:
# define PI 3.14
Defines the PI constant with 3.14 value. In the program we will writes every time in
place of 3.14, PI.
Equal statement
operator =value assigns a value to a variable.
Or:
variable = expression;
The sign " = " is not the equal sign of mathematics.
Is called the assignment sign.
Think like that: cover what is on the left of the sign " = ", calculate what is on the right of
the sign " = ", (if is necessary), and the result is assigned to the left member.
Example:
a=21
Means:
Think like that: cover what is on the left of the sign " = ", calculate what is on the right of
the sign " = ", (if is necessary), and the result is assigned to the left member.
Example:
a=21;
Think like that: cover what is on the left of the sign " = ", (a), calculate what is on the
right of the sign " = ", (if is necessary, and in this case is not, is only a value, 21), and
the result is assigned to the left member, means "a " has received value 21.
Other example:
x=2
y=3-x
Think like that: cover what is on the left of the sign " = ", (x), calculate what is on the
right of the sign " = ", (if is necessary, and in this case is not, is only a value, 2), and
the result is assigned to the left member, means "x " has received value 2.
Again, think like that: cover what is on the left of the sign " = ", (y), calculate what is
on the right of the sign " = ", (and is necessary, x has value 3, and 3 minus x, means
3 minus 2, and the result will be 1). "y " has received value 1.
For example, the following statements:
a=2
a=a+1
Means:
Think like that: cover what is on the left of the sign " = ", (a), calculate what is on the
right of the sign " = ", (if is necessary, and in this case is not, is only a value, 2), and
the result is assigned to the left member, means "a " has received value 2.
Again, think like that: cover what is on the left of the sign " = ", (a), calculate what
is on the right of the sign " = ", (and is necessary, a has value 2, and a plus 1,
means 2 plus 1, and the result will be 3). "a " has received value 3.
Mathematically speaking, there is not in this world, a number to wich if i add 1, the
number will remain the same as the one before.
int x, y;
x = 23; x become 23.
y = 2 * x; y become 46 (2*x means 2*23, whih is equal with 46).
The equal statement is not the same with the mathematic equal.
The following statement calculates the value of its expression.
2 multiplied by x, ie 46, and assigns this value to y.
Languages ??C and C ++ have special expression.
Let's a variable x and an expression e.
x op = e
is equivalent to
x = x op e
The following table lists these operators
175.statement: x = x + e short form: x + = e
176.statement: x = x – e short form: x - = e
177.statement: x = x * e short form: x * = e
178. statement: x = x / e short form: x + = e
179. statement: x / = e short form: x + = e
180. statement: x = x% e short form: x% = e
181. The statements are executed from right to left.
182. Statement:
183. z = 1;
184. x = y = z;
185. y take the value of z, so y= 1, and x will be the value of the y, that is 1.
186. Statement:
187. x + = y + = z;
188. y + = z;
189. is equivalent to y = y + z. Evaluating the expression from right side: y + z, will
be 2, because y and z were equal to 1.
190. The value is attributed to y, y = 2.
191. Statement:
192. x + = y;
193. is equivalent to x = x + y. Evaluate the expression on the part right: x + y,
will produce 3. The value is assigned to x, x = 3.
194. Function libraries
195. C and C ++ languages ??have standard prototype libraries. These libraries
are announced with the directive includes.
196. # includes
197. These libraries are files with the h extension and are called "header".
198. For example, the library with prototypes of mathematical functions is
199. # includes
200. The library with C type input / output functions is
201. The library with C-mode strings is
202. The library with C ++ input / output functions is
203. Arithmetic expressions
204. Arithmetic expressions consist of constants, and variables. Operators are
9 advice
sleep 8 hours per day
205. +, - * and /, and in the case of type operands integer, and% (the remainder of
the division of two integers). The rest dividing two integers, a and b, is thus defined:
206. a% b = a - (a / b) * b
207. E.g:
208. 15% 4 = 3
209. 11% 5 = 2
210. The floor (x) function calculates the value of the largest number
211. the integer in x, and the ceil (x) function calculates the value of the smallest
integer and greater than x).
212. Let's back to our problem:
213. input data: the sides length of a triangle.
214. output data: the sides the area of the triangle.
215. input data processing to obtain output data will be: Heron's formula.
216. Input data must be read from the keyboard or from the file, output data must
be displayed on the display of the computer or write within a file.e
217. In the pseudocode, these statements are:
218. read name_of_the_variable
219. write name_of_the_variable
220. In our case the pseudocode will be:
221. 1. read l1 (the length of the first side of the triangle)
222. 2. read l2 (the length of the second side of the triangle)
223. 3. read l3 (the length of the third side of the triangle)
224. 4. input data processing to obtain output data will be: Heron's formula
225. 5. write a (means area of the triangle)
226. Let's refine step 4
227. 1. read l1 (the length of the first side of the triangle)
228. 2. read l2 (the length of the second side of the triangle)
229. 3. read l3 (the length of the third side of the triangle)
230. 4. input data processing to obtain output data will be: Heron's formula
231. Heron's formula is:
232. area = sqrt(p*(p-a)*(p-b)*(p-c))
233. where p=(l1+l2+l3)/2, and it means semipermeter
234. 5. write a (means area of the triangle)
235. Again let's refine step 4
236. 1. read l1 (the length of the first side of the triangle)
237. 2. read l2 (the length of the second side of the triangle)
238. 3. read l3 (the length of the third side of the triangle)
239. 4. p=(l1+l2+l3)/2
240. 5. a = sqrt(p*(p-a)*(p-b)*(p-c))
241. 6. write a
242. If we have the pseudocode, it's very easy to " translate " in any
progamming language
Niciun comentariu:
Trimiteți un comentariu
Rețineți: Numai membrii acestui blog pot posta comentarii.