Anyway to make this simpler...

Probably to much to ask, and I think I have the code down to as simple as I'm going to get it. However I'd still like someone else to look at it to see if it's as short as it can be. I tend to have my code's Needlessly long, and loss marks on it because of it. It is a homework assignment That is complete and does what it's suppose to. Just trying to tweak it so it's shorter and does what it's suppose to.... Any ideas?

I'm using visual studio 2008 professional. ( Gotta it free off dreamspark and I use it in school for my programming quiz's so I got no plans on changing to another program.)

Here's the code.

#include "stdafx.h"
#define SIZE 10

int main( void )
{
int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
int i;
int j;
int count;

printf( "Histogram\n\n" );
printf( "Value Histogram\n" );

for ( count = 20; count > 0; count--)
{
if( count > 9 )
{
printf( " %d ", count );
}
else
{
printf( " %d ", count);
}
for( i = 0; i < SIZE; i++ )
{
if( n[ i ] >= count )
{
printf( "*" );
}
else
{
printf( " " );
}
}
printf( "\n" );
}
printf( "Element 0123456789" );
scanf( "%d, &wait" );
return 0;
}

Output is:

Histogram

Value Histogram
20
19 *
18 *
17 * *
16 * *
15 * * *
14 * * *
13 * * * *
12 * * * *
11 * * * * *
10 * * * * *
-9 * * *** *
-8 * * *** *
-7 * ***** *
-6 * ***** *
-5 * *******
-4 * *******
-3 *********
-2 *********
-1 **********
Element 0123456789

Any help or idea would be great Thanks =D
cout would be simpler than printf. cin simpler than scanf
you should use switch statement to make is more simple and easy to read
Use code and output tags for legibility. Format options on the right, <> for code, [code] your code here [/code]. Similarly for output.
Both the source code and output are unreadable without these tags.

Well, this looks like C code rather than C++. If that's what you are required to do, it limits some of the options available. But still the code can be simplified and shortened quite a lot.

First, you can make better use of the formatting options of printf().
Second, you could use the ternary operator to simplify the code. You might also consider the use of puts(), putchar() and getchar() as alternatives to printf and scanf when dealing with a single character.or a straightforward string.

If you do those things, this block reduces to a single line of code:
1
2
3
4
5
6
7
8
		if( count > 9 )
		{
			printf( "  %d     ", count );
		}
		else
		{
			printf( "   %d     ", count);
		}


and this reduces to one or two lines depending on taste:
1
2
3
4
5
6
7
8
			if( n[ i ] >= count )
			{ 
				printf( "*" );
			}
			else
			{
				printf( " " );
			}

In the first case above, use a suitable printf format string.
In the second, use the ternary operator.

I got the code down to 25 lines, and that includes several blank lines for readability. (If I scrunch it up until it looks ugly, it's down to 16 lines).

see "conditional operator" on this page:
http://www.cplusplus.com/doc/tutorial/operators/
and printf here http://www.cplusplus.com/reference/clibrary/cstdio/printf/
Last edited on
Thanks, always helpful to learn neat tricks on how to write code...

as for the cin and cout. I'd perfer to use them myself but the instructor says we shouldn't. Just gotta stick by how it's done in the book...
instead of declaring each 'counters' like
1
2
int i;
int j;


why don't you 'declare on spot' so,

1
2
3
4
5
6
7
8

for(i = 0;i < SIZE;i++) // instead of that...


for(int i = 0; i < SIZE;i++) // have that, then your code will be shorter  and 
//won't waste memory space

Last edited on
Topic archived. No new replies allowed.