Answer to the Indentation Self-Test
Obviously, there are problems with that program other than the
indentation. If you want to learn how to fix the other problems and
learn what the program does, visit the
Developing Good Programming Style page.
Indentation is just one part of good style.
Your answer is probably not going to look exactly like mine; not to worry.
If you made similar decisions, then your answer is probably just fine.
Here is my solution:
#include <stdio.h>
void count(int, int, int, int, int*);
int main(void)
{
int d1, d2, d3, d4, m=0;
for (d1=0; d1<2; d1++)
for (d2=0; d2<10; d2++)
for (d3=0; d3<6; d3++)
for (d4=0; d4<10; d4++)
count(d1,d2,d3,d4,&m);
return 0;
}
void count (int d1, int d2, int d3, int d4, int *m)
{
int seg[10] = {6,2,5,5,4,5,6,3,7,6};
int ts,td;
if ((!((d1==0)&&(d2==0))) && (!((d1==1)&&(d2>2))))
{
if (d1==0)
{
ts = seg[d2] + seg[d3] + seg[d4];
td = d2 + d3 + d4;
if (ts == td)
{
(*m)++;
printf(" %1d:%1d%1d\n",d2,d3,d4);
}
}
else
{
ts = seg[d1] + seg[d2] + seg[d3] + seg[d4];
td = d1 + d2 + d3 + d4;
if (ts == td)
{
(*m)++;
printf("%1d%1d:%1d%1d\n",d1,d2,d3,d4);
}
}
}
}
Answers to some common questions:
- Why did you add a function?
There wasn't much choice; the nested loops, if indented properly, leave
very little room per line for code inside the inner-most loop. Adding
the function added room for the loop body to be indented properly within
the confines of an 80-column screen.
- Why did you choose the "align braces" style?
Why not? I know, I know; it's not the style I usually use. So long as
I use it consistently throughout the program (and I think I did), it's
a completely acceptable style.
- What the heck does this program do, anyway?
Go back and read the top of this page again.