Jumaat, 18 Februari 2011

Looping

1. What happens in a while loop if the control condition is false (i.e., zero) initially.

2. How does the break statement provide better control of loops?

3. What is wrong with the following loop:
a. while(n<=100) {sum+=n*n; }

4. if s is a statement, and e1, e2, and e3 are expressions, then what is the difference between the program fragment: a. for(e1,e2,e3) {s;}
And the fragment :
b. e1; while(e2) {s; e3; }



5. Describe the output from this program
int main()
{for(int i=0; i<8;i++)
if (i%2 ==0) cout<else if (i%3 ==0) cout<else if (i%5 == 0) cout<<2*i-1<<”\t”;
else cout<}

Isnin, 17 Januari 2011

Uji Kemahiran..Gud Luck!

Ok guys...answer the questions below without compile it. Sharpen your analytical thinking

a. Write a program that enable user to key-in code number (1 - 5) to display month. Use switch statement


b. Write and run a program that prints the sum, difference, product, quotient and remainder of two integers. Initialize the integers with the values 60 and 7.

c. Write and run a program that prints the sum, difference, product, quotient and remainder of two integers that are input interactively.

d. Write and run a test program that shows how your system handles uninitialized variables.

Ahad, 16 Januari 2011

AGAIN..BASIC PROGRAM EXERCISE
Cube jawab soalan dibawah.. ada byk lagi yang sedang menanti.. :)

1. Describe the two ways to include comments in a C++ program.


2. What is wrong with this program?
#include
int main()
{
//”luving C++:”
cout<<”I luv C++\n” }


3. What is wrong with the following C-style comment?
cout<<”Hello, /*change*/ world.\n”;

4. What does a declaration do?

5. What is the shortest possible C++ program?

6. Where the name “C++” does comes from?

7. What’s wrong with these declarations:
int first = 67, last = 100, new = 2, old = 6;

8. In each of the following, assume that m has the value 5 and n has the value 2 before the statement executes. Tell what the values of m and n will be after each of the following statements executes:
a. m*=n++;
b. m+=--n;


9. What is a run-time error? Give examples of two different kinds of run-time errors.


10. What is a compile-time error? Give examples of two different kinds of compile-time errors.

answer:
1. i. /* like this */
ii. //like this

2. The semicolon is missing from the last statement

3. Everything between the double quotes will be printed, including the intended comment

4. A declaration tells the compiler the name and type of the variable being declared. It also may be initialized in the declaration.


5. void main(){}

6. The name refers to the C language and its increment operator ++. The name suggests that C++ is an advance over C.

7. the only thing wrong with these declarations is that new is a keyword. Keywords are reserved and cannot be used for names of variables.

8. a. m will be 10 and n will be 1
b. m will be 6 and n will be 1


9. A run time error is an error that occurs when a program is running. Numeric overflow and division by zero are examples of run time errors.

10. A compile-time error is an error that occurs when a program is being compiled. Examples: syntax errors such as omitting a required semicolon, using an undeclared variable, using a keyword for the name of a variable.




Tinta buat pelajar DIP 2C yang tercinte

Cube praktikkan ini:

1. Seboleh-bolehnya beli buku rujukan C++ yang berbeza antara satu sama lain. Jadi anda boleh bertukar-tukar buku. Lebih byk contoh program boleh dirujuk dan dikongsi. Pastikan juga buku yg ingin dibeli mengandungi topic objek oriented programming utk kegunaan sem 3 nanti dan sampai bile2.

2. Bina sekurang-kurangnya satu program sehari tanpa bantuan rakan untuk mantapkan kemahiran dari segi sintaksis dan mengasah kemahiran berfikir secara logik (program berkaitan pengiraan/matematik).

3. Teknik terbaik untuk memahami subjek ini (tidak tertakhluk pade subjek ini shj), setelah belajar sendiri cube ajar rakan anda yang lain (termasuk pelajar DIP 2B dan 2A). Dan PASTIKAN rakan anda fhm :)

4. Sentiase berfikir seperti compiler.. tetapi tidak perlu terperinci.

5. Lukiskan gambarajah atau nyatakan aliran kerja compiler untuk lebih mudah memahami aturcara.

6.Sentiasa positif dalam menuntut ilmu.

latihan asas

Basic C++ Exercise

1. Which of the following is true?
A. 1
B. 66
C. .1
D. -1
E. All of the above

2. Which of the following is the boolean operator for logical-and?
A. &
B. &&
C.
D. &

3. Evaluate !(1 && !(0 1)).
A. True
B. False
C. Unevaluatable

4. Which of the following shows the correct syntax for an if statement?
A. if expression
B. if { expression
C. if ( expression )
D. expression if

5. What is the correct value to return to the operating system upon the successful completion of a program?
A. -1
B. 1
C. 0
D. Programs do not return a value.

6. What is the only function all C++ programs must contain?
A. start()
B. system()
C. main()
D. program()

7. What punctuation is used to signal the beginning and end of code blocks?
A. { }
B. -> and <-
C. BEGIN and END
D. ( and )
8. What punctuation ends most lines of C++ code?
A. .
B. ;
C. :
D. '
9. Which of the following is a correct comment?
A. */ Comments */
B. ** Comment **
C. /* Comment */
D. { Comment }
10. Which of the following is not a correct variable type?
A. float
B. real
C. int
D. Double
11. Which of the following is the correct operator to compare two variables?
A. :=
B. =
C. equal
D. ==
12. Which follows the case statement?
A. :
B. ;
C. -
D. A newline
13. What is required to avoid falling through from one case to the next?
A. end;
B. break;
C. Stop;
D. A semicolon.
14. What keyword covers unhandled possibilities?
A. all
B. contingency
C. default
D. Other
15. What is the result of the following code?
int x=0; switch(x)
{
case 1: cout<<"One";
case 0: cout<<"Zero";
case 2: cout<<"Hello World";
}
A. One
B. Zero
C. Hello WorlD
D. ZeroHello World
Section B:
1. Write a program to calculate area of triangle. User to key in the value of side of rectangle. (Hint: ½*width*height)
p/s: latihan ini adalah untuk menambahkan ilmu semata-mata dan persediaan midterm.
Good Luck on ur exam.