Homework
July 23, 2019
Today’s exercise comes from somebody’s homework assignment:
Write a program that fills a 50-element array with random numbers from 1 to 10. Print the array. Calculate and print the sum of the random numbers in the array. Calculate and print the number of times that a particular number appears in the array.
Your task is to complete the homework problem described above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
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;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]
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;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 ""In Python
[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})
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:
Java:
int sum=0;
int [] data = new int[50];
int count [] = new int [10];
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]++;
System.out.println(Arrays.toString(data)+”\nAddition : “+sum+”\n”+Arrays.toString(count));
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);
@dfijma… what’s your observation?
Klong, version 2
Code: l3::l2@<l2::{(l@x@0),"-->",x}'=l::50{(1+_.rn()*10),x}:*[]; .p(l); .p(+/l); .p(l3);"" Run: [5 3 10 10 10 7 3 5 3 4 7 8 2 5 3 3 3 4 2 10 4 8 7 8 2 2 8 3 8 5 4 5 2 5 1 2 1 3 5 7 2 5 7 8 5 6 3 6 5 7] 251 [[1 --> 34 36] [2 --> 12 18 24 25 32 35 40] [3 --> 1 6 8 14 15 16 27 37 46] [4 --> 9 17 20 30] [5 --> 0 7 13 29 31 33 38 41 44 48] [6 --> 45 47] [7 --> 5 10 22 39 42 49] [8 --> 11 21 23 26 28 43] [10 --> 2 3 4 19]] ""