Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Dato: 29-10-2011
- * Kursus: Imperativ Programming
- * Underviser: Kurt Nørmark
- * Opgave: Opgave 11 side 462 i Problem Solving and Program Design in C, sixth edition.
- * Tid brugt: 120 minutter.
- */
- #include <stdio.h>
- #include <stdlib.h>
- void combined_array(double *arr1, int a1_size, double *arr2, int a2_size, double *out);
- int main(void) {
- // Initialize variable arrays.
- double arr1[] = {-10.5, -1.8, 3.5, 6.3, 7.2};
- double arr2[] = {-1.8, 3.1, 6.3};
- // Get size of arrays as we can't do this in the function.
- int a1_size = sizeof(arr1)/sizeof(double);
- int a2_size = sizeof(arr2)/sizeof(double);
- double out[a1_size+a2_size];
- combined_array(arr1, a1_size, arr2, a2_size, out);
- return 0;
- }
- // Create sorting function
- void combined_array(double *arr1, int a1_size, double *arr2, int a2_size, double *out) {
- double temp[a1_size+a2_size];
- double d;
- int i, j, k;
- // Merge
- for (i = 0, j = 0, k = 0; i < a1_size && j < a2_size;) {
- if (arr1[i] < arr2[j]) {
- temp[k] = arr1[i];
- k++;
- i++;
- }
- else {
- temp[k] = arr2[j];
- k++;
- j++;
- }
- }
- while (i < a1_size) {
- temp[k++] = arr1[i++];
- }
- while (j < a2_size) {
- temp[k++] = arr2[j++];
- }
- // Remove duplicates.
- i = 0;
- j = 0;
- int counter = 0;
- int duplicate_counter = 0;
- while (i < a1_size+a2_size) {
- d = temp[i];
- j = i+1;
- while (j < a1_size+a2_size) {
- if (temp[i] == temp[j]) {
- i++;
- duplicate_counter++;
- }
- j++;
- }
- out[counter] = d;
- counter++;
- i++;
- }
- printf("Duplicates: %d\n", duplicate_counter);
- for(i = 0; i < a1_size + a2_size; i++) {
- printf("%f ", out[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement