Wednesday, Oct 6 th You’re gonna learn how to program today! (If you’ve programmed before, then ponder on this brain teaser to relieve your boredom). main() { int a = 5, b = 10; a = <fill this in>; b = <fill this in>; a = <fill this in>; } Using only addition and subtraction, fill in the blanks to swap the values of a and b…
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Wednesday, Oct 6th
You’re gonna learn how to program today!
(If you’ve programmed before, then ponder on this brain teaser to relieve your
boredom). main(){ int a = 5, b = 10;
a = <fill this in>;
b = <fill this in>;
a = <fill this in>; }
Using only addition and subtraction, fill in the blanks to swap the
values of a and b…
Agenda
• Learn about the programming process
• Questions about Project #1?• Our first C++ programs:
– Learn how to print to the screen– Learn about variables– Learn about mathematical
operators in C++– Learn how to input more than
one number at a time
The Programming ProcessDesign your
program
Edit your program
Compileyour
program
Run andtest your program
Does it compile without errors?Yes
No
Does it work correctly?NoYes
Design
Goal: Figure out how your program is going to work before you write a single line of
logic.
Things to think about:
• What is the problem you’re trying to solve?• What are your requirements? • What are the main tasks required to solve the problem?• How should you organize your program to best address the problem?
Editing Your ProgramGoal: Type in your program source code in a
Compiling your ProgramGoal: Translate your program from C++
source code into a working program!
MyProg
GO BRUINS!
(What’s happening under the hood is a bit more
complicated than this, but that’s
the general idea…)
Running and Testing Your Program
Goal: Make sure you don’t have any semantic errors in your program logic.
Things to do:
• Test your program to make sure it works• Make sure your program works properly even if the user doesn’t use it correctly.• Fix any problems you find and then repeat needed testing.
Our First C++ Programs
Goal: Learn about C++ by working through simple examples.
Things to think about:
• Learn proper C++ syntax• Learn proper programming style• Learn how to accomplish certain tasks:
• Printing to the screen• Inputting data from the user• Making decisions
• Learn how to spot errors
// Our first C++ program: Hello world! #include <iostream> int main(void){
The #include<filename> command basically means: “Hey compiler, the file called “filename” contains important information you’ll need to compile my program.”
iostream is a special file that contains information on how to print information to the computer screen.
#include <cmath>
iostream
Similarly, you could #include<cmath> if your program used math operations like sin/cosine/sqrt.
std::cout << “The cosine of 52 is “ << cos(52);
// Our first C++ program: Hello world! #include <iostream> int main(void){
To declare a variable, we first specify the variable’s type, and then its name.
If we like, we can define multiple variables at the same time.
int goblins, goobers, burps;
And we can initialize variables when we declare them if we like.
int noogie = 7, farts;
Variable Names 1. Variable names may have letters,
numbers and underscores _’s but no other symbols.
2. Variable names may not begin with a number.
3. Variable names are case sensitive.aGoodVariable15Amount$Made31CSROSTERANICE*VARIABLENum Goats_EarWaxVolume
Good or bad?
Types of Variables
In the previous example, we declared two integer variables:#include <iostream> int main(void){ int years_old, num_hairs; // two integers std::cout <<"How old’s Carey?"; std::cin >> years_old; num_hairs = 100 - years_old; std::cout << “Carey has " << num_hairs << " hairs left." << std::endl; }
An integer (int) variable can hold:
• both positive and negative whole numbers
• any value between -2147483648 to 2147483647Now lets learn about other types of variables we
can use in our programs…
Floating-point VariablesAs we’ve seen,
integer variables can only hold whole
number values.
What if we want to perform
calculations with real numbers?
If you want to store real values in your program, you need to use the float variable
type.int main(void){ float pi; // float variables can hold a real #
pi = 3.14159;
std::cout << “PI times 10 is: “ << pi*10;
}
pi
3.14159
A floating point (float) variable can hold:
• both positive and negative real numbers
• any value between ± 3.4 * 10-38 to ± 3.4 * 1038
Unsigned Variables
In some cases, we want an integer variable to hold only non-negative values.
In this case, we use an unsigned variable.
int main(void){ unsigned int age; // ages can’t be negative!
age = 10;
All you do is place the word “unsigned” in front of the standard type name.
-5; // ERROR!
Unsigned Variables
int main(void){ unsigned int age;
age = 10;
An unsigned integer (unsigned int) variable can hold:
• both zero or positive whole numbers∙ any value between 0 and 4294967296
The unsigned keyword can only be used with whole-number style variables.
float age; // ERROR!
Other Variable Typeslong Variables long variables are the same as int variables on PCs/MACs.
int main(void){
short Variables A short variable is basically a small int and can hold values between -32768 to 32767
unsigned short variables can hold values between 0 and 65535.
long careysEgo; // same as int careysEgo;unsigned long smallbergsAge;
short careysIQ = 32766;unsigned short smallbergsWeight = 65500;
Other Variable Types
double Variables double variables are basically extremely precise float variables. They hold really big values with high precision:
± 1.7 * 10-308 to ± 1.7 * 10308int main(void){
char zits = 120; unsigned char grits = 254;
char Variables char variables are extremely small integer variables. They can hold values between -128 to 127.
unsigned char variables can hold values between 0 and 255.
double big = 3.925e123; // 3.925123
Each Variable Has A…
Type: Describes the type of data a variable holds (e.g. an int, char, short, float, or double).
Size: Each variable occupies a certain number of slots in the computer’s memory…
Value: Every variable has a value. All variables start with a random value until they are initialized.
Name: Every variable has a name that the programmer can use to reference it.
The computer’s memory (RAM) is split
up into slots called bytes.
Every variable occupies one or more slots
(bytes).
char 1 byte short 2 bytes int and long 4 bytes float 4 bytes double 8 bytes
Variables Take Up Space!
} 1 byte
Variables Take Up Space!
char 1 byte short 2 bytes int and long 4 bytes float 4 bytes double 8 bytesint main(void){
short sh = 1927;
float fl = 3.14159;
char ch = 27;
...
sh 19
27
fl 3.1
4159
ch 27
Expressions are evaluated from left to right according to standard precedence rules.
You can use the standard math operators in C++.
// learn how to use math operations in C++#include <iostream>