Question:
Programming C questions?
*Rebe*
2010-07-31 15:17:46 UTC
This is programming homework please let me know if is alright thanks!

1. passes = passes + 1 can also be written as:

(a) passes ++;
(b) ++ passes;
(c) passes + = 1;
(d) all of the above (my answer (d) all of the above)

and the other one is

2.The statement c = c + 3 can also be written as

(a) c++ 3;
(b) c+ = 3;
(c) c + 3 =; ( my answer is (c) c+ 3 =; )
(d) none of the above

Also can someone tell me about a page that explains this operators THANKS!
Three answers:
ladaghini
2010-07-31 18:03:05 UTC
There are several ways of incrementing in the C language.



Often, I might need to increment a number, and so I might do something like this:

i = i + 1



In fact, I might (I will) do it so often that there is a nice short hand.

i += 1.

This does the exact same thing as i = i + 1, just requires less typing.



It not only works for addition, but also subtraction, multiplication, division, and even logical operators.

Let's say I want to make a power function that allows me to calculate expressions like 3^4:



int power(int base, int exponent)

{

. . . . int result = 1;

. . . . while (exponent > 0)

. . . . {

. . . . . . . . result *= base;

. . . . . . . . exponent = exponent - 1;

. . . . }

. . . . return result;

}



What are powers in mathematics? They are just repeated multiplication.

So, for 3^4.

We start with the variable result, which eventually hold the answer. But it starts at 1.

Multiply that by 3, and keep the product stored in result. So far, result = 3.

Again, multiply by 3, and store in result: so far, result = 9

and then result = 27

and result = 81.



result *= base is the same as saying result = result * base.



Notice that the power function shown above also decrements exponent using the same kind of operator, except this time subtraction:

exponent = exponent - 1.



I could have said exponent -= 1, which would be equivalent.



Furthermore, I could have said

exponent--;

or

--exponent;



These last to options are decrements (subtracting by 1). These decrements correspond to the

following increments:

exponent++

or

++exponent.



But what is the difference between doing ++i and i++.



Both these expressions modify i in a larger expression. ++i increments i first, and then i is used in the larger expression. With i++, i is used first, and then it is incremented



For example, suppose I have

i = 0;

printf("The value of i is %d\n", ++i); // i is incremented first, then used in the printf statement.

[Output: The value of i is 1.]



// continuing the same program:

printf("The value of i is %d\n", i++); // i is used in the printf statement and then incremented

[Output: The value of i is 1.]



The operator used as ++i is called the pre-increment operator (pre means before, and it increments the variable before the variable is used), and i++ is called the post increment operator.



Similarly, --i and i-- are called, respectively, the pre decrement and post-decrement operators.



This ability to specify exactly when a variable is incremented or decremented can come in handy. Take the power function from earlier:



int power(int base, int exponent)

{

. . . . int result = 1;

. . . . while (exponent > 0)

. . . . {

. . . . . . . . result *= base;

. . . . . . . . exponent = exponent - 1;

. . . . }

. . . . return result;

}





We can use the decrement operator on exponent:



int power(int base, int exponent)

{

. . . . int result = 1;

. . . . while (exponent > 0)

. . . . {

. . . . . . . . result *= base;

. . . . . . . . exponent--; // changed line

. . . . }

. . . . return result;

}



Or we can do



int power(int base, int exponent)

{

. . . . int result = 1;

. . . . while (exponent > 0)

. . . . {

. . . . . . . . result *= base;

. . . . . . . . --exponent; // same as exponent-- in this context;

. . . . }

. . . . return result;

}



In the above two examples, exponent-- and --exponent both make the function work because exponent is not part of a broader statement. That is, the timing of the increment does not affect any statements.



However we can make the function simpler by decrementing exponent immediately *after* the comparison exponent > 0: by using the post-decrement operator:



int power(int base, int exponent)

{

. . . . int result = 1;

. . . . while (exponent-- > 0) // can't do --exponent because it will reduce the number of loops by 1.

. . . . {

. . . . . . . . result *= base;

. . . . }

. . . . return result;

}



So, based on the above explanation, in your question 2, the answer is B, not C.
chesick
2016-10-31 06:19:59 UTC
NeilLinux2010:~$ vi maxminnumbers.cpp NeilLinux2010:~$ g++ -g -O2 maxminnumbers.cpp -o maxminnumbers NeilLinux2010:~$ ./maxminnumbers 3 8 22 19 a million EOF detected--dumping effects max=22 min=a million NeilLinux2010:~$ right this is the code: /* maxminnumbers.cpp objective: examine the regular enter for integers, checklist the optimum and minimum values, then report them on the tip whilst end of stdin is reached. 2010-02-01 */ #contain iostream // for cin, cout #contain cstdio // for feof, stdin utilizing namespace std; int important(int argc, char **argv) { int cmax, cmin, num; // decl of vars cin >> num; // get the 1st huge type to initialize all the others. cmax = cmin = num; // init all different vars to same cost at a similar time as (!feof(stdin)) { cin >> num; // get different numbers till end of report (feof()) if (num>cmax) { cmax = num; // a sparkling optimum grew to become into detected. } else if (num
2010-07-31 15:29:43 UTC
Mmm girl programmers are hot


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...