Académique Documents
Professionnel Documents
Culture Documents
int main()
{
int magic; // magic number
int guess; // user's guess
return 0;
}
listing 2
// Magic Number program: 1st improvement.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int magic; // magic number
int guess; // user's guess
return 0;
}
listing 3
// Divide the first number by the second.
#include <iostream>
using namespace std;
int main()
{
int a, b;
listing 4
if(b == 0) cout << a/b << '\n';
listing 5
if(i) {
if(j) statement1;
if(k) statement2; // this if
else statement3; // is associated with this else
}
else statement4; // associated with if(i)
listing 6
// Magic Number program: 2nd improvement.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int magic; // magic number
int guess; // user's guess
if (guess == magic) {
cout << "** Right **\n";
cout << magic << " is the magic number.\n";
}
else {
cout << "...Sorry, you're wrong.";
if(guess > magic) cout <<" Your guess is too high.\n";
else cout << " Your guess is too low.\n";
}
return 0;
}
listing 7
// Demonstrate an if-else-if ladder.
#include <iostream>
using namespace std;
int main()
{
int x;
return 0;
}
listing 8
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num;
double sq_root;
return 0;
}
listing 9
#include <iostream>
using namespace std;
int main()
{
int i;
return 0;
}
listing 10
for(count=10; count < 5; count++)
cout << count; // this statement will not execute
listing 11
for(x=0, y=10; x<=10; ++x, --y)
cout << x << ' ' << y << '\n';
listing 12
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i;
return 0;
}
listing 13
#include <iostream>
using namespace std;
int main()
{
int x;
for(x=0; x != 123; ) {
cout << "Enter a number: ";
cin >> x;
}
return 0;
}
listing 14
cout << "Enter tab position: ";
cin >> x;
listing 15
for(;;)
{
//...
}
listing 16
for(x=0; x<1000; x++) ;
listing 17
// Demonstrate the switch using a simple "help" program.
#include <iostream>
using namespace std;
int main()
{
int choice;
switch(choice) {
case 1:
cout << "for is C++'s most versatile loop.\n";
break;
case 2:
cout << "if is C++'s conditional branch statement.\n";
break;
case 3:
cout << "switch is C++'s multi-way branch statement.\n";
break;
default:
cout << "You must enter a number between 1 and 3.\n";
}
return 0;
}
listing 18
#include <iostream>
using namespace std;
int main()
{
int i;
return 0;
}
listing 19
switch(i) {
case 1:
case 2:
case 3: do_something();
break;
case 4: do_something_else();
break;
listing 20
switch(ch1) {
case 'A': cout << "This A is part of outer switch";
switch(ch2) {
case 'A':
cout << "This A is part of inner switch";
break;
case 'B': // ...
}
break;
case 'B': // ...
listing 21
/* This program displays all printable characters,
including the extended character set, if one exists.
*/
#include <iostream>
using namespace std;
int main()
{
unsigned char ch;
ch = 32;
while(ch) {
cout << ch;
ch++;
}
return 0;
}
listing 22
#include <iostream>
using namespace std;
int main()
{
int len;
return 0;
}
listing 23
while(rand() != 100) ;
listing 24
#include <iostream>
using namespace std;
int main()
{
int num;
do {
cout << "Enter a number (100 to stop): ";
cin >> num;
} while(num != 100);
return 0;
}
listing 25
// Magic Number program: 3rd improvement.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int magic; // magic number
int guess; // user's guess
do {
cout << "Enter your guess: ";
cin >> guess;
if(guess == magic) {
cout << "** Right ** ";
cout << magic << " is the magic number.\n";
}
else {
cout << "...Sorry, you're wrong.";
if(guess > magic)
cout << " Your guess is too high.\n";
else cout << " Your guess is too low.\n";
}
} while(guess != magic);
return 0;
}
listing 26
#include <iostream>
using namespace std;
int main()
{
int x;
return 0;
}
listing 27
#include <iostream>
using namespace std;
int main()
{
int t;
return 0;
}
listing 28
for(i=0; i<1000; i++) {
// do something
if(kbhit()) break;
}
listing 29
#include <iostream>
using namespace std;
int main()
{
int t, count;
return 0;
}
listing 30
/* This program finds the prime numbers from
2 to 1000.
*/
#include <iostream>
using namespace std;
int main()
{
int i, j;
return 0;
}
listing 31
x = 1;
loop1:
x++;
if(x < 100) goto loop1;
listing 32
for(...) {
for(...) {
while(...) {
if(...) goto stop;
.
.
.
}
}
}
stop:
cout << "Error in program.\n";
listing 33
// Magic Number program: Final improvement.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int option;
int magic;
magic = rand();
do {
cout << "1. Get a new magic number\n";
cout << "2. Play\n";
cout << "3. Quit\n";
do {
cout << "Enter your choice: ";
cin >> option;
} while(option<1 || option>3);
switch(option) {
case 1:
magic = rand();
break;
case 2:
play(magic);
break;
case 3:
cout << "Goodbye\n";
break;
}
} while(option!=3);
return 0;
}