C++ Basics March 10th

advertisement
C++ Basics
March 10th
A C++ program
//if necessary include headers
//#include <foo.h>
void main() {
//variable declaration
//read values input from user
//computation
//print output to user
}
Notes:
• what follows after // on the same line is considered comment
• indentation is for the reader; compiler ignores all spaces and new line ; the
delimiter for the compiler is the semicolon
• all statements ended by ;
• Lower vs. upper case matters!!
– Void is different than void
– Main is different that main
Variable declaration
type variable-name;
Meaning: variable <variable-name> will be a variable of type <type>
Where type can be:
– int
– double
– char
Example:
int a, b, c;
double x;
int sum;
char my-character;
//integer
//real number
//character
Input statements
cin >> variable-name;
Meaning: read the value of variable <variable-name> from
the user
Example:
cin >> a;
cin >> b >> c;
cin >> x;
cin >> my-character;
Output statements
cout << variable-name;
Meaning: print the value of variable <variable-name> to the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl;
Meaning: print a new line
Example:
cout << a;
cout << b << c;
cout << “This is my character: “ << my-character << “ he he he”
<< endl;
If statements
True
if (condition) {
S1;
}
else {
S2;
}
S3;
condition
False
S2
S1
S3
Boolean conditions
..are built using
• Comparison operators
==
equal
!=
not equal
<
less than
>
greater than
<=
less than or equal
>=
greater than or equal
• Boolean operators
&&
and
||
or
!
not
Examples
Assume we declared the following variables:
int a = 2, b=5, c=10;
Here are some examples of boolean conditions we can use:
• if (a == b) …
• if (a != b) …
• if (a <= b+c) …
• if(a <= b) && (b <= c) …
• if !((a < b) && (b<c)) …
If example
#include <iostream.h>
void main() {
int a,b,c;
cin >> a >> b >> c;
if (a <=b) {
cout << “min is “ << a << endl;
}
else {
cout << “ min is “ << b << endl;
}
cout << “happy now?” << endl;
}
While statements
while (condition) {
S1;
}
S2;
True
condition
S1
S2
False
While example
//read 100 numbers from the user and output their sum
#include <iostream.h>
void main() {
int i, sum, x;
sum=0;
i=1;
while (i <= 100) {
cin >> x;
sum = sum + x;
i = i+1;
}
cout << “sum is “ << sum << endl;
}
Arrays
Used to store a collection of elements (variables)
type array-name[size];
Meaning:
This declares a variable called <array-name> which contains <size>
elements of type <type>
The elements of an array can be accessed as: array-name[0],…arrayname[size-1]
Example:
int a[100]; //a is a list of 100 integers, a[0], a[1], …a[99]
double b[50];
char c[10];
Array example
//Read 100 numbers from the user
#include <iostream.h>
void main() {
int i, a[100], n;
i=0; n=100;
while (i<n) {
cout << “Input element “ << i << “: ”;
cin >> a[i];
i = i+1;
}
//do somehing with it ..
}
Problems
Write a C++ program to read a sequence of (non-negative) integers from
the user ending with a negative integer and write out
•
•
•
•
the average of the numbers
the smallest number
the largest number
the range of the numbers (largest - smallest)
• Example:
– The user enters: 3, 1, 55, 89, 23, 45, -1
– Your program should compute X{3, 1, 55, 89, 23, 45}
Download