introduction to computers and programming class 12 introduction to c professor avi rosenfeld

11
Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Upload: daniella-harrington

Post on 14-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Introduction to Computers and Programming

Class 12

Introduction to C

Professor Avi Rosenfeld

Page 2: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Math Functions

• Math.h Library– Sqrt– Pow– Fabs (absolute value)– Sin, Cos, Tan, etc.

Page 3: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Square Root Example

#include <stdio.h>#include <math.h>#define number 2.0

void main(){

printf("The square root of %.2f is %.2f\n", number, sqrt(number));

}

Page 4: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Power Example

#include <stdio.h>#include <math.h>void main(){

float base, exponent;printf("Please type a base followed by its exponent\n");scanf("%f%f",&base, &exponent);printf("%.2f to the power of %.2f is %.2f\n", base, exponent, pow(base, exponent));

}

Page 5: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Loops and Sqrt

#include <stdio.h>#include <math.h>

void main(){

for (int i = 100; i > 0; i--)printf("The square root of %d is %.2f\n", i, sqrt(i));

}

Page 6: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Nested Loops

• Logic within Logic– If / Loops (while, do while, for) inside of others

• Power Programming Technique

Page 7: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Nested While Loops

#include <stdio.h>

void main(){

int count1 = 0;while (count1 < 5){

int count2 = 0;while(count2 < 5){

printf("This is iteration %d within the bigger iteration %d\n",count2, count1);

count2++;}count1++;

}}

Page 8: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Nested For Loops

#include <stdio.h>

void main(){

for (int i = 0; i < 10; i++){

for (int j = 0; j < 10; j++)printf("*");

printf("\n");}

}

Page 9: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Nested For Loops #2

#include <stdio.h>void main(){

for (int i = 0; i < 10; i++){

for (int j = 0; j <= i; j++)printf("%c", 'A'+j);

printf("\n");}

}

Page 10: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Deitel’s Example/* Fig. 4.6: fig04_06.c Calculating compound interest */#include <stdio.h>#include <math.h>int main(){ int year; double amount, principal = 1000.0, rate = .05; printf( "%4s%21s\n", "Year", "Amount on deposit" ); for ( year = 1; year <= 10; year++ ) { amount = principal * pow( 1.0 + rate, year ); printf( "%4d%21.2f\n", year, amount ); } return 0;}

/************************************************************************** * (C) Copyright 2000 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/

Page 11: Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld

Program Ideas

• GPA calculator

• 1-800-iloveny

• Diamond problems (see 4.16 in Deitel)

• Histograms (see 4.18)

• Drawing large letters (S, H, A)

• 4.11, 4.12, 4.13 in Deitel