Hi,
I am trying to write a program (exercise from a book) that reads in information about salespersons and products they sold and summarize output in tabular format. I am nearly there, when i compile it there are lots of errors (i'm very new 2 c, about 2 weeks), and later i fixed all of them except for two. Pls spot them for me. Thnx in advance! Also if possible, is there a way to make the code even better? Thnx again.
Code:/* Read in salesperson and product information, process them and output in tabular format. */ #include <stdio.h> #define PERSON 4 #define PRODUCT 5 /* Function prototype */ void output( const int array[][ PRODUCT ], int person, int product ); int main() { int sales[ PERSON ][ PRODUCT ] = { 0 }; int i, j; /* Loop counters */ for ( i = 0; i <= PERSON - 1; i++ ) { for ( j = 0; j <= PRODUCT - 1; j++ ) { printf( "Enter cost of product %d for sales-person %d (Enter -1 if nothing): ",i, j); scanf( "%d", &sales[ i ][ j ] ); /* Store input in array */ } printf( "\n" ); } output( sales, PERSON, PRODUCT ); /* Print summary */ return 0; } void output( const int array[][ PRODUCT ], int person, int product ) { int i, j, k, total = 0; printf( "\n ***Report***\n" ); /* Print title */ printf( "%s%4s%4s%4s%6s", "Person 1", "Person 2", "Person 3", "Person 4", "Totals" ); /* Column headers */ for ( i = 0; i <= person + 1; i++ ) { printf( "\n" ); printf( "Product %d", i ); if ( i != 4 ) { for ( j = 0; j <= product; j++ ) { if ( array[ i ][ j ] == -1 ) printf( "%4s", "/" ); else { printf( "%4d", array[ i ][ j ] ); total += array[ i][ j]; if ( j == 4 ) printf( "%4d\n", total ); } total = 0; } } printf( "\nTotals" ); printf( "%4d", array[ 0 ][ 0 ] + array[ 0 ][ 1 ] + array[ 0 ][ 2 ] + array[ 0 ][ 3 ] + array[ 0 ][ 4 ] ); printf( "%4d", array[ 1 ][ 0 ] + array[ 1 ][ 1 ] + array[ 1 ][ 2 ] + array[ 1 ][ 3 ] + array[ 1 ][ 4 ] ); printf( "%4d", array[ 2 ][ 0 ] + array[ 2 ][ 1 ] + array[ 2 ][ 2 ] + array[ 2 ][ 3 ] + array[ 2 ][ 4 ] ); printf( "%4d", array[ 3 ][ 0 ] + array[ 3 ][ 1 ] + array[ 3 ][ 2 ] + array[ 3 ][ 3 ] + array[ 3 ][ 4 ] ); for ( k = 0; k <= 50; k++ ) printf( "*" ); printf( "\n" ); }



LinkBack URL
About LinkBacks



. However, a legitimate advantage this change has is that the values of PERSON and PRODUCT may be changed in the future without having to change any code here. For example, if the store hired additional salespeople or introduced a new product, it would be much easier to change the preprocessor definitions of PERSON and PRODUCT than it would to rewrite the output() function each time.