Thursday, January 5, 2012

Thursday 1/5/12

@autoreleasepool {
int a = 25, b = 5, c = 10, d = 7;

NSLog(@"a %% b = %i", a % b);
NSLog(@"a %% c = %i", a % c);
NSLog(@"a %% d = %i", a % d);
NSLog(@"a / d * d + a %% d = %i", a / d * d + a % d);
}


@autoreleasepool {
float f1 = 123.125, f2;
int i1, i2 = -150;

i1 = f1;

NSLog(@"%f assigned to an int produces %i", f1, i1);
f1 = i2; //integer to floating conversion
NSLog(@"%i assigned to a float produces %f", i2, f1);
f1 = i2 / 100; //integer divided by integer
NSLog(@"%i divided by 100 produces %f", i2, f1);

f2 = i2 / 100.0; //integer divided by a float
NSLog(@"%i divided by 100.0 produces %f", i2, f2);

f2 = (float) i2 / 100; //type cast operator
NSLog (@"(float) %i divided by 100 produces %f", i2, f2);

}

#include

void congratulateStudent(char *student, char *course, int numDays)
{
printf("%s has done as much %s programming as I could fit into %d days.\n", student, course, numDays);
}

int main (int argc, const char * argv[])
{
congratulateStudent("Mark", "Cocoa", 5);
congratulateStudent("Bo", "Objective-C", 2);
congratulateStudent("Mike", "Python", 5);
congratulateStudent("Ted", "iOS", 5);
}

#include

void singTheSong(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n");
} else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n", oneFewer);
singTheSong(oneFewer); //this function class itself
}
}


int main (int argc, const char * argv[])
{
singTheSong(99);



#include

float fahrenheitFromCelsius(float cel)
{
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit\n", cel, fahr);
return fahr;

}


int main (int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(-freezeInC);
printf("Water freezes at %f degrees Fahrenheit\n", freezeInF);
return 0;
}



#include

float remainingAngle(float angleA, float angleB)
{
return 180 - (angleA + angleB);
}

int main (int argc, const char * argv[])
{

float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB);
printf("The third angle is %.2f\n", angleC);
return 0;
}




#include

int main (int argc, const char * argv[])
{
int x = 255;
printf("x is %d.\n", x);
printf("In octal, x is %o.\n", x);
printf("In hexadecimal, x is %x.\n", x);

long y = 2873;
printf("y is %ld.\n", y);
printf("In octal, y is %lo.\n", y);
printf("In hexadecimal, y is %lx.\n", y);

printf("3 * 3 + 5 * 2 = %d\n", 3 * 3 + 5 * 2);
printf("11 / 3 = %d remainder of %d \n", 11 / 3, 11 % 3);

//use the cast operator
printf("11 / 3.0 = %f\n", 11 / (float)3);


printf("The absolute value of -5 is %d\n", abs(-5));
return 0;

}



#include

int main (int argc, const char * argv[])
{
int i = 0;
while (i < 12) {
printf("%d. Aaron is Cool.\n", i);
i++;
}

for (i = 9; i < 12; i++){
printf("Checking i = %d\n", i);
if (i + 90 == i * i){
break;
}
}
printf("The answer is %d.\n", i);

i = 99;
while (i >= 0){
printf("%d\n", i);
if(i % 5 == 0){
printf("Found one!\n");
}
i -= 3;

}
return 0;
}




int main (int argc, const char * argv[])
{
int i = 17;
float *ptr;
int *addressOfI = &i;
printf("i stores its value at %p\n", addressOfI);

printf("i stores its value at %p\n", &i);
printf("the int stored at addressOfI is %d\n", *addressOfI);
*addressOfI = 87;
printf("Now i is %d\n", i);
printf("this function starts at %p\n", main);

int j = 23;
int *addressOfj = &j;
printf("j stores its value at %p\n", addressOfj);
*addressOfj = 100;
printf("Now j is %d\n", j);
printf("An int is %zu bytes\n", sizeof(int));
printf("A pointer is %zu bytes\n", sizeof(int *));

printf("A float is %zu bytes.\n", sizeof(float));
return 0;
}



#include
#include

typedef struct {
float heightInMeters;
int weightInKilos;
} Person;



int main (int argc, const char * argv[])
{
Person person;
person.weightInKilos = 96;
person.heightInMeters = 1.8;
printf("person weighs %i kilograms\n", person.weightInKilos);
printf("person is %.2f meters tall\n", person.heightInMeters);

long secondsSince1970 = time(NULL);
printf("it has been %ld seconds since 1970\n", secondsSince1970);

struct tm now;
printf("The time is %d:%d:%d\n", now.tm_hour, now.tm_min, now.tm_sec);

return 0;
}

