I’ve update my code Could you please have a look at it

I need to write a right justified two user defined functions that use array parameter. I'm having a problem with my counters in the last section not keeping a accurate total.Any help would be greatly appreciated.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    
  
#include <iostream>
#include <iomanip>

using namespace std;


void inputData(int Act[], int Exp[], int size);
void printArray(int Act[], int Exp[], int size);
int running_total;
char sign;
int Poor =0;
int Distinquished = 0; 
int Average = 0;
int Outstanding = 0;
int Commendable = 0;



int main()



{
  
 
    int Exp[13] = {6, 7, 7, 5, 6, 6, 8, 7, 7, 6, 6, 7, 5};
    int Act[13] = {0};
  

    cout << "Get the data" << endl;
    inputData(Exp, Act, 13);
    cout << "***********************************************"
         << endl << endl;


    printArray(Exp, Act, 13);
    cout << "***********************************************" 
         << endl << endl;
	
   

    return 0;
}



void inputData(int Exp[], int Act[], int size)
{
     int i;
	   
    for (i = 0; i < size; i++)
    {
      cout << "Week "<< i + 1 << " ( Expected Sales " << Exp[i] <<" ): ";
	  cin >> Act[i];
    }
}


void printArray(int Exp[], int Act[], int size)
{
    int var = 0;
    
    string Rating ;

    for (int i = 0; i < size; i++)
    
    {
    	var = Act[i] - Exp[i];
        if (var > 5) 						{Rating = "Distinquished";Distinquished++; }
           
        else if ( -1 <= var && var <= 1) 	{Rating = "Average";Average++; }
        
        else if (3 <var && var <=5)			{Rating = "Outstanding";Outstanding++; }
        
        else if (2 < var && var <=3)		{Rating = "Commendable";Commendable++; }
        
        else if (var <= -2)					{Rating = "Poor";Poor++ ;}
           
           
        //  Add more if statements here
      
        {
        	  running_total=running_total+var;
        if(running_total>0)
        	sign='+';
        else if
        	(running_total<0)
        	sign='-';
		}
        
        
         
        cout<<setw(6)<< "Week "<< i + 1 << ":Exp: "<<Exp[i]<<":Act: "<< Act[i]<< "- Rating:  "<< Rating<<sign<<running_total<<"(var:"<<var<<")"<<endl;

       
    }
    	 

    cout<<endl;
    cout<<setw(6)<<"Totals:"<<endl;
    cout<<setw(17)<<"Quarter Variance:"<<sign<<running_total<<endl;
    cout<<setw(14)<<"Distinquished:"<<Distinquished<<endl;
    cout<<setw(12)<<"Outstanding:"<<Outstanding<<endl;
    cout<<setw(12)<<"Commendable:"<<Commendable<<endl;
    cout<<setw(8)<<"Average:"<<Average<<endl;
    cout<<setw(5)<<"Poor:"<<Poor<<endl;
}

Last edited on
Hints:
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
// I need to write a right justified two user defined functions that use array 
// parameter. I'm having a problem with the second function, outputting a 
// summary of the quarterly sales results showing
// *the cumulative quarter variance
// *the number of distinguished ratings.
// Any help would be greatly appreciated.
#include <iostream>
#include <iomanip>

void inputData(int Exp[], int Act[], int size);
void printArray(int Exp[], int Act[], int size);

int main()
{
    int Exp[13] = {6, 7, 7, 5, 6, 6, 8, 7, 7, 6, 6, 7, 5};
    int Act[13] = {0};

    std::cout << "Get the data\n";
    inputData(Exp, Act, 13);
    std::cout << "***********************************************\n\n";
    printArray(Exp, Act, 13);
    std::cout << "***********************************************\n\n";
    return 0;
}

void inputData(int Exp[], int Act[], int size)
{
    for (int i = 0; i < size; i++)
    {
        std::cout << "Week "<< i + 1 << " (expected sales: " << Exp[i] << "): ";
        std::cin >> Act[i];
    }
}

