Professional Documents
Culture Documents
*/ }
{ }
cout << "The answer is " << answer; cout << "count: " << count<<endl;
return 0; func2();
} }
Example 2: //demo 2 }
int & min_swap(int &x, int &y) Program 5: /* this program squares a number
{ and displays the result*/
int temp; // use references to exchange
//the values of the arguments void main(void)
temp = x;
x = y; {
y = temp;
// return reference to minimum arg int i;
if(x < y)
return x; for(prompt(); i=readnum(); prompt())
else
return y; sqrnum(i); //calling ‘sqrnum’ function
}
return;
Program 6:
//this program recursively multiplies natural N os Program 2: /*this prog finds max, min,sum, and
average of array elements*/
Int Mul( int a, int b) //function defintion
#include<iostream.h>
{
Int const size=21;
If(b==1)
void main()
return a;
#define MAX 5
void main () switch(ch)
{ int i; // the array loop variable {
int temp; case 'E':
int x[]={100,200,300,400,500}; enter_grades();
break;
cout << "The array firstly looks like this :\n"; case 'R':
for (i = 0; i < MAX; i++) disp_grades(grade);
cout << x[i] << " "; break;
cout << endl; case 'Q':
void main()
{
int choice;
Program 6:
do {
// a program that reverses a string choice = menu(); // get selection
switch(choice) {
#include <iostream.h> case 0: break;
void main () case 1: enter();
{ int i; // loop variable break;
char temp; case 2: report();
char s[14] = "saipuakaupias"; break;
int string_length, half_way; default: cout << "Try again.\n\n";
}
// determine the length of the //string } while(choice != 0);
for (i = 0; i <= 14; i++)
if (s[i] == '\0') return ;
string_length = i - 1; }
/* Note that string_length is the position of the
character before the \0 */ // Return a user's selection.
int menu()
half_way = string_length / 2; {
int choice;
cout << "Before the swap : " << s;
cout<< endl; cout << "0. Quit\n";
cout << "1. Enter information\n";
// now swap cout << "2. Report information\n";
for (i = 0; i < half_way; i++) cout << "\nChoose one: ";
{ cin >> choice;
temp = s[i];
int i;
Structures
int *p;
Program 1: // Demonstrate structure assignments.
p = &i; // p now points to i
{ #include <string.h>
};
cout << "Structures before assignment.\n"; int lead_time; // number of days before
//resupply
cout << "svar1: " << svar1.a << ' ' << svar1.b;
} invtry[SIZE];
cout << '\n';
//prototypes
cout << "svar2: " << svar2.a << ' ' << svar2.b;
void enter(), init_list(), display();
cout << "\n\n";
void update(), input(int i);
svar2 = svar1; // assign structures
int menu();
cout << "Structures after assignment.\n";
int main()
cout << "svar1: " << svar1.a << ' ' << svar1.b;
{ char choice;
cout << '\n';
init_list();
cout << "svar2: " << svar2.a << ' ' << svar2.b;
for(;;) {
return 0;
choice = menu();
}
switch(choice) {
Program 2: /* A simple inventory program that
uses an array of structures. */ case 'e': enter();
} void enter()
} { int i;
{ if(!*invtry[i].item) break;
} return;
{ char ch; }
do { void input(int i)
cout << "Lead time to resupply (in days): "; for(t=0; t<SIZE; t++) {
cout << "Enter item: "; cout << invtry[t].lead_time << " days\n\n";
if(!strcmp(name, invtry[i].item)) }
break;
cout << "Item not found.\n"; Prog 1:/* this prog writes the numbers 1 to 10 to
a file called TEST */
#include<iostream.h>
#include<fstream.h>
const char* filename = "TEST";
void main()
{
int i = 0; // count from 1 to 10
ofstream numbers (filename);
// Creates a file variable called 'numbers'
//and ties it to the name TEST on the disc
Now, to make a game, first of all, you need to have an idea of what kind of game you
want to make.
Below are simple programs which you can enrich them further by adding whatever you
think should be integrated. Have Fun!
/* ****************************
// Define the greatest possible value: cout << "\nInsert a value between 1 and "
<< MAX_RANGE << " : ";
do { else
cin >> input; // Get user input { // else it is the correct number!
counter++; //count user attempts cout << "That's right! Value was " << input;
cout << "\nYou have needed " << counter
if (value>input <<" attempts.";
******************************************************************************
Game2: "Higher or Lower?" Brief Description:
*********************************************************************************
stone -> scissors -> paper -> stone... case 0: option='s'; break;
The first who wins WINSCORE times is the case 1: option='x'; break;
competition winner.
case 2: option='p'; break;
****************************** */
}
return option;
#include <iostream.h>
}
#include <stdlib.h>
// Function WhoWins
#include <time.h>
// * check which of the characters passed wins.
#define WINSCORE 3
// return values:
// Function PickRandomOption
// 0= tie, 1=the first, 2=the second, -1=error
{ int yourpoints=0;
if (b=='x') return 1;
else return 0; do {
if (b=='s') return 1; cout << "I say: " << me << "\n";
} else if (winner==1)
else if (winner==2)
cout << "Sorry. You entered an Invalid The user has to discover all the letters in a
option\n"; secret keyword one by one. Each letter said that
is in the keyword is shown in its rightful
// show current scoreboard. location. If you know the hangman game,
cout << "POINTS: You:" << yourpoints; You have the opportunity to fail up to
FAILS_ALLOWED times.
cout << " Me:" << mypoints << "\n";
Enjoy the Game!
} while (yourpoints<WINSCORE &&
mypoints<WINSCORE); ******************************** */
#include <iostream.h>
} #define KEY_MAX_WIDTH 20
#define FAILS_ALLOWED 7
***************************************
// This will store the key
This game makes use of Shows:
char key [KEY_MAX_WIDTH];
Strings, arrays of strings, string functions:
strcpy and strlen
// This will store the string shown on screen
//with letters already discovered.
int CheckLetter (char letter); cout << "Enter a letter (You may fail " <<
fails << " times): ";
main ()
cin >> input;
{
cin.ignore (100,'\n');
char input;
// check if letter is valid
int valid;
valid = CheckLetter (input);
int fails = FAILS_ALLOWED;
// if it is valid, increase discovered letters
unsigned int discovered = 0; //counter; if not, decrease allowed fails
discovered+=valid;
srand ( time (NULL) ); // Initialize } while (discovered < strlen(key) && fails>0);
random //number generator
// The loop ends if key is discovered or fails are
int value = rand()%N_KEYS; // Get random exhausted.
//between 0 and NKEYS-1
// Display CORRECT! only if key was
strcpy (key,possiblekeys[value]); // Copy key discovered.
cout << "\nDiscover the secret key: " << int CheckLetter (char letter)
outstring << "\n";
{
outstring[n]=key[n];
} int main(void)
return found; {
} char done;
Below are some of the possible moves: init_matrix(); //function to initialize array
do{
O O
disp_matrix();
X X X X X wins!
O O O O X O
get_player_move();
X X O X X O X X O
X X X
done = check(); /* see if winner */
O
if(done!= ' ') break; /* winner!*/
X X O
get_computer_move();
cout<<"You won!\n";
disp_matrix(); /* show final positions */ matrix[x][y] = 'X'; //put the ‘X’ mark
return 0; }
void get_computer_move(void)
} if(matrix[i][j]==' ')
void get_player_move(void) }
int x, y; if(i*j==9) {
cin>>x>>y; exit(0);
x--; y--; }
} void disp_matrix(void)
{ return matrix[0][0];
cout<<"\n";
char check(void)
int i;
return matrix[i][0];
if(matrix[0][i]==matrix[1][i] &&
matrix[0][i]==matrix[2][i])
return matrix[0][i];
/* test diagonals */
if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])