Wednesday, January 4, 2012

Wednesday 1.4.12

Option Strict On
Imports System
Namespace StringSearch
Class Tester
Public Sub Run()
'create some strings to work with
Dim s1 As String = "One Two Three Four"
Dim index As Integer
'get the index of the last space
index = s1.LastIndexOf(" ")
'get the last word
Dim s2 As String = s1.Substring((index + 1))
'set s1 to the substring starting at 0
'and ending at index (the start of the last word)
'thus s1 has One Two Three
s1 = s1.Substring(0, index)

'find the last space in s1 (after "two")
index = s1.LastIndexOf(" ")

'set s3 to the substring starting at index
'the space after "two" plus one more
'thus s3 = "three"
Dim s3 As String = s1.Substring((index + 1))

'reset s1 to the substring starting at 0
'and ending at index, thus the string "one Two"
s1 = s1.Substring(0, index)

'reset index to the space betwee "One" and "Two"
index = s1.LastIndexOf(" ")

'set s4 to the substring starting one space after index, thus the substring "two"
Dim s4 As String = s1.Substring((index + 1))

'reset s1 to teh substring starting at 0
' and ending at index, thus "one"
s1 = s1.Substring(0, index)

'set index to the last space, but there is none so index now = -1
index = s1.LastIndexOf(" ")

'set s4 to the substring at one past the last space, there was no last space
'so this sets s5 to the substring starting at zero
Dim s5 As String = s1.Substring((index + 1))

Console.WriteLine("s1: {0}", s1)
Console.WriteLine("s2: {0}", s2)
Console.WriteLine("s3: {0}", s3)
Console.WriteLine("s4: {0}", s4)
Console.WriteLine("s5: {0}", s5)

End Sub 'run

Public Shared Sub main()
Dim t As New Tester()
t.Run()
End Sub 'main
End Class 'tester
End Namespace 'StringSearch


Public Sub Run()
'create some strings to work with
Dim s1 As String = "One, Two, Three Liberty Associates, Inc."

'constants for the space and comma characters
Const Space As Char = " "c
Const Comma As Char = ","c

'array of delimiters to split the sentence with
Dim delimiters() As Char = {Space, Comma}
Dim output As String = ""
Dim ctr As Integer = 0

'split the string and then iterate over the
'resulting array of string
Dim resultArray As String() = s1.Split(delimiters)

Dim substring As String
For Each substring In resultArray
ctr = ctr + 1
output &= ctr.ToString()
output &= ": "
output &= substring
output &= Environment.NewLine
Next
Console.WriteLine(output)
End Sub 'run


namespace SimpleProject2
{
class Program
{
static int Main(string[] args)
{
ShowEnvironmentDetails();
Console.ReadLine();

return -1;
}

static void ShowEnvironmentDetails()
{
//print out drives no this machine, and other interesting details
foreach (string drive in Environment.GetLogicalDrives())
Console.WriteLine("Drive: {0}", drive);
Console.WriteLine("OS: {0}", Environment.OSVersion);
Console.WriteLine("Number of processors: {0}", Environment.ProcessorCount);
Console.WriteLine(".NET Version: {0}", Environment.Version);
}
}
}


static int Main(string[] args)
{
Console.WriteLine("***** Basic Console I/O *****");
GetUserData();
Console.ReadLine();

return -1;
}

static void GetUserData()
{
//get name and age
Console.Write("Please enter your name: ");
string userName = Console.ReadLine();
Console.Write("Please enter your age: ");
string userAge = Console.ReadLine();

//change echo color, just for fun
ConsoleColor prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;

//echo the console
Console.WriteLine("Hello {0}! You are {1} years old.", userName, userAge);

//restore previous color;
Console.ForegroundColor = prevColor;
}


static int Main(string[] args)
{
Console.WriteLine("***** Format Numerical Data *****");
FormatNumericalData();
Console.ReadLine();

return -1;
}

static void FormatNumericalData()
{
Console.WriteLine("The value 99999 in various formats:");
Console.WriteLine("c format: {0:c}", 99999);
Console.WriteLine("d9 format: {0:d9}", 99999);
Console.WriteLine("f3 format: {0:f3}", 99999);
Console.WriteLine("n format: {0:n}", 99999);

//notice that upper or lowercasing for hex
//determines if letters are upper or lowercase
Console.WriteLine("E format: {0:E}", 99999);
Console.WriteLine("e format: {0:e}", 99999);
Console.WriteLine("X format: {0:X}", 99999);
}


