typedef struct pet Pet; struct pet { char name[MAX_LEN]; char breed[MAX_LEN]; int age; }; int main(void){ Pet myPet; strcpy(myPet.name,"Boris"); strcpy(myPet.breed,"dog"); myPet.age = 7; etc...
typedef struct date Date; struct date { int day; int month; int year; }; typedef struct pet Pet; struct pet { char name[MAX_LEN]; char breed[MAX_LEN]; Date dateOfBirth; }; int main(void){ Pet myPet; strcpy(myPet.name,"Boris"); strcpy(myPet.breed,"dog"); myPet.dateOfBirth.day = 1; myPet.dateOfBirth.month = 4; myPet.dateOfBirth.year = 1988; etc...
typedef struct point Point; struct point { double x; double y; };
double distA( double p[2], double q[2] ); double distS( Point p, Point q );Recall that the formula for the distance between 2 points is
distance = sqrt ((x2-x1)^2 + (y2-y1)^2)
#include <math.h> double distA( double p[2], double q[2] ) { return( sqrt( ( p[0] - q[0] )*( p[0] - q[0] ) +( p[1] - q[1] )*( p[1] - q[1] ))); } double distS( Point p, Point q ) { return( sqrt( ( p.x - q.x )*( p.x - q.x ) + ( p.y - q.y )*( p.y - q.y ) )); }
Do you find one version of the function "easier to read" than the other?
It should have the following prototype
int readPoints(Point points[], int maxPoints);
int readPoints(Point points[],int maxPoints){ int i = 0; while ( i < maxPoints && scanf("%lf %lf",&(points[i].x), &(points[i].y)) == 2){ i = i + 1; } return i; }
(4.5,9.0)
void printPoints(Point points[], int numPoints){ int i = 0; while ( i < numPoints ){ printf("(%.1lf,%.1lf)",points[i].x,points[i].y); i = i + 1; } printf("\n"); }
#define MAX_POINTS 100 int main(void){ Point points[MAX_POINTS]; int numPoints = readPoints(points, MAX_POINTS); printPoints(points,numPoints); return EXIT_SUCCESS; }
Point furthestFromOrigin(Point points[], int numPoints){ Point origin; origin.x = 0; origin.y = 0; int maxIndex = -1; double maxDist; double dist; int i = 0; while ( i < numPoints ) { dist = distS(origin, points[i]); if( maxIndex == -1 || dist > maxDist ){ maxDist = dist; maxIndex = i; } i = i + 1; } if(maxIndex != -1 ){ return points[maxIndex]; } else { //will only happen if numPoints <= 0 return origin; } }
#define MAX_PLATE typedef struct parkingFine ParkingFine; struct parkingFine{ double amount; char numberPlate[MAX_PLATE]; }; int readParkingFine ( ParkingFine fine ); int main(void){ ParkingFine f; if(readParkingFine(f)){ printf("%lf %s\n",f.amount, f.numberPlate); } else { fprintf(stderr,"Invalid Input\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } // Returns 1 if valid input was read in // Returns 0 otherwise int readParkingFine ( ParkingFine fine ){ if ( scanf("%lf",&fine.amount) != 1 || fgets(fine.numberPlate,MAX_PLATE,stdin) == NULL){ return 0; } else { return 1; } }
structs are passed by value, so a copy of the struct has been passed into the function, so any changes made to it do not affect the variable in the main function. To change this we could do the following and pass it in by reference by passing in the address of the struct. int readParkingFine ( ParkingFine * fine ); int main(void){ ParkingFine f; if(readParkingFine(f)){ printf("%lf %s\n",f.amount, f.numberPlate); } else { fprintf(stderr,"Invalid Input\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } // Returns 1 if valid input was read in // Returns 0 otherwise // Note: fine->numberPlate is shorthand for (*fine).numberPlate int readParkingFine ( ParkingFine * fine ){ if ( scanf("%lf",&fine->amount) != 1 || fgets(fine->numberPlate,MAX_PLATE,stdin) == NULL){ return 0; } else { return 1; } }
#define MAX_LEN 100 typedef struct student Student; struct student{ int zid; char name[MAX_LEN]; }Write a function to return a pointer to a Student with the given zid from an array of Student. It should return NULL otherwise. Your function should have the following prototype
Student * findStudent(Student class[], int size, int zid);
Student * findStudent(Student class[], int size, int zid) { int i; for ( i = 0 ; i < size; i++){ if( class[i].zid == zid){ return &(class[i]); } } return NULL; }