I am still having a problem with total function.
The function is adding the row values, and place the total in the last column, and this works.
The function is also suppose to add the values in each column, and place the column totals in the last row, this does not work, and I don't understand why.
Here is the output
Enter the Salesperson number: 1
Enter the Product number: 1
Enter the Amount: 1111
Are you done? 1 = Yes 0 = No
0
Enter the Salesperson number: 1
Enter the Product number: 2
Enter the Amount: 1111
Are you done? 1 = Yes 0 = No
0
Enter the Salesperson number: 2
Enter the Product number: 1
Enter the Amount: 2222
Are you done? 1 = Yes 0 = No
0
Enter the Salesperson number: 2
Enter the Product number: 2
Enter the Amount: 2222
Are you done? 1 = Yes 0 = No
1
0 1 2 3 4 0
1 1111 2222 0 0 0
2 1111 2222 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
5 0 0 0 0 0
0 0 0 0 0 0
0 1 2 3 4 0
1 1111 2222 0 0 3333
2 1111 2222 0 0 3333
3 0 0 0 0 0
4 0 0 0 0 0
5 0 0 0 0 0
0 0 0 0 0 0
Column 1 should have a value of 2222 in the last row
Column 2 should have a value of 4444 in the last row
Code:
#include <stdio.h>
/* Preload the array with predetermined values */
int sales[7][6] = {{0, 1, 2, 3, 4}, {1}, {2}, {3}, {4}, {5}};
/* Define Functions */
/* print out table */
void print_table(void)
{
int rows;
int columns;
for(rows = 0; rows < 7; ++rows)
{
for(columns = 0; columns < 6; ++columns)
printf("%5i", sales[rows][columns]); /* %5i makes column 5 characters wide */
printf("\n");
}
}
/* zeros out the table */
void zero_table(void)
{
int rows;
int columns;
for(rows = 1; rows < 7; ++rows)
{
for(columns = 1; columns < 6; ++columns)
sales[rows][columns] = 0; /* all values set to zero */
}
}
int get_information(int number, int low, int high) /* "number" is "salesperson" or "product" number */
{
int a;
do
{
printf("Enter "); /* add extra space to improve look of output */
switch(number) /* no semi colon after switch statement */
{
case 1: printf("the Salesperson number: "); /* add two extra spaces to improve look of output */
break;
case 2: printf("the Product number: "); /* add two extra spaces to improve look of output */
break;
case 3: printf("the Amount: "); /* add two extra spaces to improve look of output */
break;
}
scanf("%i", &a);
if(a < low || a > high)
printf("Invalid entry... please try again \n");
}
while(a < low || a > high);
return(a);
}
/* this function will repeat a infinite number of times until zero is selected for Are you done? */
void enter_all_daily_sales(void)
{
int done;
int salesperson;
int product;
int amount;
do
{
salesperson = get_information (1, 1, 4);
product = get_information (2, 1, 5);
amount = get_information (3, 0, 9999999); /* need to pick a high number for the last entry */
sales[product][salesperson] = sales[product][salesperson] + amount;
printf("Are you done? 1 = Yes 0 = No \n");
scanf("%i", &done);
}
while(! done);
}
/* this function adds up the values in each row, and places total for each row in last column */
/* after adding the row values, column values are added, and total placed for each column in last row */
void totals(void)
{
int columns;
int rows;
/* this function is broken */
/* function is only totaling the rows & displaying row totals in last column */
/* function is suppose to total columns, display column totals in last row, but does not work */
/* adding extra curley braces did nothing */
for(rows = 1; rows < 6; ++rows)
{
for(columns = 1; columns < 5; ++columns)
sales[rows][5] = sales[rows][5] + sales[rows][columns];
}
for(columns = 1; columns < 5; ++columns)
{
for(rows < 1; rows < 6; ++rows);
sales[6][columns] = sales[6][columns] + sales[6][rows];
}
}
int main(void)
{
/* this will print the table so the format of rows and columns in the table can be displayed */
print_table();
/* this printf statement just adds space between table print outs */
printf("\n\n\n\n");
zero_table();
/* this will re-print the table, and can verify all values are zero */
print_table();
printf("\n\n\n\n");
enter_all_daily_sales();
/* this will print the table without totally the rows and columns */
print_table();
printf("\n\n\n\n");
/* this function is broken */
/* function is only totaling the rows & displaying row totals in last column */
/* function is suppose to total columns, display column totals in last row, but does not work */
totals();
/* this will print the table after totally the rows and columns */
print_table();
printf("\n\n\n\n");
}