#import

int main (int argc, const char * argv[])
{

@autoreleasepool {

// insert code here...
NSLog(@"Programming is fun!");
NSLog(@"Programming in Objective-C is even more fun!");
NSLog(@"Testing...\n...1\n...2\n...3");
int sum;
sum = 50 + 25;
NSLog(@"The sum of 50 and 25 is %i", sum);
int value1, value2;
value1 = 50;
value2 = 25;
sum = value1 + value2;
NSLog(@"\nThe sum of %i and %i is %i", value1, value2, sum);
}
return 0;
}


int main (int argc, const char * argv[])
{

@autoreleasepool {
NSLog(@"In Objective-C, lowercase letters are significant");
NSLog(@"\n main is where program execution begins.");
NSLog(@"\nOpen and closed braces enclose program statements in a routine.");

int difference;
difference = 87 - 15;
NSLog(@"\n The difference of 87 and 15 is %i", difference);

int answer, result;
answer = 100;
result = answer - 10;
NSLog(@"The result is %i\n", result + 5);
}
return 0;
}



#import

@interface Fraction: NSObject

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
numerator = n;
}

-(void) setDenominator: (int) d
{
denominator = d;
}

@end

// ----- program section -------

int main (int argc, const char * argv[])
{

@autoreleasepool {
Fraction *myFraction;

//create an instance of a fraction
myFraction = [Fraction alloc];
myFraction = [myFraction init];

//set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

//display the fraction using the print method
NSLog(@"The value of myFraction is:");
[myFraction print];
}
return 0;
}

//----- interface section -----
@interface Fraction: NSObject

-(void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;

@end

//---implementation section -----

@implementation Fraction
{
int numerator;
int denominator;
}

-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
numerator = n;
}

-(void) setDenominator: (int) d
{
denominator = d;
}

@end
// ----- program section -------

int main (int argc, const char * argv[])
{


@autoreleasepool {
Fraction *frac1 = [[Fraction alloc] init];
Fraction *frac2 = [[Fraction alloc] init];

//set first fraction to 2/3
[frac1 setNumerator: 2];
[frac1 setDenominator: 3];

//set second fraction to 3/7
[frac2 setNumerator: 3];
[frac2 setDenominator: 7];

//display the fractions
NSLog(@"First fraction is:");
[frac1 print];
NSLog(@"Second fraction is:");
[frac2 print];

}
return 0;
}





@interface Fraction: NSObject

- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;
- (int) numerator;
- (int) denominator;

@end

//--- @implementation section ----

@implementation Fraction
{
int numerator;
int denominator;
}

-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
numerator = n;
}

- (void) setDenominator: (int) d
{
denominator = d;
}

- (int) numerator
{
return numerator;
}

- (int) denominator
{
return denominator;
}
@end



// ----- program section -------

int main (int argc, const char * argv[])
{


@autoreleasepool {
Fraction *myFraction = [[Fraction alloc] init];

//set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

//display the fraction using our two new methods
NSLog(@"The value of myFraction is: %i/%i", [myFraction numerator], [myFraction denominator]);


}
return 0;
}



int main (int argc, const char * argv[])
{
{
@autoreleasepool {
int integerVar = 100;
float floatingVar = 331.79;
double doubleVar = 8.44e+11;
char charVar = 'W';

NSLog(@"integerVar = %i", integerVar);
NSLog(@"floatingVar = %f", floatingVar);
NSLog(@"doubleVar = %g", doubleVar);
NSLog(@"charVar = %c", charVar);
}
}


return 0;
}


int main (int argc, const char * argv[])
{
{
@autoreleasepool {
int a = 100;
int b = 2;
int c = 25;
int d = 4;
int result;

result = a - b;
NSLog(@"a - b = %i", result);

result = b * c;
NSLog(@"b * c = %i", result);

result = a / c;
NSLog(@"a / c = %i", result);

result = a + b * c;
NSLog(@"a + b * c = %i", result);

NSLog(@"a * b + c * d = %i", a * b + c * d);
}
}


return 0;
}



@autoreleasepool {
int a = 25;
int b = 2;
float c = 25.0;
float d = 2.0;

NSLog(@"6 + a / 5 * b = %i", 6 + a / 5 * b);
NSLog(@"a / b * b = %i", a / b * b);
NSLog(@"c / d * d = %f", c / d * d);
NSLog(@"-a = %i", -a);
}