int pop0, pop1, pop2, pop3, pop4;
Manipulating these variables become cumbersome.
int pop[5];
The integer (5) inside of [ ] indicate that the array pop has 5 array elements in this case.
pop[0] means the first compartment of an array pop.
You can use it as if it is a simple variable.
#include <stdio.h>
int main (void) {
int pop[5], newbaby = 5, newpop;
pop[0] = 25; pop[1] = 34;
pop[0] += newbaby;
pop[0] = pop[0] + pop[1];
pop[1] = 0;
printf("After the pop marge, pop0 is %d, pop2 is %d", pop[0], pop[1]);
return 0;
}
Declaration:
double score[1000]
#define NUM_SUBPOP 10 int pop[NUM_SUBPOP];
int numSubPop = 100; int pop[numSubPop];
int i, a[100];
for (i = 0; i < 100; i++) {
a[i] = 10;
}
double prob[3] = {0.36, 0.48, 0.16};
int pop[] = {25, 34, 11, 51, 38};
You can omit the size of an array (pop[]) with this method.
double b[100] = {1.0};
All elements are initialized to be 1.0.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define VECT_SIZE 10
void MkRandVect(void);
void PrintVect(void);
/* global variables */
int gVectA[VECT_SIZE];
int gVectB[VECT_SIZE];
int gVectC[VECT_SIZE];
int main (void) {
int i, seed;
seed=time(NULL);
srand48(seed);
MkRandVect();
PrintVect();
/* Call the vector manipulation function here */
PrintVect();
return 0;
}
void MkRandVect(void) {
int j;
for (j = 0; j < VECT_SIZE; j++) {
gVectA[j] = (int) (drand48() * 10);
gVectB[j] = (int) (drand48() * 10);
}
}
~ C in
the following format.
Inner product of 2 vectors:
is
In addition to printing the largest and smallest value, can you modify the program to print out the positions (indices) of the elements which contain the two extreme values?