Arduino insertion sort issue with struct array - c++

Here is the struct:
struct RGB {
byte r;
byte g;
byte b;
byte v;
};
And here is the sorting function:
void insertion_sort(RGB arr[], size_t capacity) {
RGB temp;
size_t j;
for(size_t i = 1; i < capacity; i++) {
temp = arr[i];
j = i - 1;
while(j >= 0 && arr[j].v > temp.v) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
the v member of the RGB struct is the object's position in the array before scrambling (and hopefully where it ends up after sorting). Here is the output when printing the v member of every element of the array:
165 171 53 164 171 13 13 167 156 168 163 14 15 16 17 18 19 20 20 156 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 142 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
I get the same output every time.
There are 150 elements in the array, and everything seems fine after 21. The issue is that the elements before that are wrong, and also made up of values higher than 149. Currently, the b and g members of RGB are all 0 throughout the array, and r is evenly-spaced values from 0-254.
It should be noted that the exact same sorting function works when I recreated everything in C++, and it even works to sort an array of ints in Arduino. The only issue is sorting an array of RGB structs, which seems to be causing memory issues.

Your problem is the definition of j.
Your options are:
Serial output in critical lines and analyze the serial monitor -> Arduinos way of debuging
using one of the hundred tested sorting functions (e.g. here on stack exchange) like quick sort etc.
using the std::sort
For the last option you have to:
The best solution is to use the C++ standard library function std::sort. This function is type-safe, and doesn't require you to cast anything to void * and back, and you don't have to manually enter the sizes of the array and its elements.
#include < Arduino_Helpers.h\>
#include < AH/STL/algorithm\>
#include < AH/STL/iterator\>
auto cmpfunc = [](RGB A, RGB B) { return A.value < B.value; };
// or if you do not like this notation use
bool cmpfunc(RGB A, RGB B) {
return A.value < B.value;
}
std::sort(std::begin(RGB arr[]), std::end(RGB arr[]), cmpfunc); // Dummy for a RGB arr[] containing all your values
-

The problem was because of the size_t datatype, which is unsigned and could not become a value < 0 when it was supposed to. Thank you #JohnFilleau

Related

Segmentation Fault SIGSEGV while running C++ code

I am getting error while executing my C++ code
Error is:
The inferior stopped because it received a signal from the operating system.
Signal name: SIGSEGV
Signal meaning : Segmentation fault
IDE : QT Creator
I know there is something wrong with the way I have declared the 2 dimentional array of structs in header file
But don't know exactly what!
Can someone please explain..
Please note, I plan to create multiple instances of Datamatrix class as part of my work in a recursive or iterative manner.
Each instance containing 2 dimensional array of structs..
So Any help explanation is welcome.
There are many areas of C++ which I have not explored! still learning :)
Please see the code below
main.cpp
----------
#include <iostream>
#include "datamatrix.h"
#include "utils.h"
using namespace std;
void printArray(int items[],int size)
{
for(int j=0;j<size;j++)
{
cout<<items[j]<<" ";
}
cout<<endl;
}
int main()
{
cout << "Hello there" << endl;
int a[250];
for(int i=0;i<250;i++)
{
a[i] = i+1;
}
printArray(a,250);
DataMatrix *matrix = new DataMatrix();
Utils u;
u.myFunction(matrix); **<-- All goes well till here**
cout<<"End of Main method"; **<-- This line of code is never reached**
return 0;
}
datamatrix.h
------------
#ifndef DATAMATRIX_H
#define DATAMATRIX_H
#include <string>
using namespace std;
struct KVPair { **<--- This is the line where issue comes**
string key;
double value;
};
class DataMatrix
{
public:
DataMatrix();
KVPair matrix[250][130];
};
#endif // DATAMATRIX_H
datamatrix.cpp
--------------
#include "datamatrix.h"
DataMatrix::DataMatrix()
{
for(int i=0;i<250;i++)
{
for(int j=0;j<130;j++)
{
string k = "f"+i+1;
matrix[i][j].key = k;
matrix[i][j].value = i+5;
}
}
}
utils.h
--------
#ifndef UTILS_H
#define UTILS_H
#include "datamatrix.h"
class Utils
{
public:
Utils();
DataMatrix myFunction(DataMatrix* dm);
};
#endif // UTILS_H
utils.cpp
---------
#include "utils.h"
#include <algorithm>
#include <time.h>
#include <math.h>
#include "datamatrix.h"
Utils::Utils()
{
}
DataMatrix Utils::myFunction(DataMatrix* dm)
{
printf("inside myFunction ");
}
Just thought to add console output as well(please see console output below)
Hello there
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 52 53 54 55 56 5
7 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
248 249 250
inside myFunction
Thanks in advance
The following line:
string k = "f"+i+1;
Causes the issue. It takes the address far beyond the string literal "f", which is an invalid address, and passes that to std::string constructor, which reads from that address to determine the length of the string and causes the crash.
One way to fix would be:
string k = "f" + std::to_string(i + 1);
Eliminating the one warning from gcc -Wall prevents the crash. One way to do that is to add a return value in Utils::myFunction, as follows.
DataMatrix Utils::myFunction(DataMatrix* dm)
{
printf("inside myFunction ");
return *dm;
}
It appears that main allocates space for a DataMatrix on the stack with the expectation that the call to Utils::myFunction will populate that space. The following gdb session excerpt on a binary with the added return statement in Utils::myFunction reinforces that claim.
(gdb) break main.cpp:30
Breakpoint 1 at 0xfaa: file main.cpp, line 30.
(gdb) run > /dev/null
Starting program: main > /dev/null
Breakpoint 1, main () at main.cpp:30
30 u.myFunction(matrix);
(gdb) print matrix
$1 = (DataMatrix *) 0x7ffff7e95010
(gdb) set $matrix = (DataMatrix *) ($rbp - 0x13d640)
(gdb) print matrix.matrix[249][129].key
$2 = "\030J\017\vw\b\200"
(gdb) print $matrix.matrix[249][129].key
$3 = <incomplete sequence \303>
(gdb) next
31 cout<<"End of Main method";
(gdb) print $matrix.matrix[249][129].key
$4 = "\030J\017\vw\b\200"
(gdb)
In my testing, the crash occurs during an attempt to deallocate an uninitialized KVPair consisting of arbitrary stack contents. The details of that are not revealed in the preceding gdb session excerpt, but the point is that after Utils::myFunction returns a value other than void, the two copies of the DataMatrix agree about the contents of the last array element (the first one processed in the destructor, in my testing). The following excerpt from gdb disassembly explains the $rbp offset used for $matrix.
0x0000555555554fc7 <+183>: lea -0x13d640(%rbp),%rax
0x0000555555554fce <+190>: mov %rax,%rdi
0x0000555555554fd1 <+193>: callq 0x555555555092 <DataMatrix::~DataMatrix()>
Another way to prevent the crash is to declare Utils::myFunction as returning void, in which case main does not allocate space on the stack, and there is no call to the DataMatrix destructor within main.

Why are my bit shifts giving incorrect numbers

I'm trying to store a number in an array of 4 integers. The array is in the class Num. My problem is that when I call getValue, the function returns numbers that aren't correct. I tried go through the program on paper, doing all the calculations in Microsoft's calculator, and the program should give the correct output. I don't even know which function could be problematic since there aren't any errors or warnings, and both worked on paper.
21 in binary:10101
What I'm trying to do:
Input to setValue function: 21
setValue puts the first four bits of 21 (0101) into num[3]. So num[3] is now 0101 in binary. Then it should put the next four bits of 21 into num[2]. The next four bits are 0001 so 0001 goes into num[2] The rest of the bits are 0 so we ignore them. Now num is {0,0,1,5}. getValue first goes to num[3]. There is 5 which is 0101 in binary. So it puts that into the first four bits of return value. It then puts 0001 into the next four bits. The rest of the numbers are 0 so it is supposed to ignore them. Then the output of the function getValue is directly printed out. The actual output is at the bottom.
My code:
#include <iostream>
class Num {
char len = 4;
int num[4];
public:
void setValue(int);
int getValue();
};
void Num::setValue(int toSet)
{
char len1=len-1;
for (int counter = len1;counter>=0;counter--)
{
if(toSet&(0xF<<(len1-counter))!=0)
{
num[counter]=(toSet&(0xF<<(len1-counter)))>>len1-counter;
} else {
break;
}
}
}
int Num::getValue()
{
char len1 = len-1;
int returnValue = 0;
for(char counter = len1; counter>=0;counter--)
{
if (num[counter]!=0) {
returnValue+=(num[counter]<<(len1-counter));
} else {
break;
}
}
return returnValue;
}
int main()
{
int x=260;
Num number;
while (x>0)
{
number.setValue(x);
std::cout<<x<<"Test: "<<number.getValue()<<std::endl;
x--;
}
std::cin>>x;
return 0;
}
Output:
260Test: -1748023676
259Test: 5
258Test: 5
257Test: 1
256Test: 1
255Test: 225
254Test: 225
253Test: 221
252Test: 221
251Test: 213
250Test: 213
249Test: 209
248Test: 209
247Test: 193
246Test: 193
245Test: 189
244Test: 189
243Test: 181
242Test: 181
241Test: 177
240Test: 177
239Test: 177
238Test: 177
237Test: 173
236Test: 173
235Test: 165
234Test: 165
233Test: 161
232Test: 161
231Test: 145
230Test: 145
229Test: 141
228Test: 141
227Test: 133
226Test: 133
225Test: 1
224Test: 1
223Test: 161
222Test: 161
221Test: 157
220Test: 157
219Test: 149
218Test: 149
217Test: 145
216Test: 145
215Test: 129
214Test: 129
213Test: 125
212Test: 125
211Test: 117
210Test: 117
209Test: 113
208Test: 113
207Test: 113
206Test: 113
205Test: 109
204Test: 109
203Test: 101
202Test: 101
201Test: 97
200Test: 97
199Test: 81
198Test: 81
197Test: 77
196Test: 77
195Test: 5
194Test: 5
193Test: 1
192Test: 1
191Test: 161
190Test: 161
189Test: 157
188Test: 157
187Test: 149
186Test: 149
185Test: 145
184Test: 145
183Test: 129
182Test: 129
181Test: 125
180Test: 125
179Test: 117
178Test: 117
177Test: 113
176Test: 113
175Test: 113
174Test: 113
173Test: 109
172Test: 109
171Test: 101
170Test: 101
169Test: 97
168Test: 97
167Test: 81
166Test: 81
165Test: 77
164Test: 77
163Test: 69
162Test: 69
161Test: 1
160Test: 1
159Test: 97
158Test: 97
157Test: 93
156Test: 93
155Test: 85
154Test: 85
153Test: 81
152Test: 81
151Test: 65
150Test: 65
149Test: 61
148Test: 61
147Test: 53
146Test: 53
145Test: 49
144Test: 49
143Test: 49
142Test: 49
141Test: 45
140Test: 45
139Test: 37
138Test: 37
137Test: 33
136Test: 33
135Test: 17
134Test: 17
133Test: 13
132Test: 13
131Test: 5
130Test: 5
129Test: 1
128Test: 1
127Test: 225
126Test: 225
125Test: 221
124Test: 221
123Test: 213
122Test: 213
121Test: 209
120Test: 209
119Test: 193
118Test: 193
117Test: 189
116Test: 189
115Test: 181
114Test: 181
113Test: 177
112Test: 177
111Test: 177
110Test: 177
109Test: 173
108Test: 173
107Test: 165
106Test: 165
105Test: 161
104Test: 161
103Test: 145
102Test: 145
101Test: 141
100Test: 141
99Test: 133
98Test: 133
97Test: 1
96Test: 1
95Test: 161
94Test: 161
93Test: 157
92Test: 157
91Test: 149
90Test: 149
89Test: 145
88Test: 145
87Test: 129
86Test: 129
85Test: 125
84Test: 125
83Test: 117
82Test: 117
81Test: 113
80Test: 113
79Test: 113
78Test: 113
77Test: 109
76Test: 109
75Test: 101
74Test: 101
73Test: 97
72Test: 97
71Test: 81
70Test: 81
69Test: 77
68Test: 77
67Test: 5
66Test: 5
65Test: 1
64Test: 1
63Test: 161
62Test: 161
61Test: 157
60Test: 157
59Test: 149
58Test: 149
57Test: 145
56Test: 145
55Test: 129
54Test: 129
53Test: 125
52Test: 125
51Test: 117
50Test: 117
49Test: 113
48Test: 113
47Test: 113
46Test: 113
45Test: 109
44Test: 109
43Test: 101
42Test: 101
41Test: 97
40Test: 97
39Test: 81
38Test: 81
37Test: 77
36Test: 77
35Test: 69
34Test: 69
33Test: 1
32Test: 1
31Test: 97
30Test: 97
29Test: 93
28Test: 93
27Test: 85
26Test: 85
25Test: 81
24Test: 81
23Test: 65
22Test: 65
21Test: 61
20Test: 61
19Test: 53
18Test: 53
17Test: 49
16Test: 49
15Test: 49
14Test: 49
13Test: 45
12Test: 45
11Test: 37
10Test: 37
9Test: 33
8Test: 33
7Test: 17
6Test: 17
5Test: 13
4Test: 13
3Test: 5
2Test: 5
1Test: 1
I compiled this with g++ 6.3.0 with the command g++ a.cpp -o a.exe
When compiling with -Wall, there are a number of warnings:
orig.cpp: In member function ‘void Num::setValue(int)’:
orig.cpp:15:39: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
if(toSet&(0xF<<(len1-counter))!=0)
~~~~~~~~~~~~~~~~~~~~~^~~
orig.cpp:17:61: warning: suggest parentheses around ‘-’ inside ‘>>’ [-Wparentheses]
num[counter]=(toSet&(0xF<<(len1-counter)))>>len1-counter;
~~~~^~~~~~~~
orig.cpp: In member function ‘int Num::getValue()’:
orig.cpp:30:24: warning: array subscript has type ‘char’ [-Wchar-subscripts]
if (num[counter]!=0) {
^
orig.cpp:31:38: warning: array subscript has type ‘char’ [-Wchar-subscripts]
returnValue+=(num[counter]<<(len1-counter));
^
If you were to print the values of num before changing them, you'd see that some might be non-zero (i.e. they are uninitialized), which causes undefined behavior and probably breaks your for loops in getValue and setValue.
So change:
int num[4];
Into:
int num[4] = { 0 };
Here's a cleaned up version with the warnings fixed:
#include <iostream>
class Num {
int len = 4;
int num[4] = { 0 };
public:
void setValue(int);
int getValue();
void showval();
};
void Num::setValue(int toSet)
{
int len1=len-1;
for (int counter = len1;counter>=0;counter--)
{
if ((toSet & (0xF << (len1-counter))) != 0)
{
num[counter] = (toSet & (0xF << (len1-counter))) >> (len1-counter);
} else {
break;
}
}
}
int Num::getValue()
{
int len1 = len-1;
int returnValue = 0;
for(int counter = len1; counter>=0;counter--)
{
if (num[counter]!=0) {
returnValue+=(num[counter]<<(len1-counter));
} else {
break;
}
}
return returnValue;
}
void Num::showval()
{
for (int i = 0; i < len; ++i)
std::cout << i << ": show: " << num[i] << "\n";
#if 0
for (int i = 0; i < len; ++i)
num[i] = 0;
#endif
}
int main()
{
int x=260;
Num number;
number.showval();
while (x>0)
{
number.setValue(x);
std::cout << x << " Test: " << number.getValue() << std::endl;
x--;
}
std::cin>>x;
return 0;
}
To break a number into nibbles, the shift counts should be multiples of 4. Otherwise slices of 4 bits are extracted that don't line up.
00010101 (21)
^^^^ first nibble
^^^^ second nibble
The second nibble is displaced by 4 bits so it needs to be shifted right by 4, not by 1.
You could multiply your shift counts by 4, but there is an easier way: only ever shift by 4. For example:
for (int i = len - 1; i >= 0; i--) {
num[i] = toSet & 0xF;
toSet >>= 4;
}
Then every iteration extracts the lowest nibble in toSet, and shifts toSet over so that the next nibble becomes the lowest nibble.
I didn't put in a break and there should not be one. It definitely shouldn't be the kind of break that you had, which stops the loop also whenever a number has a zero in the middle of it (for example in 0x101 the middle 0 causes the loop to stop). The loop also should not stop when the entire rest of the number is zero, since that leaves junk in the other entries of num.
It's more common to store the lowest nibble in the 0th element and so on (then you don't have to deal with all the "reverse logic" with down-counting loops and subtracting things from the length) but that's up to you.
Extracting the value can be done symmetrically, building up the result while shifting it, instead of shifting every piece into its final place immediately. Or just multiply (len1-counter) by 4. While extracting the value, you also cannot stop when num[i] is zero, since that does not prove that the rest of the number is zero too.

Prim's algorithm not working on a certain implementation

I have two similar C++ implementations of Prim's algorithm for a minimum spanning tree, but one of them works in every situation, while the other fails in some. The difference is the method of comparison used within the priority queue.
This is the one that fails in a few situations (it uses a struct function in order to compare the cost of given arches):
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>``
#include <fstream>
using namespace std;
#define NMax 200005
#define oo (1 << 30)
ifstream fin("apm.in");
ofstream fout("apm.out");
int T[NMax], n, m, C[NMax];
bool inmst[NMax];
struct comp{
bool operator()(int x, int y)
{
return C[x] > C[y];
}
};
vector <pair <int, int> > G[NMax];
priority_queue<int, vector <int>, comp> pq;
void Prim()
{
for(int i = 1; i <= n; i++)
C[i] = oo;
pq.push(1);
C[1] = 0;
while(!pq.empty())
{
int nod = pq.top();
pq.pop();
inmst[nod] = 1;
for(int i = 0; i < G[nod].size(); i++)
{
int v = G[nod][i].first;
int c = G[nod][i].second;
if(!inmst[v] && C[v] >= c)
{
C[v] = c;
pq.push(v);
T[v] = nod;
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(x, c));
}
Prim();
int ct = 0;
for(int i = 1; i <= n; i++)
ct += C[i];
fout << ct << "\n" << n-1 << "\n";
for(int i = 1; i <= n; i++)
{
if(T[i] == 0) continue;
fout << i << " " << T[i] << "\n";
}
}
And this is the one that succeeds every time (it uses the greater within the priority queue and stores both the cost associated with a node and the node itself within the queue):
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
#include <fstream>
using namespace std;
#define NMax 200005
#define oo (1 << 30)
typedef pair<int, int> iPair;
ifstream fin("apm.in");
ofstream fout("apm.out");
int T[NMax], n, m, C[NMax];
bool inmst[NMax];
struct comp{
bool operator()(int x, int y)
{
return C[x] < C[y];
}
};
vector <pair <int, int> > G[NMax];
priority_queue< pair<int, int>, vector <pair<int, int> > , greater<pair<int, int> > > pq;
void Prim()
{
for(int i = 1; i <= n; i++)
C[i] = oo;
pq.push((make_pair(0, 1)));
C[1] = 0;
while(!pq.empty())
{
int nod = pq.top().second;
pq.pop();
inmst[nod] = 1;
for(int i = 0; i < G[nod].size(); i++)
{
int v = G[nod][i].first;
int c = G[nod][i].second;
if(!inmst[v] && C[v] > c)
{
C[v] = c;
pq.push(make_pair(C[v], v));
T[v] = nod;
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(x, c));
}
Prim();
int ct = 0;
for(int i = 1; i <= n; i++)
ct += C[i];
fout << ct << "\n" << n-1 << "\n";
for(int i = 1; i <= n; i++)
{
if(T[i] == 0) continue;
fout << i << " " << T[i] << "\n";
}
}
Why doesn't the first one work as intended every time and how would I go about changing it while still using the struct function and the code basis I already have? I tried a similar implementation to the one I use for Dijkstra's algorithm.
The program reads the number of nodes and arches and all the given arches from a file and it's supposed to show the minimum cost of the spanning tree, the number of arches and the arches in any order.
The examples I have that fail for the 1st implementation are of pretty abnormal sizes, I don't know if they will be of any help:
apm.in:
164 531
155 74 113
73 15 817
38 87 -153
92 100 699
125 114 -210
5 50 -29
1 4 335
11 39 846
112 59 -745
157 86 -146
92 47 397
146 48 -614
81 123 539
8 36 -799
39 17 175
55 36 -133
14 129 809
134 107 948
153 97 -428
144 73 -975
31 41 293
76 95 -550
111 96 -906
132 54 35
19 36 -627
142 31 -949
127 52 -363
44 17 -913
141 69 36
163 2 -610
17 12 -187
65 41 941
160 39 931
69 127 966
98 128 528
144 145 354
49 86 -201
7 102 569
113 31 -151
155 139 752
13 94 -659
140 69 672
32 8 -945
72 19 -592
104 68 -631
5 87 -760
37 58 -211
32 74 134
88 60 622
128 77 -480
4 20 151
38 66 823
133 41 886
128 58 -522
115 68 403
62 128 379
24 156 3
152 22 -630
126 160 322
61 16 852
57 69 -650
126 99 371
87 54 -659
101 93 -44
4 22 899
128 16 222
96 132 -523
67 74 993
143 110 -346
128 9 -892
146 86 -773
14 8 306
114 156 116
91 77 -581
44 95 978
89 38 609
147 139 -486
67 34 -952
119 36 829
44 90 653
1 164 -197
77 157 -273
7 130 606
3 143 701
37 91 363
44 18 -814
53 34 -170
124 39 -384
38 80 -51
72 4 30
133 59 -53
121 10 676
20 142 967
159 84 -456
118 110 75
68 75 828
154 91 -83
94 128 -740
22 72 269
1 81 -371
58 154 98
73 146 -240
11 6 -254
80 62 -70
72 156 611
119 118 990
109 23 -67
103 97 -281
6 135 388
116 159 -948
1 50 -551
31 60 -577
52 47 -514
164 91 562
113 112 178
45 106 -667
160 22 952
17 156 748
125 34 36
94 93 440
139 117 890
43 144 -179
91 60 346
85 96 853
118 156 -991
67 107 718
60 89 649
17 28 -105
64 47 666
26 136 -174
31 147 835
79 115 -235
106 66 693
8 74 107
162 136 712
83 116 506
82 139 489
110 114 -299
96 69 -94
19 129 -361
30 129 612
122 91 -571
62 2 -318
38 143 662
117 142 -34
91 124 385
112 50 -460
14 81 405
129 145 227
71 106 -774
27 15 -339
157 85 251
146 84 726
127 46 -711
95 134 175
134 51 103
31 154 661
95 150 56
164 27 -845
31 127 -452
46 29 -264
74 102 -477
72 39 320
89 23 -811
12 83 -672
68 136 194
82 90 -326
52 81 819
109 47 -204
148 24 870
146 141 148
83 158 -198
77 9 -351
64 25 575
114 28 -992
29 139 630
21 130 -979
153 104 611
80 162 217
155 74 768
2 51 -643
75 97 -706
124 62 -69
36 135 -181
100 43 -163
146 129 54
126 106 799
15 155 907
125 153 658
85 51 702
112 95 80
149 84 -782
46 105 859
69 73 280
98 150 11
60 93 -997
82 134 -786
20 40 -166
134 90 -810
93 76 893
100 51 -161
43 92 314
162 28 525
121 141 -953
30 58 991
97 100 -890
54 23 314
63 92 -480
97 155 -736
29 136 -817
64 86 -424
86 53 613
96 38 -4
57 65 -789
8 6 -586
18 145 -959
136 19 -497
74 86 -895
7 155 -281
43 6 -814
43 69 361
134 141 257
67 46 -326
111 73 -249
11 13 -581
98 143 -909
133 78 -551
83 25 325
93 162 -503
138 31 -386
161 92 135
84 79 -80
12 108 41
86 158 109
71 3 334
27 62 -517
59 15 613
46 63 -316
74 39 996
37 11 -438
38 102 369
15 129 313
131 6 -841
152 109 -529
124 53 878
35 78 -994
116 117 -312
47 109 -12
47 36 534
84 63 -626
152 142 667
87 79 -201
52 22 -518
99 103 -434
139 53 262
162 1 -524
120 69 836
131 93 368
155 135 5
133 117 -818
59 15 854
37 139 931
111 8 -15
92 58 411
126 19 789
111 12 763
66 147 605
20 142 -446
156 163 -566
137 36 -350
127 156 -307
49 22 376
124 24 -863
36 161 175
63 27 -687
5 29 -375
27 69 -260
24 149 447
141 117 32
58 64 239
87 149 -919
65 101 -807
127 117 237
145 44 -731
32 74 535
142 13 -869
49 5 315
152 105 -747
10 133 -232
69 98 -253
134 86 458
129 48 -322
121 132 46
64 15 -216
103 38 -530
63 17 -871
69 38 -577
55 106 141
111 90 583
138 100 -478
67 43 -608
141 88 921
45 55 596
155 91 338
128 3 532
28 124 -839
134 101 240
36 41 -888
3 156 -139
79 151 446
41 150 978
160 3 70
5 45 -597
89 103 -849
123 56 -820
16 123 779
114 89 -534
160 147 -359
52 140 239
142 80 66
124 66 -18
89 23 945
162 108 -353
151 143 -912
68 121 656
23 122 730
17 8 -81
87 130 44
116 158 -234
16 123 430
66 130 -398
29 65 -844
105 43 -854
84 99 -730
107 162 729
128 153 175
146 13 538
135 43 -753
93 123 996
23 3 -436
45 133 -203
151 132 931
49 159 -358
147 120 -274
132 131 -192
94 141 -758
19 153 946
155 33 284
85 18 646
69 148 -720
142 125 965
80 63 -96
29 140 -129
116 50 -111
38 124 -750
156 102 -674
39 67 -459
50 150 -261
110 29 904
11 83 -520
58 65 449
34 144 -362
103 76 567
97 85 322
151 76 118
61 58 -636
7 143 535
61 26 40
10 57 -155
120 33 -871
28 53 -176
57 86 -602
161 92 -96
69 151 555
49 17 876
43 71 -91
47 118 -191
70 49 576
102 139 -920
60 153 873
80 124 222
30 20 -147
37 158 587
65 9 314
46 69 957
117 16 -831
74 106 -756
95 92 -222
52 147 310
2 61 -427
138 21 -256
113 94 273
162 105 -53
40 80 572
21 143 386
154 115 229
97 126 362
106 40 -164
60 100 -405
29 109 -506
133 35 -867
114 70 -169
132 46 -145
161 133 434
94 47 -939
86 91 543
110 155 -277
20 162 -683
32 145 304
135 124 -23
5 123 283
11 65 -858
31 128 223
54 97 585
8 93 688
91 58 -974
123 97 931
140 61 822
19 13 69
161 162 976
126 115 887
47 84 -502
12 157 -824
135 98 420
114 6 -605
66 138 -354
70 138 -596
56 87 145
37 159 -207
78 136 -573
49 57 271
154 14 859
103 139 -848
32 105 -927
163 126 -935
49 19 262
14 108 408
164 145 -612
37 135 -454
68 20 892
78 164 470
24 109 -598
70 53 -403
71 148 -983
92 46 851
26 34 -602
88 47 -667
66 59 750
142 99 319
43 57 967
109 17 654
85 108 -947
74 33 694
150 83 -845
129 7 -37
65 70 -668
4 102 771
158 69 -615
85 148 225
12 55 172
7 41 -83
123 103 937
110 137 -36
42 131 -392
153 18 -844
86 94 -404
77 44 -307
18 114 -372
75 115 99
53 4 -472
140 159 -844
46 67 -991
84 162 599
91 106 406
149 108 -811
142 22 -653
136 44 496
131 43 -2
122 37 375
103 109 -692
26 58 -822
84 128 -967
71 53 -162
74 18 -123
145 103 722
65 99 -961
56 15 -908
44 147 176
41 133 -145
96 63 915
35 20 -106
122 73 -845
11 150 917
5 105 -361
119 44 -261
27 76 215
129 11 -102
157 140 338
160 120 958
28 131 -194
36 106 -468
130 52 679
16 76 -514
151 156 236
2 137 -335
112 80 724
35 78 -535
44 121 119
62 59 -379
110 141 780
52 37 342
29 33 -49
89 7 828
87 131 171
44 130 419
86 45 438
103 128 558
24 129 -565
86 99 -758
51 110 -571
117 94 476
61 78 -875
127 101 -359
146 40 -871
87 48 -600
97 149 -165
3 58 507
88 37 -764
51 43 918
41 76 10
13 88 -835
apm.ok:
-105854
163
60 93
35 78
114 28
46 67
118 156
71 148
21 130
144 73
91 58
84 128
65 99
18 145
121 141
67 34
142 31
116 159
85 108
32 8
94 47
163 126
32 105
102 139
87 149
44 17
151 143
98 143
56 15
111 96
74 86
128 9
97 100
36 41
61 78
120 33
63 17
146 40
142 13
133 35
124 24
11 65
105 43
89 103
103 139
150 83
164 27
122 73
140 159
153 18
29 65
131 6
28 124
13 88
117 16
12 157
26 58
123 56
133 117
29 136
43 6
44 18
89 23
149 108
134 90
65 101
8 36
57 65
82 134
149 84
71 106
146 86
88 37
5 87
94 141
86 99
74 106
135 43
38 124
152 105
112 59
94 128
97 155
84 99
69 148
127 46
75 97
103 109
63 27
20 162
156 102
12 83
65 70
45 106
88 47
87 54
142 22
2 51
61 58
104 68
152 22
19 36
84 63
158 69
146 48
163 2
67 43
114 6
26 34
24 109
70 138
72 19
91 77
31 60
122 91
51 110
156 163
24 129
1 50
76 95
162 1
96 132
11 83
52 22
27 62
16 76
93 162
147 139
63 92
138 100
53 4
112 50
39 67
159 84
23 3
64 86
70 53
66 130
42 131
1 81
160 147
49 159
66 138
137 36
143 110
27 15
7 155
147 120
119 44
111 73
79 115
10 133
125 114
87 79
113 31
30 20
55 36
80 63
161 92
154 91
134 51
68 136
14 8
83 25
67 107
apm.out:
-105439
163
2 163
3 23
4 53
5 87
6 43
7 155
8 32
9 128
10 133
11 13
12 83
13 142
14 8
15 27
16 117
17 63
18 44
19 36
20 162
21 130
22 142
23 89
24 124
25 83
26 58
27 63
28 114
29 65
30 20
31 60
32 105
33 120
34 67
35 78
36 8
37 88
38 69
39 67
40 146
41 36
42 131
43 105
44 17
45 106
46 67
47 88
48 146
49 159
50 1
51 2
52 22
53 70
54 87
55 36
56 15
57 65
58 61
59 112
60 93
61 78
62 27
63 84
64 86
65 11
66 138
67 43
68 136
69 148
70 65
71 106
72 19
73 122
74 86
75 97
76 16
77 91
78 136
79 87
80 63
81 1
82 134
83 11
84 128
85 108
86 99
87 149
88 13
89 114
90 134
91 58
92 63
93 162
94 47
95 76
96 111
97 100
98 143
99 65
100 138
101 65
102 139
103 89
104 68
105 152
106 74
107 67
108 149
109 103
110 51
111 73
112 50
113 31
114 6
115 79
116 159
117 133
118 156
119 44
120 147
121 141
122 91
123 56
124 28
125 114
126 163
127 46
128 94
129 24
130 66
131 6
132 96
133 35
134 51
135 43
136 29
137 36
138 70
139 103
140 159
141 94
142 31
143 110
144 73
145 18
146 86
147 139
148 71
149 84
150 83
151 143
152 22
153 18
154 91
155 97
156 102
157 12
158 69
159 84
160 147
161 92
162 1
163 156
164 27
Every times you pop a node u from priority_queue, you need to update C[v] with v is adjacent node with u. These v may be already in the queue, so technically, you indirectly update the value of some elements in priority_queue (since value of an element is expressed via C[]). It will break the structure of the queue and lead to incorrect answer.
In the correct version, you push 2 int to queue, and these values never change. Therefore you do not get the above problem.
Looking through the exposed sample codes, I got a suspicion about an essential difference of comparison behaviors. To prove that suspicion, I put both versions of the sample codes into WinMerge and compared them. They look really similar. So, I'm quite sure I'm on the right track:
The first version uses this for comparison:
struct comp{
bool operator()(int x, int y)
{
return C[x] > C[y];
}
};
The second version uses this instead:
std::greater<pair<int, int> >
The big difference: std::greater considers both members of std::pair in comparison but the comparison function in first sample only C[x] and C[y] (but not x and y).
According to cppreference.com
template< class T1, class T2 >
constexpr bool operator>(const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
is defined as rhs < lhs and
template< class T1, class T2 >
constexpr bool operator<(const pair<T1,T2>& lhs, const pair<T1,T2>& rhs);
does
If lhs.first<rhs.first, returns true. Otherwise, if rhs.first<lhs.first, returns false. Otherwise, if lhs.second<rhs.second, returns true. Otherwise, returns false.
So, if the first comparison shall resemble the second, the comparison functor of first sample has to be changed to e.g.:
struct comp{
bool operator()(int x, int y)
{
return x == y ? C[x] > C[y] : x > y;
}
};
That looks a bit stupid as x == y surely results in C[x] == C[y]. However, it should return the exact same results as the std::greater<std::pair<int, int> > for the second sample code.
Concerning the original problem of the OP, the first version may actually be the correct one (and there is yet another mistake or a wrong expectation about output). In this case, the comparison of second sample had to be adjusted, e.g. introducing a resp. functor for std::pair:
struct comp{
bool operator()(
const std::pair<int, int> &xCx, const std::pair<int, int> &yCy) const
{
return xCx.second > yCy.second;
}
};
As I do not know Prim's algorithm, I cannot decide which one is the actually correct solution. The OP might leave a note regarding this (and I'm would be glad to edit this in).

call and print function which return array in c++

I have this functions which return a random array in c++:
int* randomArray(int countOfRows){
int test1 [countOfRows] = {};
int insertValue;
int check;
for (int n=0; n < countOfRows; ++n){
srand(time (NULL) );
while (test1[n] == NULL){
insertValue = (rand () %100 + 1 );
for(int i = 0; i < countOfRows; i++){
if (test1[i] == insertValue){
check = 1;
break;
}
else{
check = 0;
}
}
if (check == 0){
test1[n] = insertValue;
}
}
}
return test1;
}
How can I call that array?
what is the difference between int* and int[]
thank you :)
Your code has four significant problems, one of them critical, one non-standard and implementation dependent, and two general algorithmic problems.
First, the most important, you're returning the address of an automatic variable, which means it is both useless and will invoke undefined behavior to dereference by the caller. Declared at the top of your function is:
int test1 [countOfRows] = {};
which itself brings up the second point, this non-standard for two reasons: variable-length arrays are not supported by the C++ standard, and by inference, initialization of said-same is likewise not supported. Then later...
return test1;
The caller of your function will receive an address, but that address is useless. It no longer addresses anything concrete, as test1 no longer exists once the function returns. This is remedied a number of ways, and considering this is C++, the easiest is using a std::vector<int>, which supports value-return.
The two significant algorithm problems are
Your seeding of srand should not be in the for loop. In fact, if you're using srand and rand, the seeding should be done once in your entire process.
The process of exhaustive searching to see if a current random pick has already been used to avoid duplicates is needless if you simply use a different algorithm, which I'll cover later.
Therefore, the simplest fix for your code will be to do this:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
std::vector<int> randomArray(int countOfRows)
{
std::vector<int> test1(countOfRows);
int check = 0;
for (int n=0; n < countOfRows; ++n)
{
while (test1[n] == 0)
{
int insertValue = (rand () %100 + 1 );
for(int i = 0; i < countOfRows; i++)
{
if (test1[i] == insertValue){
check = 1;
break;
}
else{
check = 0;
}
}
if (check == 0){
test1[n] = insertValue;
}
}
}
return test1;
}
int main()
{
std::srand(static_cast<unsigned>(std::time(NULL)));
std::vector<int> vec = randomArray(20);
for (auto x : vec)
std::cout << x << ' ';
std::cout.put('\n');
}
Output (varies, obviously)
8 50 74 59 31 73 45 79 24 10 41 66 93 43 88 4 28 30 13 70
A Finite Set Algorithm
What you're really trying to generate here is a finite set of integers in the range of 1..100. I.e., there are no duplicate values used, and the number of items being returned could be anything from 1..100 as well. To do this, consider this algorithm:
Generate a sequence of 1..100 in a std::vector<int>
Using a pseudorandom generator from the standard library, shuffle the sequence using std::shuffle
Resize the resulting vector to be the number of elements you want to return.
Regarding #3 from above, consider a small example, suppose you wanted just ten elements. Initially you build a sequence vector that looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13... ...99 100
Now you shuffle this vector using a std::shuffle and a pseudorandom generator like std::mt19937 : (the first twenty elements shown for brevity):
48 39 31 44 68 84 98 40 57 76 70 16 30 93 9 51 63 65 45 81...
Now, you simply resize the vector down to the size you want, in this case ten elements:
48 39 31 44 68 84 98 40 57
And that is your result. If this sounds complicated, you may be surprised to see how little code it actually takes:
Code
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <random>
std::vector<int> randomSequence100(std::size_t count)
{
if (count > 100)
count = 100;
static std::random_device rd;
std::vector<int> result(100);
std::iota(result.begin(), result.end(), 1);
std::shuffle(result.begin(), result.end(), std::mt19937(rd()));
result.resize(count);
return result;
}
int main()
{
// run twenty tests of random shuffles.
for (int i=0; i<20; ++i)
{
auto res = randomSequence100(20);
for (auto x : res)
std::cout << x << ' ';
std::cout.put('\n');
}
}
Output
27 71 58 6 74 65 56 37 53 44 25 91 10 86 51 75 31 79 18 46
6 61 92 74 30 20 91 89 64 55 19 12 28 13 5 80 62 71 29 43
92 42 2 1 78 89 65 39 37 64 96 20 62 33 6 12 85 34 29 19
46 63 8 44 42 80 70 2 68 56 86 84 45 85 91 33 20 83 16 93
100 99 4 20 47 32 58 57 11 35 39 43 87 55 77 51 80 7 46 83
48 39 31 44 68 84 98 40 57 76 70 16 30 93 9 51 63 65 45 81
32 73 97 83 56 49 39 29 3 59 45 89 43 78 61 5 57 51 82 8
21 46 25 29 48 37 77 74 32 56 87 91 94 86 57 67 33 9 23 36
27 46 66 40 1 72 41 64 53 26 31 77 42 38 81 47 58 73 4 11
79 77 46 48 70 82 62 87 8 97 51 99 53 43 47 91 98 81 64 26
27 55 28 12 49 5 70 94 77 29 84 23 52 3 25 56 18 45 74 48
95 33 25 80 81 53 55 11 70 2 38 77 65 13 27 48 40 57 87 93
70 95 66 84 15 87 94 43 73 1 13 89 44 96 10 58 39 2 23 72
43 53 93 7 95 6 19 89 37 71 26 4 17 39 30 79 54 44 60 98
63 26 92 64 83 84 30 19 12 71 95 4 81 18 42 38 87 45 62 70
78 80 95 64 71 17 14 57 54 37 51 26 12 16 56 6 98 45 92 85
89 73 2 15 43 65 21 55 14 27 67 31 54 52 25 72 41 6 85 33
4 87 19 95 78 97 27 13 15 49 3 17 47 10 84 48 37 2 94 81
15 98 77 64 99 68 34 79 95 48 49 4 59 32 17 24 36 53 75 56
78 46 20 30 29 35 87 53 84 61 65 85 54 94 68 75 43 91 95 52
Each row above was a set of twenty elements take from the sequence of 1..100. No single row has duplicates (check if you want).
Caveat
This technique works wonderfully for either small domains or large result sets from larger domains. But it has its limits to consider.
For example: Once your potential domain reaches the point significant size (say, numbers in 1...1000000) and you want only small result sets (say, no larger than 100 elements), you're better off using a std::unordered_set and iterative probing similar to what you're doing now. The technique you use depends entirely on your performance goals and your usage patterns.
Counterexample: If you wanted a half-million unique elements shuffled from a million-element domain, the load/shuffle/resize technique will work well.
Ultimately you have to decide, and measure to confirm.
Some useful links about some of the things used here (bookmark this site, as it is absolute gold for information about C++):
std::vector
std::iota
std::random_device
std::mt19937
std::shuffle
From my view this function has problems.
It return the point of test1, which is allocated in the stack, which is invalid out of the scope of randomArray.
So if you change to malloc, this is allocated in heap, then it still valid when out of the scope of randomArray.
int *test1 = (int*) malloc(countOfRows* sizeof(int));
And you can using test1[x] to get the value of each int, for sure you should know the length of test1 is countOfRows.
Please don't forget to delete this point when it is not used...
Call this array is simple
int* values = randomArray(1000);
printf("%d\r\n",values[0]);
In the function randomArray() declare test1[] as a static int[].
return the array using pointers,
" return test1 "
in the main function use a pointer to access the return value
" int *ptr=randomArray(n) "

Time Limit Exceeded in Codeforces solution. How can I improve my solution?

Problem-http://codeforces.com/contest/454/problem/B
Problem Synopsis- A given integer sequence is to be changed into ascending order by applying shifting operations(each element is shifted by 1 place to its right, the last element becomes the first). Find minimum number of operations to make the sequence ascending. Print -1 if it is not possible.
I'm getting a TIME_LIMIT_EXCEEDED for test case 6 in the above link. I know that there are more efficient solutions for this problem but if possible, I would like to make changes in my solution to make it run within the time limit. Would that be possible? If yes, how can I do that?
My solution:
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
typedef long long LL;
using namespace std;
bool checkAsc(vector<int> x, int N)
{
for(int i=0;i<(N-1);i++)
{
if(x[i+1]<x[i])
return false;
}
return true;
}
vector <int> shiftRight(vector <int> &x, int N)
{
int temp=x[N-1];
for(int i=(N-1);i>=1;i--)
{
x[i]=x[i-1];
}
x[0]=temp;
vector <int> y=x;
return y;
}
bool ifPossible(vector <int> x, int N)
{
for(int i=1;i<(N-1);i++)
{
if((x[i]>x[i-1])&&(x[i]>x[i+1])&&(x[i+1]>x[i-1]))
return false;
}
return true;
}
int main()
{
int n, turns=0;
vector <int> a(100000);
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
if(!ifPossible(a, n))
cout<<"-1";
else
{
while(!checkAsc(a, n))
{
shiftRight(a, n);
turns++;
}
cout<<turns;
}
return 0;
}
Checker's log:
Test: #6, time: 1000 ms., memory: 784 KB, exit code: -1, checker exit code: 0, verdict: TIME_LIMIT_EXCEEDED
Input
99998
99997 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 15...
First, fix your algorithm, for {3, 2, 1}, you have infinite loop (instead of -1).
From your code, pass vector by const ref instead of value to avoid useless copies.
your shiftRight may use vector.insert(v.back()); vector.pop_back() which is same complexity, but use some faster method (as memcpy); and change return type to void(as the result is not used) and so discard y and its copy.
But your algorithm is still O(N²) whereas it can be done in O(N)
int get_unicorn_shift(const std::vector<int>& v)
{
auto mid = std::is_sorted_until(v.begin(), v.end());
if (mid == v.end()) {
return 0;
}
auto end = std::is_sorted_until(mid, v.end());
if (end != v.end() || v.front() < v.back()) {
return -1;
}
return end - mid;
}
Live example