Skip to main content

Sales of products by sales-persons

Short answer

Use a 3x3 array for sales data (rows: salespersons, columns: products). While reading each entry, add it to row and column accumulators. Then display each product total (column sum) and each salesperson total (row sum).

A multinational company has hired 3 sales persons for marketing/selling its 3 different products in Kathmandu. Each sales person sells each of these products. Write a program to read number of each product sold by all sales-persons. Calculate total sells of each item and the total sells of each sales-person. Use arrays.

Assumptions. Three sales persons (rows) and three products (columns). A 3×3 array sold[i][j] holds the number of product j sold by person i. Two 1D arrays hold the totals: personTotal[i] (row sum) and prodTotal[j] (column sum). Both are updated while reading each value.

#include <stdio.h>

int main(void)
{
    int sold[3][3], prodTotal[3] = {0}, personTotal[3] = {0};

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) {
            printf("Units of product %d sold by salesperson %d: ", j + 1, i + 1);
            scanf("%d", &sold[i][j]);
            personTotal[i] += sold[i][j];
            prodTotal[j]   += sold[i][j];
        }

    printf("\n");
    for (int j = 0; j < 3; j++)
        printf("Total sales of product %d = %d\n", j + 1, prodTotal[j]);
    for (int i = 0; i < 3; i++)
        printf("Total sales of salesperson %d = %d\n", i + 1, personTotal[i]);
    return 0;
}

Sample run

Input (entered one after another): 10 20 30 5 15 25 8 12 16

Total sales of product 1 = 23
Total sales of product 2 = 47
Total sales of product 3 = 71
Total sales of salesperson 1 = 60
Total sales of salesperson 2 = 45
Total sales of salesperson 3 = 36

Check: row totals 60 + 45 + 36 = 141 and product totals 23 + 47 + 71 = 141.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 7 questions →