Problem with an Algorithm

I'm writing a program that analyzes winnings and losses for a combination of wagers (x2 round robin) and need to write a loop that determines the earnings if a certain team loses. I've gotten started but I am getting answers that don't make sense. I tried tracing the loop and still don't see where I'm going wrong.

Here it is:

1
2
3
4
5
6
7
8
9
10
    for (int j=1; j<(num-1); j++)
    {
        for (int i=(j+1); i<num; i++)
        {
            wins = wins + bet[j][i];//something here is wrong!
        }
    }

    total = losses - wins;
    cout << "If " << team[0] << " loses, you lose " << total << ".\n";



Here is my whole code so far if it is more helpful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>

using namespace std;


int main()
{
    int num;
    cout << "Enter the number of teams:\n";
    cin >> num;
    int numberofbets=0;

    for (int i=1; i<num; i++)
    {
        int stud;
        stud = (num - i);
        numberofbets= numberofbets + stud;
    }

    string team[num];
    double price[num];
    int unit;
    double mult[num];
    double bet[50][50];
    double wins = 0.0;
    double losses = 0.0;
    double total = 0.0;

    losses = unit * (num-1);

    cout << "Enter the teams and the price one at a time.\n";

    for (int i=0; i<num; i++)
    {
        cout << "Team: \n";
        cin >> team[i];
        cout << "Price: \n";
        cin >> price[i];
    }

    cout << "Enter unit size:\n";
    cin >> unit;

    for (int i=0; i<num; i++)
    {
         mult[i] = 1 + (100/(price[i]));
    }

    for (int j=0; j<(num-1); j++)
    {
        for (int i=(j+1); i<num; i++)
        {
            bet[j][i] = unit * ((mult[j]*mult[i])-1);
        }
    }

    cout << "Summary: \n";

    for (int j=0; j<(num-1); j++)
    {
            for (int i=(j+1); i<num; i++)
            {
                cout << "\n" << team[j] << "\n" << team[i] << "\n  Bet " << unit << " to win " << bet[j][i] << ".\n";
            }
    }

    for (int j=1; j<(num-1); j++)
    {
        for (int i=(j+1); i<num; i++)
        {
            wins = wins + bet[j][i];//something here is wrong!
        }
    }

    total = losses - wins;
    cout << "If " << team[0] << " loses, you lose " << total << ".\n";

    wins = 0.0;

    for (int j=0; j<(num-1); j++)
    {
        for (int i=(j+1); i<num; i++)
        {
            wins = wins + bet[j][i];
        }
    }

    cout << "If all teams win, you win " << wins << ".\n";

    return 0;
}

Line 29: losses = unit * (num-1); unit is uninitialized in this point.

1
2
3
4
    int num;
    cin >> num;    
    string team[num]; //this is not standard
    double price[num]
use over-dimension, dynamical allocation or stl container.

Line 67: for (int j=1; j<(num-1); j++) Why you don't start from 0?

Comment your code
That seems to be the problem, thanks. And I don't start that loop from 0 because I don't want to include the j=0 elements of the array in winnings when team[0] loses.
Topic archived. No new replies allowed.