cs351 week 1
Download
Skip this Video
Download Presentation
CS351 – Week 1

Loading in 2 Seconds...

play fullscreen
1 / 28

CS351 – Week 1 - PowerPoint PPT Presentation


  • 45 Views
  • Uploaded on

CS351 – Week 1. Topics. Native Types Memory representation Variable initialization declaration and allocation. Previous knowledge. How to select variable type (string, integer, float, or boolean ) Concept of capturing external files for use in programming (import or include)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CS351 – Week 1' - nara


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
topics
Topics
  • Native Types
  • Memory representation
  • Variable initialization declaration and allocation
previous knowledge
Previous knowledge
  • How to select variable type (string, integer, float, or boolean)
  • Concept of capturing external files for use in programming (import or include)
  • Reading strings from standard input and parsing for numeric types if needed
  • Converting and concatenating strings for output to screen
  • Compute and save numerical results
  • Understanding of how to use a randomize function
objectives
Objectives
  • Identify native types in C++ that are generally available
  • Understand how to declare, allocate and initialize a variable
  • Declare variables in global or local scope
  • Create a basic C++ program that takes input from standard in, computes a value and prints the value to the screen
    • Using constant values
    • Appropriately including needed files
    • Correctly coding the main signature
    • Using object for inputting and outputting values
including files
Including files
  • Reusing code from a library
    • Python import math
    • C #include <math.h>
  • Differences between C and Python?
  • How do you find what file has the function you want?
computer memory
Computer memory

Conceptually the memory in a computer is very much like building lots laid out by lot number on a map of the streets of a city before any houses have been built.

computer memory1
Computer memory

Lots can be used individually or as groups

Although memory has a basic unit, units can be used together to represent more complex data

data for variables
Data for variables
  • “Jane Doe”
  • $475.24
  • 123-45-6789
  • 26 years of age
  • January
native types in c
Native types in C++
  • char – typically holds data encoded to represent written character sets
  • integer types – whole numbers; can be signed or unsigned; variations represent integers that use from 8 bits to 64 bits
  • float types – Stores numbers as fraction, exponent and sign; not precise
  • boolean – true or false
  • Reference (pointer) – location that refers to another location
  • strings – usually as array of characters; have special handling in the language; variable name refers to first location

Size of types is implementation dependent

Resources: http://en.wikipedia.org/wiki/Primitive_data_type, http://www.cplusplus.com/doc/tutorial/variables/

sample program

wget http:/bama.ua.edu/~anderson/size.cc

Sample program

//size.cc

#include <iosteam>

using namespace std;

int main() {

cout<<“sizeof(int) is ”<<sizeof(int)<<“ bytes”<<endl;

return 0;

}

Needed for cout and endl

Like java packages

Will discuss later

sizeof give the size in bytes of a type or variable

Needed since sizes vary

String literal

** What are the non-reserved words?

sample program1
Sample program

//size.cc

#include <iosteam>

using namespace std;

intmain() {

cout<<“sizeof(int) is ”<<sizeof(int)<<“ bytes”<<endl;

return 0;

}

other general rules
Other general rules
  • Everything is case sensitive
  • These identifiers are reserved (can’t be used as variable names)

What does it mean to be a reserved word?

http://en.cppreference.com/w/cpp/keyword

other syntax
Other syntax
  • semicolons are used to denote the end of a statement except
    • after the #include statement
    • before a block
  • Whitespace is not part of syntax

//size.cc

#include <iosteam>

using namespace std;

int main() {

cout<<“sizeof(int) is ”<<sizeof(int)<<“ bytes”<<endl;

return 0;

}

programmed example 10 min work in pairs only pairs
Programmed Example - 10 minWork in pairs (only pairs)
  • Create a C++ program that prints out the size of the following variables
    • char
    • int
    • unsigned int
    • short
    • long
    • long long
    • float
    • double
  • Fix w1ex1.cc so that it compiles and prints Hello World

Turning in programs

Put both names at top

Comments start with //

cat <program name> |mailx –s “<lastname 1> <lastname2> Week # Date” [email protected]

Example:

