title: Trouble with function that is supposed to find highest number in an array
body: <p>I have this program that is designed to help a maker of chips and salsa keep track of sales for five different types of salsa. There are two parallel arrays, the first being &quot;names&quot; and the second being &quot;num_of_jars&quot;. In the &quot;names&quot; array there are the five types of salsa(“Mild”, “Medium”, “Sweet”, “Hot”, “Zesty”). The second array will have numbers the user needs to input. The program should prompt the user to enter the number of jars sold(into the num_of_jars array) for each type of salsa.. Once this sales data has been entered, the program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling product.I am almost done with the program, my only issue is with a function I made (find_max). This function is supposed to find which salsa had the highest sales report and output it. So for example. if the user input &quot;100&quot; for &quot;Mild&quot;, &quot;5&quot; for &quot;Medium&quot;, &quot;10&quot; for &quot;sweet&quot;, &quot;20&quot; for &quot;hot&quot;, and &quot;1&quot; for &quot;zesty&quot;, the function will output something along the lines of &quot;Best selling product is Mild&quot;. I'm still somewhat new to functions so keep that in mind please. Below is my program.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  const string names[] {&quot;Mild&quot;, &quot;Medium&quot;, &quot;Sweet&quot;, &quot;Hot&quot;, &quot;Zesty&quot;};
  int num_of_jars [5];
  int find_max (const int a[], int n);
  
  int main() {
    int total;
    int sum = 0;
    int v;
    
    // Loop for user input
    for (int i = 0; i &lt; 5; i++) {
    cout &lt;&lt; &quot;How many jars of&quot; &lt;&lt; &quot; &quot; &lt;&lt; names[i] &lt;&lt; &quot; &quot; &lt;&lt; &quot;sold? &quot;;
      cin &gt;&gt; num_of_jars[i];
      }
    
    cout &lt;&lt; endl;
    
    // Sales report table
    cout &lt;&lt; &quot;Salsa Sales Report&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;------------------&quot; &lt;&lt; endl;
    cout &lt;&lt; names[0] &lt;&lt; setw(14) &lt;&lt; num_of_jars[0] &lt;&lt; endl;
    cout &lt;&lt; names[1] &lt;&lt; setw(12) &lt;&lt; num_of_jars[1] &lt;&lt; endl;
    cout &lt;&lt; names[2] &lt;&lt; setw(13) &lt;&lt; num_of_jars[2] &lt;&lt; endl;
    cout &lt;&lt; names[3] &lt;&lt; setw(15) &lt;&lt; num_of_jars[3] &lt;&lt; endl;
    cout &lt;&lt; names[4] &lt;&lt; setw(13) &lt;&lt; num_of_jars[4] &lt;&lt; endl;
  
    // Loop for gathering total of jars sold 
    for (int i = 0; i &lt; 5; i++) {
      sum += num_of_jars[i];
      }
    
    cout &lt;&lt; setw(18) &lt;&lt; &quot;-------&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;Total&quot; &lt;&lt; setw(13) &lt;&lt; sum &lt;&lt; endl;
  
    v = find_max(num_of_jars, 5);
    cout &lt;&lt; v;
  
  return 0;
  }
  
  // Function to find highest number
  int find_max (const int a[], int n) {
    int max = 0;
    for (int i = 1; i &lt; n; i++)
      if (a[i] &gt; a[max])
        max = i;
        return max;
      }
  </code></pre>
  <p>This is the output so far</p>
  <p>How many jars of Mild sold? 100
  How many jars of Medium sold? 5
  How many jars of Sweet sold? 10
  How many jars of Hot sold? 20
  How many jars of Zesty sold? 1</p>
  <h2>Salsa Sales Report</h2>
  <p>Mild           100
  Medium           5
  Sweet           10
  Hot             20
  Zesty            1
  -------
  Total          136
  0</p>
  <p>The &quot;0&quot; at the very bottom is from the function I discussed earlier. I have not yet coded the functions output to say &quot;Best selling product is...&quot;. I have just been testing to see what it would output, which in this case should've been &quot;100&quot;. I feel that the array &quot;names&quot; should be included in the function but I am unsure.</p>