A Pair of Arrays

Removing redundancies from your C code may not be your first goal, but it’s something you can definitely find later. One example is when you must initialize a pair of arrays. Why use two loops when one will do?

For this month’s Exercise, your challenge is to initialize two 50-element arrays, alpha[] and beta[]. Sounds simple, but array alpha[] is set to values 1 through 50 while array beta[] is set to values 51 through 100. Here is sample output showing each array’s values:

 1	51
 2	52
 3	53
 4	54
 5	55
 6	56
 7	57
 8	58
 9	59
10	60
...
40	90
41	91
42	92
43	93
44	94
45	95
46	96
47	97
48	98
49	99
50	100

If you believe that this Exercise sounds simple, try performing the initialization using only one loop. Further, use a single statement to assign the values to both array elements. Remember, the goal here is to optimize code. A single loop can easily initialize the two arrays, but a single statement?

Click here to view my solution, but please try this challenge on your own before you peek to see what I’ve done.

Leave a Reply