Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

total sales in array c++ two dimensional array

view sourceprint?
01
#include <iostream>
02
using std::cin;
03
using std::cout;
04
using std::endl;
05
 
06
#include <conio.h>
07
 
08
#include <iomanip>
09
using std::fixed;
10
using std::setw;
11
using std::setprecision;
12
using std::showpoint;
13
 
14
int main()
15
{
16
    const static int PEOPLE = 5; //number of sales people (4)
17
    const static int PRODUCTS = 6;  //number of different products (5)
18
 
19
    int sales[5][6] = {{1, 2, 3, 4}, {1, 2, 3, 4, 5}};
20
    double value;
21
    double totalSales;
22
    double productSales[PRODUCTS] = {0.0};
23
    int salesPerson;
24
    int product;
25
 
26
    cout << " Enter the salesperson's number (1-4), the product number (1-5), and total product sales.
 "
27
         << "Enter -1 for salesperson to end input.
";
28
    cin >> salesPerson;
29
 
30
    while (salesPerson != -1)
31
    {
32
        cin >> product >> value;
33
 
34
            value++;
35
         
36
        cin >> salesPerson;
37
    }
38
 
39
    cout << "
The total sales for each salesperson are displayed at the "
40
        << "end of each row,
" << "and the total sales for each product "
41
        << "are displayed at the bottom of each column.
 " << setw( 12 )
42
        << 1 << setw( 12 ) << 2 << setw( 12 ) << 3 << setw( 12 ) << 4
43
        << setw( 12 ) << 5 << setw( 13 ) << "Total
" << fixed << showpoint;
44
 
45
    for (int i = 1; i < 4; i++)
46
    {
47
        totalSales = 0.0;
48
        cout << i;
49
 
50
        for (int j = 1; j < 5; j++)
51
        {
52
            ++totalSales;
53
 
54
            cout << setw (12) << setprecision (2) << sales [i][j];
55
 
56
            += productSales;
57
        }
58
 
59
        cout << setw (12) << setprecision (2) << totalSales << '
';
60
   }
61
 
62
    cout << "
Total" << setw( 8 ) << setprecision( 2 )
63
      << productSales[ 1 ];
64
 
65
   // display total product sales
66
   for ( int j = 2; j < totalSales; j++ )
67
      cout << setw( 12 ) << setprecision( 2 ) << productSales[ j ];
68
 
69
   cout << endl;
70
    
71
   _getch();
72
}
Source by www.dreamincode.net #
 
PREVIOUS NEXT
Tagged: #total #sales #array #dimensional #array
ADD COMMENT
Topic
Name
6+7 =