- 2007, Jun 8th Fri
C++:
- Array initialization:
* double monthly_sales[3] = { 1, 2, 3 };
* double monthly_sales[] = { 1, 2, 3 }; // specifying size isn't necessary
* int monthly_sales[12] = { 0 }; // all 12 elements are 0 now.
* int monthly_sales[12] = { 1, 2, 3 }; // elements 4 - 12 are 0.
* out of bound index is your problem.
* arrays are passed to functions by reference only.
* void my_initialize_function( int some_array[], int size) {
for (int i = 0; i < size; i++) {
some_array[i] = 0;
}
}
* void copy_array(const int a[], int b, int size) {
for (int i = 0; i < size; i++) {
b[i] = a[i];
}
}
* monthly_sales[0] <- first element address is the base address of the array
* functions can not return a value of type array.
- 2007, Jun 7th Thr
C++:
- A data type is called simple if variables of that type can store only
one value at a time.
- In contrast, in a structured data type each data item is
a collection of other data items. Simple data types are building blocks of
structured data types.
- An array in C++ is a collection of a fixed number of components,
wherein all of the components are of the same data type.
- int monthly_sales[12]; // monthly_sales is an array of size 12
- int i = 0;
cout << "January sales = " << monthly_sales[i] << endl;
- cin >> monthly_sales[11]; // read December's sales into the last space
- largest element in the array:
maxIndex = 0;
for(i = 1; i < 10; i++) {
if (monthly_sales[i] > monthly_sales[maxIndex]) {
maxIndex = i;
}
}
cout << "Max sales were = " << monthly_sales[maxIndex];
- 2006, Jan 27th Fri
Scheme:
- Compound Data Types
- strings
- strings are sequences of characters
"Hello World"
(string #\h #\e #\l #\l #\o) => "hello"
- (define superhero "Batman")
- (string-ref superhero 0) => \#B
- (string-append superhero " and " "Robin") => "Batman and Robin"
- (make-string 6) => #\000 #\000 #\000 #\000 #\000 #\000 # creates a 6 char string
- (string-set! superhero 0 #\C) => "Catman"
- (string? superhero)
- vectors
(vector 1 2 3) => #(1 2 3)
(make-vector 5) => #(() () () () ())
(vector? some-vector)
(vector-ref some-vector index)
(vector-set! some-vector index value)
- dotted pairs
(cons "hello" 3.14) => ("hello" . 3.14)
(define foo (cons "hello" 3.14))
(car foo) => "hello"
(cdr foo) => 3.14
(set-car! foo "goodbye") # set first element
(set-cdr! foo 2.17) # set last element
- list
() # empty list
'() # Im talking about an empty list
(define alist '())
(cons 1 alist) => (1) # cons-tructing a list
(define alist (cons 1 alist)) => (1)
(define alist (cons 2 alist)) => (2 1)
(define alist (cons 3 alist)) => (3 2 1)
(define alist '(1 2 3)) => (1 2 3)
(list-ref alist index)
(list-tail alist index) => (2 3) # returns the tail of the list
# starting from the given index
(pair?
(null? ()) => #t
(list?
- Conversions between data types
(char->integer #\a) => 97
(integer->char 97) => #\a
(string->list "hello") => #\h #\e #\l #\l #\o
(string->number "97") => 97
(number->string 97) => "97"
(string->number "hello") => #f
- a "form"
- some forms are self-evaluating e.g. 3.14 => 3.14
- some not: (+ 1 2) => 3
- scheme evaluates a list form
by examining the head of the form,
if it evaluates to a procedure,
then the rest of the form is evaluated to get the procedures arguments.
- special forms evaluate depending on the form.
- user created procedures
- (lambda (procedure-args) (procedure-body))
- (lambda (a b) (+ a b))
- (define add (lambda (a b) (+ a b)))
- (add 1 2) => 3
- (apply (procedure) (list)) # applies procedure to list
- (apply + '(1 2 3)) => 6
- (begin forms) # forms are evaluated in sequence
e.g.
(begin (display "hello") (newline) (display "world"))
- lambda bodies are implicit begins.
(define greeting (lambda ((display "hello") (newline) (display "world"))))
- conditionals
- (and 1 2) => 2 # returns final true form
(and #f 1) => #f
- (or 1 2) => 1 # returns first true form
(or #f 2) => 2
- these forms are evaluated left to right
- (if (x > 10)
'yes
'no)
(if (x > 10)
(display "ok"))
(if (x > 10)
(display "ok")
(display "not ok"))
- 2006, Jan 26th Thr
Java:
abstract classes
packages
Ruby:
class variables can only be accessed via a method outside of the class.
Scheme: