Deloppgave 1

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
andreped
Date:
Tue Oct 24 11:35:27 2017 +0000
Commit message:
Oving 8_1

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b6d868ecca70 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 24 11:35:27 2017 +0000
@@ -0,0 +1,81 @@
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX);
+
+int numbers[100];
+
+int get_seed();
+int get_number_of_elements();
+void fill_array(int seed, int arr[], int len);
+void sort_array(int arr[], int len);
+void print_array(int arr[], int len);
+int print();
+
+int main()
+{
+    int mySeed, numElements;
+    mySeed = print();
+    numElements = get_number_of_elements();
+    fill_array(mySeed, numbers, numElements);
+    print_array(numbers, numElements);
+    sort_array(numbers, numElements);
+    print_array(numbers, numElements);
+
+    return 0;
+}
+
+int print()
+{
+    int tall;
+    pc.printf("Skriv inn et heltall mellom 1 og 10.000 ");
+    pc.scanf("%d", &tall);
+    return tall;
+}
+int get_number_of_elements()
+{
+    int tall;
+    pc.printf("Skriv inn antall elementer, tabellen skal inneholde ");
+    pc.scanf("%d", &tall);
+    return tall;
+}
+void fill_array(int seed, int arr[], int len)
+{
+    srand(seed);
+    for (int i = 0; i < len; i++) {
+        arr[i] = rand()%101;
+    }
+}
+
+void print_array(int arr[], int len)
+{
+
+    for (int i = 0; i < len; i++) {
+        pc.printf ("%5d", arr[i]);
+        if((i % 10) == 9)
+            pc.printf("\n");
+    }
+    pc.printf("\n");
+
+}
+
+
+void sort_array(int arr[], int len)
+{
+    int sortering;
+    for(int i=0; i<len; i++) {
+        for(int j=i+1; j < len; j++) {
+            if(arr[i]>arr[j]) {
+                sortering=arr[i];
+                arr[i] = arr[j];
+                arr[j] = sortering;
+            }
+        }
+    }
+}
+
+
+
+
+
+
+
diff -r 000000000000 -r b6d868ecca70 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Oct 24 11:35:27 2017 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/b484a57bc302
\ No newline at end of file