I have wrote a c++ program inputing 35 array values, calculating max and min which I have, but I have to get a vat of 29% and the average price for an array.
I would appreciate some help,
regards Kansas4Ever.
Printable View
I have wrote a c++ program inputing 35 array values, calculating max and min which I have, but I have to get a vat of 29% and the average price for an array.
I would appreciate some help,
regards Kansas4Ever.
You could post your code you have worked so far in[code] [/code] tags.
Code:#include <iostream>
using namespace std;
int main()
{
int i,d,p;
int scores[35];
int total = 0;
int max = 0;
int min = 0;
max = scores[35];
min = scores[0];
for (i=0;i<=34;i++)
{
cout << "Enter a score ";
cin >> scores[i];
}
for (i=0;i<=34;i++)
{
if(scores[i]>max)
{
max = scores[i];
}
if(scores[i]<min)
{
min = scores[i];
}
total = total + scores[i];
}
cout << "total is " << total <<endl;
cout << "Maximum is " << max <<endl;
system ("pause");
return 0;
}
you should remove the lines :-
max = scores[35];
min = scores[0];
scores[35] doesn't even exist.
At this point scores contains random values. Taking the value stored in scores[0] as the minimum is pointless before the array has been filled.
As to your problem, to work out the average loop through the array and keep an accumulating total of the elements, then divide that total by the number of elements in the array, which is 35.
As for the vat, do you want just what the vat would be? or what the price would be including vat? Also if your array is holding 'prices' is int the right data type for it? Prices tend to have fractional parts.
i need a 2 column table displaying each items price and the vat aswell.
You will need two arrays in that case. One holding the original price, one holding the vat. I take it you know how to work out 29% of a quantity, its fairly basic maths! So you will need in a loop read one array, work out 29% of that read and stuff the answer in a 2nd array.
Im still concerned that int isn't the right data type for this. Prices usually quoted to 2 decimal places so floats would be adequate.
Then its just a case of looping through both arrays printing the prices followed by a tab or two and then the vat.
On a style issue where arrays are concerned, its probably preferable to get used to controlling loops with less than size rather than less than or equal to size minus one.
but how do you do that?
I'm kind of curious why your program is referring to scores, both in prompts and variable names, yet your requirements refer to items, prices and tax.
Me too.
Is there a chance to take a std::vector instead of the array? The maximum/minimum value in that vector could then easily be retrieved using max_element resp. min_element.
And as Russco already stated, once you know whether you want scores or prices, rethink the type of the input variable.
I have this so far. I have added to two colums, with Vat as the 2nd column, and multiplied each price by 100 and divided by 29 to get the VAT?Code:#include <iostream>
using namespace std;
int main()
{
int i,x,y;
int scores[35];
int total = 0;
int max = 0;
int min = 0;
max = scores[35];
min = scores[0];
for (i=0;i<=34;i++)
{
cout << "Enter a score ";
cin >> scores[i];
}
for (i=0;i<=34;i++)
{
if(scores[i]>max)
{
max = scores[i];
}
if(scores[i]<min)
{
min = scores[i];
}
total = total + scores[i];
}
cout << "\tTotal\t\t\VAT" << endl;
for (i=1;i<=34;i++)
{
cout << "\t" << i << "\t\t\t" << (i*100/29) << endl;
}
cout << "total is " << total <<endl;
cout << "Maximum is " << max <<endl;
system ("pause");
return 0;
}
I am not ignoring, this is just my very first go at this and i'm very unsure about what goes in.
I will mutiply by .29.
Also, for finding your min and max, you should (as you do) initialize max to 0, but you should initialize min to a number greater then the maximum permissible cost (score?).
As well, you probably want to use floating point numbers, as integer calculations are going to cause a bit of a problem here (most likely).
OK, I have figured out what I have need to do, only have to find the average.
int avg
avg = (1 + 2 + 3 + 4 + 5+ 6 + 7 + 8 + 9 +10 + 11 + 12 + 13 (up to 35) / 35;
cout << "Avererage is : " <<avg;
Is this close?
EDIT
RDcast, I can get the vat alright with int
No you can't, unless you want it measured in cents. An int can't store anything but whole numbers. If your item was a dollar, your tax would bet 29 cents or .29. An int can't store .29 as it isn't a whole number, and it will drop any fractional components when used in division. 1/2 = 0 if you're just using integers.