CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Dec 2009
    Posts
    7

    need help calculating vat from multiple array items

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: need help calculating vat from multiple array items

    You could post your code you have worked so far in[code] [/code] tags.

  3. #3
    Join Date
    Dec 2009
    Posts
    7

    Re: need help calculating vat from multiple array items

    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;
          }

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: need help calculating vat from multiple array items

    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.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  5. #5
    Join Date
    Dec 2009
    Posts
    7

    Re: need help calculating vat from multiple array items

    i need a 2 column table displaying each items price and the vat aswell.

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: need help calculating vat from multiple array items

    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&#37; 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.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Join Date
    Dec 2009
    Posts
    7

    Re: need help calculating vat from multiple array items

    but how do you do that?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: need help calculating vat from multiple array items

    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.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: need help calculating vat from multiple array items

    Quote Originally Posted by Russco View Post
    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&#37; 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.
    I'd just one use array to store prices and calculate the tax on the fly. I don't see a need for two arrays here.

  10. #10
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: need help calculating vat from multiple array items

    Quote Originally Posted by GCDEF View Post
    I'd just one use array to store prices and calculate the tax on the fly. I don't see a need for two arrays here.
    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.

  11. #11
    Join Date
    Dec 2009
    Posts
    7

    Re: need help calculating vat from multiple array items

    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 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?

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: need help calculating vat from multiple array items

    Quote Originally Posted by Kansas4Ever View Post
    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 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?
    Why are you outputting i and using i in your calculations instead of the values in your scores array?

    What's wrong with just multiplying by .29?

    Why are you ignoring the comments about setting min and max to uninitialized and in one case invalid array values?

  13. #13
    Join Date
    Dec 2009
    Posts
    7

    Re: need help calculating vat from multiple array items

    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.

  14. #14
    Join Date
    Apr 2005
    Posts
    107

    Re: need help calculating vat from multiple array items

    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).

  15. #15
    Join Date
    Dec 2009
    Posts
    7

    Re: need help calculating vat from multiple array items

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured