-1

So i have 2 lines of arrays. First line is M, second line is N. On the first line the user inputs 2 digits, first for the length of M, and second for the length of N. On the second line user inputs M, on the third - N. All i want is to display what elements of N are not contained in m, but in ascending order. Here's an example:

7 5

3.12 7.96 3.51 4.77 10.12 1.11 9.80

10.12 3.51 3.12 9.80 4.77

Output:

1.11 7.96

And here is my attempt at the program:

#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;

int ifexists(double z[], int u, int v)
{
    int i;
    if (u == 0)
        return 0;
    for (i = 0; i <= u; i++)
        if (z[i] == v)
            return (1);
    return (0);
}
void main()
{
    double bills[100], paid[100], result[100];
    int m, n;
    int i, j, k;
    cin >> m >> n;
    if (n > m) {
        cout << "Wrong input.";
        exit(3);
    }
    for (int i = 0; i < m; i++) {
        cin >> bills[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> paid[i];
    }
    for (i = 0; i < n; i++)
        k = 0;
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            if (bills[i] == paid[j]) {
                break;
            }
        }
        if (j == n) {
            if (!ifexists(result, k, bills[i])) {
                result[k] = bills[i];
                k++;
            }
        }
    }
    for (i = 0; i < k; i++)
        cout << result[i] << " ";
}

The output i'm getting is almost correct - 7.96 1.11. Basically in reverse order. How can i flip this around? Also, chances are my report is too slow. Is there a way to increase the speed?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 2
    Such questions are better for [Code Review](https://codereview.stackexchange.com/questions) from my experience here. – πάντα ῥεῖ Dec 12 '20 at 13:27
  • 1) It is time to learn about [minimal complete examples](https://stackoverflow.com/help/minimal-reproducible-example). 2) If you want to make code faster, it pays to learn about algorithmic compexity. 3) If you sort `M` and `N`, you may find that the task is easier and the code faster. – Beta Dec 12 '20 at 13:30
  • You can increase the efficiency by using `std::set` to check if a value is included, both for second array and result. Output of a `std::set` is sorted. – Damien Dec 12 '20 at 13:32
  • `void main()` is not a correct signature for main. Related: [https://stackoverflow.com/questions/636829/difference-between-void-main-and-int-main](https://stackoverflow.com/questions/636829/difference-between-void-main-and-int-main) – drescherjm Dec 12 '20 at 13:35

1 Answers1

0

You can use std::set_difference:

std::sort(bills, bills + m);
std::sort(paid, paid + n);
auto end = std::set_difference(bills, bills + m, paid, paid + n, result);
for (auto it = result; it != end; ++it) {
    std::cout << *it << " ";
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302