1

I am trying to create some test cases for my 'minimum dot product' problem. I want 10 test cases , each generating different set of values for both vector a and b.

The Problem is that even after using srand( time( NULL ) ) though a new input is generated every time I compile and run the code but that same input is used for all the 10 test cases.

#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using std::vector;

void sort_asc(vector<int> &manav, int sizes)
{
    int temp = 0;

    for (int i = 0; i<sizes; i++)
    {

        for (int j = i + 1; j<sizes; j++)
        {

            if (manav[i] > manav[j])
            {

                temp = manav[i];
                manav[i] = manav[j];
                manav[j] = temp;

            }
        }
    }

    std::cout << "b in asc order : ";
    for (int i = 0; i<sizes; i++)
    {
        std::cout << manav[i] << " ";

    }

    std::cout << std::endl;

}

void sort_desc(vector<int> &manav, int sizes)
{
    int temp = 0;

    for (int i = 0; i<sizes; i++)
    {

        for (int j = i + 1; j<sizes; j++)
        {

            if (manav[i] < manav[j])
            {

                temp = manav[i];
                manav[i] = manav[j];
                manav[j] = temp;

            }
        }

    }

    std::cout << "a in desc : ";
    for (int i = 0; i<sizes; i++)
    {
        std::cout << manav[i] << " ";

    }
    std::cout << std::endl;

}

long long min_dot_product(vector<int> a, vector<int> b, int sizes) {

    long long result = 0;

    sort_desc(a, sizes);

    sort_asc(b, sizes);


    for (size_t i = 0; i < sizes; i++) {
        result += a[i] * b[i];
    }
    return result;
}

int main() {

    srand(time(NULL));
    /*
    std::cin >> n;
    vector<int> a(n), b(n);
    for (size_t i = 0; i < n; i++) {
    std::cin >> a[i];
    }
    for (size_t i = 0; i < n; i++) {
    std::cin >> b[i];
    }
    */

    //================================================================ TESTING =========================================================================
    int z = 0;
    int n = (rand() % 10) + 1; // generating the size of the vectors [1-10]
    std::cout << "n = " << n << "\n";
    vector<int> a;
    vector<int> b;

    while (z != 10) {

        for (int i = 0; i < n; ++i)
        {
            int p = (rand() % 10) - 5;
            a.push_back(p); // input values [-5,4] in 'a'

        }

        std::cout << "Unsorted Vector a = ";
        for (int i = 0; i<n; i++)
        {
            std::cout << a[i] << " ";
        }

        std::cout << std::endl;

        for (int i = 0; i < n; ++i)
        {
            int q = (rand() % 10) - 5;
            b.push_back(q); // inputing values [-5,4] in 'b'

        }

        std::cout << "Unsorted Vector b = ";
        for (int i = 0; i<n; i++)
        {
            std::cout << b[i] << " ";
        }

        std::cout << std::endl;

        std::cout << "min_dot_product = " << min_dot_product(a, b, n) << std::endl;
        z++;

    }

    return 0;
}

I somehow want to generate a different set of values for vector a and b for all of the 10 test cases every time I run the code.

I have tried srand(i) within the respective for loops before pushing the value in vectors but its not working for me, also reusing srand( time( NULL ) ) within the for loops is not gonna help either. Is there some other simple way I can achieve this?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Manav Saxena
  • 453
  • 2
  • 8
  • 21

1 Answers1

1

The problem is you never clear out the vector on each iteration. Since you don't all of the new random numbers you generate are being added to the end of the vector and you ignore them since n never changes.

What you need to do is add

a.clear();
b.clear();

to the end of the while loop. This will clear out the vectors and then when you start the next iteration the new random numbers will get added into the part of the vector you use in your functions.

You could also set the vector the proper size and then use [] to access the elements. This way you would just overwrite the previous values and you would not have to call clear()

vector<int> a(n);
vector<int> b(n);
//...
for (int i = 0; i < n; ++i)
{
    a[i] = (rand() % 10) - 5;
    b[i] = (rand() % 10) - 5;
}

I put both assignments in the same for loop to save space. You can do this in two separate loops but it is not needed.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402