# Quick overview of C

## Making a point

### Using two names

```double x, y ;
x = 13.5 ;
y = 2*x ;
```

### As a tuple

```double p[2] ;
p[0] = 13.5 ;
p[1] = 2*p[0] ;
```

### Using named components

```struct point {
double x ;
double y ;
} ;
struct point P ;
P.x = 13.5 ;
P.y = 2*P.x ;
```

### With C++ abstraction

```class Point {
double x ;
double y ;
public:
Point(double, double) ;
void setX (double) ;
void setY (double) ;
double getX (void) ;
double getY (void) ;
}

Point(0.0, 0.0) P ;
P.setX(13.5) ;
P.setY(2*P.getY()) ;
```

## Using points

### Computing distance

```double dist = sqrt(px*px + py*py) ;
```

### Computing dot product

```double dotp = p.x*q.x + p.y*q.y ;
```

### Computing convolution

```double dotp = p[0]*q[1] + p[1]*q[0] ;
```

```double x, y ;
scanf("%lf %lf", &x, &y) ;
printf("The sum of your numbers is %6.2f\n", x+y) ;
```

## Testing numbers

```if (x > y) {
puts("The first was bigger!") ;
} else {
puts("The second was bigger!") ;
}
```

## Iteration

```for (int i=100 ; i>0; i = i-1) {
printf("%3d\n", i) ;
}
puts("Blastoff!") ;

count = 0 ;
while (x ~= 1) {
if (x%2 == 0) {
x = x/2 ;
} else {
x = 3*x+1 ;
}
++count ;
}
```

## Functions

```double midi2freq(int midiNum) {
/* MIDI number 69 is A440 */
int halfStepsFromA440 = midiNum - 69 ;
double relFreq = pow(2, halfStepsFromA440/12.0) ;
return relFreq * 440 ;
}

double freq midC = midi2freq(60) ;
```