For Loop Initiation

I need help with DO_2. How do you initialize 2 variables in a for loop? I thought you could only do 1 variable? Thanks for helping out!
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
#include <iostream>
#include <iomanip>
using namespace std;

const int MAX_ARRAY_SIZE = 30;
const int NO_DATA = -1;

void ReadData(int idNumbers[], int amountSold[], int& numIds); 
void ReadAndUpdateAmountSold(int& SoldAmount);
int FindMaxIndex(const int ary[], int size);

// DO_4:  Complete the prototype for function IdIndex()



int main()
{
   int idNumbers[MAX_ARRAY_SIZE];
   int amountSold[MAX_ARRAY_SIZE];
   int numOfIds, maxIndex;

   // DO_2:  Write a for loop to initialize each element of
   //        idNumbers to NO_DATA and amountSold to 0.
   




   ReadData(idNumbers, amountSold, numOfIds);

   // DO_7:  Call function FindMaxIndex to find and return
   //        the index of the maximum value in amountSold
   //        storing the value in maxIndex


   // DO_8:  Complete this output statement to write the
   //        class room number
   cout << "Classroom with most boxes sold was class room #" 
        << 
   cout << '.' << endl;

   // DO_9:  Complete this output statement to write the
   //        number of boxes sold in the class room.
   cout << "That classroom sold " << 
        << " boxes." << endl;

   return 0;
}

// ReadData: Read in the data to update amountSold for each id #
//           also updates numIds when a new id is encountered.
// params:  (inout, inout, out)
void ReadData(int idNumbers[], int amountSold[], int& numIds)
{
   int id;  // used to read in id #
   numIds = 0;
   cin >> id;
   while (!cin.eof())
   {
      int index = IdIndex(idNumbers, numIds, id);
      if (index == NO_DATA)
      {
         idNumbers[numIds] = id;
         index = numIds;
         numIds++;
      }
      // DO: 6. Call ReadAndUpdateAmountSold to update the 
      // array element in amountSold with subscript: index


      cin >> id;
   } 
}

// Function IdIndex() has three parameters:
//     ary[]: array of int
//     size : int, number of elements in ary[]
//     key  : int
// Function IdIndex() searchs ary[] for key and returns the index of
//    where key appears in ary; returns NO_DATA if not found
// params: (in, in, in)
// Do_3: Complete the header and the body of this linear search
//       function.  








// DO_5:  Complete the body of the function by
//        writing a for loop to find the index of 
//        the largest value in the array.
//        DONT FORGET TO RETURN THIS INDEX!
// FindMaxIndex: Returns the index of largest value in the array
//               Assumes that there is at least one value in array.
//               If two or more elements have the same max value,
//               return the first index.
// params: (in, in)
int FindMaxIndex(const int ary[], int size)
{





}

// ReadAndUpdateAmountSold : Reads in an amount and adds this value
//                    to the amount-sold parameter.
// params: (inout)
void ReadAndUpdateAmountSold(int& SoldAmount)
{
   int amount;
   cin >> amount;            // read in the amount to increment by
   SoldAmount += amount;     // increment the inout parameter by amount
}
Hello piotrowskid,

I am think of something like this:

1
2
3
4
5
for (int lc = 0; lc < MAX_ARRAY_SIZE; lc++)
{
	idNumbers[lc] = NO_DATA;
	amountSold[lc] = 0;
}

This will initialize both arrays at the same time. If you do not need to do the second array delete the second line. Then you could do something like this: int amountSold[MAX_ARRAY_SIZE]{};. The empty {}s will initialize the array to zero for each element.

Hope that helps,

Andy
Hello piotrowskid,
Initializing 2 variables in a loop is explained there : http://www.cplusplus.com/doc/tutorial/control/
where it says : and I quote "
it would be possible for a for loop to handle two counter variables, initializing and increasing both:
// 2 variables initializes
for ( n = 0, i = 100 ; n != i ; ++n, --i )
{
// whatever here...
}

Michel Chassey
Topic archived. No new replies allowed.