cat w1ex1.cc |mailx –s “honganderson Week 1 011414” [email protected]

user input form keyboard
User input form keyboard

//cin.cc

#include <iosteam>

using namespace std;

int main() {

int value;

cin >>value;

cout<<“The user entered” <<value<<endl;

cout<<“value * 3 ==<<” <<value*3<<endl;

return 0;

}

1st assignment 5 min work in pairs only pairs
1st assignment – 5 minWork in pairs (only pairs)
  • Fix w1ex2.cc so that it calculates the user’s mileage reimbursement

Turning in programs

Put both names at top

Comments start with //

cat w1ex2.cc|mailx –s “Week 1: 011414” [email protected]

c variables
C++ variables
  • Declaration – Give variable a name and a type
  • Allocation – Identify location where variable will be stored
  • Initialization – Set variable to appropriate initial value

When this happens depends on scope…

variable scope
Variable scope
  • A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
    • Inside a function or a block which is called local variables,
    • In the definition of function parameters which is called formal parameters.
    • Outside of all functions which is called global variables.
c native type rules
C++ native type rules
  • For local variables
    • <type> <identifier>; will only declare and allocate
    • <type> <identifier>=<value>; will declare, allocate and initialize
local variable declaration
Local variable declaration

#include <iostream>

using namespace std;

int main () {

// Local variable declaration:

int a, b;

int c;

// actual initialization

a = 10;

b = 20;

c = a + b;

cout << c;

return 0;

}

c native type rules1
C++ native type rules
  • For global declarations
    • <type> <identifier>; will declare, allocate and initialize
    • <type> <identifier>=<value>; will declare, allocate and initialize
mixed declaration
Mixed declaration

//mixed.cc

#include <iostream>

using namespace std;

int g;

int main () {

// Local variable declaration/allocation:

int a, b;

// actual initialization

a = 10;

b = 20;

g = a + b;

cout << g;

return 0;

}

cout <<a; //not guaranteed to be coherent

same variable in two scopes
Same variable in two scopes

//twoscopes.cc

#include <iostream>

using namespace std;

// Global variable declaration:

int g = 20;

int main () {

// Local variable declaration:

int g = 10;

cout << g;

return 0;

}

Is this an error? If not, what prints here?

The enclosing scope has precedence

variable scope matters
Variable scope matters
  • If the scope of a variable is global (defined outside of braces),
    • Can be used throughout a program (any function or code; even in other source files
    • Memory is know to be needed at compile time
    • Compiler can designate a place for this memory as part of the program setup
  • If a variable has scope local to some block
    • it can only be used by statements in that block
    • compiler knows about it *but* we don’t know when functions will be run;
    • To conserve memory, we reused the memory when it is not needed (out of scope)

Memory in these two different cases are allocated in different places in the process space (program)

c constants
C++ constants
  • Uses const modifier in front of variable declaration
  • Value cannot be changed (compiler code will not be generated)
  • Allows compiler to optimize access
  • Acts as a semantic check
  • Better than #define because the type is specified (allows type checking by compiler)
  • #define is a macro; replaces text before compiling
constants
Constants

//const.cc

#include <iostream>

using namespace std;

// Global variable declaration:

constintmonthsInYear = 12;

int main () {

cout<<monthsInYear<<endl;

int age;

cin>>age; //what happens if you put in a floating poitn number?

cout<<“Entered age “<<age<<endl;

monthsInYear=0; //error line

inttotalMonths = monthsInYear*age;

count<<totalMonths<<endl;

return 0;

}

c reference types
C++ reference types
  • Strings, objects, arrays, and structures are not native types but are reference types
  • Composed of primitive types
  • Different rules for declaration, allocation and initialization
  • For all types, if you initialize, you automatically allocate
2 nd assignment
2nd Assignment
  • Fix w1ex3.cc so that it:
    • prints prompts for user input
    • prints out the number of weeks in the current month
    • Calculates the current weekly salary based on monthly salary input and weeks in the current month
  • Write a program that takes an angle in degrees from the user and converts it to radians. Use a constant for π
  • Consult instructions for submission
  • Quiz online on this material posted Monday; complete before class on Tuesday
ad