I am currently a student working on an insertion sort method.
Below is the code:
//Insertion Sorting of an Integer array
void InsertionSort(int insertVals[]){
//Systematic processing of the Array
for(int i = 0; i < INITSIZE - 1; i++){
//Value to check
int temp = insertVals[i];
//Index placeholder for the insterion sort
int k;
//Shifts the int array
for(k = i; k > 0 && insertVals[k-1] > temp; k--){
insertVals[k] = insertVals[k-1];
}
//Inserts the checked value back into the array
insertVals[k] = temp;
}
}
In my tests, I have given it the array from left to right:
307 249 73 158 430 272 44 378 423 209
440 165 492 42 487 3 327 229 340 112
303 169 209 157 60 433 99 278 316 335
97 326 12 267 310 133 479 149 79 321
467 172 393 336 485 245 228 91 194 357
1 153 208 444 168 490 124 196 30 403
222 166 49 24 301 353 477 408 228 433
298 481 135 13 365 314 63 36 425 169
115 94 129 1 17 195 105 404 451 298
188 123 5 382 252 66 216 337 438 144
The method produces from left to right:
314 63 314 63 36 425 36 169 425 169
115 115 94 129 94 129 1 17 195 105
404 451 298 188 123 5 382 252 66 216
337 438 144 1 17 195 105 404 451 298
188 123 5 382 252 66 216 337 438 144
228 229 245 249 252 267 272 278 298 298
301 303 307 310 314 316 321 326 327 335
336 337 340 353 357 365 378 382 393 403
404 408 423 425 430 433 433 438 440 444
451 467 477 479 481 485 487 490 492 144
What am I incorrectly coding?
Thanks!
EDIT:
//In main...
Printing(insertionSortValues, "Insertion Sorted Array");
//Function for Print
void Printing(int vals[], string s){
cout << s << ":" << endl;
for(int i = 0; i < INITSIZE; i++){
if(i % 10 == 0){
cout << endl;
}
cout << setw(3) << vals[i] << " ";
}
cout << endl;
}
The solution to this problem was solved through #PaulMcKenzie.
The line:
for(int i = 0; i < INITSIZE - 1; i++){
needed to become:
for(int i = 0; i <= INITSIZE - 1; i++){
Below is the corrected function.
//Insertion Sorting of an Integer array
void InsertionSort(int insertVals[]){
//Systematic processing of the Array
for(int i = 0; i <= INITSIZE - 1; i++){
//Value to check
int temp = insertVals[i];
//Index placeholder for the insterion sort
int k;
//Shifts the int array
for(k = i; k > 0 && insertVals[k-1] > temp; k--){
insertVals[k] = insertVals[k-1];
}
//Inserts the checked value back into the array
insertVals[k] = temp;
}
}
Related
I am working with CUDA and I am trying to stop my kernels work (i.e. terminate all running threads) after a certain if block is being hit. How can I do that? I am really stuck in here.
The CUDA execution model doesn't allow for inter-block communication by design. That can potentially make this sort of kernel abort on condition operation difficult to achieve reliably without resorting to the assert or trap type approaches which can potentially result in context destruction and loss of data which isn't what you probably want.
If your kernel design involves a small number of blocks with "resident" threads, then the only approach is some sort of atomic spinlock, which is hard to get to work reliably, and which will greatly degrade memory controller performance and achievable bandwidth.
If, on the other hand, your kernel design has rather large grids with a lot of blocks, and your main goal is to stop blocks which are not yet scheduled from running, then you could try something like this:
#include <iostream>
#include <vector>
__device__ unsigned int found_idx;
__global__ void setkernel(unsigned int *indata)
{
indata[115949] = 0xdeadbeef;
indata[119086] = 0xdeadbeef;
indata[60534] = 0xdeadbeef;
indata[37072] = 0xdeadbeef;
indata[163107] = 0xdeadbeef;
}
__global__ void searchkernel(unsigned int *indata, unsigned int *outdata)
{
if (found_idx > 0) {
return;
} else if (threadIdx.x == 0) {
outdata[blockIdx.x] = blockIdx.x;
};
unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x;
if (indata[tid] == 0xdeadbeef) {
unsigned int oldval = atomicCAS(&found_idx, 0, 1+tid);
}
}
int main()
{
const unsigned int N = 1 << 19;
unsigned int* in_data;
cudaMalloc((void **)&in_data, sizeof(unsigned int) * size_t(N));
cudaMemset(in_data, 0, sizeof(unsigned int) * size_t(N));
setkernel<<<1,1>>>(in_data);
cudaDeviceSynchronize();
unsigned int block_size = 1024;
unsigned int grid_size = N / block_size;
unsigned int* out_data;
cudaMalloc((void **)&out_data, sizeof(unsigned int) * size_t(grid_size));
cudaMemset(out_data, 0xf0, sizeof(unsigned int) * size_t(grid_size));
const unsigned int zero = 0;
cudaMemcpyToSymbol(found_idx, &zero, sizeof(unsigned int));
searchkernel<<<grid_size, block_size>>>(in_data, out_data);
std::vector<unsigned int> output(grid_size);
cudaMemcpy(&output[0], out_data, sizeof(unsigned int) * size_t(grid_size), cudaMemcpyDeviceToHost);
cudaDeviceReset();
std::cout << "The following blocks did not run" << std::endl;
for(int i=0, j=0; i<grid_size; i++) {
if (output[i] == 0xf0f0f0f0) {
std::cout << " " << i;
if (j++ == 20) {
std::cout << std::endl;
j = 0;
}
}
}
std::cout << std::endl;
return 0;
}
Here I have a simple kernel which is searching for a magic word in a large array. To get the early exit behaviour, I use a single global word, which is set atomically by those threads which "win" or trigger the termination condition. Every new block checks the state of this global word, and if it is set, they return without doing any work.
If I compile and run this on a moderate sized Kepler device:
$ nvcc -arch=sm_30 -o blocking blocking.cu
$ ./blocking
The following blocks did not run
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 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 251
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
504 505 506 507 508 509 510 511
you can see that a large number of blocks in the grid saw the change in the global word and early terminated without running the search code. This might be the best you can do without a severely invasive spinlock approach which will greatly harm performance.
I assume you want to stop a running kernel (not a single thread).
The simplest approach (and the one that I suggest) is to set up a global memory flag which is been tested by the kernel.
You can set the flag using cudaMemcpy() (or without if using unified memory).
Like the following:
if (gm_flag) {
__threadfence(); // ensure store issued before trap
asm("trap;"); // kill kernel with error
}
ams("trap;") will stop all running thread
Note that since cuda 2.0 you can use assert() to terminate a kernel!
A different approach could be the following (I haven't tried the code!)
__device__ bool go(int val){
return true;
}
__global__ void stopme(bool* flag, int* val, int size){
int idx= blockIdx.x *blockDim.x + threadIdx.x;
if(idx < size){
bool canContinue = true;
while(canContinue && (flag[0])){
printf("HELLO from %i\n",idx);
if(!(*flag)){
return;
}
else{
//do some computation
val[idx]++;
val[idx]%=100;
}
canContinue = go(val[idx]);
}
}
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
int main(void)
{
int size = 128;
int* h_val = (int*)malloc(sizeof(int)*size);
bool * h_flag = new bool;
*h_flag=true;
bool* d_flag;
cudaMalloc(&d_flag,sizeof(bool));
cudaMemcpy(d_flag,h_flag,1,cudaMemcpyHostToDevice);
int* d_val;
cudaMalloc(&d_val,sizeof(int)*size );
for(int i=0;i<size;i++){
h_val[i] = i;
}
cudaMemcpy(d_val,h_val,size,cudaMemcpyHostToDevice);
int BSIZE=32;
int nblocks =size/BSIZE;
printf("%i,%i",nblocks,BSIZE);
stopme<<<nblocks,BSIZE>>>(d_flag,d_val,size);
//--------------sleep for a while --------------------------
*h_flag=false;
cudaMemcpy(d_flag,h_flag,1,cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
gpuErrchk( cudaPeekAtLastError() );
printf("END\n");
}
where the kernel stopMe keeps running until someone from the host side sets up the flag to false. Note that your kernel could be much more complicated than this and the effort to synchronize all threads in order to execute the return could be much more than this (and can affect performance). Hope this helped.
More info here
I've written the code for eliminating the largest 2 elements of an array, but this code gives junk value for testcase > 1. Why?
Input:
no of TestCase
size of array
elements of array
Sorting function:
int sort_asc(int arr[], int n)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[i])
{
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
int main() {
//code
int test;
cin>>test;
while(test--){
//taking size and array as inputs
int size;
cin>>size;
int a[size];
cin>>a[size];
for(int i=0;i<size;i++){
cin>>a[i];
}
//sorting the array
sort_asc(a,size);
//printing the output discarding last 2 elements of the array
for(int i=0;i<size-2;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
}
return 0;
}
Expected:
12 23 28 43 44 59 60 68 70 85 88 92 124 125 136 168 171 173 179 199 212
230 277 282 306 314 316 325 328 336 337 363 365 368 369 371 374 387 394 414
422 427 430 435 457 493 506 527 531 538 541 546 568 583 650 691 730 737 751
764 778 783 785 789 794 803 809 815 847 858 863 874 887 896 916 920 926 927 930 957
My output:
12 23 28 43 44 59 60 68 70 81 85 88 92 124 125 136 168 171 173 179 199 212 230 277 282 306 314 316 325 328 336 337 363 365 368 369 371 374 387 394 414 422 427 430 435 457 493 506 527 531 538 541 546 568 583 650 691 730 737 751 764 778 783 785 789 794 803 809 815 847 858 863 874 887 896 916 920 926 930 957
A VLA (variable length array) is invalid C++ code. It is tolerated by some compilers, but it is still invalid.
But that is not your main problem. You produced an out of bound error. An array index starts with 0. The last element is at position size-1. So your statement
cin>>a[size];
will write past the end of your array. Producing undefined behavior.
I am not sure, why you put the statement at all, but after that, anything undefined can and most probably will happen.
I have an assignment in Codejudge which I write a command line program which reads a space separated list of integers from the command line and prints the ordered sublist consisting of the input prime numbers.
I tried numerous times but I can't seem to work
this is input argument:
9308 2034 9466 283 7949 1153 7241 5341 4693 6910 6852 5540 8015 9305 5697 1395 4727 9159 8661 1367 6096 2911 4797 8025 2593 5460 5767 5543 2429 8371 6024 2343 285 8657 9869 5388 5295 6279 3084 9573 6980 2362 1565 5134 5185 1991 7142 3699 5937 4151 3044 2468 8005 1603 662 2989 752 6971 3152 3681 9743 653 4542 719 2081 5772 9179 4034 5904 5494 1653 251 130 6646 2835 2260 8998 7464 112 2179 6592 8502 7381 5990 6681 8237 1331 537 2048 3342 9353 7883 1041 621 1022 4569 1421 9592 877 657 7097 2828 6242 2216 387 4605 8017 2784 4509 5818 7959 1612 491 6381 6530 5773 2220 2802 6478 7401 9084 1845 8805 8192 9806 6940 6578 9132 3144 8793 4854 1087 3238 8622 419 346 2598 1194 5766 4626 4740 6191 8639 7948 9833 3117 232 5839 8726 4863 4532 3498 6717 4874 3496 2951 5750 6982 1779 9614 9519 5980 3245 2698 6771
etc.
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
int main(int argc, char* argv[]) {
std::vector<int> input;
std::vector<int> output;
for (int a = 0; a < argc; a++) {
input.push_back(std::atoi(argv[a]));
}
int count = 0;
for (int i = 0; i < input.size(); i++) {
if (input.at(i) % 2 != 0 && (input.at(i) % 3 != 0 || input.at(i) / 3 == 1) && (input.at(i) % 5 != 0 || input.at(i) / 5 == 1) /*&& input.at(i)*input.at(i)% input.at(i)!=0*/) {
output.push_back(input.at(i));
count++;
}
}
sort(output.begin(), output.end());
for (int i = 0; i < count; i++) {
std::cout << output[i] << " ";
}
}
expected result:
1 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241
actual result:
1 3 5 7 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83 89 91 97 101 103 107 109 113 119 121 127 131 133 137 139 143 149 151 157 161 163 167 169 173 179 181 187 191 193 197 199 203 209 211 217 221 223 227 229 233 239 241
there are difference between the expected and the actual.
keep in mind that the vector of random numbers are in random orders and not from smallest to largest and they all are
for (int a = 0; a < argc; a++) {
input.push_back(std::atoi(argv[a]));
}
should be
for (int a = 1; a < argc; a++) {
input.push_back(std::atoi(argv[a]));
}
The first argument argv[0] is the program name.
I am working on a programme which takes an initial vector as an inpit.This vector is vector of size 20.The programme then generates 10 random vectors from this vector.For this purpose I choose 2 ransom indices in the initial vector and swap them with each other to generate a new vector.This is done to generate all the 10 new vectors.
The 10 new vectors generated should be stored in the following 2 dimensional vector
vector<vector<int>> allparents
I have been able to generate 2 random indice numbers using the srand() function and then swap the elements at these indices for the initial vector.However I am unable to generate 10 of such random parents and then store them in the allparents 2D vector.My code is as follows :
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<ctime>
using namespace std;
int main () {
srand(time(NULL));
int i1=(rand()%19+1);
int i2=(rand()%19+1);
cout<<i1<<endl;
cout<<i2<<endl;
vector<int> initial={71,127,428,475,164,253,229,395,92,189,41,110,443,490,278,305,28,58,371,560};
vector<vector<int>> allparents;
for(int r=0;r<10;r++){
for(int c=0;c<20;c++){
swap(initial[i1],initial[i2]);
allparents[r][c]=initial[c];
cout<<allparents[r][c]<" "<<endl;
}
}
return 0;
}
As I am new to vectors,I would request your help in this programme.Thanks.
First of all, I would not say that you're "generating random vectors", you're just shuffling a predefined one.
Second, I suggest to create small working functions and assemble your program with those:
vector<int> shuffle(vector<int> v) {
// Use the implementation you want here, I will use a std algorithm
static auto rng = std::default_random_engine {};
std::shuffle(std::begin(v), std::end(v), rng);
return v; // this is a copy of the vector
}
int main() {
vector<int> initial= {
71,127,428,475,164,253,229,395,92,189,41,110,443,490,278,305,28,58,371,560
};
// Generate 10 shuffled vectors
vector<vector<int>> shuffledVectors;
for (int i = 0; i < 10; i++) {
vector<int> shuffled = shuffle(initial);
shuffledVectors.push_back(shuffled);
}
// Print them
for (vector<int>& v : shuffledVectors) {
for (int& i : v)
cout << i << " ";
cout << endl;
}
return 0;
}
Output:
164 41 110 305 278 28 58 127 229 189 475 395 560 428 71 443 253 371 490 92
490 71 305 58 428 127 28 110 92 443 189 229 278 475 371 395 560 41 253 164
395 278 560 490 28 164 71 229 58 41 428 305 127 253 475 371 92 189 110 443
164 475 92 253 229 189 127 560 71 58 41 443 428 395 371 490 110 278 28 305
443 253 428 110 278 71 475 127 58 41 371 229 305 189 395 164 28 490 92 560
560 28 58 71 229 41 490 475 189 443 253 395 305 164 371 278 428 92 110 127
395 443 371 58 253 305 92 127 475 110 428 229 189 41 164 278 71 560 28 490
278 189 71 127 443 110 28 428 305 560 371 58 229 253 395 164 41 490 475 92
28 395 92 443 560 278 371 71 58 305 475 253 428 490 229 189 164 110 41 127
443 71 428 229 127 278 490 58 475 253 164 110 92 189 395 560 305 41 28 371
Apologies for the long code. This is as far as I could reduce it.
#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include <QtGui/QImage>
#include <QtGui/QPainter>
#include <vector>
using namespace std;
class View : public QWidget {
typedef pair<double, double> Point;
unsigned char* _buffer;
double centerx, centery, scale;
double xmin, xmax, ymin, ymax;
double xprec, yprec;
double xratio, yratio;
double fwidth, fheight;
double xlen, ylen;
int width;
int height;
public:
View(int w, int h) : width(w), height(h) {
_buffer = new unsigned char[4 * w * h];
fwidth = static_cast<double>(width);
fheight = static_cast<double>(height);
double aspectRatio = fwidth / fheight;
centerx = 0;
centery = 0;
scale = 2.3;
xlen = aspectRatio * scale;
ylen = 1.0 * scale;
xmin = -(xlen * 0.5) + centerx;
xmax = -xmin;
ymin = -(ylen * 0.5) + centery;
ymax = -ymin;
xprec = xlen / fwidth;
yprec = ylen / fheight;
xratio = fwidth / scale / aspectRatio;
yratio = fheight / scale;
}
double roundX(double x) { return std::floor(x / xprec) * xprec; }
double roundY(double y) { return std::floor(y / yprec) * yprec; }
protected:
void paintEvent(QPaintEvent* event) {
QPainter painter(this);
render();
painter.drawImage(
QPoint(0, 0),
QImage(_buffer, width, height, QImage::Format_RGB32));
}
private:
void render() {
memset(_buffer, 0, 4 * width * height);
for (double i = xmin; i < xmax; i += xprec) {
for (double j = ymin; j < ymax; j += yprec) {
Point p(roundX(i), roundY(j));
int x = static_cast<int>((p.first * xratio) - (xmin * xratio) );
int y = static_cast<int>((p.second * yratio) - (ymin * yratio) );
_buffer[4 * (x * width + y) ] = 255;
_buffer[4 * (x * width + y) + 1] = 255;
_buffer[4 * (x * width + y) + 2] = 255;
}
}
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
View view(512, 512);
view.show();
return app.exec();
}
The code, instead of producing a white window, produces a white window with lines which are the result of round-off error. I think the source of the problem are roundX() and roundY() functions, but I'm not sure. I also don't know how to fix this. Any ideas?
I don't have Qt and I prefer C. So, below is a C program that imitates your render() in render1() and has a corrected implementation in render2().
The main problem of your render() is that it uses prematurely rounded values from Point p(roundX(i), roundY(j));. If you use unrounded i and j, things will improve dramatically, but you're still going to suffer from the rounding errors of your additions, multiplications and so on.
But those rounding errors aren't large and they don't accumulate to a large error in the end. So, the final correction is the addition of .5 prior to conversion to the integers x and y. Without it, the floating point value can be slightly less than the nearest integer that you want to get and when you convert this double into int, you get a value that is one less.
#include <stdio.h>
#include <string.h>
#include <math.h>
double centerx, centery, scale;
double xmin, xmax, ymin, ymax;
double xprec, yprec;
double xratio, yratio;
double fwidth, fheight;
double xlen, ylen;
int width;
int height;
void InitView(int w, int h)
{
double aspectRatio;
width = w; height = h;
// _buffer = new unsigned char[4 * w * h];
fwidth = width;
fheight = height;
aspectRatio = fwidth / fheight;
centerx = 0;
centery = 0;
scale = 2.3;
xlen = aspectRatio * scale;
ylen = 1.0 * scale;
xmin = -(xlen * 0.5) + centerx;
xmax = -xmin;
ymin = -(ylen * 0.5) + centery;
ymax = -ymin;
xprec = xlen / fwidth;
yprec = ylen / fheight;
xratio = fwidth / scale / aspectRatio;
yratio = fheight / scale;
}
double roundX(double x) { return floor(x / xprec) * xprec; }
double roundY(double y) { return floor(y / yprec) * yprec; }
void render1(void)
{
double i, j;
int cnt;
char usedx[1 + 512 + 1];
char usedy[1 + 512 + 1];
printf("render1():\n");
memset(usedx, 0, sizeof usedx);
memset(usedy, 0, sizeof usedy);
printf("x's:\n");
for (cnt = 0, i = xmin; i < xmax; i += xprec)
{
int x = ((roundX(i) * xratio) - (xmin * xratio));
printf("%d ", x);
cnt++;
usedx[1 + x] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedx[cnt])
printf("missing x: %d\n", cnt - 1);
printf("y's:\n");
for (cnt = 0, j = ymin; j < ymax; j += yprec)
{
int y = ((roundY(j) * yratio) - (ymin * yratio));
printf("%d ", y);
cnt++;
usedy[1 + y] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedy[cnt])
printf("missing y: %d\n", cnt - 1);
}
void render2(void)
{
double i, j;
int cnt;
char usedx[1 + 512 + 1];
char usedy[1 + 512 + 1];
printf("render2():\n");
memset(usedx, 0, sizeof usedx);
memset(usedy, 0, sizeof usedy);
printf("x's:\n");
for (cnt = 0, i = xmin; i < xmax; i += xprec)
{
int x = ((i * xratio) - (xmin * xratio) + .5);
printf("%d ", x);
cnt++;
usedx[1 + x] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedx[cnt])
printf("missing x: %d\n", cnt - 1);
printf("y's:\n");
for (cnt = 0, j = ymin; j < ymax; j += yprec)
{
int y = ((j * yratio) - (ymin * yratio) + .5);
printf("%d ", y);
cnt++;
usedy[1 + y] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedy[cnt])
printf("missing y: %d\n", cnt - 1);
}
int main(void)
{
InitView(512, 512);
render1();
render2();
return 0;
}
Output (ideone):
render1():
x's:
0 0 0 2 2 4 5 5 7 7 9 10 10 12 12 14 15 15 17 17 19 20 20 22 22 24 25 25 27 27 29 30 30 32 32 33 35 36 36 37 38 40 41 41 42 43 45 46 46 47 48 50 51 51 52 54 55 56 56 57 59 60 61 61 62 64 65 66 66 67 69 70 71 71 72 74 75 76 76 77 79 80 81 81 82 84 85 86 86 87 89 90 91 91 92 94 95 95 96 97 99 100 100 101 103 104 105 105 106 108 109 110 110 111 113 114 115 115 116 118 119 120 120 121 123 124 125 125 126 128 129 130 130 131 133 134 135 135 136 138 139 140 140 141 143 144 145 146 146 148 148 150 151 152 153 153 155 156 157 158 158 160 162 163 163 165 166 167 168 168 170 171 172 173 174 175 175 177 178 179 180 180 182 183 184 185 185 187 188 189 190 190 192 193 194 195 195 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 371 373 374 375 376 376 378 379 380 381 381 383 384 385 386 386 388 389 390 391 391 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 420 422 423 424 425 425 427 428 429 430 430 432 433 434 435 435 437 438 439 440 440 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 479 481 482 482 484 484 486 487 487 489 489 491 492 492 494 495 496 497 497 499 500 501 502 502 504 505 506 507 507 509 510 511
count: 512
missing x: 1
missing x: 3
missing x: 6
missing x: 8
missing x: 11
missing x: 13
missing x: 16
missing x: 18
missing x: 21
missing x: 23
missing x: 26
missing x: 28
missing x: 31
missing x: 34
missing x: 39
missing x: 44
missing x: 49
missing x: 53
missing x: 58
missing x: 63
missing x: 68
missing x: 73
missing x: 78
missing x: 83
missing x: 88
missing x: 93
missing x: 98
missing x: 102
missing x: 107
missing x: 112
missing x: 117
missing x: 122
missing x: 127
missing x: 132
missing x: 137
missing x: 142
missing x: 147
missing x: 149
missing x: 154
missing x: 159
missing x: 161
missing x: 164
missing x: 169
missing x: 176
missing x: 181
missing x: 186
missing x: 191
missing x: 196
missing x: 372
missing x: 377
missing x: 382
missing x: 387
missing x: 392
missing x: 421
missing x: 426
missing x: 431
missing x: 436
missing x: 441
missing x: 480
missing x: 483
missing x: 485
missing x: 488
missing x: 490
missing x: 493
missing x: 498
missing x: 503
missing x: 508
y's:
0 0 0 2 2 4 5 5 7 7 9 10 10 12 12 14 15 15 17 17 19 20 20 22 22 24 25 25 27 27 29 30 30 32 32 33 35 36 36 37 38 40 41 41 42 43 45 46 46 47 48 50 51 51 52 54 55 56 56 57 59 60 61 61 62 64 65 66 66 67 69 70 71 71 72 74 75 76 76 77 79 80 81 81 82 84 85 86 86 87 89 90 91 91 92 94 95 95 96 97 99 100 100 101 103 104 105 105 106 108 109 110 110 111 113 114 115 115 116 118 119 120 120 121 123 124 125 125 126 128 129 130 130 131 133 134 135 135 136 138 139 140 140 141 143 144 145 146 146 148 148 150 151 152 153 153 155 156 157 158 158 160 162 163 163 165 166 167 168 168 170 171 172 173 174 175 175 177 178 179 180 180 182 183 184 185 185 187 188 189 190 190 192 193 194 195 195 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 371 373 374 375 376 376 378 379 380 381 381 383 384 385 386 386 388 389 390 391 391 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 420 422 423 424 425 425 427 428 429 430 430 432 433 434 435 435 437 438 439 440 440 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 479 481 482 482 484 484 486 487 487 489 489 491 492 492 494 495 496 497 497 499 500 501 502 502 504 505 506 507 507 509 510 511
count: 512
missing y: 1
missing y: 3
missing y: 6
missing y: 8
missing y: 11
missing y: 13
missing y: 16
missing y: 18
missing y: 21
missing y: 23
missing y: 26
missing y: 28
missing y: 31
missing y: 34
missing y: 39
missing y: 44
missing y: 49
missing y: 53
missing y: 58
missing y: 63
missing y: 68
missing y: 73
missing y: 78
missing y: 83
missing y: 88
missing y: 93
missing y: 98
missing y: 102
missing y: 107
missing y: 112
missing y: 117
missing y: 122
missing y: 127
missing y: 132
missing y: 137
missing y: 142
missing y: 147
missing y: 149
missing y: 154
missing y: 159
missing y: 161
missing y: 164
missing y: 169
missing y: 176
missing y: 181
missing y: 186
missing y: 191
missing y: 196
missing y: 372
missing y: 377
missing y: 382
missing y: 387
missing y: 392
missing y: 421
missing y: 426
missing y: 431
missing y: 436
missing y: 441
missing y: 480
missing y: 483
missing y: 485
missing y: 488
missing y: 490
missing y: 493
missing y: 498
missing y: 503
missing y: 508
render2():
x's:
0 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 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
count: 512
y's:
0 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 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
count: 512