Lowlands Tactical: Forum

Full Version: De grote programmeer opdracht 2016!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Neem de taal die je kent en laat zien wat je kan en hoe dit eruit ziet:
Archu Wrote:-Create an array of ten integers
-Fill the array with ten random numbers (1-100)
-Copy the array (into another array of the same capacity)
-Change the last value in the first array to a -7
-Display the contents of both arrays

Toon hier jullie resultaten!
Code:
#!/usr/bin/env python3
import random
myArray = []
for n in range(0,10):
 myArray.append(random.randint(1,100))
urArray = myArray.copy()
myArray[-1] = -7
print("1st Array: "+str(myArray)+"\n"+"2nd Array: "+str(urArray))

Result Wrote:$ ./test.py
1st Array: [32, 67, 97, 67, 5, 23, 53, 33, 4, -7]
2nd Array: [32, 67, 97, 67, 5, 23, 53, 33, 4, 63]
Voor PHP:

PHP Code:
$arr1 = array();
for(
$i 0$i 10$i++){
   $arr1[] = rand(099);
}
$arr2 $arr1;
$arr1[9] = -7;
var_dump($arr1,$arr2); 



Code:
# Result:
array (size=10)
 0 => int 93
 1 => int 80
 2 => int 53
 3 => int 49
 4 => int 85
 5 => int 87
 6 => int 52
 7 => int 33
 8 => int 39
 9 => int -7
array (size=10)
 0 => int 93
 1 => int 80
 2 => int 53
 3 => int 49
 4 => int 85
 5 => int 87
 6 => int 52
 7 => int 33
 8 => int 39
 9 => int 14
Fur Java:

Code:
public class CopyArrays {

    static ArrayList arr1 = new ArrayList();
    static ArrayList arr2;

    private static ArrayList<Integer> randomize(ArrayList<Integer> array) {

        Integer cnt1;
        Random rndm = new Random();

        for (int i = 0; i < 10; i++) {
            cnt1 = rndm.nextInt(100);
            array.add(cnt1);
        }
        return array;
    }

    public static void main(String[] args) {

        randomize(arr1);
        arr2 = (ArrayList<String>) arr1.clone();
        arr1.set(9, "-7");

        System.out.println("Array 1: " + arr1 + "\n" + "Array 2: " + arr2);
    }
}


Result:
Quote:Array 1: [77, 38, 3, 37, 58, 48, 27, 18, 16, -7]

Array 2: [77, 38, 3, 37, 58, 48, 27, 18, 16, 42]
PHP Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
  
int myArray[10];
  
int urArray[10];
  
int ij;
  
time_t t;

  
/* Seed with me */
  
srand((unsignedtime(&t));

  for (
i=0;i<10;i++) {
    
myArray] = rand() % 100;
  }

  for (
i=0;i<10;i++) {
    
urArray[i] = myArray[i];
  }

  
myArray[9] = -7;

  
printf("1st Array: ");
  for (
j=0;j<10;j++) {
    
printf("%d ",myArray[j]);
  }
  
printf("\n");

  
printf("2nd Array: ");
  for (
j=0;j<10;j++) {
    
printf("%d ",urArray[j]);
  }
  
printf("\n");



Code:
$ ./a.out
1st Array: 39 63 41 96 97 47 79 45 30 -7
2nd Array: 39 63 41 96 97 47 79 45 30 31
Een externe contributie:

PHP Code:
#!/bin/bash

declare -a myArray
for i in `seq 0 9`; do myArray[$i]=$(($(random -er 100;echo $?)+1)); done
for index in "${!myArray[@]}"; do newArray[$index]=${myArray[$index]}; done
myArray
[$index]=-7;
B=$index;
A=0;echo -"1st Array:[";for index in "${!myArray[@]}"; do echo -${myArray[$index]}; if [ $A -lt $B ]; then echo -","fi;A=$(($A+1)); done; echo \];
A=0;echo -"2nd Array:[";for index in "${!newArray[@]}"; do echo -${newArray[$index]}; if [ $A -lt $B ]; then echo -","fi;A=$(($A+1)); done; echo \];