[HELP] converting a for loop to do while

i have a function that contains a for loop and i need to change the loop to a do while. im getting build errors with my changes.


1
2
3
4
5
6
7
8
9
10
11
12
13
//original
double getHighest(double array[], int size)
{
   double highest;

   highest = array[0];
   for (int count = 1; count < size; count++)
   {
      if (array[count] > highest)
         highest = array[count];
   }
   return highest;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
//converted
double getHighest(double array[], int size)
{
   double lowest;

   lowest = array[0];
   do
   {
      if (array[count] < lowest)
         lowest = array[count];
   }while(count < size);
   return lowest;
}
Last edited on
btw this is the entire code:
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

// This program gets five days of sales data from the user
// and displays the total, average, highest, and lowest amounts.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

// Function prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
int getDays();

int main()
{
    
   //const int NUM_DAYS = 5; // Number of days
   double sales[100], // To hold sales amounts
          total,           // To hold the total sales
          average,         // To hold the average
          highest,         // To hold the highest sales
          lowest;          // To hold the lowest sales

   // Get the sales data.
   int days;
   // Instructor Comment: Correct function call
   days = getDays();
   
   for (int count = 0; count < days; count++)
   {
      cout << "Day " << (count +1) <<": ";
      cin >> sales[count];
   }

   // Get the total sales.
   total = sumArray(sales, days);

   // Calculate the average.
   average = total / days;

   // Find the highest sales amount.
   highest = getHighest(sales, days);

   // Find the lowest sales amount.
   lowest = getLowest(sales, days);

   // Display the results.
   cout << fixed << showpoint << setprecision(2);
   cout << "The total sales are $" << total << endl;
   cout << "The average sales amount is $" << average << endl;
   cout << "The highest sales amount is $" << highest << endl;
   cout << "The lowest sales amount is $" << lowest << endl;
   
   system("PAUSE");
   return 0;
}

//***************************************************
// Definition of sumArray                           *
// This function accepts a double array and its size *
// as arguments. The sum of the array's elements    *
// is returned as an double.                         *
//***************************************************

double sumArray(double array[], int size)
{
   double total = 0; // Accumulator
   int count = 0;
   while(count < size)
    {
        count++;
        total += array[count];
    }
    return total;

   /*for (int count = 0; count < size; count++)
      total += array[count];
   return total;*/
}

//****************************************************
// Definition of getHighest                          *
// This function accepts a double array and its size  *
// as arguments. The highest value in the array is   *
// returned as an double.                             *
//****************************************************

   double getHighest(double array[], int size)
{
double highest;

   highest = array[0];
   int count = 1;
   do
   {
     count+1;
   }while (count < size);
    if (array[count] > highest)
       highest = array[count];
   return highest;
}


//****************************************************
// Definition of getLowest                           *
// This function accepts a double array and its size  *
// as arguments. The lowest value in the array is    *
// returned as an double.                             *
//****************************************************

double getLowest(double array[], int size)
{
   double lowest;

   lowest = array[0];
   do
   {
      if (array[count] < lowest)
         lowest = array[count];
   }while(count < size);
   return lowest;
}

int getDays()
{
    int days;
    
    do{
    cout << "How many sales days are you analyzing?\n";
    cin >> days;
    if (days < 0)
        cout << "Days cannot be negative.\n";
    else if(days > 100)
        cout << "Days exceed array size.\n";
    }while (days < 0 || days > 100);
    
    return days;
}
in the converted function you don't define count.

1
2
3
4
5
6
7
8
9
10
11
12
13
//converted
double getHighest(double array[], int size)
{
   double lowest = array[0];
   int count = 1;//You need to define and initialize count.
   do
   {
      if (array[count] < lowest)
         lowest = array[count];
      count++; //You need to increment count each iteration
   }while(count < size);
   return lowest;
}


You're compiler gives useful errors. If you see an error learn to understand what it is telling you.
Thank you, i butchered the code, but managed to figure what i messed up and implement your help.
Topic archived. No new replies allowed.