![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Deloppgave 1
Dependencies: mbed
Diff: main.cpp
- Revision:
- 0:b6d868ecca70
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; + } + } + } +} + + + + + + +