You are on page 1of 4

Simple Examples

Basic C++ Program


// // #include <iostream.h> Author: Bert G. Wachsmuth Version: Oct. 2000

int main(void ! cout << "#ello$ this is m% &irst '(( )ro*ram+ << endl, return 0, -

Data Types and Variable names


// // #include <iostream.h> Author: Bert G. Wachsmuth Version: Oct. 2000

int main(void ! dou.le / 0 1.2, int i 0 30, char c 0 4B5, // the &ollo6in* three lines 6or7s &ine: / 0 / ( 28/, // / has the ne6 value 30.2 dou.le % 0 / ( i, // % has the value 31.2 dou.le 9 0 321:2;30<12, // 9 has lar*e ran*e // the &ollo6in* t6o lines is not o7a%: int = 0 / ( i, // can not store dou.le as an int int 7 0 321:2;>?@0321:2;>@0, // ran*e o& 7 is not lar*e enou*h return 0, -

Input/Output
#include <iostream.h> int main(void ! dou.le / 0 0.0, // it5s *ood )ractice to initiali9e all varia.les .% assi*nin* a value cout << "Alease enter a num.er: ", cin >> /, cout << "Bou entered " << / << "$ 6hose sCuare is " << /8/ << endl, return 0, -

Basic Operations
int / 0 30, int % 0 20, int 9 0 (/ ( % 8 (/ D % / (2 8 % E %, // has value 2

Computational S ortcuts
int / 0 30, /((, // / has value 33 / D0 30, // / has value 3, / 80 2, // / has value 2,

!ogical Operators and Tests and Conditional Execution


Assuming i is some integer, what is the difference between:
i& ( (i E 2 00 0 cout << "i is an even num.er " << endl, cout << "i is an odd num.er " << endl, i& ( (i E 2 00 0 cout << "i is an even num.er " << endl, else cout << "i is an odd num.er " << endl, i& (i < cout else i& cout else i& cout else cout 0 << "i is ne*ative " << endl, ( (i E 2 00 0 << "i is even and not ne*ative+ << endl, ( (i E 2 00 3 << "i is odd and not ne*ative+ << endl, << "Bou have &ound a ne6 t%)e o& inte*er+ << endl,

What is the output of:


dou.le / 0 dou.le % 0 i& ( ((/ E cout << else cout << 1, 30, 2 00 0 FF G(% < / "Hhis+ << endl, "Hhat+ << endl,

Which of the following tests could crash? Why or why not?


i& ( (/ cout i& ( (/ cout G0 0 FF (3// > 2 << "Ieci)rocal o& / is .i**er than 2+ << endl, > 0 JJ (sCrt(/ < @ << "/ is .et6een 0 and 1+ << endl,

!oops
How often do the following loops execute:
&or (int i 0 0, i < 30, i(01 cout << "#o6 o&ten: + << i << endl, int i 0 0, 6hile (i < 30 ! cout << i << endl, i (0 :, -

What is the final value of sum:


int sum 0 0, &or (int i 0 3, i < 2, i(( sum (0 i,

What are the three ingredients for every loop?

"unctions
How do you use each of the functions defined below:
void &un3(void ! /8 does somethin* 8/ void &un2(int i ! /8 does somethin* 8/ dou.le &un1(int i$ dou.le / ! /8 does somethin* 8/ -

efine a function that computes the area of a rectangle! "t should not contain any cin or cout statements! efine a function that as#s the user to enter a double number and returns that number!

#e$erence and Value Parameters


What is the value of / and % at the end of the main function:
void &3(dou.le /$ dou.le % ! / 0 %, % 0 2, void &2(dou.leF /$ dou.le % ! / 0 D3, % 0 D2, int main(void ! dou.le / 0 30, dou.le % 0 30, &3(/$ % , &2(%$ / , cout << "/ 0 " << / << "$ % 0 " << % << endl, return 0, -

"f &3 and &2 are defined as above, which of the following calls are invalid:
&3(30$ 20 , &2(10$ :0 , dou.le / 0 30, &3(/$ / , &2(/$ / , &2(30$ / , &2(/$ 30 ,

Scope and Scope #ules


Which lines in the following code are invalid, and what$s the output if you were to ignore those errors:
#include <iostream.h> dou.le / 0 30, void ! / % void ! / &3(dou.le % 0 20, 0 10, &2(dou.le / 0 :0,

% 020, void &1(dou.leF 9 ! 9 0 ;0, / 0 >0, int main(void ! dou.le % 0 2, &3(/(% , &2(% , &1(/ D % , &1(/ , cout << "/ 0 " << / << "$ % 0 " << % << endl, return 0, -

%lobal Variables and %lobal Constants


#include <iostream.h> const dou.le AK 0 1.3:32, dou.le sur&ace(dou.le radius ! return 28AK8radius, dou.le area(dou.le radius ! return AK8radius8radius, -

&rrays
%! '! )! ,! .! efine an array A of %& doubles! efine an array A of %& doubles and set all of them to %!&( Assume A is an array of * integers! +ind the sum of the first and last entries and assign it to the third element! Write some code segment that prints out an array of doubles of si-e * Write some code segment that multiplies each element of an array by ', storing it bac# in the array!

&rrays as Input Parameters


%! '! )! ,! .! Write a function that prints out an array of doubles of si-e *! Write a function that multiplies an array by a number! What are the possible function headers for such a function? Write a function that finds the sum of two arrays! What are the possible function headers for such a function? Write a function that adds up all elements in an array! What are the possible function headers for such a function! Write one function each to find /a0 the maximum, /b0 the minimum, and /c0 the max and min of elements in an array! What are the possible function headers? Write a function that returns the location of the maximum value in an array! What are the possible function headers? 2se that function in a main method!

1!

You might also like