# Array examples

Comments and questions to John Rowe.

Here we use `scanf()` to read in `sides[0]`
and `sides[1]` and use them in arithmetic expression,
again just like a variable.

#include <stdio.h>
#include <math.h>
int main() {
double sides[3];
printf("Please enter the two sides of a right-angled triangle\n");
scanf("%lf %lf", &sides[0], &sides[1]);
sides[2] = sqrt(sides[0] * sides[0] + sides[1] * sides[1]);
return 0;
}

**Step through this code
**

Having said it would be useful to have a function called
`readarray()`, here it is. To demonstrate that it is the
address of the array that is passed to a function we print out the
value of "`numbers`" and "`input`" to demonstrate they
are the same.

#include <stdio.h>
#define VALUES 1000000
void readarray(int input[], int n) {
int i;
printf("In readarray() the array is stored at address %p\n", input);
for (i = 0; i < n; ++i ) {
scanf("%d\n", &input[i]);
}
}
void printarray(int output[], int n) {
int i;
for (i = 0; i < n; ++i ) {
printf("%d\n", output[i]);
}
}
int main() {
int numbers[VALUES];
printf("In main() the array is stored at address %p\n", numbers);
readarray(numbers, VALUES);
printf("The value of numbers[2] is: %d\n", numbers[2]);
printarray(numbers, VALUES);
return 0;
}

**Step through this code
**

The output is:

In main() the array is stored at address 0x7fff0f495e40
In readarray() the array is stored at address 0x7fff0f495e40
The value of numbers[7] is: 7

(Note that addresses are printed in hexadecimal.)
Of course, the exact address printed out may be different each time.
When I run it again a few seconds later I get:

In main() the array is stored at address 0x7fffe9c3d9f0
In readarray() the array is stored at address 0x7fffe9c3d9f0
The value of numbers[7] is: 7

### Aside: a better version

The above version of `readarray()` doesn't check to see if
it was actually able to read in the required number of integers.
We can use what we learnt in the last lecture
about the return value of `fscanf()` to
improve it a little as follows:

int better_readarray(int input[], int maxv) {
int i;
FILE *infile;
if ((infile = fopen("mydata.dat", "r")) == NULL ) {
fprintf(stderr, "Failed to open mydata.dat for input\n");
return -1;
}
printf("In readarray() the array is stored at address %p\n", input);
for (i = 0; i < maxv; ++i ) {
if (fscanf(infile, "%d\n", &input[i]) != 1)
break;
}
return i;
}

**Step through this code
**

### Dot product of vectors

As stressed in the lecture,
in C a two-dimensional array is just an array of
one-dimensional arrays. Consider
the following dot-product function:

float dotprod(float v1[], float v2[], int n) {
float res = 0;
for (int i = 0; i < n; ++i)
res += v1[i]*v2[i];
return res;
}
#define VECLEN 2
#define NVECS 3
int main() {
float dot[NVECS][NVECS], vector[NVECS][VECLEN] =
{ 1.5, 3.9, 4.7, 7.2, 2.1, 5.13, };
int j, k;
for (j = 0; j < NVECS; ++j) {
for (k = j; k < NVECS; ++k)
dot[j][k] = dotprod(vector[j], vector[k], VECLEN);
}
return 1;
}

**Step through this code
**

Here we are using symmetry to
save time and using the fact that we are allowed to
write `a = b = c` which can occasionally
be useful but should be used with care. For example
`y=2*(z=a+b)` is also legal but is certainly not wise!

Notice also that the `dotprod()` function neither knows or
cares if it has been passed two "genuine" one-dimensional arrays or
two different parts of a two dimensional aray (or even if it has been
passed the same array twice, which isn't a problem in this case
but might be in other circumstances.)