## Homework

### July 23, 2019

We’ve done this kind of thing before:

```Petite Chez Scheme Version 8.4

> (define (f n m t)
(define (rand n)
(inexact->exact (floor (+ (* (random 1.0) n) 1))))
(let ((nums (map rand (make-list n m))))
(display nums) (newline)
(display (apply + nums)) (newline)
(display (length (filter (lambda (x) (= x t)) nums))) (newline)))
> (f 50 10 3)
(5 7 3 7 1 8 1 7 1 1 3 10 7 7 2 5 9 8 9 6 9 5 3 4 7
8 7 9 8 10 2 3 9 1 10 6 3 3 1 1 1 7 6 10 4 4 8 1 5 5)
267
6```

You can run the program at https://ideone.com/EAgAmB.

Pages: 1 2

### 10 Responses to “Homework”

1. James Curtis-Smith said

Perl “golfy code” originally one liner. Line 1 creates the array – generates the counts for each number and sums them; Line 2 just outputs the data!

```(\$t+=\$_,\$X{\$_}++)foreach@A=map{1+int rand 10}1..50;
say join"\n","@A",\$t,map{"\$_\t\$X{\$_}"}sort{\$a<=>\$b}keys%X;
```
2. James Curtis-Smith said

Slightly shorter and faster!
[sourcecode lang="perl"]
(\$t+=\$,\$X[\$]++)foreach@A=map{1+int rand 10}1..50;
say join”\n”,”@A”,\$t,map{“\$\t\$X[\$]”}1..10;
[/sourccode]

3. James Curtis-Smith said

Slightly shorter and faster!

```(\$t+=\$,\$X[\$]++)foreach@A=map{1+int rand 10}1..50;
say join”\n”,”@A”,\$t,map{“\$\t\$X[\$]”}1..10;
```
4. Steve said

Klong version (62 characters)

```{[l]; l::50{(1+_.rn()*10),x}:*[];.p(l);.p(+/l);.p(#l?x)}(5);""
[4 3 7 5 9 4 5 6 5 3 1 4 10 1 1 4 9 1 4 10 9 5 3 9 4 1 9 2 6 4 4 10 7 5 4 1 5 2 5 8 10 10 5 10 3 6 1 2 9 6]
261
8
""

```
5. Milbrae said

In Python

```import random
from collections import Counter

arr50 = [random.randint(0, 10) + 1 for i in range(50)]
print (arr50)
print (sum(arr50))
print (Counter(arr50))
```

[5, 5, 6, 5, 9, 6, 2, 11, 8, 7, 4, 9, 3, 10, 11, 2, 1, 7, 4, 7, 1, 6, 4, 7, 5, 9, 3, 3, 9, 10, 10, 5, 5, 4, 2, 4, 4, 8, 8, 9, 7, 3, 11, 5, 1, 9, 4, 1, 7, 9]
295
Counter({5: 7, 9: 7, 4: 7, 7: 6, 3: 4, 1: 4, 6: 3, 2: 3, 11: 3, 8: 3, 10: 3})

6. Daniel said

Here’s a solution in C.

```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 50
#define MAX 10

void print_array(int* array, int n) {
for (int i = 0; i < n; ++i) {
if (i > 0) printf(" ");
printf("%d", array[i]);
}
printf("\n");
}

int main(void) {
int array[N];
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
srand(t.tv_nsec);
// Fill and print an array with random numbers.
for (int i = 0; i < N; ++i) {
// This approach has a slightly higher probability
// of choosing lower numbers.
int x = rand() % MAX + 1;
array[i] = x;
}
print_array(array, N);
// Calculate and print the sum of numbers.
int sum = 0;
for (int i = 0; i < N; ++i) {
sum += array[i];
}
printf("%d\n", sum);
// Calculate and print the number of times the numbers occur.
int count[MAX] = {0};
for (int i = 0; i < N; ++i) {
++(count[array[i] - 1]);
}
print_array(count, MAX);
return EXIT_SUCCESS;
}
```

Example:

```\$ ./a.out
7 9 1 3 1 7 9 3 6 7 6 8 3 5 3 9 3 4 4 9 3 4 8 3 6 10 7 10 8 10 5 4 9 7 6 1 4 6 3 9 4 10 8 7 4 1 5 9 6 10
294
4 0 8 7 3 6 6 4 7 5
```
7. akshay said

Java:
int sum=0;
int [] data = new int;
int count [] = new int ;
Random r =new Random();
for(int i =0;i<50;i++) { data[i] = r.nextInt(10)+1;sum+=data[i]; }
for (int i=0;i< data.length;i++) count[data[i]-1]++;

8. dfijma said

Actually filling the array is an implementation detail. This observation leads to the optimized version (in pseudo code):

print(275, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5);

9. Milbrae said

```Code: