problem with array sorting - urgent responce needed, due tommrow

T

trs

thanks for reading this.
problem:
logical error is preventing array from sorting right
code:
#include <iostream>
#include <cmath>
using namespace std;
struct coords
{
int x;
int y;
};
coords smallestNumber(int A[][30], int startr, int startc, int size);
void GetRandom(int A[][30], int size);
void SortArray(char major, int A[][30], int size);
void PrintArray(int A[][30], int size);
void swapMeet(coords Old, coords n3w, int A[][30]);
int temp[750];
int main()
{

coords currentSmall;
int grid1[25][30];

GetRandom(grid1,25);
PrintArray(grid1,25);
currentSmall = smallestNumber(grid1, 0, 0, 25);
SortArray('c',grid1,25);

cout << endl << endl << "Sorted array:\n";
PrintArray(grid1,25);
cout << endl << endl << "Row Major:\n";
SortArray('R',grid1,25);
PrintArray(grid1,25);

}

void GetRandom(int A[][30], int size)
{
int count = 749;
for(int i = 0;i <= size -1; i ++)
{
for(int ii = 0; ii <= 30 - 1; ii++)
{
//A[ii] = rand() % 99;
A[ii] = count--;
}
}
}

void SortArray(char major, int A[][30], int size)
{
coords currentsmallest, index, temp;
temp.x = 1;
temp.y=1;
if(major == 'R' || major == 'r')
{
for (int i = 0; i <= size -1;i++)
{
for (int ii = 0;ii <= 30 -1; ii++)
{
currentsmallest = smallestNumber(A, i, ii, size);
index.x = i ;
index.y = ii;
swapMeet(index, currentsmallest, A);
//cout << "(" << currentsmallest.x << "," <<
currentsmallest.y << "):" << A[currentsmallest.x][currentsmallest.y] <<
endl;
//swapMeet(index, temp, A);
}
}
}
if (major == 'C' || major == 'c' && major != 'R' && major != 'r')
{
for (int i = 0;i <= 30 -1; i++)
{
for (int ii = 0;ii <= size -1;ii++)
{
currentsmallest = smallestNumber(A,ii,i,size);
index.x = ii;
index.y = i;
swapMeet(index, currentsmallest, A);
}
}
major = 's';
}
}

void swapMeet(coords Old, coords n3w, int A[][30])
{
int temp = A[Old.x][Old.y];
//make sure it works
if(A[Old.x][Old.y] < A[n3w.x][n3w.y])
{
cout << "(" << Old.x << ","<< Old.y << ")" << " > (" << n3w.x
<< "," << n3w.y
<< "):" << A[Old.x][Old.y] << " > " << A[n3w.x][n3w.y] << endl;
}
A[Old.x][Old.y] = A[n3w.x][n3w.y];
A[n3w.x][n3w.y] = temp;
}

coords smallestNumber(int A[][30], int startr, int startc, int size)
{
coords temppoint;
int min = A[startr][startc], indexr = startr, indexc = startc;
int count = 0;//take out later
for ( int i = startr; i <= size -1;i++)
{
for (int ii = startc;ii <= 30 -1; ii++)
{

if (A[ii] <= min)
{
count++;
min = A[ii];
temppoint.x = i;
temppoint.y = ii;
// cout << "(" << temppoint.x << "," << temppoint.y
<< "):" << min<<endl;
}
}
}
if (count != 1)
{
// cout << "not equal" << count << endl;
}
count = 0;
return temppoint;
}

void PrintArray(int A[][30], int size)
{
//PrintArray prints out the array on the screen; each row per line
//also we require each number occupies exactly 3 characters with
//right alighment
for (int i = 0;i <= size - 1; i++)
{
for (int ii = 0; ii <= 30 -1; ii++)
{
cout << A[ii];
if (A[ii] < 10 )
cout << " ";
else
cout << " ";
}
cout << endl;
}
}

output:
749 748 747 746 745 744 743 742 741 740 739 738 737 736 735 734 733 732
731 730 729 728 727 726 725 724 723 722 721 720
719 718 717 716 715 714 713 712 711 710 709 708 707 706 705 704 703 702
701 700 699 698 697 696 695 694 693 692 691 690
689 688 687 686 685 684 683 682 681 680 679 678 677 676 675 674 673 672
671 670 669 668 667 666 665 664 663 662 661 660
659 658 657 656 655 654 653 652 651 650 649 648 647 646 645 644 643 642
641 640 639 638 637 636 635 634 633 632 631 630
629 628 627 626 625 624 623 622 621 620 619 618 617 616 615 614 613 612
611 610 609 608 607 606 605 604 603 602 601 600
599 598 597 596 595 594 593 592 591 590 589 588 587 586 585 584 583 582
581 580 579 578 577 576 575 574 573 572 571 570
569 568 567 566 565 564 563 562 561 560 559 558 557 556 555 554 553 552
551 550 549 548 547 546 545 544 543 542 541 540
539 538 537 536 535 534 533 532 531 530 529 528 527 526 525 524 523 522
521 520 519 518 517 516 515 514 513 512 511 510
509 508 507 506 505 504 503 502 501 500 499 498 497 496 495 494 493 492
491 490 489 488 487 486 485 484 483 482 481 480
479 478 477 476 475 474 473 472 471 470 469 468 467 466 465 464 463 462
461 460 459 458 457 456 455 454 453 452 451 450
449 448 447 446 445 444 443 442 441 440 439 438 437 436 435 434 433 432
431 430 429 428 427 426 425 424 423 422 421 420
419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402
401 400 399 398 397 396 395 394 393 392 391 390
389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 374 373 372
371 370 369 368 367 366 365 364 363 362 361 360
359 358 357 356 355 354 353 352 351 350 349 348 347 346 345 344 343 342
341 340 339 338 337 336 335 334 333 332 331 330
329 328 327 326 325 324 323 322 321 320 319 318 317 316 315 314 313 312
311 310 309 308 307 306 305 304 303 302 301 300
299 298 297 296 295 294 293 292 291 290 289 288 287 286 285 284 283 282
281 280 279 278 277 276 275 274 273 272 271 270
269 268 267 266 265 264 263 262 261 260 259 258 257 256 255 254 253 252
251 250 249 248 247 246 245 244 243 242 241 240
239 238 237 236 235 234 233 232 231 230 229 228 227 226 225 224 223 222
221 220 219 218 217 216 215 214 213 212 211 210
209 208 207 206 205 204 203 202 201 200 199 198 197 196 195 194 193 192
191 190 189 188 187 186 185 184 183 182 181 180
179 178 177 176 175 174 173 172 171 170 169 168 167 166 165 164 163 162
161 160 159 158 157 156 155 154 153 152 151 150
149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132
131 130 129 128 127 126 125 124 123 122 121 120
119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102
101 100 99 98 97 96 95 94 93 92 91 90
89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66
65 64 63 62 61 60
59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36
35 34 33 32 31 30
29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
5 4 3 2 1 0


Sorted array:
0 25 49 73 97 121 142 166 191 215 240 257 282 308 333 360 373 399 425
451 461 488 516 543 570 600 607 633 661 690
1 26 50 74 98 122 143 167 192 216 241 258 283 309 334 361 374 400 426
452 480 489 517 544 571 601 630 643 662 691
2 27 51 75 99 123 144 168 193 217 242 259 284 310 335 362 376 401 427
453 481 510 518 545 572 602 631 660 668 693
3 28 52 76 100 124 145 169 194 218 243 261 285 311 336 363 390 402 428
454 482 511 522 546 573 603 632 663 692 699
4 29 53 77 101 125 150 170 195 219 244 270 286 312 337 364 391 403 429
455 483 512 540 547 574 604 634 664 694 720
5 30 54 78 102 126 151 171 196 220 245 271 287 313 338 365 392 406 430
456 484 513 541 552 575 605 635 665 695 721
6 31 55 79 103 127 152 172 197 221 246 272 288 314 339 366 393 420 431
457 485 514 542 558 577 606 637 666 696 722
7 32 56 80 104 128 153 173 198 222 247 273 300 315 340 367 394 421 432
458 486 515 548 576 578 608 638 667 697 723
8 33 57 81 105 129 154 180 199 223 248 274 301 316 341 368 395 422 437
459 487 519 549 579 583 609 639 669 698 724
9 34 58 82 106 130 155 181 200 224 249 275 302 317 342 369 396 423 450
460 490 520 550 580 588 611 640 670 700 725
10 35 60 83 107 131 156 182 201 225 250 276 303 321 343 370 397 424 462
467 492 521 551 581 610 613 641 671 703 727
11 36 61 84 108 132 157 183 202 226 251 277 304 330 344 371 398 433 463
472 493 523 553 582 612 618 642 673 704 729
12 37 62 85 109 133 158 184 206 227 252 278 305 331 345 372 404 434 464
491 494 524 554 584 614 636 644 674 706 730
13 38 63 86 110 134 159 185 210 228 253 279 306 332 346 375 405 435 465
495 497 525 555 585 615 645 672 679 707 732
14 39 64 87 111 135 160 186 211 229 254 280 307 347 351 377 407 436 466
496 526 527 556 586 616 646 675 682 709 733
15 40 65 90 112 136 161 187 212 230 255 281 318 348 378 382 408 438 468
498 528 533 557 587 617 648 676 701 714 734
16 41 66 91 113 137 162 188 213 231 256 289 319 349 379 409 412 439 469
499 529 559 563 589 619 649 677 702 715 735
17 42 67 92 114 138 163 189 214 236 260 291 320 350 380 410 417 440 470
502 530 560 590 593 620 650 678 705 726 736
18 43 68 93 115 139 164 190 232 262 266 292 322 352 381 411 441 442 471
503 531 561 591 621 623 651 680 708 728 737
19 44 69 94 116 140 165 203 233 263 290 293 323 353 383 413 443 447 473
504 532 562 592 622 647 653 681 710 731 739
20 45 70 95 120 141 174 204 234 264 294 296 324 354 384 414 444 474 500
505 534 564 594 624 652 654 684 711 738 743
21 46 71 96 146 175 176 205 235 265 295 325 355 357 385 415 445 475 501
535 538 565 595 625 655 683 686 712 740 744
22 47 72 117 147 177 207 237 267 297 326 327 356 386 387 416 446 476 506
536 566 568 596 626 656 685 713 717 742 745
23 48 88 118 148 178 208 238 268 298 328 358 388 418 448 477 478 507 508
537 567 597 598 627 657 687 716 719 746 747
24 59 89 119 149 179 209 239 269 299 329 359 389 419 449 479 509 539 569
599 628 629 658 659 688 689 718 741 748 749


Row Major:
0 25 49 73 97 121 142 166 191 215 240 257 282 308 333 360 373 399 425
451 461 488 516 543 570 600 607 633 661 690
1 26 50 74 98 122 143 167 192 216 241 258 283 309 334 361 374 400 426
452 480 489 517 544 571 601 630 643 662 691
2 27 51 75 99 123 144 168 193 217 242 259 284 310 335 362 376 401 427
453 481 510 518 545 572 602 631 660 668 693
3 28 52 76 100 124 145 169 194 218 243 261 285 311 336 363 390 402 428
454 482 511 522 546 573 603 632 663 692 699
4 29 53 77 101 125 150 170 195 219 244 270 286 312 337 364 391 403 429
455 483 512 540 547 574 604 634 664 694 720
5 30 54 78 102 126 151 171 196 220 245 271 287 313 338 365 392 406 430
456 484 513 541 552 575 605 635 665 695 721
6 31 55 79 103 127 152 172 197 221 246 272 288 314 339 366 393 420 431
457 485 514 542 558 577 606 637 666 696 722
7 32 56 80 104 128 153 173 198 222 247 273 300 315 340 367 394 421 432
458 486 515 548 576 578 608 638 667 697 723
8 33 57 81 105 129 154 180 199 223 248 274 301 316 341 368 395 422 437
459 487 519 549 579 583 609 639 669 698 724
9 34 58 82 106 130 155 181 200 224 249 275 302 317 342 369 396 423 450
460 490 520 550 580 588 611 640 670 700 725
10 35 60 83 107 131 156 182 201 225 250 276 303 321 343 370 397 424 462
467 492 521 551 581 610 613 641 671 703 727
11 36 61 84 108 132 157 183 202 226 251 277 304 330 344 371 398 433 463
472 493 523 553 582 612 618 642 673 704 729
12 37 62 85 109 133 158 184 206 227 252 278 305 331 345 372 404 434 464
491 494 524 554 584 614 636 644 674 706 730
13 38 63 86 110 134 159 185 210 228 253 279 306 332 346 375 405 435 465
495 497 525 555 585 615 645 672 679 707 732
14 39 64 87 111 135 160 186 211 229 254 280 307 347 351 377 407 436 466
496 526 527 556 586 616 646 675 682 709 733
15 40 65 90 112 136 161 187 212 230 255 281 318 348 378 382 408 438 468
498 528 533 557 587 617 648 676 701 714 734
16 41 66 91 113 137 162 188 213 231 256 289 319 349 379 409 412 439 469
499 529 559 563 589 619 649 677 702 715 735
17 42 67 92 114 138 163 189 214 236 260 291 320 350 380 410 417 440 470
502 530 560 590 593 620 650 678 705 726 736
18 43 68 93 115 139 164 190 232 262 266 292 322 352 381 411 441 442 471
503 531 561 591 621 623 651 680 708 728 737
19 44 69 94 116 140 165 203 233 263 290 293 323 353 383 413 443 447 473
504 532 562 592 622 647 653 681 710 731 739
20 45 70 95 120 141 174 204 234 264 294 296 324 354 384 414 444 474 500
505 534 564 594 624 652 654 684 711 738 743
21 46 71 96 146 175 176 205 235 265 295 325 355 357 385 415 445 475 501
535 538 565 595 625 655 683 686 712 740 744
22 47 72 117 147 177 207 237 267 297 326 327 356 386 387 416 446 476 506
536 566 568 596 626 656 685 713 717 742 745
23 48 88 118 148 178 208 238 268 298 328 358 388 418 448 477 478 507 508
537 567 597 598 627 657 687 716 719 746 747
24 59 89 119 149 179 209 239 269 299 329 359 389 419 449 479 509 539 569
599 628 629 658 659 688 689 718 741 748 749
 
N

Niels Dybdahl

trs said:
thanks for reading this.
problem:
logical error is preventing array from sorting right
...

Some comments:
- It would have been nice if you had used some words on what the output
should have been and where the actual output differ.
- Have you tried using a debugger ?
- Have you tried running the algorithm with a smaller data set (which would
make debugging easier) ?
- Why dont you use a sort function from a library ? (Is it homework ?)

Niels Dybdahl
 
T

trs

Victor said:
Niels said:
thanks for reading this.
problem:
logical error is preventing array from sorting right
...

[...]
(Is it homework ?)


Couldn't "due tomorrow" be a hint?
yeah it was homework :p i ended up going about it in a completly
diffrent way, but i would still like to know what i did wrong
 
V

Victor Bazarov

trs said:
Victor said:
Niels said:
thanks for reading this.
problem:
logical error is preventing array from sorting right
...


[...]
(Is it homework ?)



Couldn't "due tomorrow" be a hint?

yeah it was homework :p i ended up going about it in a completly
diffrent way, but i would still like to know what i did wrong

Hey, if you got time now, it would be beneficial for you to locate your
mistakes yourself... Divide and conquer. Split your code into sections
and make sure every section works as you intended it and that they talk
to each other correctly...
 
K

Karl Heinz Buchegger

trs said:
Victor said:
Niels said:
thanks for reading this.
problem:
logical error is preventing array from sorting right
...

[...]
(Is it homework ?)


Couldn't "due tomorrow" be a hint?
yeah it was homework :p i ended up going about it in a completly
diffrent way, but i would still like to know what i did wrong

The first thing you should do, is:
cut down the data amount. Instead of debugging with a
25*25 array, do the very same thing with, lets say,
4*4. Now you have much more chance to step through with
your debugger, paper and pencil and figure out what goes wrong.

PS: For the very same reason, I will not debug your code.
I see no point in running through hundreds of loop iterations,
just to get at the one, where things go wrong.

PS: Up to now, you havn't said why you consider your program
to be buggy. What is the problem you are facing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,028
Latest member
IsmaelLans

Latest Threads

Top