CS3241 Tutorial - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS3241 Tutorial

Description:

student pasha; //we create pasha of type student. pasha.name='Pasha'; //assign him a name ... double fn =((double)rand()/RAND_MAX)*(b-a) a; Math functions ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 17
Provided by: pav
Category:

less

Transcript and Presenter's Notes

Title: CS3241 Tutorial


1
CS3241 Tutorial
  • Michael S. Brown
  • Pavel Korshunov
  • Tutorial 1
  • 21 Aug 2009

Tutorials Page http//www.comp.nus.edu.sg/pavelk
or/cs3241/
2
About Me
  • Full Name
  • Pavel Valerievich Korshunov ?
  • Call me PASHA (from Russia)
  • Chinese name ??
  • Email pavelkor_at_gmail.com
  • Website www.comp.nus.edu.sg/pavelkor
  • Tutorial website www.comp.nus.edu.sg/pavelkor/cs
    3241
  • Office COM1, 02-46, (TA Office)

3
Typical C-code
  • include ltstdio.hgt //include standard libraries,
    stdio has printf inside
  • include ltmath.hgt //math has sin(), cos(), abs()
    and other math functions
  • double useless_global_variable34.5 //global
    variables
  • void whatsup (int problem) //prototype of the
    function that is defined below
  • int main () //this is the standard main
    function, where program starts
  • int p1 //variables always defined first
  • whatsup(p) //then, do other stuff logic of
    your program
  • return 0
  • void whatsup (int problem) //define here the
    function that we declared above
  • if (problem)
  • printf (Whats up? You got a problem
    dude?) //print stuff

4
Have to learn C No Choice
  • Main data types you can use
  • int integers
  • double float numbers
  • char characters
  • int iam1 double iamfloating4.98 char cc1
  • Arrays are like everywhere else
  • int arr45, 6, 7, 2 //indexes start from 0

5
Input and output
  • Read from input with scanf() or getchar()
  • Print with printf()
  • int age100
  • printf(People of Earth! I am d years old\n,
    age)
  • d says where the variable age will be
    printed. d means it will be printed as integer.
    \n prints new line.
  • Different ways to format printing string (google
    it)
  • Printing to files is fprintf(), open files with
    fopen()

6
typedef
  • Use typedef to create a new data type
  • typedef int3 apples
  • apples apple1 //now apple1 is array int3
  • typedef is commonly used together with structures

7
Structures
  • typedef struct student
  • char name100
  • char gender
  • int age
  • student
  • //first student is the name of structure,
    second student is the new type
  • student pasha //we create pasha of type
    student
  • pasha.namePasha //assign him a name
  • pasha.genderM //gender, and so on
  • pasha.age29

8
Type casting
  • Unlike Java, not all data types are automatically
    converted to each other, hence use explicit
    typecasting
  • Common examples
  • int inum (int) 4.5 //after type casting,
    4.5 becomes 4 (all decimal points are dropped).
  • double dd (double) 1/5 //if you do not
    typecast, 1/5 will be computed as integer
    operation with result 0. Writing (double) makes
    division a float operation with result 0.2.

9
Pointers
  • Analogy
  • The variable is like a person
  • Pointer is like a GPS location of the person

int a3int ppa
Variable a in memory
value
3
p is pointer to a
!
0010
Address of the variable in memory
0010
value
0300
Address of the pointer in memory
10
Arrays and memory
memory
4 bytes each value
int arr10 arr03 arr12 arr232
Values in array
3
2
32

4
0010
0014
0018
0022
arr
0010
Addresses
0458
arr24 means 0010240018 gt at address 0018
write 4
11
Arrays and memory continued
memory
4 bytes each value
int arr10 int pa paarr0
Values in array
3
2
4

7
arr
0010
0014
0018
0022
0010
pa
0458
0010
Addresses
0670
(pa2)7 means (001024)0018 gt at address
0018 write 7
12
Pointers and new memory
  • int parr //nothing is created in memory
  • parr (int)malloc(sizeof(int)10)
  • malloc allocates block of bytes in memory
  • sizeof(int) number of bytes in int (its 4)
  • sizeof(int)10 410 50 bytes
  • malloc returns (void), which is type-casted to
    (int)
  • (parr3)5 //writes 5 into 4th element of array
  • free(parr) //to free the memory after use

13
2D and 3D arrays
  • Any array is essentially a pointer to a place in
    memory. The following define 2D arrays
  • int arr33 //3x3 matrix is created in memory
  • int pparr //this does not specify the size of
    the array and no actual array is created in
    memory
  • pparrarr00 //pparr now points to 2D array

14
2D and 3D arrays
int arr33 3,2,1, 2,6,9, 56,3,4
To make all elements 0, define arrayint
arr460
3
2
1
In memory
3
2
1
2
6
9
56

4
2
6
9
0026
0030
0034
56
3
4
0010
0014
0018
0022
arr
0010
0458
Row
Size of int
arr204 means 0010234040034 to this
address 4 is written
Total number in a row
15
Random function
  • rand() - integer from 0 to RAND_MAX
  • But first initiate with srand(). It seeds
    random generator.
  • srand( time(NULL)) //just use it this way
  • Random integer between a and b
  • int i rand() (b-a1) a
  • Random float/double
  • double fn ((double)rand()/RAND_MAX)(b-a) a

16
Math functions
  • You would need trigonometry functions
  • include ltmath.hgt
  • define PI 3.1415 //this is how you define a
    constant in C
  • double half_pie sin(PI/2)
  • double qt_pie cos(PI/4)
  • printf(We got sinf and cosf \n, half_pie,
    qt_pie )
Write a Comment
User Comments (0)
About PowerShow.com