Still have a totaling issue after modifying code

This is a discussion on Still have a totaling issue after modifying code within the C Programming forums, part of the General Programming Boards category; I am still having a problem with total function. The function is adding the row values, and place the total ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Question Still have a totaling issue after modifying code

    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");
    	
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays start at zero, not 1. I swear I covered this exact same thing with you yesterday. Must have been someone else using a 7 x 6 array, right?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    The for loops in the total function start with 1 instead of 0, because I do not want to include the values in the first row or first column in the totals.
    The first row [0] was prefilled with 1,2,3,4 to represent the salesperson number.
    The first column [0] was prefilled with 1,2,3,4,5 to represent the product number.

    The first and second for loops in the totals function are working, but the thrid and fourth are not working, and this is where I am stuck as to why

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Thats becuase of the silly mistakes that you do

    shouldn't this be
    Code:
    for(rows < 1; rows < 6; ++rows)
    this
    for(rows = 1; rows < 6; ++rows)
    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a function that sums a specified row, and returns that value. Make one that does the same, but for a column. Call each of those one at a time, in a loop, to assign that sum to the right spot.

    Edit: nice catch ssharish

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    and also the semi-colon at the end. Why????

    Code:
    for(rows < 1; rows < 6; ++rows);
    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Self modifying code
    By orbitz in forum Linux Programming
    Replies: 1
    Last Post: 06-06-2003, 06:09 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21