These puzzles involve arrays. Arrays are extremely important in programming. I doubt that there is any practical program that does not use arrays extensively. If you wish to be a programmer, you must become familiar with arrays.
[E-6]
Write a subroutine void printArray( int arr[], int size )
that
prints the elements of an integer array one per line. Write a main()
program that declares an array and prints it out using the function. Here is
some typical output:
index value ----- ----- 0: 0 1: 9 2: 23 3: -6 4: 5 5: 2 6: 71 7: 45 8: -9 9: 3
Here is a skeleton of the program. Your job is to finish it. If you are using a programming environment (such as Bloodshed Dev-c++) use copy-and-paste to copy this code into a source file.
#include <stdio.h> #include <stdlib.h> /* Puzzle C01 — print each element in an array of ints, one per line */ void printArray( int arr[], int size ) { } int main(int argc, char *argv[]) { int x[] = {0, 9, 23, -6, 5, 2, 71, 45, -9, 3 }; printArray( x, 10 ); printf("\n"); system("pause"); /* remove this if desired */ return 0; }
[E-8]
Write a function that prints the elements of an integer array, 10 elements
per line. Don't write out the array indexes.
Here is a framework:
void printArray( int arr[], int size ) { } int main(int argc, char *argv[]) { int x[] = { 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 }; printArray( x, 40 ); printf("\n"); system("pause"); return 0; }
[E-4]
Write a function that fills an array by putting the same integer in
each slot.
Here is a framework:
#include <stdio.h> #include <stdlib.h> /* Puzzle C03 - fill an array by making each element the same integer x */ void fillArrayConst( int arr[], int size, int x ) { ... fill in here ... } void printArray( int arr[], int size ) { ... from above ... } const int SIZE = 100; int main(int argc, char *argv[]) { int x[ SIZE ]; fillArrayConst( x, SIZE, 7 ); printArray( x, SIZE ); printf("\n"); system("pause"); return 0; }
[E-4]
Write a function that fills an array with integers in ascending order, starting
with a designated integer.
[E-4]
Write a function that fills an array with integers in descending order, starting
with a designated integer.
[E-4]
Write a function that fills an array with random integers.
Pick integers in the range Low..High (inclusive.)
Here is the output of a program that tests this function by
filling an array of 100 elements with integers in the range 0..100.
0 56 19 81 59 48 35 90 83 75 17 86 71 51 30 1 9 36 14 16 99 45 12 0 0 38 53 57 60 61 16 66 45 35 5 61 79 81 52 30 88 73 96 93 54 14 46 23 87 21 78 85 100 100 61 39 26 30 84 2 37 9 68 5 0 92 27 27 59 69 84 73 48 20 75 47 46 95 75 10 60 38 74 61 57 36 15 22 42 81 52 99 75 34 17 66 49 6 70 50
Use a function from the B section of puzzles to generate the
random integers.
[H-3]
Or do this from scratch if you want practice.
[E-4]
Write a function that fills an array with randomly selected double precision floats
in a specified range. Notice that the range does not include the high value.
Here is sample output of a program that tests the function,
using a range of 0.0 to 10.0.
Printing the array is done with the solution of the next puzzle.
0.0125 5.6357 1.9330 8.0872 5.8499 4.7986 3.5028 8.9594 8.2281 7.4658 1.7410 8.5892 7.1048 5.1352 3.0399 0.1498 0.9140 3.6444 1.4731 1.6589 9.8849 4.4568 1.1908 0.0467 0.0891 3.7787 5.3165 5.7117 6.0175 6.0715 1.6623 6.6302 4.5078 3.5211 0.5704 6.0767 7.8329 8.0258 5.1987 3.0194 8.7595 7.2665 9.5587 9.2569 5.3934 1.4233 4.6207 2.3532 8.6221 2.0959 7.7963 8.4363 9.9677 9.9966 6.1148 3.9243 2.6620 2.9727 8.4012 0.2374 3.7585 0.9262 6.7719 0.5621 0.0879 9.1876 2.7588 2.7289 5.8789 6.9116 8.3759 7.2647 4.8492 2.0535 7.4371 4.6844 4.5795 9.4913 7.4442 1.0828 5.9903 3.8522 7.3499 6.0895 5.7239 3.6133 1.5155 2.2510 4.2514 8.0286 5.1709 9.8996 7.5153 3.4555 1.6898 6.5729 4.9188 0.6354 6.9974 5.0479Work on puzzles C07 and C08 together. You could do C08 first, but it would be tedious to test without a function that fills an array.
[E-7]
Write a function that prints the elements in an array of doubles. See the above
for example output.
[E-6]
Write a function that fills an array with integers, where each integer is larger
than its predecessor by a random amount in the range 1..MaxStep. Start the array
with a random value between 0 and MaxStep-1. A prototype for the function is:
void fillArrayRandomAscending( int arr[], int size, int maxStep )
Sample output of a main program that tests the function for a MaxStep of 10 is:
0 6 8 17 23 28 32 41 50 58 60 69 77 83 87 88 89 93 95 97 107 112 114 115 116 120 126 132 139 146 148 155 160 164 165 172 180 189 195 199 208 216 226 236 242 244 249 252 261 264 272 281 291 301 308 312 315 318 327 328 332 333 340 341 342 352 355 358 364 371 380 388 393 396 404 409 414 424 432 434 440 444 452 459 465 469 471 474 479 488 494 504 512 516 518 525 530 531 538 544
This array started out at 0, but it could have started with any value between 0 and 9.
[M-15]
Write a function that fills an array with a sequence of double precision floats.
Each float is greater than its predecessor by an average amount avg
,
but may
be greater or less than avg
by up to but not including an amount dev
. In other
words, the amount inc
added to one element to get the next is in the range
(avg-dev)< inc < (avg+dev).
A prototype for the function is:
void fillDoubleArrayRandomAscending( double arr[], int size, double avg, double dev )
Start the array with a value
(avg-dev)< arr[0]< (avg+dev).
avg
of 1.0 and
a dev
of 1.5 is:
0.4564 2.5835 3.9723 4.6389 5.0146 7.2102 6.7249 7.8433 9.0546 9.9227 11.3645 11.0373 12.0200 13.9607 16.0684 16.1864 17.5251 18.2726 20.6133 22.2673 24.6562 25.9857 26.4264 27.8649 28.9353 30.9444 31.2108 31.3442 31.1604 32.2450 32.4328 32.1780 32.1044 33.7280 33.6398 36.0319 37.7449 37.5206 39.5221 39.2572 40.0321 40.3241 42.2031 43.4218 44.7634 45.0128 46.1254 47.1837 48.4832 49.9341
With that choice for avg
and dev
,
the array is more-or-less ascending, but
sometimes elements are out of order.