void printArray(int Exp[], int Act[], int size)
{
    for (int i = 0; i < size; i++)
    {
        int var = Act[i] - Exp[i];
        std::string Rating;
        if (var > 5)                 { Rating = "Distinguished"; }
        else if (3 < var && var <=5) { Rating = "Outstanding"; }
        else if (2 < var && var <=3) { Rating = "Commendable"; }
        else if (1 < var && var <=2) { Rating = "Average"; }
        else if (var <= 1)           { Rating = "Poor"; }

        //  Add more if statements here

        std::cout << "Week " << i + 1 << ": Exp: " << Exp[i] 
                  << "; Act: " << Act[i] << " - Rating: "  << Rating 
                  << " (var: " << var << ")\n";
    }
}

Hi, fresh716.
If you modify an existing post of yours, it doesn’t bump up as it happens when you add a new one. So I suppose nobody noticed you’ve updated your code and needed further help.

I think you’d better adding a new post with the new code or just a phrase like: “I’ve update my code. Could you please have a look at it?” That way you ensure you get help from a wide group of programmers, and the answer you get is likely to be correct.

Although I’ve been glad to receive your private message, I’m bound to warn you I’ve been corrected several times by other forum users for having given poor answers. The fact my answer can be read by all the others is what guarantee you about its accuracy.

Hope this code can give you some hints about how to solve your issues. If you spent some word more on what you want to achieve, It would be easier for us to help you. For example, it’s not clear to me what do you want your variable “running_total” to represent.

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
#include <iostream>
#include <iomanip>

void inputData(int Exp[], int Act[], int size);
void printArray(int Exp[], int Act[], int size);

int main()
{
    int Exp[13] = {6, 7, 7, 5, 6, 6, 8, 7, 7, 6, 6, 7, 5};
    int Act[13] = {0};

    std::cout << "Get the data\n";
    inputData(Exp, Act, 13);
    std::cout << "***********************************************\n\n";
    printArray(Exp, Act, 13);
    std::cout << "***********************************************\n\n";
    return 0;
}

void inputData(int Exp[], int Act[], int size)
{
    for (int i = 0; i < size; i++)
    {
        std::cout << "Week " << std::setw(2) << i + 1 << " (expected sales: " << Exp[i] << "): ";
        std::cin >> Act[i];
    }
}

void printArray(int Exp[], int Act[], int size)
{
    int Poor = 0;
    int Average = 0;
    int Commendable = 0;
    int Outstanding = 0;
    int Distinquished = 0;
    int running_total = 0;
    for (int i = 0; i < size; i++)
    {
        int var = Act[i] - Exp[i];
        std::string Rating;
        if (var > 5)                 { Rating = "Distinguished"; Distinquished++; }
        else if (3 < var && var <=5) { Rating = "Outstanding";   Outstanding++; }
        else if (2 < var && var <=3) { Rating = "Commendable";   Commendable++; }
        else if (1 < var && var <=2) { Rating = "Average";       Average++; }
        else if (var <= 1)           { Rating = "Poor";          Poor++; }

        running_total += var;

        std::cout << "Week " << std::setw(2) << i + 1 
                  << ": Exp: " << Exp[i] << "; Act: " << std::setw(2) << Act[i] 
                  << " - Rating: " << std::setw(13) << Rating << ' '
                  << std::showpos << std::setw(3) << running_total
                  << " (var: " << var << ")\n" << std::noshowpos;
    }
    
    std::cout << "\nTotals:\nQuarter variance: " << std::showpos << running_total
              << '\n' << std::setw(18) << "Distinguished: " << Distinquished
              << '\n' << std::setw(18) << "Outstanding: "   << Outstanding
              << '\n' << std::setw(18) << "Commendable: "   << Commendable
              << '\n' << std::setw(18) << "Average: "       << Average
              << '\n' << std::setw(18) << "Poor: "          << Poor << '\n';
}

Topic archived. No new replies allowed.