Input of Elements Not Acting as Should.

This program is to take an array of random numbers ranging from 10, 100, 1000 elements. The program is behaving correctly with 10 elements. When I input 100, and 1000 elements it will only run the program when I paste them at the first cin, "cin >> elements". If I input the numbers in the second cin, "cin >> arr[i]" it never executes the program. But it will not process 10 elements when I input on the first cin, "cin >> elements". I have no idea what is going on... Any help would be greatly appreciated.

#include <iostream>
#include <vector>
#include <chrono>

using namespace std;
using namespace std::chrono;

std::vector <pair<int, int>> ans;
int *arr = new int [1200000];

int main()
{
int elements;

cout << "Enter number of array elements: ";
cin >> elements;

cout << "Enter the elements: ";

for (int i = 0; i < elements; i++)
{
cin >> arr[i];
}

auto start = high_resolution_clock::now();

// Sort the array

sort(arr, arr + elements);

int smallest = INT_MAX;

for (int i = 1; i < elements; i++)
{
if ((arr[i] - arr[i - 1]) <= smallest)
{
if ((arr[i] - arr[i - 1] ) < smallest)
ans.clear();

smallest = arr[i] - arr[i - 1];
ans.push_back({arr[i - 1], arr[i]});
}
}

auto stop = high_resolution_clock::now();

cout << "\n\n******* The closest Pair(s) *******" << endl;
for (int i = 0; i < ans.size(); i++)
{
cout << " \t\t" << ans[i].first << "\t " << ans[i].second << endl;
}

auto duration = duration_cast<microseconds>(stop - start);

cout << "Time taken by the function: "
<< duration.count() << " microseconds" << endl;

return 0;
}
Last edited on
Your question makes no sense.
You say "random numbers" but there are no random numbers being generated.
You talk about the "first cin" and the "second cin", but there is only one cin in the program.
And with the program you've shown there's no reason why the number of inputs (10, 100, 1000) should make a difference.
Sorry, for the confusing question, I should have taken a few deep breathes before posting this.

I input the random numbers from random.org. I copy and paste them each time.

By first cin I mean cin >> elements
-This is where you indicate if you want to enter 10, 100, or 1000 elements.

second cin, is cin >> arr[i]
This takes the all 10, 100, 1000 elements.

It shouldn't make a difference but for some reason it does. I don't understand what is going on at all.
I guess there are two cins after all!

