Last active
April 18, 2018 14:31
-
-
Save not7cd/556f5058578daf63107e648c14fbe297 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* example8-2.c */ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
#include <assert.h> | |
#include <math.h> | |
void printVector(int* a, int n) | |
{ | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
printf("%2i: %3i\n",i, a[i]); | |
} | |
} | |
float v_sum(int* v, int n) | |
{ | |
float sum = 0; | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
sum += v[i]; | |
} | |
return sum; | |
} | |
float v_len(int* v, int n) { | |
float sum = 0; | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
sum += v[i] * v[i]; | |
} | |
return sqrt(sum); | |
} | |
void freq_analysis(int* v, int n, int* f) { | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
f[v[i]]++; | |
} | |
} | |
int main() | |
{ | |
int vec[100]; | |
int vec_2[3] = {3, 4, 0}; | |
assert(v_sum(vec_2, 3) == 7.); | |
assert(v_len(vec_2, 3) == 5.); | |
/* seeding the random number generator: */ | |
srand(time(NULL)); | |
int i; | |
for (i = 0; i < 100; i++) | |
{ | |
vec[i] = rand() % 10 + 1; | |
} | |
printf("The vector is:\n"); | |
printVector(vec, 100); | |
printf("frequency:\n"); | |
int freq[11] = {0}; | |
freq_analysis(vec, 100, freq); | |
printVector(freq, 11); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* example8-4.c */ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
void printVector(float* a, int n) | |
{ | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
printf("%2i: %7.3f\n",i, a[i]); | |
} | |
} | |
float v_sum(float* v, int n) | |
{ | |
float sum = 0; | |
int i; | |
for (i = 0; i < n; i++) | |
{ | |
sum += v[i]; | |
} | |
return sum; | |
} | |
float v_avg(float* v, int n) { | |
float s = v_sum(v, n); | |
return s / n; | |
} | |
float rand_float() | |
{ | |
return (((float)rand()) / ((float)RAND_MAX) - .5) * 4; | |
} | |
int main() | |
{ | |
float vec[100]; | |
/* seeding the random number generator: */ | |
srand(time(NULL)); | |
int i; | |
for (i = 0; i < 100; i++) | |
{ | |
/* generating a random floating point number between 0 and 1 */ | |
vec[i] = rand_float(); | |
} | |
printf("The vector is:\n"); | |
printVector(vec, 100); | |
printf("sum:%f\n", v_sum(vec, 100)); | |
printf("avg:%f\n", v_avg(vec, 100)); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int my_rand(int max_range) { | |
return rand() % max_range + 1; | |
} | |
int main() | |
{ | |
int n, max_range = 10; | |
scanf("%i %i", &n, &max_range); | |
srand(time(NULL)); | |
int i; | |
for (i = 1; i <= n; i++) { | |
printf("%i\n", my_rand(max_range)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment