Adjusting Output (Wordwrap maybe?) [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My output needs to look like this :
There are 8 prime numbers less than or equal to 19
2 3 5 7 11 13 17 19
There are 95 prime numbers less than or equal to 500
2 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 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
There are 239 prime numbers less than or equal to 1500
2 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 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541
547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809
811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069
1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223
1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373
1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499
Instead it looks like this:
There are 8 prime numbers less than or equal to 19
2 3 5 7 11 13 17 19
There are 95 prime numbers less than or equal to 500
2 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 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
There are 239 prime numbers less than or equal to 1500
2 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 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499
How can I fix this.
Code for this:
void writeToOutputFile(FILE *fpout, const int *array, int n, int count){
int i;
fprintf(fpout, "\n \nThere are %d prime numbers less than or equal to %d \n \n", count, n);
for(i = 0; i < count; i++){
if(*(array + i) != 0){
fprintf(fpout, "%d ", *(array + i));
}
}
}
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

printf returns the number of characters printed onto the screen. You can use this information to print newlines when you reach a certain limit:
int width = 0;
for (/* some loop */) {
width += printf(/*some numbers */);
if (width > 70) {
printf("\n");
width = 0;
}
}
within your function:
void writeToOutputFile(FILE *fpout, const int *array, int n, int count){
int i;
int width = 0;
fprintf(fpout, "\n \nThere are %d prime numbers less than or equal to %d \n \n", count, n);
for(i = 0; i < count; i++){
if(*(array + i) != 0){
width += fprintf(fpout, "%d ", *(array + i));
if (width > 70) {
fprintf(fpout, "\n");
width = 0;
}
}
}
}

Related

Heap sort in 1d array doesn't work properly

I'm trying to heap sort 1d array by growth, but my code doesn't work properly. The problem is too many arguments in function call:
too many arguments to function call, expected 2, have 3.
My code is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
int k = 0, t = 0; // k - value of sort, t - value of swaps
int n= 1000;
void heapify(int a[1000], int i) {
// Find largest among root, left child and right child
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
k++;
if (left < n && a[left] > a[largest])
{
t++;
largest = left;
}
if (right < n && a[right] > a[largest])
{
t++;
largest = right;
}
// Swap and continue heapifying if root is not largest
if (largest != i)
{
t++;
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
int main () {
int i, j;
double a[1000];
int largest;
srand (time (NULL));
for ( i = 0; i < 1000; i++)
{
a[i] = rand() % 1000;
}
printf ("Array: \n");
for ( i = 0; i < 1000; i++)
{
printf ("%4.0f", a[i]);
}
for ( i = 1000 / 2 - 1; i >= 0; i--)
{
heapify(a, n, i);
}
for ( i = 999; i >= 0; i--)
{
swap (a[0], a[i]);
t++;
heapify (a, i , 0);
}
printf ("\n");
printf ("Sorted array: \n");
for ( i = 0; i < 1000; i++)
{
printf ("%4.0f", a[i]);
}
printf ("\n");
printf ("k = %d", k);
printf ("\nt = %d", t);
return 0;
}
I need 3 arguments, but I do not know how to make the code work properly.
In code there are 1000 elements in array, but I tried with various values, but it didn't work either.
My goal is to receive a sorted array by growth like this: (example, used bubble sort to display it)
Array:
124 151 417 511 448 792 137 435 829 817 439 656 694 776 35 112 854 890 683 953 130 485 515 518 51 384 795 535 515 771 35 662 954 190 806 630 154 737 210 905 245 703 400 211 221 700 788 875 416 39 791 81 358 464 183 247 870 76 43 175 8 521 718 518 122 862 177 542 47 109 619 990 779 755 826 796 705 179 788 339 53 852 948 919 178 806 663 248 572 267 401 922 106 357 163 149 994 494 421 89 878 373 391 255 132 326 911 874 291 307 494 97 340 814 340 574 531 434 52 665 218 699 968 164 185 541 170 51 100 571 961 177 796 966 815 710 956 632 35 674 390 319 361 116 465 512 980 244 943 891 485 194 181 505 457 154 15 307 113 185 241 15 905 715 408 199 720 27 929 577 502 439 62 823 199 980 581 452 840 545 301 18 812 244 569 938 250 222 780 873 461 172 682 218 661 468 646 465 22 840 782 186 135 321 817 843 259 264 232 435 975 864 839 672 632 494 439 142 794 483 79 676 973 377 737 850 428 339 174 974 325 705 454 508 582 2 755 227 508 32 728 87 877 357 52 995 18 264 631 824 207 641 32 579 522 303 412 870 333 547 874 833 933 267 279 736 978 791 315 34 893 320 312 486 356 377 754 5 129 120 245 776 226 12 920 639 506 453 915 304 418 251 8 903 601 422 437 340 278 797 973 530 687 35 326 780 14 938 34 579 945 884 107 597 93 279 564 957 163 725 591 376 342 702 576 896 957 686 966 180 720 203 241 46 665 715 480 565 766 169 310 503 8 349 237 329 560 435 299 994 79 949 6 830 731 859 826 237 616 693 624 784 991 853 382 258 568 275 357 304 187 171 950 756 751 723 35 96 319 618 833 450 262 398 252 721 845 159 320 763 327 575 794 382 174 68 921 954 143 267 654 186 816 373 256 244 998 839 853 886 405 780 948 786 806 165 342 878 154 158 816 227 684 894 810 19 572 131 942 645 591 745 764 611 550 816 156 770 733 26 548 300 728 13 994 123 623 569 51 127 353 705 283 130 389 751 662 505 745 930 255 71 341 607 102 388 846 403 122 782 853 947 385 499 411 617 928 195 623 538 289 455 243 827 358 36 315 588 215 253 967 370 333 188 755 969 591 15 16 349 623 309 869 470 726 499 583 907 636 612 211 692 595 487 689 330 51 296 190 587 955 492 800 929 676 651 769 909 389 718 265 20 629 226 478 915 52 350 502 665 139 117 537 684 390 268 343 15 920 525 620 828 459 655 134 20 200 539 674 435 168 674 169 718 816 360 80 931 269 108 736 300 996 828 384 542 327 249 544 832 829 667 456 480 160 950 456 427 808 55 942 896 171 220 262 21 543 737 929 938 16 133 246 238 463 213 254 543 471 385 905 613 277 413 90 491 33 98 179 363 50 931 126 512 751 934 446 940 406 549 867 315 275 538 467 144 159 509 216 556 999 260 49 470 68 452 352 648 686 593 716 948 95 450 168 756 736 871 997 979 543 708 837 554 646 707 963 271 422 701 32 120 826 686 187 711 917 943 336 856 287 478 949 516 122 569 136 828 264 820 505 872 816 755 384 697 335 586 860 428 572 979 539 979 564 323 181 983 65 17 685 212 453 431 543 782 624 199 440 326 985 949 896 887 859 521 509 76 145 932 268 674 264 177 66 940 151 798 225 454 396 648 112 381 475 279 191 442 570 207 464 845 213 69 665 529 100 753 732 674 679 53 662 75 718 733 91 520 878 668 913 772 729 572 978 843 701 76 523 291 2 33 522 297 834 347 222 476 787 816 11 842 327 363 810 131 950 601 456 677 475 857 315 674 616 556 116 214 845 684 852 830 879 933 232 870 350 688 659 190 598 403 11 538 124 933 963 122 554 782 758 37 877 839 772 10 261 528 274 595 639 108 579 950 384 994 320 208 724 179 257 978 446 459 832 896 316 626 534 186 931 735 920 116 715 900 575 429 322 266 955 956 376 735 779 744 62 810 90 448 234 794 558 937 741 824 954 184 53 517 387 257 517 534 638 527 243 307 651 619 295 837 366 338 310 185 34 110 521 487 239 713 18 193 687 434 674 27 265 602 599 170 401 5 60 978 388 706 552 918 37 302 358 364 835 721 80 446 894 96 363 141 960 73 760 56 165 189 931 524 344 233 166 328 319 228 139 326 792 168 305 964 455 407 832 651 646 755 359 777 627 118 635 418 448 824 56 524 421 459 349 933 292 690 139 316 353 508 1 14 323 906 608 967 12 597 389 98 535 224
Sorted array:
1 2 2 5 5 6 8 8 8 10 11 11 12 12 13 14 14 15 15 15 15 16 16 17 18 18 18 19 20 20 21 22 26 27 27 32 32 32 33 33 34 34 34 35 35 35 35 35 36 37 37 39 43 46 47 49 50 51 51 51 51 52 52 52 53 53 53 55 56 56 60 62 62 65 66 68 68 69 71 73 75 76 76 76 79 79 80 80 81 87 89 90 90 91 93 95 96 96 97 98 98 100 100 102 106 107 108 108 109 110 112 112 113 116 116 116 117 118 120 120 122 122 122 122 123 124 124 126 127 129 130 130 131 131 132 133 134 135 136 137 139 139 139 141 142 143 144 145 149 151 151 154 154 154 156 158 159 159 160 163 163 164 165 165 166 168 168 168 169 169 170 170 171 171 172 174 174 175 177 177 177 178 179 179 179 180 181 181 183 184 185 185 185 186 186 186 187 187 188 189 190 190 190 191 193 194 195 199 199 199 200 203 207 207 208 210 211 211 212 213 213 214 215 216 218 218 220 221 222 222 224 225 226 226 227 227 228 232 232 233 234 237 237 238 239 241 241 243 243 244 244 244 245 245 246 247 248 249 250 251 252 253 254 255 255 256 257 257 258 259 260 261 262 262 264 264 264 264 265 265 266 267 267 267 268 268 269 271 274 275 275 277 278 279 279 279 283 287 289 291 291 292 295 296 297 299 300 300 301 302 303 304 304 305 307 307 307 309 310 310 312 315 315 315 315 316 316 319 319 319 320 320 320 321 322 323 323 325 326 326 326 326 327 327 327 328 329 330 333 333 335 336 338 339 339 340 340 340 341 342 342 343 344 347 349 349 349 350 350 352 353 353 356 357 357 357 358 358 358 359 360 361 363 363 363 364 366 370 373 373 376 376 377 377 381 382 382 384 384 384 384 385 385 387 388 388 389 389 389 390 390 391 396 398 400 401 401 403 403 405 406 407 408 411 412 413 416 417 418 418 421 421 422 422 427 428 428 429 431 434 434 435 435 435 435 437 439 439 439 440 442 446 446 446 448 448 448 450 450 452 452 453 453 454 454 455 455 456 456 456 457 459 459 459 461 463 464 464 465 465 467 468 470 470 471 475 475 476 478 478 480 480 483 485 485 486 487 487 491 492 494 494 494 499 499 502 502 503 505 505 505 506 508 508 508 509 509 511 512 512 515 515 516 517 517 518 518 520 521 521 521 522 522 523 524 524 525 527 528 529 530 531 534 534 535 535 537 538 538 538 539 539 541 542 542 543 543 543 543 544 545 547 548 549 550 552 554 554 556 556 558 560 564 564 565 568 569 569 569 570 571 572 572 572 572 574 575 575 576 577 579 579 579 581 582 583 586 587 588 591 591 591 593 595 595 597 597 598 599 601 601 602 607 608 611 612 613 616 616 617 618 619 619 620 623 623 623 624 624 626 627 629 630 631 632 632 635 636 638 639 639 641 645 646 646 646 648 648 651 651 651 654 655 656 659 661 662 662 662 663 665 665 665 665 667 668 672 674 674 674 674 674 674 674 676 676 677 679 682 683 684 684 684 685 686 686 686 687 687 688 689 690 692 693 694 697 699 700 701 701 702 703 705 705 705 706 707 708 710 711 713 715 715 715 716 718 718 718 718 720 720 721 721 723 724 725 726 728 728 729 731 732 733 733 735 735 736 736 736 737 737 737 741 744 745 745 751 751 751 753 754 755 755 755 755 755 756 756 758 760 763 764 766 769 770 771 772 772 776 776 777 779 779 780 780 780 782 782 782 782 784 786 787 788 788 791 791 792 792 794 794 794 795 796 796 797 798 800 806 806 806 808 810 810 810 812 814 815 816 816 816 816 816 816 817 817 820 823 824 824 824 826 826 826 827 828 828 828 829 829 830 830 832 832 832 833 833 834 835 837 837 839 839 839 840 840 842 843 843 845 845 845 846 850 852 852 853 853 853 854 856 857 859 859 860 862 864 867 869 870 870 870 871 872 873 874 874 875 877 877 878 878 878 879 884 886 887 890 891 893 894 894 896 896 896 896 900 903 905 905 905 906 907 909 911 913 915 915 917 918 919 920 920 920 921 922 928 929 929 929 930 931 931 931 931 932 933 933 933 933 934 937 938 938 938 940 940 942 942 943 943 945 947 948 948 948 949 949 949 950 950 950 950 953 954 954 954 955 955 956 956 957 957 960 961 963 963 964 966 966 967 967 968 969 973 973 974 975 978 978 978 978 979 979 979 980 980 983 985 990 991 994 994 994 994 995 996 997 998 999
k = 988011
t = 248617
I would highly appreciate your help!
If you want to use recursion and pass largest as an argument, just add one more argument to the declaration of the heapify function:
void heapify(int a[1000], int i, int largest) {...}
And in the body of the function, do whatever you want with largest.

Radix Sort doesn't work properly in 1d array (c)

I'm trying to radix sort 1d array by growth, but it doesn't sort: it displays the same arrays. When I'm trying to find mistakes, compiler says there are no ones.
My code is (I used an example given on Programiz site to write Maximum and radix parts: https://www.programiz.com/dsa/radix-sort):
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
int a[1000];
int n;
int place;
int k = 0, t = 0; // k - value of sorting, t - value of swaps
int Maximum (int a[1000], int n) {
int max = a[0];
for (int i = 0; i < n; i++)
{
k++;
if (a[i] > max)
{
max = a[i];
t++;
}
}
return max;
}
void Counting (int a[1000], int n, int place) {
int max = (a[0] / place) % 10;
for (int i = 1; i < n; i++)
{
k++;
if (((a[i] / place) % 10) > place)
{
max = a[i];
t++;
}
}
int count[max+1];
for (int i = 0; i < max; i++)
{
count[i] = 0;
}
for (int i = 0; i < n; i++)
{
count[(a[i]/place) % 10]++;
}
for (int i = 0; i < 10; i++)
{
count[i] += count[i-1];
}
int output[n+1];
for (int i = n-1; i >= 0; i--)
{
output[count[(a[i] / place) % 10] -1] = a[i];
count[(a[i]/place) % 10]--;
}
for (int i = 0; i < n; i++)
{
a[i] = output[i];
}
}
void radix(int a[1000], int n) {
int max = Maximum(a, n);
for (int place = 1; max/place > 0; place *= 10)
{
Counting(a, n, place);
}
}
int main() {
int i;
int a[1000];
srand (time (NULL));
for ( i = 0; i < 1000; i++)
{
a[i] = rand() % 1000;
}
printf ("Array: \n");
for ( i = 0; i < 1000; i++)
{
printf ("%4.0d", a[i]);
}
printf ("\n");
printf ("Sorted array: \n");
radix(a, n);
for ( i = 0; i < 1000; i++)
{
printf ("%4.0d", a[i]);
}
printf ("\n");
printf ("k = %d", k);
printf ("\nt = %d", t);
return 0;
}
The result displayed is:
Array:
264 843 736 441 404 801 478 517 81 328 424 935 241 585 97 556 453 118 950 305 977 626 450 829 828 507 197 164 278 16 756 295 898 652 973 103 784 422 355 782 466 931 546 958 205 61 700 777 323 214 526 697 86 717 642 279 134 770 166 477 481 506 130 913 24 59 52 391 195 570 526 670 706 409 371 566 211 991 407 981 726 726 399 172 29 234 602 543 287 605 381 231 141 123 345 889 349 988 720 39 361 623 571 928 934 944 942 615 871 358 331 727 347 699 262 218 177 328 892 689 494 678 615 640 328 246 421 218 233 807 568 890 831 979 621 725 366 193 488 177 197 870 210 383 682 701 957 805 70 877 708 765 316 426 71 882 624 418 976 145 49 870 680 4 537 937 190 4 534 499 538 185 924 396 572 286 438 349 983 271 999 921 967 539 819 808 649 399 802 962 491 744 794 890 107 162 816 725 727 366 94 677 377 4 930 958 303 31 574 112 753 445 309 393 461 365 435 975 366 205 272 686 613 906 702 67 24 717 923 246 752 696 397 568 689 457 728 567 838 517 903 680 86 792 68 290 398 506 794 146 919 308 990 435 501 78 273 878 989 480 108 275 713 995 912 818 878 30 107 784 782 403 950 5 564 853 635 748 350 680 606 261 548 985 318 907 45 203 751 77 641 998 829 857 925 545 618 458 510 307 615 660 415 176 808 265 693 486 707 436 733 263 133 952 769 919 760 780 325 754 300 910 827 798 163 502 74 73 464 671 920 194 406 785 356 287 635 496 442 712 543 943 187 795 168 641 131 327 901 754 18 598 570 909 782 682 416 703 792 834 470 513 611 26 746 205 250 930 866 248 747 424 191 825 980 943 138 6 34 7 122 640 22 432 337 660 808 300 444 161 250 771 408 544 134 8 961 347 422 141 16 708 433 64 754 704 664 160 332 691 441 117 80 310 8 49 316 6 954 281 399 113 202 974 590 99 78 89 590 612 280 740 990 147 348 750 341 401 71 695 396 376 4 113 542 224 221 83 825 880 969 233 343 824 860 28 998 808 399 122 536 305 928 439 278 70 419 842 379 766 342 779 651 20 64 479 259 514 206 712 543 983 647 865 956 760 790 316 822 528 589 375 230 132 624 580 921 610 542 287 626 185 707 496 743 697 240 876 901 441 931 864 9 387 350 600 846 591 42 384 730 435 147 975 912 370 296 520 173 372 985 75 469 350 630 932 352 303 969 542 915 715 457 111 384 915 208 527 453 175 9 995 69 979 963 854 838 763 592 546 366 14 297 353 343 187 729 769 488 77 701 556 842 385 148 434 886 805 534 628 734 678 854 491 160 801 999 247 261 656 820 65 986 133 228 927 870 700 160 332 674 271 619 473 988 880 933 670 102 743 223 177 121 241 195 956 970 573 226 329 955 708 607 628 78 801 906 494 311 497 280 363 718 98 395 494 994 646 781 697 788 671 831 679 70 637 374 597 578 213 243 951 489 635 900 739 397 729 400 578 377 171 251 553 823 555 522 924 582 705 500 312 345 515 829 631 804 376 301 134 226 448 539 659 265 306 572 863 715 552 13 51 842 426 546 137 283 491 971 52 47 391 190 430 664 170 22 413 505 990 460 385 831 934 972 30 763 892 387 98 27 510 701 253 365 486 638 86 836 547 936 341 713 772 482 355 51 521 460 997 770 897 836 270 672 578 788 879 574 439 15 416 132 4 957 39 994 756 588 300 494 952 963 212 59 109 931 22 731 694 855 69 263 573 542 82 367 612 48 747 285 748 989 263 640 758 708 208 787 114 235 144 304 331 205 236 126 934 869 30 475 889 597 575 992 287 23 112 670 672 687 602 576 211 105 724 409 227 760 8 725 481 973 610 379 119 491 877 298 390 94 694 231 404 577 106 687 145 546 164 331 379 353 952 971 666 219 58 352 105 812 475 572 664 174 584 260 279 694 363 251 8 133 45 648 691 964 273 216 489 917 888 758 227 359 652 918 539 801 18 653 378 958 836 44 463 375 458 525 694 173 300 954 670 457 459 766 822 303 187 118 946 630 748 819 697 620 675 484 795 89 36 763 628 278 37 660 689 468 640 434 261 641 514 230 25 196 642 358 197 629 192 534 842 935 26 757 656 682 292 621 318 168 425 497 456 127 227 373 651 798 609 179 924 285 603 74 252 312 688 25 687 947 754 635 444 773 63 312 145 665 201 892 635 341 952 410 675 632 623 738 471 506 464 913 829 942 945 135 894 579 245 762 737 989 129 487 391 677 338
Sorted array:
264 843 736 441 404 801 478 517 81 328 424 935 241 585 97 556 453 118 950 305 977 626 450 829 828 507 197 164 278 16 756 295 898 652 973 103 784 422 355 782 466 931 546 958 205 61 700 777 323 214 526 697 86 717 642 279 134 770 166 477 481 506 130 913 24 59 52 391 195 570 526 670 706 409 371 566 211 991 407 981 726 726 399 172 29 234 602 543 287 605 381 231 141 123 345 889 349 988 720 39 361 623 571 928 934 944 942 615 871 358 331 727 347 699 262 218 177 328 892 689 494 678 615 640 328 246 421 218 233 807 568 890 831 979 621 725 366 193 488 177 197 870 210 383 682 701 957 805 70 877 708 765 316 426 71 882 624 418 976 145 49 870 680 4 537 937 190 4 534 499 538 185 924 396 572 286 438 349 983 271 999 921 967 539 819 808 649 399 802 962 491 744 794 890 107 162 816 725 727 366 94 677 377 4 930 958 303 31 574 112 753 445 309 393 461 365 435 975 366 205 272 686 613 906 702 67 24 717 923 246 752 696 397 568 689 457 728 567 838 517 903 680 86 792 68 290 398 506 794 146 919 308 990 435 501 78 273 878 989 480 108 275 713 995 912 818 878 30 107 784 782 403 950 5 564 853 635 748 350 680 606 261 548 985 318 907 45 203 751 77 641 998 829 857 925 545 618 458 510 307 615 660 415 176 808 265 693 486 707 436 733 263 133 952 769 919 760 780 325 754 300 910 827 798 163 502 74 73 464 671 920 194 406 785 356 287 635 496 442 712 543 943 187 795 168 641 131 327 901 754 18 598 570 909 782 682 416 703 792 834 470 513 611 26 746 205 250 930 866 248 747 424 191 825 980 943 138 6 34 7 122 640 22 432 337 660 808 300 444 161 250 771 408 544 134 8 961 347 422 141 16 708 433 64 754 704 664 160 332 691 441 117 80 310 8 49 316 6 954 281 399 113 202 974 590 99 78 89 590 612 280 740 990 147 348 750 341 401 71 695 396 376 4 113 542 224 221 83 825 880 969 233 343 824 860 28 998 808 399 122 536 305 928 439 278 70 419 842 379 766 342 779 651 20 64 479 259 514 206 712 543 983 647 865 956 760 790 316 822 528 589 375 230 132 624 580 921 610 542 287 626 185 707 496 743 697 240 876 901 441 931 864 9 387 350 600 846 591 42 384 730 435 147 975 912 370 296 520 173 372 985 75 469 350 630 932 352 303 969 542 915 715 457 111 384 915 208 527 453 175 9 995 69 979 963 854 838 763 592 546 366 14 297 353 343 187 729 769 488 77 701 556 842 385 148 434 886 805 534 628 734 678 854 491 160 801 999 247 261 656 820 65 986 133 228 927 870 700 160 332 674 271 619 473 988 880 933 670 102 743 223 177 121 241 195 956 970 573 226 329 955 708 607 628 78 801 906 494 311 497 280 363 718 98 395 494 994 646 781 697 788 671 831 679 70 637 374 597 578 213 243 951 489 635 900 739 397 729 400 578 377 171 251 553 823 555 522 924 582 705 500 312 345 515 829 631 804 376 301 134 226 448 539 659 265 306 572 863 715 552 13 51 842 426 546 137 283 491 971 52 47 391 190 430 664 170 22 413 505 990 460 385 831 934 972 30 763 892 387 98 27 510 701 253 365 486 638 86 836 547 936 341 713 772 482 355 51 521 460 997 770 897 836 270 672 578 788 879 574 439 15 416 132 4 957 39 994 756 588 300 494 952 963 212 59 109 931 22 731 694 855 69 263 573 542 82 367 612 48 747 285 748 989 263 640 758 708 208 787 114 235 144 304 331 205 236 126 934 869 30 475 889 597 575 992 287 23 112 670 672 687 602 576 211 105 724 409 227 760 8 725 481 973 610 379 119 491 877 298 390 94 694 231 404 577 106 687 145 546 164 331 379 353 952 971 666 219 58 352 105 812 475 572 664 174 584 260 279 694 363 251 8 133 45 648 691 964 273 216 489 917 888 758 227 359 652 918 539 801 18 653 378 958 836 44 463 375 458 525 694 173 300 954 670 457 459 766 822 303 187 118 946 630 748 819 697 620 675 484 795 89 36 763 628 278 37 660 689 468 640 434 261 641 514 230 25 196 642 358 197 629 192 534 842 935 26 757 656 682 292 621 318 168 425 497 456 127 227 373 651 798 609 179 924 285 603 74 252 312 688 25 687 947 754 635 444 773 63 312 145 665 201 892 635 341 952 410 675 632 623 738 471 506 464 913 829 942 945 135 894 579 245 762 737 989 129 487 391 677 338
k = 0
t = 0
The goal is to receive sorted array by growth like this: (example, used bubble sort to do it)
Array:
193 664 875 780 939 115 105 79 758 183 458 515 132 265 192 22 765 381 784 128 278 865 657 387 268 183 262 173 337 410 141 825 198 907 398 149 387 780 806 370 798 753 19 458 590 135 653 880 30 53 281 654 245 674 686 133 728 161 255 977 241 927 405 336 262 269 560 979 443 480 4 131 140 199 851 590 704 889 90 132 898 262 315 368 448 989 539 533 75 84 100 307 877 402 573 611 708 947 895 867 733 548 450 668 844 301 675 627 514 965 839 939 540 397 697 991 675 611 37 171 479 446 533 76 246 391 264 11 692 316 315 635 156 319 24 19 150 128 909 291 811 812 427 533 872 538 155 896 698 278 158 609 899 596 704 600 325 81 748 128 66 418 245 724 76 181 590 143 268 657 278 529 750 553 652 17 524 940 325 193 95 533 165 404 18 433 257 820 319 822 25 712 223 662 837 26 675 832 53 277 858 191 414 501 574 852 705 54 782 831 275 627 144 618 695 931 825 343 488 580 843 954 144 437 753 819 398 570 585 572 856 489 993 934 587 778 387 685 851 132 336 125 37 988 330 414 761 53 87 940 587 294 27 296 393 41 110 531 123 905 450 467 548 867 283 536 946 780 158 753 566 963 704 626 368 501 152 897 260 319 655 625 763 682 889 581 319 965 401 754 231 532 58 794 817 839 479 182 494 993 755 896 968 968 667 17 613 176 108 407 726 986 490 555 783 70 10 21 710 85 560 727 433 870 121 788 120 216 393 720 839 579 332 486 948 394 162 28 448 457 399 549 897 459 199 359 544 381 841 799 801 899 148 66 658 799 138 199 233 364 698 660 299 837 617 833 595 458 667 609 627 678 761 273 730 165 222 776 646 566 444 630 770 27 811 517 840 253 568 427 362 252 962 316 315 378 680 838 880 649 173 772 79 950 149 606 279 751 508 614 991 519 668 317 953 808 538 664 428 51 926 258 327 683 11 890 126 153 419 784 473 682 684 498 819 628 317 165 465 989 491 532 336 790 624 192 687 425 552 134 636 300 791 227 852 350 356 918 576 726 379 758 934 547 480 452 692 774 602 671 587 256 7 771 753 317 233 341 900 296 894 759 335 587 966 351 124 284 758 656 914 554 13 880 239 682 348 273 526 577 980 491 467 916 549 58 662 462 247 664 475 40 597 74 946 414 648 644 330 959 508 864 982 286 607 813 490 62 145 794 356 127 546 227 722 873 781 220 329 744 447 320 754 55 99 79 402 274 801 931 625 247 795 673 272 157 612 969 347 6 648 200 333 910 454 463 688 16 480 933 383 863 853 885 523 210 367 388 933 66 350 233 859 569 301 574 577 623 93 65 18 438 443 975 500 718 242 457 313 975 389 582 661 394 110 413 478 747 307 738 898 377 840 13 687 156 262 242 807 981 67 33 813 293 512 190 134 414 723 190 27 336 261 653 484 81 507 671 191 40 446 36 90 868 160 857 58 161 237 386 923 730 572 312 93 659 556 420 560 201 890 571 110 476 914 651 17 225 224 678 788 429 12 945 905 240 914 447 136 822 551 506 838 416 639 260 283 559 602 734 350 607 114 621 906 31 836 302 913 536 75 566 99 202 828 188 46 534 752 630 540 554 168 433 258 65 398 513 706 656 176 828 797 724 161 772 378 242 247 73 40 81 366 680 401 281 855 387 444 279 248 564 354 914 627 995 367 535 358 593 742 917 134 972 785 814 833 181 50 643 311 321 809 650 281 476 587 200 872 392 280 827 674 943 360 231 513 663 379 305 12 230 116 880 456 859 46 855 851 604 692 370 397 127 0 897 965 197 843 895 530 148 442 510 565 154 691 417 856 425 177 883 476 542 861 119 25 855 230 391 226 512 714 842 0 475 730 982 688 793 467 400 837 67 106 350 343 436 363 231 242 575 364 626 333 512 843 56 253 475 477 430 760 641 705 21 792 45 119 344 996 629 596 967 457 370 930 780 959 940 316 946 282 62 722 297 901 740 764 265 637 847 939 392 435 152 949 252 742 83 31 227 755 347 586 362 162 583 96 230 738 942 0 871 528 142 673 199 557 895 503 276 307 930 207 339 313 223 56 710 106 27 513 814 770 865 138 313 104 519 161 935 850 920 860 247 416 326 278 741 312 358 63 6 755 421 58 172 441 196 672 915 582 541 659 198 78 389 703 629 503 354 191 399 258 110 13 261 977 823 500 829 983 233 395 755 546 949 941 78 731 374 894 616 70 558 627 824 609 618 469 374 877 602 578 715 165 630 317 384
Sorted array:
0 0 0 4 6 6 7 10 11 11 12 12 13 13 13 16 17 17 17 18 18 19 19 21 21 22 24 25 25 26 27 27 27 27 28 30 31 31 33 36 37 37 40 40 40 41 45 46 46 50 51 53 53 53 54 55 56 56 58 58 58 58 62 62 63 65 65 66 66 66 67 67 70 70 73 74 75 75 76 76 78 78 79 79 79 81 81 81 83 84 85 87 90 90 93 93 95 96 99 99 100 104 105 106 106 108 110 110 110 110 114 115 116 119 119 120 121 123 124 125 126 127 127 128 128 128 131 132 132 132 133 134 134 134 135 136 138 138 140 141 142 143 144 144 145 148 148 149 149 150 152 152 153 154 155 156 156 157 158 158 160 161 161 161 161 162 162 165 165 165 165 168 171 172 173 173 176 176 177 181 181 182 183 183 188 190 190 191 191 191 192 192 193 193 196 197 198 198 199 199 199 199 200 200 201 202 207 210 216 220 222 223 223 224 225 226 227 227 227 230 230 230 231 231 231 233 233 233 233 237 239 240 241 242 242 242 242 245 245 246 247 247 247 247 248 252 252 253 253 255 256 257 258 258 258 260 260 261 261 262 262 262 262 264 265 265 268 268 269 272 273 273 274 275 276 277 278 278 278 278 279 279 280 281 281 281 282 283 283 284 286 291 293 294 296 296 297 299 300 301 301 302 305 307 307 307 311 312 312 313 313 313 315 315 315 316 316 316 317 317 317 317 319 319 319 319 320 321 325 325 326 327 329 330 330 332 333 333 335 336 336 336 336 337 339 341 343 343 344 347 347 348 350 350 350 350 351 354 354 356 356 358 358 359 360 362 362 363 364 364 366 367 367 368 368 370 370 370 374 374 377 378 378 379 379 381 381 383 384 386 387 387 387 387 388 389 389 391 391 392 392 393 393 394 394 395 397 397 398 398 398 399 399 400 401 401 402 402 404 405 407 410 413 414 414 414 414 416 416 417 418 419 420 421 425 425 427 427 428 429 430 433 433 433 435 436 437 438 441 442 443 443 444 444 446 446 447 447 448 448 450 450 452 454 456 457 457 457 458 458 458 459 462 463 465 467 467 467 469 473 475 475 475 476 476 476 477 478 479 479 480 480 480 484 486 488 489 490 490 491 491 494 498 500 500 501 501 503 503 506 507 508 508 510 512 512 512 513 513 513 514 515 517 519 519 523 524 526 528 529 530 531 532 532 533 533 533 533 534 535 536 536 538 538 539 540 540 541 542 544 546 546 547 548 548 549 549 551 552 553 554 554 555 556 557 558 559 560 560 560 564 565 566 566 566 568 569 570 571 572 572 573 574 574 575 576 577 577 578 579 580 581 582 582 583 585 586 587 587 587 587 587 590 590 590 593 595 596 596 597 600 602 602 602 604 606 607 607 609 609 609 611 611 612 613 614 616 617 618 618 621 623 624 625 625 626 626 627 627 627 627 627 628 629 629 630 630 630 635 636 637 639 641 643 644 646 648 648 649 650 651 652 653 653 654 655 656 656 657 657 658 659 659 660 661 662 662 663 664 664 664 667 667 668 668 671 671 672 673 673 674 674 675 675 675 678 678 680 680 682 682 682 683 684 685 686 687 687 688 688 691 692 692 692 695 697 698 698 703 704 704 704 705 705 706 708 710 710 712 714 715 718 720 722 722 723 724 724 726 726 727 728 730 730 730 731 733 734 738 738 740 741 742 742 744 747 748 750 751 752 753 753 753 753 754 754 755 755 755 755 758 758 758 759 760 761 761 763 764 765 770 770 771 772 772 774 776 778 780 780 780 780 781 782 783 784 784 785 788 788 790 791 792 793 794 794 795 797 798 799 799 801 801 806 807 808 809 811 811 812 813 813 814 814 817 819 819 820 822 822 823 824 825 825 827 828 828 829 831 832 833 833 836 837 837 837 838 838 839 839 839 840 840 841 842 843 843 843 844 847 850 851 851 851 852 852 853 855 855 855 856 856 857 858 859 859 860 861 863 864 865 865 867 867 868 870 871 872 872 873 875 877 877 880 880 880 880 883 885 889 889 890 890 894 894 895 895 895 896 896 897 897 897 898 898 899 899 900 901 905 905 906 907 909 910 913 914 914 914 914 915 916 917 918 920 923 926 927 930 930 931 931 933 933 934 934 935 939 939 939 940 940 940 941 942 943 945 946 946 946 947 948 949 949 950 953 954 959 959 962 963 965 965 965 966 967 968 968 969 972 975 975 977 977 979 980 981 982 982 983 986 988 989 989 991 991 993 993 995 996
k = 952047
t = 249349
I would highly appreciate your help!
When I'm trying to find mistakes, compiler says there are no ones
In general, most problems logical errors are not detected by compilers. There are more automated tools at your disposal though, such as compiling with -fsanitize=address (or similar compilation option, the exact spelling of which depends on which compiler you use). ASan can detect certain cases in which memory is accessed improperly, and it finds an issue in this code. But accessing memory improperly is not the only bug this code has.
Let's look at this piece of code:
int max = (a[0] / place) % 10;
for (int i = 1; i < n; i++)
{
k++;
if (((a[i] / place) % 10) > place)
{
max = a[i];
t++;
}
}
This looks like it's supposed to find the maximum digit, but it doesn't do that. ((a[i] / place) % 10) > place is the wrong comparison, and max = a[i] saves the wrong value. max could end up very large (potentially causing a stack overflow due causing a large count array to be allocated) or very small (causing the count array to be accessed out of bounds later).
This is unnecessary anyway, you can use int count[10]; and skip calculating max.
By the way t doesn't count swaps and I don't think that's even a relevant measurement to make in the context of radix sort - it's not based on swaps.
Let's look at this piece of code:
for (int i = 0; i < 10; i++)
{
count[i] += count[i-1];
}
So when i = 0, we access count[-1] (ASan detects this). That's bad, even if it "works", it risks pulling in some random value from whatever is before the array and adding it to every entry. Start this loop at 1.
Let's look at this piece of code:
radix(a, n);
Looks innocent.. but n was never assigned its proper value, and is still zero (being a global variable, it is initialized to zero). Most of the sorting code doesn't do anything, because it thinks the array has length 0.
It has been a while since I programmed in C but my first guess is that the int a[1000] parameter might copy the data all the time and that you need to pass the array by reference instead.
Investigated a little further:
the line if (((a[i] / place) % 10) > place) should be if (((a[i] / place) % 10) > max)
n is used uninitialised

Scanning a 2d array for prime numbers in C

I have a 20x20 array randomly generated. I need a function to scan through the array, find all the prime numbers within the array, and add them to a new array. The array was generated like this:
#define TOTAL_ROWS 20
#define TOTAL_COLUMNS 20
void fillMatrix(int A[TOTAL_ROWS][TOTAL_COLUMNS], int *set_rows, int *set_columns)
{
int rows = 20, columns = 20;
for(int i = 0; i < rows; i++)
for(int j = 0; j < columns; j++)
A[i][j] = rand() % 500;
*set_rows = rows;
*set_columns = columns;
}
I also have it sorted, with this function:
void sortMatrix(int A[TOTAL_ROWS][TOTAL_COLUMNS], int rowsize, int colsize)
{
for(int r = 0; r < rowsize; r++)
qsort(A[r], colsize, sizeof(int), compare);
}
There is also a display function. Here is main which calls the functions:
int main(void)
{
int array[TOTAL_ROWS][TOTAL_COLUMNS] = { 0 };
int rows, columns;
fillMatrix(array, &rows, &columns);
sortMatrix(array, rows, columns);
displayArray(array, rows, columns);
return 0;
}
An example of the generated array:
0 41 145 169 205 224 281 327 327 334 358 436 442 461 462 464 467 478 491 495
35 38 104 153 167 216 218 226 271 292 299 369 382 391 394 395 402 412 421 447
29 37 47 141 144 162 164 173 203 211 223 241 253 257 278 311 322 333 359 368
6 35 40 48 101 106 148 190 229 264 288 305 316 342 350 370 390 393 442 446
37 38 41 82 84 118 123 126 129 256 308 323 340 376 429 431 439 444 454 466
12 21 72 73 97 115 139 158 173 204 245 270 277 306 329 333 386 424 430 477
7 31 52 74 107 136 150 155 161 191 224 267 290 337 350 355 430 441 466 486
6 30 88 91 155 168 209 221 253 258 262 287 383 400 409 413 422 445 446 457
20 21 37 41 48 95 96 102 124 168 199 291 336 348 350 359 374 410 483 484
14 53 117 127 148 228 234 281 288 307 310 313 393 400 418 421 438 467 483 499
8 19 56 93 100 109 116 195 202 224 249 298 303 309 343 344 435 451 485 489
3 9 23 38 80 87 89 118 122 157 200 281 292 296 298 298 314 448 458 472
11 38 55 134 146 156 157 179 190 191 202 272 315 328 362 375 386 388 433 458
3 10 21 57 75 100 142 151 199 212 322 344 369 369 381 389 392 416 476 498
2 85 88 117 154 169 182 188 189 221 255 257 285 289 332 361 401 423 426 432
12 29 49 55 60 139 145 187 192 218 253 279 329 368 423 425 434 441 476 496
49 105 114 171 193 195 201 234 263 282 286 286 297 316 366 416 437 449 455 488
53 58 129 144 146 161 185 196 222 256 308 313 313 321 332 355 412 445 481 482
10 18 22 24 35 44 149 154 159 168 173 186 245 253 292 313 439 450 466 474
18 70 125 159 177 202 287 297 314 324 333 334 372 374 391 405 414 458 477 487
I need an isPrime() function to go in main that will find each prime in the arrays and add them to a new array. I'm just not sure how to.
You can use this.
int is_prime(int num)
{
if (num <= 1) return 0;
if (num % 2 == 0 && num > 2) return 0;
for(int i = 3; i < num / 2; i+= 2)
{
if (num % i == 0)
return 0;
}
return 1;
}
int prime_numbers_array[10000];
int array_length = 0;
int i,j;
for(i = 0; i < rows; i++){
for(j = 0 ;j < cols; j++){
if(is_prime(array[i][j])){
prime_numbers_array[array_length++] = array[i][j]
}
}
}
The fastest way is to run a sieve of Eratosthenes:
bool prime[max_number + 1];
void sieve()
{
memset(prime, true, sizeof(prime));
for (int i=2; i*i<=max_number; i++)
{
if (prime[i] == true)
{
for (int j=i*i; j<=max_number; j += i)
prime[j] = false;
}
}
}
And use the prime array as a lookup to check if a given number p is prime in constant time just by checking if prime[p] == true. Here max_number should be the maximum number that you will be having as an input.

Textbook C Code produces incorrrect output [duplicate]

This question already has answers here:
Convert int to double
(3 answers)
Closed 4 years ago.
I am learning C from a text. An example code provided by the author is:
#include <math.h>
main()
{ int i;
printf("\t Number \t\t Square Root of Number\n\n");
for (i=0; i<=360; ++i)
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
}
which on my computer produces an incorrect output I don't understand:
Number Square Root of Number
0 259
1 515
2 771
3 1027
4 1283
5 1539
6 1795
7 2051
8 2307
9 2563
10 2819
11 3075
12 3331
13 3587
14 3843
15 4099
16 4355
17 4611
18 4867
19 5123
20 5379
21 5635
22 5891
23 6147
24 6403
25 6659
26 6915
27 7171
28 7427
29 7683
30 7939
31 8195
32 8451
33 8707
34 8963
35 9219
36 9475
37 9731
38 9987
39 10243
40 10499
41 10755
42 11011
43 11267
44 11523
45 11779
46 12035
47 12291
48 12547
49 12803
50 13059
51 13315
52 13571
53 13827
54 14083
55 14339
56 14595
57 14851
58 15107
59 15363
60 15619
61 15875
62 16131
63 16387
64 16643
65 16899
66 17155
67 17411
68 17667
69 17923
70 18179
71 18435
72 18691
73 18947
74 19203
75 19459
76 19715
77 19971
78 20227
79 20483
80 20739
81 20995
82 21251
83 21507
84 21763
85 22019
86 22275
87 22531
88 22787
89 23043
90 23299
91 23555
92 23811
93 24067
94 24323
95 24579
96 24835
97 25091
98 25347
99 25603
100 25859
101 26115
102 26371
103 26627
104 26883
105 27139
106 27395
107 27651
108 27907
109 28163
110 28419
111 28675
112 28931
113 29187
114 29443
115 29699
116 29955
117 30211
118 30467
119 30723
120 30979
121 31235
122 31491
123 31747
124 32003
125 32259
126 32515
127 32771
128 33027
129 33283
130 33539
131 33795
132 34051
133 34307
134 34563
135 34819
136 35075
137 35331
138 35587
139 35843
140 36099
141 36355
142 36611
143 36867
144 37123
145 37379
146 37635
147 37891
148 38147
149 38403
150 38659
151 38915
152 39171
153 39427
154 39683
155 39939
156 40195
157 40451
158 40707
159 40963
160 41219
161 41475
162 41731
163 41987
164 42243
165 42499
166 42755
167 43011
168 43267
169 43523
170 43779
171 44035
172 44291
173 44547
174 44803
175 45059
176 45315
177 45571
178 45827
179 46083
180 46339
181 46595
182 46851
183 47107
184 47363
185 47619
186 47875
187 48131
188 48387
189 48643
190 48899
191 49155
192 49411
193 49667
194 49923
195 50179
196 50435
197 50691
198 50947
199 51203
200 51459
201 51715
202 51971
203 52227
204 52483
205 52739
206 52995
207 53251
208 53507
209 53763
210 54019
211 54275
212 54531
213 54787
214 55043
215 55299
216 55555
217 55811
218 56067
219 56323
220 56579
221 56835
222 57091
223 57347
224 57603
225 57859
226 58115
227 58371
228 58627
229 58883
230 59139
231 59395
232 59651
233 59907
234 60163
235 60419
236 60675
237 60931
238 61187
239 61443
240 61699
241 61955
242 62211
243 62467
244 62723
245 62979
246 63235
247 63491
248 63747
249 64003
250 64259
251 64515
252 64771
253 65027
254 65283
255 65539
256 65795
257 66051
258 66307
259 66563
260 66819
261 67075
262 67331
263 67587
264 67843
265 68099
266 68355
267 68611
268 68867
269 69123
270 69379
271 69635
272 69891
273 70147
274 70403
275 70659
276 70915
277 71171
278 71427
279 71683
280 71939
281 72195
282 72451
283 72707
284 72963
285 73219
286 73475
287 73731
288 73987
289 74243
290 74499
291 74755
292 75011
293 75267
294 75523
295 75779
296 76035
297 76291
298 76547
299 76803
300 77059
301 77315
302 77571
303 77827
304 78083
305 78339
306 78595
307 78851
308 79107
309 79363
310 79619
311 79875
312 80131
313 80387
314 80643
315 80899
316 81155
317 81411
318 81667
319 81923
320 82179
321 82435
322 82691
323 82947
324 83203
325 83459
326 83715
327 83971
328 84227
329 84483
330 84739
331 84995
332 85251
333 85507
334 85763
335 86019
336 86275
337 86531
338 86787
339 87043
340 87299
341 87555
342 87811
343 88067
344 88323
345 88579
346 88835
347 89091
348 89347
349 89603
350 89859
351 90115
352 90371
353 90627
354 90883
355 91139
356 91395
357 91651
358 91907
359 92163
360 92419
Any idea or clue as to why? If its obvious maybe point to a reference that will point out some stupid mistake I have made?
Read up on this: printf
And use %f for double not %d which is for int
You're using the wrong format specifier here:
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
Make sure to use %f for variables of type double (what a function like sqrt() returns):
printf("\t %d \t\t\t %f \n",i, sqrt((double) i));
You have used a wrong format specifier.
Try,
printf("\t %d \t\t\t %f \n" ,i , sqrt((double) i));
%f for double. Good luck.

Subroutine recursion in Perl

EDIT: I'm glad no one has spent any time pointing out that the actual text in line 6 and 7 has a different number than the input for their respective function calls. Eventually I'll be doing it for those two numbers (724 and 27), but for the sake of troubleshooting, I picked numbers with much smaller sequences. So, if anyone was wondering, that's why...
So, I've been learning Perl, and am relatively new to programming in general. My supervisor has a set of exercises for me to go through. The current one deals with Hailstone sequences, and she wants me to write a subroutine that prints the sequence for a given number.
The problem I'm running into is that, no matter what I've tried, if I call the function more than once, it will produce the sequence for the first number I call the function with, but the second time I call the function, it produces the sequence of the first call followed by the sequence of the second. So, this code:
#!usr/bin/perl
use strict;
use warnings;
print "\nThe hailstone sequence for 724 is:\n" . &hail(8) . "\n\n";
print "The hailstone sequence for 27 is:\n" . &hail(16) . "\n\n";
my $n;
my #seq;
sub hail {
no warnings 'recursion';
$n = $_[0];
if ($n > 1) {
push #seq, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
&hail($n);
} else {
push #seq, $n;
}
return "#seq";
}
produces:
The hailstone sequence for 724 is:
8 4 2 1
The hailstone sequence for 27 is:
8 4 2 1 16 8 4 2 1
I understand that this is most likely due to the fact that #seq doesn't get cleared out after each time the subroutine runs, but I've tried as many different ways as I can think of to clear it out so that each time I call the subroutine, it displays the sequence for -only- that number but they all either result in what I show here, or in showing nothing. How do I go about clearing the array each time?
Thanks very much.
You don't need recursion here. In my Fibonacci example in Mastering Perl, I show that it's easier to do it with iteration where you manage the queue yourself rather than using the call stack to do it.
Here's a general iterative solution that uses an array to keep track of the work left to do:
use strict;
use warnings;
use v5.10;
say "The hailstone sequence for 724 is:\n\t" .
join " ", hail(8);
say "The hailstone sequence for 27 is:\n\t" .
join " ", hail(16);
sub hail {
my #queue = ( $_[0] );
my #sequence = ();
while( my $next = shift #queue ) {
if( $next > 1 ) {
push #queue, do {
if( $next % 2 == 0 ) { $next / 2 }
else { 3*$next + 1 }
};
}
push #sequence, $next;
}
#sequence;
}
From there, I could add caching and other things so I can reuse sequences I've already generated (which works even without showing off some exciting new Perl features such as subroutine signatures and postfix dereferencing that I find quite fun):
use strict;
use warnings;
use v5.22;
use feature qw(signatures postderef);
no warnings qw(experimental::signatures experimental::postderef);
say "The hailstone sequence for 724 is:\n\t" .
join " ", hail(8)->#*;
say "The hailstone sequence for 27 is:\n\t" .
join " ", hail(16)->#*;
sub hail ( $n ) {
my #queue = ( $_[0] );
state $sequence = { 1 => [ 1 ] };
return $sequence->{$n} if exists $sequence->{$n};
my #sequence = ();
while( my $next = shift #queue ) {
say "Processing $next"; # to watch what happens
if( exists $sequence->{$next} ) {
push #sequence, $sequence->{$next}->#*;
next;
}
push #queue, do {
if( $next % 2 == 0 ) { $next / 2 }
else { 3*$next + 1 }
};
push #sequence, $next;
}
$sequence->{$n} = \#sequence;
}
I threw a say in there to show what I process. You can see that with 16, it doesn't have to go past 8 because it already knows that answer:
Processing 8
Processing 4
Processing 2
Processing 1
The hailstone sequence for 724 is:
8 4 2 1
Processing 16
Processing 8
The hailstone sequence for 27 is:
16 8 4 2 1
I was curious which numbers might cause a problem, so I slightly modified your example to return a list so I could easily count the number of elements. Several numbers produced sequences with over 100 numbers:
use strict;
use warnings;
use v5.10;
foreach my $n ( 0 .. 100 ) {
hail( $n, \my #seq );
say "$n [" . #seq . "] #seq";
}
sub hail {
my $n = $_[0];
my $s = $_[1];
if ($n > 1) {
push #$s, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
hail($n, $s);
} else {
push #$s, $n;
}
}
The output, without the deep recursion warnings (which should be the hint not to do it that way ;):
0 [1] 0
1 [1] 1
2 [2] 2 1
3 [8] 3 10 5 16 8 4 2 1
4 [3] 4 2 1
5 [6] 5 16 8 4 2 1
6 [9] 6 3 10 5 16 8 4 2 1
7 [17] 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
8 [4] 8 4 2 1
9 [20] 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
10 [7] 10 5 16 8 4 2 1
11 [15] 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
12 [10] 12 6 3 10 5 16 8 4 2 1
13 [10] 13 40 20 10 5 16 8 4 2 1
14 [18] 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
15 [18] 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
16 [5] 16 8 4 2 1
17 [13] 17 52 26 13 40 20 10 5 16 8 4 2 1
18 [21] 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
19 [21] 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
20 [8] 20 10 5 16 8 4 2 1
21 [8] 21 64 32 16 8 4 2 1
22 [16] 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
23 [16] 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
24 [11] 24 12 6 3 10 5 16 8 4 2 1
25 [24] 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
26 [11] 26 13 40 20 10 5 16 8 4 2 1
27 [112] 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
28 [19] 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
29 [19] 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
30 [19] 30 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
31 [107] 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
32 [6] 32 16 8 4 2 1
33 [27] 33 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
34 [14] 34 17 52 26 13 40 20 10 5 16 8 4 2 1
35 [14] 35 106 53 160 80 40 20 10 5 16 8 4 2 1
36 [22] 36 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
37 [22] 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
38 [22] 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
39 [35] 39 118 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
40 [9] 40 20 10 5 16 8 4 2 1
41 [110] 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
42 [9] 42 21 64 32 16 8 4 2 1
43 [30] 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
44 [17] 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
45 [17] 45 136 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
46 [17] 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
47 [105] 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
48 [12] 48 24 12 6 3 10 5 16 8 4 2 1
49 [25] 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
50 [25] 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
51 [25] 51 154 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
52 [12] 52 26 13 40 20 10 5 16 8 4 2 1
53 [12] 53 160 80 40 20 10 5 16 8 4 2 1
54 [113] 54 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
55 [113] 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
56 [20] 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
57 [33] 57 172 86 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
58 [20] 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
59 [33] 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
60 [20] 60 30 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
61 [20] 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
62 [108] 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
63 [108] 63 190 95 286 143 430 215 646 323 970 485 1456 728 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
64 [7] 64 32 16 8 4 2 1
65 [28] 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
66 [28] 66 33 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
67 [28] 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
68 [15] 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
69 [15] 69 208 104 52 26 13 40 20 10 5 16 8 4 2 1
70 [15] 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
71 [103] 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
72 [23] 72 36 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
73 [116] 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
74 [23] 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
75 [15] 75 226 113 340 170 85 256 128 64 32 16 8 4 2 1
76 [23] 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
77 [23] 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
78 [36] 78 39 118 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
79 [36] 79 238 119 358 179 538 269 808 404 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
80 [10] 80 40 20 10 5 16 8 4 2 1
81 [23] 81 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
82 [111] 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
83 [111] 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
84 [10] 84 42 21 64 32 16 8 4 2 1
85 [10] 85 256 128 64 32 16 8 4 2 1
86 [31] 86 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
87 [31] 87 262 131 394 197 592 296 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
88 [18] 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
89 [31] 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
90 [18] 90 45 136 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
91 [93] 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
92 [18] 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
93 [18] 93 280 140 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
94 [106] 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
95 [106] 95 286 143 430 215 646 323 970 485 1456 728 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
96 [13] 96 48 24 12 6 3 10 5 16 8 4 2 1
97 [119] 97 292 146 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
98 [26] 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
99 [26] 99 298 149 448 224 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
100 [26] 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Should you want a true function — one that doesn't have any side-effects, and thus none of the problems you are having — it would look as follows:
sub hail {
no warnings qw( recursion );
my ($n) = #_;
if ($n > 1) {
if ($n % 2 == 0) {
return $n, hail($n/2);
} else {
return $n, hail(3*$n + 1);
}
} else {
return $n;
}
}
Note that recursion is totally unneeded here, greatly slowing down your program and increasing its memory footprint. The following is such an iterative solution:
sub hail {
my ($n) = #_;
my #rv;
while (1) {
push #rv, $n;
last if $n <= 1;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = 3*$n + 1;
}
}
return #rv;
}
A question about the above code was asked in the comments. The following is the answer:
last jumps to the statement after the loop.
Another programmer might have written the following:
push #rv, $n;
while ($n > 1) {
...;
push #rv, $n;
}
But that contains duplicated code. Ideally, one seeks to avoid duplicated code. But since
while (EXPR) { STATEMENTS }
can be rewritten as
while (1) { last if !EXPR; STATEMENTS }
and since
push #rv, $n;
while ($n > 1) {
...;
push #rv, $n;
}
can be rewritten as
push #rv, $n;
while (1) {
last if $n <= 1;
...;
push #rv, $n;
}
we can remove the duplicated code as follows:
while (1) {
push #rv, $n;
last if $n <= 1;
...;
}
Not sure about perl, but in other languages I'd do
sub hail {
my #seq;
return hailRecursive(\#seq, $_[0])
}
And then implement hailRecursive in terms of array ref and $n
So, the full assignment was to print the sequence for any number < 100000 of my choosing, print the sequence of 27, and then find the number < 100000 with the longest sequence, and print the number of elements in the sequence (but not the sequence itself) I greatly appreciate the help everyone has provided about making a more effective subroutine, and I really will go through the different suggestions to learn the different tips and tricks from each one. I didn't modify my main subroutine code too much to keep it more in my (current) style for the sake of the exercise. (My supervisor won't care that I got help, but it still feels...-something-...to just copy somebody else's code without also knowing I could figure it out with my own hair-brained ideas, along with -her- suggestion to use a recursive subroutine, as well).
I took one person's advice to not return the array as a string, which helped with the part of the exercise I didn't originally mention, but then I had to rework how I printed the actual sequences which seemed easy enough. Beyond learning more effective subroutines, my main concern is still with clearing the array. Someone suggested just putting #seq = () after each instance, and that works. What I want to know is why what I actually have running in the code (the lines with ##### -after- the code) works to clear the array each time, but why it doesn't work to simply clear out the array after I return it in the subroutine, like I have with the commented out line. That still aggregates the sequence each time the subroutine is called.
#!usr/bin/perl
use strict;
use warnings;
my $num_win = 0;
my $elem_win = 0;
my #seq;
my $elements;
print "\nThe hailstone sequence for 724 is:\n";
&sequence(&hail(724));
#seq = (); #####
print "\n\nThe hailstone sequence for 27 is:\n";
&sequence(&hail(27));
#seq = (); #####
for (my $i=1; $i<100000; $i++) {
$elements = &hail($i);
if ($elements > $elem_win) {
$elem_win = $elements;
$num_win = $i;
}
#seq = (); #####
}
print "\n\nThe number with the largest sequence is: $num_win\n";
print "The number of elements in $num_win is: $elem_win\n\n";
my $n;
sub hail {
no warnings 'recursion';
$n = $_[0];
if ($n > 1) {
push #seq, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
&hail($n);
} else {
push #seq, $n;
}
return #seq;
##### #seq = ();
}
sub sequence {
my #hail_seq = #_;
foreach (#hail_seq) {
my $number = $_;
print "$number, ";
}
}
with the results:
The hailstone sequence for 724 is:
724, 362, 181, 544, 272, 136, 68, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1,
The hailstone sequence for 27 is:
27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1,
The number with the largest sequence is: 77031
The number of elements in 77031 is: 351
Change your 2nd else block to this and remove the return statement after that block.
else {
push #seq, $n;
my $seq = "#seq";
undef #seq;
return $seq;
}
You just need to initialize the array #seq before calling the hail subroutine a second time. Try this ...
my #seq;
print "\nThe hailstone sequence for 724 is:\n" . &hail(8) . "\n\n";
#seq = ();
print "The hailstone sequence for 27 is:\n" . &hail(16) . "\n\n";
my $n;
Similar to what others have posted.
sub hailstone {
my $n = shift;
return if $n == 1;
if ( $n % 2 == 0 ){
$n = $n / 2;
} else {
$n = ( $n * 3 ) + 1;
}
return $n."\n",hailstone($n)
}
Upon calling the subroutine:
say hailstone(5);
16
8
4
2
1

Resources