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.

Advertisements

Pages: 1 2

8 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[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));

  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);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: