Deloppgave 1

Dependencies:   mbed

Committer:
andreped
Date:
Tue Oct 24 11:35:27 2017 +0000
Revision:
0:b6d868ecca70
Oving 8_1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andreped 0:b6d868ecca70 1 #include "mbed.h"
andreped 0:b6d868ecca70 2
andreped 0:b6d868ecca70 3 Serial pc(USBTX, USBRX);
andreped 0:b6d868ecca70 4
andreped 0:b6d868ecca70 5 int numbers[100];
andreped 0:b6d868ecca70 6
andreped 0:b6d868ecca70 7 int get_seed();
andreped 0:b6d868ecca70 8 int get_number_of_elements();
andreped 0:b6d868ecca70 9 void fill_array(int seed, int arr[], int len);
andreped 0:b6d868ecca70 10 void sort_array(int arr[], int len);
andreped 0:b6d868ecca70 11 void print_array(int arr[], int len);
andreped 0:b6d868ecca70 12 int print();
andreped 0:b6d868ecca70 13
andreped 0:b6d868ecca70 14 int main()
andreped 0:b6d868ecca70 15 {
andreped 0:b6d868ecca70 16 int mySeed, numElements;
andreped 0:b6d868ecca70 17 mySeed = print();
andreped 0:b6d868ecca70 18 numElements = get_number_of_elements();
andreped 0:b6d868ecca70 19 fill_array(mySeed, numbers, numElements);
andreped 0:b6d868ecca70 20 print_array(numbers, numElements);
andreped 0:b6d868ecca70 21 sort_array(numbers, numElements);
andreped 0:b6d868ecca70 22 print_array(numbers, numElements);
andreped 0:b6d868ecca70 23
andreped 0:b6d868ecca70 24 return 0;
andreped 0:b6d868ecca70 25 }
andreped 0:b6d868ecca70 26
andreped 0:b6d868ecca70 27 int print()
andreped 0:b6d868ecca70 28 {
andreped 0:b6d868ecca70 29 int tall;
andreped 0:b6d868ecca70 30 pc.printf("Skriv inn et heltall mellom 1 og 10.000 ");
andreped 0:b6d868ecca70 31 pc.scanf("%d", &tall);
andreped 0:b6d868ecca70 32 return tall;
andreped 0:b6d868ecca70 33 }
andreped 0:b6d868ecca70 34 int get_number_of_elements()
andreped 0:b6d868ecca70 35 {
andreped 0:b6d868ecca70 36 int tall;
andreped 0:b6d868ecca70 37 pc.printf("Skriv inn antall elementer, tabellen skal inneholde ");
andreped 0:b6d868ecca70 38 pc.scanf("%d", &tall);
andreped 0:b6d868ecca70 39 return tall;
andreped 0:b6d868ecca70 40 }
andreped 0:b6d868ecca70 41 void fill_array(int seed, int arr[], int len)
andreped 0:b6d868ecca70 42 {
andreped 0:b6d868ecca70 43 srand(seed);
andreped 0:b6d868ecca70 44 for (int i = 0; i < len; i++) {
andreped 0:b6d868ecca70 45 arr[i] = rand()%101;
andreped 0:b6d868ecca70 46 }
andreped 0:b6d868ecca70 47 }
andreped 0:b6d868ecca70 48
andreped 0:b6d868ecca70 49 void print_array(int arr[], int len)
andreped 0:b6d868ecca70 50 {
andreped 0:b6d868ecca70 51
andreped 0:b6d868ecca70 52 for (int i = 0; i < len; i++) {
andreped 0:b6d868ecca70 53 pc.printf ("%5d", arr[i]);
andreped 0:b6d868ecca70 54 if((i % 10) == 9)
andreped 0:b6d868ecca70 55 pc.printf("\n");
andreped 0:b6d868ecca70 56 }
andreped 0:b6d868ecca70 57 pc.printf("\n");
andreped 0:b6d868ecca70 58
andreped 0:b6d868ecca70 59 }
andreped 0:b6d868ecca70 60
andreped 0:b6d868ecca70 61
andreped 0:b6d868ecca70 62 void sort_array(int arr[], int len)
andreped 0:b6d868ecca70 63 {
andreped 0:b6d868ecca70 64 int sortering;
andreped 0:b6d868ecca70 65 for(int i=0; i<len; i++) {
andreped 0:b6d868ecca70 66 for(int j=i+1; j < len; j++) {
andreped 0:b6d868ecca70 67 if(arr[i]>arr[j]) {
andreped 0:b6d868ecca70 68 sortering=arr[i];
andreped 0:b6d868ecca70 69 arr[i] = arr[j];
andreped 0:b6d868ecca70 70 arr[j] = sortering;
andreped 0:b6d868ecca70 71 }
andreped 0:b6d868ecca70 72 }
andreped 0:b6d868ecca70 73 }
andreped 0:b6d868ecca70 74 }
andreped 0:b6d868ecca70 75
andreped 0:b6d868ecca70 76
andreped 0:b6d868ecca70 77
andreped 0:b6d868ecca70 78
andreped 0:b6d868ecca70 79
andreped 0:b6d868ecca70 80
andreped 0:b6d868ecca70 81