Monday, January 9, 2012

Monday 1-9-12

@interface Rectangle: NSObject
{
int w;
int h;
}

-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
- (int) width;
- (int) height;
- (int) area;
- (int) perimeter;
@end

@implementation Rectangle


-(void) setWidth:(int)width
{
w = width;
}

-(void) setHeight:(int)height
{
h = height;
}

-(int) width
{
return w;
}

-(int) height
{
return h;
}

-(int) area
{
return w * h;
}

-(int) perimeter
{
return (2 * w) + (2 * h);
}

@end


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

@autoreleasepool {
Rectangle *rec = [[Rectangle alloc] init];
[rec setWidth:11];
[rec setHeight: 12];
NSLog(@"The height is %i", [rec height]);
NSLog(@"The width is %i", [rec width]);
NSLog(@"The perimeter is %i", [rec perimeter]);
NSLog(@"THe area is %i", [rec area]);

}
return 0;
}

@autoreleasepool {

int n, triangularNumber;
triangularNumber = 0;

for (n = 1; n <= 200; n = n + 1)
triangularNumber += n;
NSLog(@"The 200th triangular number is %i", triangularNumber);

}


@autoreleasepool {
int n, triangularNumber;
NSLog(@"Table of triangular numbers");
NSLog(@" n Sum from 1 to n");
NSLog(@"-- ------");

triangularNumber = 0;

for(n = 1; n <=10; ++n)
{
triangularNumber += n;
NSLog(@" %i %i", n, triangularNumber);
}


@autoreleasepool {
int n, number, triangularNumber;

NSLog(@"What traingular number do you want?");
scanf("%i", &number);
triangularNumber = 0;
for(n=1;n<=number;++n)
triangularNumber += n;

NSLog(@"Triangular number %i is %i\n", number, triangularNumber);
}


@autoreleasepool {
int n, number, triangularNumber, counter;
for (counter = 1; counter <= 5; ++counter)
{
NSLOg(@"What triangular number do you want?");
scanf("%i", &number);
triangularNumber = 0;

for(n=1; n<=number; ++n)
triangularNumber += n;

NSLog(@"Triangular number %i is %i", number, triangularNumber);
}
}

@autoreleasepool {
int n, number, triangularNumber, counter;
for (counter = 1; counter <= 5; ++counter)
{
NSLog(@"What triangular number do you want?");
scanf("%i", &number);
triangularNumber = 0;

for(n=1; n<=number; ++n)
triangularNumber += n;

NSLog(@"Triangular number %i is %i", number, triangularNumber);
}
}




#include

//global variables. these are visible fromo any function.

int totalItems = 0;
float totalCost = 0.0;
float salesTax = 0.0925;

//declare the functions we're going to use
//we don't need to declare main() because it's built in.

void addToTotal (float cost, int quantity);
float costWithSalesTax(float price);

//this is where the program starts when it runs.

main(){
float budget = 10000.00;
//make a new line
printf("\n");

//set the prices of each item
float laptopPrice = 1799.00;
float monitorPrice = 499.80;
float phonePrice = 199.00;

//for each line item, call the addToTotal() function
//specifying the item and quantity

addToTotal(laptopPrice, 2);
addToTotal(monitorPrice, 1);
addToTotal(phonePrice, 4);

//display a line and then the final total
printf("----------------------\n");
printf("TOTAL for %i items: $%5.2f\n\n", totalItems, totalCost);

if(totalCost < budget)
{
printf("You came in under budget!\n\n");
} else
{
printf("You're over budget. Time to talk to finance.\n\n");
}
}

void addToTotal(float cost, int quantity){
printf("Adding %i items of cost $%4.2f\n", quantity, cost);
//find the cost for this item by multiple cost by quantity
//then get the real cost by applying sales tax
float calculatedCost = cost * quantity;
float realCost = costWithSalesTax(calculatedCost);

//add this amount to the total and increase the total number
//of items purchased
totalCost = totalCost + realCost;
totalItems = totalItems + quantity;

}


float costWithSalesTax(float price)
{
//remember 'salesTax' is a global variable
float taxAmount = price * salesTax;
float subtotal = price + taxAmount;

return subtotal;
}


No comments:

Post a Comment