Instructor: Professor Neena Kaushik Office hours (in AT 203): Mondays: 3:15 to 4:15 p.m. Wednesdays: 10 to 11 a.m. and by appointment Email : [email protected] Course website: http://www.deanza.edu/faculty/kaushikneena Lecture: 12:30 - 2:10 p.m. in AT 312 (1:30 – 1:35 p.m. break) (Mondays & Wednesdays) Lab: 2:15 - 2:40 p.m. in AT 312 (Mondays & Wednesdays) Lectures: 9 and 10 Dates: October 20 and October 22 Textbook Chapter: 4 Topics Covered: Standard functions and programming examples Sentences in quotes have been taken as is from the textbook. The material in these notes has been prepared by referring the textbook and the slides for the instructor. “Picture taken from slides for instructor” 1 The table below gives the declarations of some standard functions and the header files they are present in. Function Header file 1 int abs(int number); stdlib.h 2 long labs(long number); stdlib.h 3 long long llabs(long long number); stdlib.h 4 double fabs(double number); math.h 5 float fabsf(float number); math.h 6 long double fabsl(long double math.h number); 7 double ceil(double number); math.h 8 float ceilf(float number); math.h 9 long double ceill(long double math.h number); 10 double floor(double number); math.h 11 float floorf(float number); math.h 12 long double floorl(long double math.h number); 13 double trunc(double number); math.h 14 float truncf(float number); math.h 15 long double truncl(long double math.h number); 16 double round(double number); math.h 17 float roundf(float number); math.h 18 long double roundl(long double math.h number); 19 long int lround(double number); math.h 20 long int lroundf(float number); math.h 2 21 long int lroundl(long double math.h number); 22 long long int llround(double math.h number); 23 long long int llroundf(float math.h number); 24 long long int llroundl(long double math.h number); 25 double pow(double n1, double n2); math.h 26 float powf(float n1, float n2); math.h 27 long double powlf(long double n1, math.h long double n2); 28 double sqrt(double n1); math.h 29 float sqrtf(float n1); math.h 30 long double sqrtlf(long double n1); math.h 31 void srand(unsigned int seed); stdlib.h 32 int rand(void); stdlib.h 33 time_t time(time_t*num) time.h The table below lists some standard functions and what it returns Function What it returns 1 abs Absolute value of an integer. 2 labs Absolute value of a number of the type long. 3 llabs Absolute value of a number of the type long long. 4 fabs Absolute value of a number of the type double. 5 fabsf Absolute value of a number of the type float. 3 6 fabsl Absolute value of a number of the type long double. 7 ceil Smallest integral value (as a type double) greater than or equal to a number. 8 ceilf Smallest integral value (as a type float) greater than or equal to a number. 9 ceill Smallest integral value (as a type long double) greater than or equal to a number. 10 floor Largest integral value (as a type double), that is equal to or less than a number. 11 floorf Largest integral value (as a type float), that is equal to or less than a number. 12 floorl Largest integral value (as a type long double), that is equal to or less than a number. 13 trunc Integral (as a double) in the direction of 0. 14 truncf Integral (as a float) in the direction of 0. 15 truncl Integral (as a long double) in the direction of 0. 16 round Nearest integral value (as type double). 17 roundf Nearest integral value (as type float). 18 roundl Nearest integral value (as type long double). 19 lround Nearest integral value (as type long 4 int). 20 lroundf Nearest integral value (as type long int). 21 lroundl Nearest integral value (as type long int). 22 llround Nearest integral value (as type long long int). 23 llroundf Nearest integral value (as type long long int). 24 llroundl Nearest integral value (as type long long int). 25 pow Value of x raised to power y as type double. 26 powf Value of x raised to power y as type float. 27 powlf Value of x raised to power y as type long double 28 sqrt Non-negative square root of a number as type double. 29 sqrtf Non-negative square root of a number as type float. 30 sqrtlf Non-negative square root of a number as type long double. 31 srand Void (Creates starting side for a random number series). 32 rand A pseudorandom integer between 0 and RAND_MAX. 33 time The current calendar time as time_t object Here is the function usage and the return values. 5 Function usage Return value 1 abs(3); 3 2 labs(3); 3 3 llabs(3); 3 4 fabs(-3.4); 3.4 5 fabsf(-3.4); 3.4 6 fabsl(-3.4); 3.4 7 ceil(-1.9); -1.0 ceil(1.1); 2.0 8 ceilf(-3.2); -3.0 9 ceill(4.3); 5.0 10 floor(-1.1); -2.0 floor(1.9); 1.0 11 floorf(-1.1); -2.0 12 floorl(-1.2); -2.0 13 trunc(-1.1); -1.0 trunc(1.9); 1.0 14 truncf(1.3); 1.0 15 truncl(2.1); 2.0 16 round(-1.1); 1.0 17 roundf(1.9); 2.0 18 roundl(2.4); 2.0 19 lround(1.0); 1.0 20 lroundf(1.3); 1.0 21 lroundl(1.4); 1.0 22 llround(1.5); 2.0 23 llroundf(1.6); 2.0 24 llroundl(-1.6); -2.0 25 pow(3.0,4.0); 81.0 26 powf(3.4,2.3); 16.687893 6 27 powlf(2.0,2.0); 4.0 28 sqrt(25); 5.0 29 sqrtf(81); 9.0 30 sqrtlf(196); 14.0 31 srand(time(NULL)); The seed is set 32 rand(); A pseudo random number 33 time(NULL); Number of seconds since January 1, 1970 Program 1 Creating random numbers /****************************************************** ** Program written by: Taken from textbook ** Inputs: None ** Returns: An Integer ** What the program does: It prints random numbers ******************************************************/ #include “stdafx.h” #include <stdio.h> #include <time.h> #include <conio.h> int main(void) { srand(time(NULL)); printf(“\n %d”, rand()); printf(“\n %d”, rand()); printf(“\n %d”, rand()); getch(); 7 return 0; } Program 2 Creating pseudo-random numbers /****************************************************** ** Program written by: Taken from textbook ** Inputs: None ** Returns: An Integer ** What the program does: It prints pseudorandom numbers ******************************************************/ #include “stdafx.h” #include <stdio.h> #include <conio.h> int main(void) { srand(997); printf(“\n %d”, rand()); printf(“\n %d”, rand()); printf(“\n %d”, rand()); getch(); return 0; } Program 3 Generate random numbers in the range 10 to 20 /****************************************************** ** Program written by: Taken from textbook ** Inputs: None ** Returns: An Integer 8 ** What the program does: It prints random numbers in range of 10 to 20 ******************************************************/ #include “stdafx.h” #include <stdio.h> #include <time.h> #include <conio.h> int main(void) { int range; srand(time(NULL)); range = (20-10)+1; printf(“\n %d”, rand() % range + 10); printf(“\n %d”, rand() % range + 10); printf(“\n %d”, rand() % range + 10); getch(); return 0; } Program 4 Generate random numbers in the range 0.0 to 1.0 /****************************************************** ** Program written by: Taken from textbook ** Inputs: None ** Returns: An Integer ** What the program does: It prints random numbers in range of 0.0 and 1.0 ******************************************************/ #include “stdafx.h” #include <stdio.h> 9 #include <time.h> #include <conio.h> #include<time.h> int main(void) { float x; srand(time(NULL)); x=(float)rand()/RAND_MAX; print(“\n%f”, x); getch(); return 0; } 10
© Copyright 2024 Paperzz