0

Here is the output for the following code. Can someone help me put the five prods on rows and sales reps on columns w a few changes???
Enter seller's code (-1 to exit): 2
Enter product code: 2
Enter sold for seller 2, product 2: 2
Enter sellers code (-1 to exit): 3
Enter product code: 3
Enter sold for seller 3, product 3: 3
Enter sellers code (-1 to exit): 4
Enter product code: 4
Enter sold for seller 4, product 4: 4
Enter sellers code (-1 to exit): -1


[P\F] [ 1] [ 2] [ 3] [ 4] [ 5] [ Tot ]
[ 1] [0.00] [0.00] [0.00] [0.00] [0.00] [0.00]
[ 2] [0.00] [2.00] [0.00] [0.00] [0.00] [2.00]
[ 3] [0.00] [0.00] [3.00] [0.00] [0.00] [3.00]
[ 4] [0.00] [0.00] [0.00] [4.00] [0.00] [4.00]
[Tot] [0.00] [2.00] [3.00] [4.00] [0.00] [9.00]

Press any key to continue . . .

#include <stdio.h>

#define SELLERS 5
#define PRODUCTS 4


int main(void)
{/***********/

    void print_matrix(const float matrix[PRODUCTS][SELLERS],
		      int prods, int slrs);

    int sel_code;
    int prod_code;
    float sold;
    float sales[PRODUCTS][SELLERS] = { 0 };



    /* Enter the first sell-ticket: let's begin from seller
       code. If -1 exit */
    printf("Enter seller's code (-1 to exit): ");
    scanf("%d", &sel_code);

    while (sel_code != -1) {

	/* Get the product code */
	printf("Enter product code: ");
	scanf("%d", &prod_code);

	/* Get the sold */
	printf("Enter sold for seller %d, product %d: ",
	       sel_code, prod_code);
	scanf("%f", &sold);

	/* Update the sales matrix */
	sales[prod_code - 1][sel_code - 1] += sold;

	/* Restart the while */
	printf("Enter sellers code (-1 to exit): ");
	scanf("%d", &sel_code);




    }

    print_matrix(sales, PRODUCTS, SELLERS);

    return 0;
}



void print_matrix(const float matrix[PRODUCTS][SELLERS],
		  int prods, int slrs)
{/*******************************************************/

    int i, j;			/* indexes for rows and cols */
    float rowsum = 0;
    float colsum = 0;
    float totalsum = 0;

    printf("\n\n");

    printf("[P\\F]\t[   1]\t[   2]\t[   3]\t[   4]\t[   5]\t[ Tot ]\n");

    for (i = 0; i < prods; i++) {


	for (j = 0; j < slrs; j++) {
	    /* print the row header */
	    if (j == 0)
		printf("[%3d]\t", i + 1);
	    /* print the row */
	    printf("[%4.2f]\t", matrix[i][j]);
	    /* print the row sums */
	    if (j == slrs - 1) {
		for (j = 0; j < slrs; j++)
		    rowsum += matrix[i][j];
		printf("[%4.2f]", rowsum);
		/* restart to 0 */
		rowsum = 0;
	    }


	}
	printf("\n");

    }


    /* colums sum */
    printf("[Tot]\t");

    for (j = 0; j < slrs; j++) {

	for (i = 0; i < prods; i++)
	    colsum += matrix[i][j];
	printf("[%.2f]\t", colsum);
	totalsum += colsum;
	colsum = 0;
    }

    printf("[%.2f]", totalsum);

    printf("\n\n");

}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jephthah
0

swap the product and seller indices

[pseudo]
print columnheaders, newline
for p = 1 to NUMPRODS
   print "product #", p
   for s = 1 to NUMSELLERS
      print matrix[s-1][p-1]
      next s
   print newline
   next p
done
[/pseudo]

.

Edited by jephthah: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.