Okay, so I assume that you type in the number of elements (and don't paste that).

Then at that point you either you hit Return or you just type a space.

Then you paste some numbers you copied off of a website and hit Return after that.

So whether or not you hit the first return Return or just type a space is what makes the difference?

I tried that (on linux) and it doesn't make any difference for me.

These are the 100 random numbers that I tried (from random.org)

3089	7810	5600	656	3473
8524	29	804	9541	8480
4896	5787	2021	7980	7718
5800	6110	7597	1558	508
6298	9701	1705	8072	451
2137	1662	5594	9319	5967
6827	2465	2720	6938	2862
163	6464	1912	1691	7458
2522	9080	8465	484	2031
6657	4940	4753	6026	2825
369	9725	2788	365	8600
965	5082	4308	5466	7875
2803	2236	2148	3788	6607
5011	596	263	4558	749
5459	2408	5790	7361	5555
2944	4267	9413	3869	6860
6022	16	8536	2081	4729
8854	2101	4596	9639	4557
5833	7034	4902	602	2096
1502	4759	812	4101	6549


And this is the program I used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;

int main() {
    std::vector<pair<int, int>> ans;

    int elements;
    cout << "Enter number of array elements: ";
    cin >> elements;

    int *arr = new int [elements];

    cout << "Enter the elements: ";
    for (int i = 0; i < elements; i++)
        cin >> arr[i];

    sort(arr, arr + elements);

    int smallest = INT_MAX;
    for (int i = 1; i < elements; i++) {
        int diff = arr[i] - arr[i - 1];
        if (diff <= smallest) {
            if (diff < smallest) {
                ans.clear();
                smallest = diff;
            }
            ans.push_back(make_pair(arr[i - 1], arr[i]));
        }
    }

    cout << "\n\nThe closest Pair(s): diff=" << smallest << "\n";
    for (size_t i = 0; i < ans.size(); i++)
        cout << "\t" << ans[i].first << "\t" << ans[i].second << '\n';
}

I also tried an exact copy/paste of your original program (after adding includes for algorithm and climits) and got the same result.
Last edited on
Yes I type 10, 100, or 1000.
So if I type 1000, then paste the 1000 elements it never executes the remaining part of the program. It just sits with the 1000 elements.

But... if I paste those 1000 elements in the first cin, it will execute the entire program. It makes no sense!!

I'm using Xcode on a mac as well.
I input the random numbers from random.org. I copy and paste them each time.

Why? It isn't that hard to generate your own random numbers within your program.

std::cin takes input from your keyboard, not some website. Copy/pasting text can cause problems if what is being pasted is not strictly numeric text.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <random>
#include <chrono>
#include <vector>

int main()
{
   // a time-based seed value using the system clock
   unsigned seed1 { static_cast<unsigned> ( std::chrono::system_clock::now().time_since_epoch().count() ) };

   // a seed value using the non-deterministic random number generator
   unsigned seed2 { static_cast<unsigned> ( std::random_device{}() )};

   // creating a random number engine, seeding it on creation
   // there are other engines and adaptors available
   std::default_random_engine rng(seed1);

   // seed/reseed after construction
   rng.seed(seed2);

   // create a distribution, arbitrarily chosen to be between 1 and 100 inclusive
   std::uniform_int_distribution<int> dis(1, 100);

   std::cout << "How many elements? ";
   int num_elements { };
   std::cin >> num_elements;

   // let's create a sized vector to hold the number of elements
   // default constructed to be zero-filled
   std::vector<int> ran_numbers(num_elements);

   for (size_t loop { }; loop < num_elements; loop++)
   {
      // fill each elements with a random number
      // no need for push_back
      ran_numbers[loop] = dis(rng);
   }

   int count { 0 };

   // loop through the vector using a range based for loop
   for (const auto& itr : ran_numbers)
   {
      std::cout << itr << '\t';

      count++;

      if (count % 10 == 0) { std::cout << '\n'; }
   }
   std::cout << '\n';
}

If you need the same series of "random" numbers each time your program runs use the same numeric value for your seed.
@Rambus Jarbus
This is how you do it using Xcode on a Mac, the terminal and random.org
I used the random integer generator. It's the hidden return that's causing the apparent problem.

1. Go to random.org select the number of random numbers you want.
2. Make the no. of columns the same as the no. of numbers. ie get a single row of random numbers.
3. Copy the row of numbers.
3. Run the executable in Terminal
4. Specify the number of numbers, press return.
5. At the prompt for the elements simply paste the (single) row of numbers.
6. The rest follows.

Note: This works for 10 and 100 and I have no doubt 1000 will too.

Enter number of array elements: 100
Enter the elements: 46	91	42	84	20	12	47	96	58	57	32	55	66	9	55	57	71	94	42	60	61	11	52	7	68	47	27	20	20	9	23	88	78	61	33	53	10	87	18	46	19	32	17	40	86	72	7	38	78	78	10	14	36	21	7	18	41	46	83	5	77	14	72	33	55	86	36	57	75	50	54	68	50	35	34	23	2	85	45	58	27	23	18	94	10	50	49	48	63	42	55	15	50	77	100	16	41



******* The closest Pair(s) *******
 		2	 2
 		7	 7
 		7	 7
 		7	 7
 		9	 9
 		10	 10
 		10	 10
 		14	 14
 		18	 18
 		18	 18
 		20	 20
 		20	 20
 		23	 23
 		23	 23
 		27	 27
 		32	 32
 		33	 33
 		36	 36
 		41	 41
 		42	 42
 		42	 42
 		46	 46
 		46	 46
 		47	 47
 		50	 50
 		50	 50
 		50	 50
 		55	 55
 		55	 55
 		55	 55
 		57	 57
 		57	 57
 		58	 58
 		61	 61
 		68	 68
 		72	 72
 		77	 77
 		78	 78
 		78	 78
 		86	 86
 		94	 94
Time taken by the function: 44 microseconds

[Process completed]




Last edited on
PS I decided to try beyond 100 and got up to about 250 successfully. 72 microseconds

500 & 1000 has troubles and I suspect this might be some buffer limitation in Terminal, maybe.
Topic archived. No new replies allowed.