21 2 Structs 21 3 In Java you create new data types by creating classes Classes have member variables and associated methods Intro to Programming II You can control access and inherit C has structs Member variables only no methods No means of hiding information public private Structs Chris Brooks Department of Computer Science Notice typedef this declares a new type which is a struct The name of the new type is after the defnition Ends with a semicolon University of San Francisco Department of Computer Science University of San Francisco p 1 21 4 Using a struct Department of Computer Science University of San Francisco p 2 21 5 Modifying a struct looks much like working with public member variables in Java int main void Person pers strcpy pers name bob pers id 12345 printf s d pers name pers id Structs example typedef struct char name 80 int id char DOB 80 Person Exercise 1 Department of Computer Science University of San Fra 21 6 Write a Point struct It should have x and y variables Write a main method that prompts the user for two points then calculates the distance between them p x2 x1 2 y2 y1 2 d Structs as parameters Like everything else in C structs are passed by value void setName Person p char n strcpy p name n int main void Person pers strcpy pers name bob printf s pers name setName pers richard printf s pers name Department of Computer Science University of San Francisco p 4 Department of Computer Science University of San Francisco p 5 Department of Computer Science University of San Fra 21 7 Passing structs by reference 21 8 Like other data types we can pass a pointer to a struct void setName Person p char n Passing structs by reference 21 9 How do we refer to the fields of a pointer to a struct p name won t work has higher precedence than we could do p name but that s awkward strcpy p name bob int main void Passing structs by reference C has a special operator to deal with this problem denotes fields of pointers to structs void setName Person p char n strcpy p name n Person pers strcpy pers name bob printf s pers name setName pers richard printf s pers name Department of Computer Science University of San Francisco p 7 21 10 Exercise 2 Department of Computer Science University of San Francisco p 8 21 11 Make a file called Point h that contains the definition of your Point struct Inside Point c define setters and getters for the Point s x and y variables They ll need to take a pointer to a Point as an argument Arrays of Structs 21 12 We can store structs in arrays just like any other data type We need to tell malloc to allocate enough memory for the appropriate number of structs int nelements 10 Person parray Person malloc nelements sizeof Person Random numbers a digression How can we create random integers in C The function rand returns numbers between 0 and MAXINT We can use modular arithmetic to reduce this range rand 10 returns numbers between 0 and 9 Addition can be used to shift the endpoints rand 10 5 returns numbers between 5 and 14 Place your main method from exercise 1 in a file called distance c and compile all of them together like so gcc o distance distance c Point c lm Department of Computer Science University of San Francisco p 10 Department of Computer Science University of San Fra Department of Computer Science University of San Francisco p 11 Department of Computer Science University of San Fran 21 13 Random numbers a digression 21 14 Random numbers a digression 21 15 Random numbers a digression What if we want to generate random floating point numbers What if we want to generate random floating point numbers We can generalize this by using the pow function Say we want random floats between 0 and 10 with two decimal Say we want random floats between 0 and 10 with two decimal pow number exponent places places We can generate random integers from a larger range and then divide by the appropriate power of 10 double randomDouble rand 1000 100 0 this will give a decimal with two places of precision Department of Computer Science University of San Francisco p 13 21 16 Exercise 3 Department of Computer Science University of San Francisco p 14 21 17 Write a program that prompts the user for A min a max a number of random numbers to generate and then generates that many floating point numbers between Seeds Department of Computer Science University of San Fran 21 18 Your program should ve generated the same numbers each time you provided the same parameters That s not very random Computers are actually quite bad at doing truly random things Usually we want them to produce predictable results min and max Run your program multiple times What do you notice Seeds Random number generators actually generate a deterministic sequence of numbers If the sequence is long enough and well distributed it looks random from our point of view We call this a pseudorandom sequence Given a particular starting number the generator produces a long string of numbers In the last program we were always starting from the same point 0 Problem How do we specify a starting point Department of Computer Science University of San Francisco p 16 Department of Computer Science University of San Francisco p 17 Department of Computer Science University of San Fran 21 19 Seeds 21 20 Seeds 21 21 The starting point for a RNG is called a seed The starting point for a RNG is called a seed We can set the seed with the function srand int s Where can we get a seed that will be different every time Where can we get a seed that will be different every time We can use the system clock C provides us access to this through a function called time srand time NULL Exercise 4 Write a program that Prompts the user for a number of points Allocates an array with that many points in it Sets random values for the x and y values for each point using your getter and setter methods For each point in the array find the point in the array that is closest to it and print out both points x and y values Department of Computer Science University of San Francisco p 19 Department of Computer Science University of San Francisco p 20 Department of Computer Science University of San Fran
View Full Document
Unlocking...