{small lecturenumber - hepage :} Structs{small lecturenumber - hepage :} Structs example{small lecturenumber - hepage :} Using a struct{small lecturenumber - hepage :} Exercise 1{small lecturenumber - hepage :} Structs as parameters{small lecturenumber - hepage :} Passing structs by reference{small lecturenumber - hepage :} Passing structs by reference{small lecturenumber - hepage :} Passing structs by reference{small lecturenumber - hepage :} Exercise 2{small lecturenumber - hepage :} Arrays of Structs{small lecturenumber - hepage :} Random numbers: a digression{small lecturenumber - hepage :} Random numbers: a digression{small lecturenumber - hepage :} Random numbers: a digression{small lecturenumber - hepage :} Random numbers: a digression{small lecturenumber - hepage :} Exercise 3{small lecturenumber - hepage :} Seeds{small lecturenumber - hepage :} Seeds{small lecturenumber - hepage :} Seeds{small lecturenumber - hepage :} Seeds{small lecturenumber - hepage :} Exercise 4Intro to Programming IIStructsChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p. 1/??21-2: Structs•In Java, you create new data types by creating classes.◦Classes have member variables and associated methods.◦You can control access, and inherit.•C has structs.◦Member variables only (no methods)◦No means of hiding information (public/private)Department of Computer Science — University of San Francisco – p. 2/??21-3: Structs exampletypedef struct {char name[80];int id;char DOB[80];} Person;•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.Department of Computer Science — University of San Francisco – p. 3/??21-4: Using a struct•Modifying a struct looks much like working with public membervariables in Java.intmain(void) {Person pers;strcpy(pers.name, "bob");pers.id = 12345;printf("%s %d",pers.name, pers.id);}Department of Computer Science — University of San Francisco – p. 4/??21-5: Exercise 1•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.◦(d =p(x2− x1)2+ (y2− y1)2)Department of Computer Science — University of San Francisco – p. 5/??21-6: Structs as parameters•Like everything else in C, structs are passed by value.void setName(Person p, char*n) {strcpy(p.name, n);}intmain(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. 6/??21-7: Passing structs by reference•Like other data types, we can pass a pointer to a struct:void setName(Person*p, char*n) {...}intmain(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. 7/??21-8: Passing structs by reference•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”);Department of Computer Science — University of San Francisco – p. 8/??21-9: 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);}Department of Computer Science — University of San Francisco – p. 9/??21-10: Exercise 2•Make a file called Point.h that contains the definition of yourPoint struct.•Inside Point.c, define setters and getters for the Point’s x and yvariables.◦They’ll need to take a pointer to a Point as an argument•Place your main method from exercise 1 in a file calleddistance.c and compile all of them together like so:◦gcc -o distance distance.c Point.c -lmDepartment of Computer Science — University of San Francisco – p. 10/??21-11: Arrays of Structs•We can store structs in arrays, just like any other data type.•We need to tell malloc to allocate enough memory for theappropriate number of structs.int nelements = 10;Person*parray = (Person*)malloc(nelements*sizeof(Person));Department of Computer Science — University of San Francisco – p. 11/??21-12: 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.Department of Computer Science — University of San Francisco – p. 12/??21-13: Random numbers: a digression•What if we want to generate random floating point numbers?•Say we want random floats between 0 and 10, with two decimalplaces?Department of Computer Science — University of San Francisco – p. 13/??21-14: Random numbers: a digression•What if we want to generate random floating point numbers?•Say we want random floats between 0 and 10, with two decimalplaces?•We can generate random integers from a larger range, andthen 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. 14/??21-15: Random numbers: a digression•We can generalize this by using the pow() function.•pow(number, exponent)Department of Computer Science — University of San Francisco – p. 15/??21-16: Exercise 3•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 betweenmin and max.•Run your program multiple times. What do you notice?Department of Computer Science — University of San Francisco – p. 16/??21-17: Seeds•Your program should’ve generated the same numbers eachtime 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.Department of Computer Science — University of San Francisco – p. 17/??21-18: Seeds•Random number generators actually generate a deterministicsequence of numbers.◦If the sequence is long enough and well-distributed, it looksrandom from
View Full Document