Getting an error when totaling table

This is a discussion on Getting an error when totaling table within the C Programming forums, part of the General Programming Boards category; I am a novice to C programming, and a program I am writing for school is crashing when attempting to ...

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

    Question Getting an error when totaling table

    I am a novice to C programming, and a program I am writing for school is crashing when attempting to total all values in table, and I cannot figure out what I am doing wrong in the void Totals(void) function. Any suggestions would be great.

    Code:
    /* The name of this program is Week 4 Classroom */
    
    /* This program prints out sales information, number of sales persons, products sold, and amount of money */
    
    #include <stdio.h>
    
    /* Preload array with values */
    int sales[7][6] = {{0, 1, 2, 3, 4}, {1}, {2}, {3}, {4}, {5}};
     
    
    /* Define Functions */
    
    /* print out blank table */
    void print_table(void)
    {
    	int r, c; /* r = row and c = column */
    
    	for(r = 0; r < 7; ++r)
    	{
    		for(c = 0; c < 6; ++c)
    
    			printf("%5i", sales[r][c]); /* %5i makes column 5 characters wide */
    		printf("\n");
    	}
    }
    
    
    /* zeros out the table */
    void zero_table(void)
    {
    	int r, c; /* r = row and c = column */
    
    	for(r = 1; r < 7; ++r)
    	{
    		for(c = 1; c < 6; ++c)
    
    			sales[r][c] = 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");
    
    	switch(number) /* no semi colon after switch statement */
    	{
    	case 1: printf("Salesperson");
    			break;
    	
    	case 2: printf("Product");
    			break;
    
    	case 3: printf("Amount");
    			break;
    	}
    
    	scanf("%i", &a);
    
    	if(a < low || a > high)
    		printf("Invalid entry... please try again \n");
    	}
    	while(a < low || a > high);
    
    	return(a);
    }
    
    
    
    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);
    
    		sales[product][salesperson] = sales[product][salesperson] + amount;
    
    			printf("Are you done? 1 = Yes 0 = No \n");
    			scanf("%i", &done);
    	}
    		while(! done);
    }
    
    
    
    void totals(void)
    {
    	int c;  /* c = columns */
    	int rows;
    
    	for(rows = 1; c < 6; ++rows)
    
    		for(c = 1; c < 5; ++c)
    
    			sales[rows][5] = sales[rows][5] + sales[rows][c];
    
    		for(c = 1; c < 5; ++c)
    
    			for(rows = 1; rows < 6; ++rows);
    
    			sales[6][c] = sales[6][c] + sales[6][rows];
    }
    
    /* Declare Global Variables */
    
    
    
    int main(void)
    {
    	print_table();
    	printf("\n\n\n\n");
    	zero_table();
    	print_table();
    	printf("\n\n\n\n");
    	enter_all_daily_sales();
    	print_table();
    	printf("\n\n\n\n");
    	totals();
    	print_table();
    
    	printf("\n\n\n\n");
    	
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, arrays start at 0 index, instead of 1. So, if we make an array with six elements:
    Code:
    int array[ 6 ];
    We have the following valid indexes:
    Code:
    array[ 0 ];
    ...
    array[ 5 ];
    It is invalid to try to use index six:
    Code:
    array[ 6 ] = ERROR;

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

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

    Question Still not sure why this is causing problem

    I still don't understand what the first reply means. Please remember I am a novice, additional explanations and or suggestions welcomed

    My program creates a table, and then totals the values in the tables

    A salesperson sold 4 of product 1, 1 of product 2, 2 of product 3, 0 of product 4, and total sales is 4000

    The program allows user to manually enter the salesperson number, the quanity of product # sold, and the daily sales total until all data is entered

    Program should provide a total of all rows in column 6, and totals for each column in row 7 of the table, and the totaling is where my program generates an error

    salesperson 1 2 3 4 total sales
    product 1 4 1 2 0 4000
    product 2 1
    product 3 2
    product 4 0
    product 5 0
    total total total total

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is *NO* column 6, nor row 7 in your array. Arrays in C are zero-based, meaning that they always start with 0, not indexed to 1. Your sales array goes from 0 to 6 rows, with 0 to 5 columns.

    Any reference beyond column 5 or row 6, is an error and the result depends on your compiler, and the C standards it has chosen to follow (or not). That is what is wrecking your program.

    The rule is - don't work with data outside of the bounds of your array. This is a constant concern for C programmers, both on the high and the low bounds of the array.

    With that clear in your mind, I think you'll have no problem fixing your program, now.

    Just remember to check your "edges" of the array bounds, and remember to count like C programmers do: "0, 1, 2, 3, 4...", instead of "1,2,3,4"

    Welcome to C!
    Last edited by Adak; 09-30-2009 at 02:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 06:38 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 06:06 PM
  3. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 06:54 PM
  4. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 05:03 PM
  5. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 08:18 AM

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