I don't understand why my arrays won't subtract
Nov 11, 2010 at 6:06pm UTC
This is HW. I have puzzled for 5 hours as to why case 6 will not work, but case 4 and case 5 give a correct result. I can upload the full code if needed but I hope this will be enough for someone to point me in the right direction. I expected the var[i] would copy all values from maxdefects[i]-mindefects[k], but it seem that the arrays maxdefects[i], mindefects[k] aren't even holding values. I tested this by trying a cout<<mindefects[2]; in case 4's cout lines.
Thank you in advance for any help you can offer.
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
case 4: //done and debugged
cout << "Enter a product number: " ;
cin >> prodnum;
while (prodnum > num_Products || prodnum < 0)
{
cout << "Invalid Entry. Try Again." ;
cin >> prodnum;
}
for (i=0, k=0; i < days; i++, k++)
{
j = prodnum - 1;
maxdefects[k] = defects[j][i];
}
max = 0;
for (i=0; i < days; i++)
{
if (maxdefects[i] > max)
max = maxdefects[i];
}
cout << endl << "The maximum for product "
<< prodnum << " is "
<< max << " defects." << endl;
break ;
case 5://done and debugged
cout << "Enter a product number: " ;
cin >> prodnum;
while (prodnum > num_Products || prodnum < 0)
{
cout << "Invalid Entry. Try Again." ;
cin >> prodnum;
}
for (i=0, k=0; i < days; i++, k++)
{
j = prodnum - 1;
mindefects[k] = defects[j][i];
}
min = mindefects[0];
for (i=0; i < days; i++)
{
if (mindefects[i] < min)
min = mindefects[i];
}
cout << endl << "The minimum for product "
<< prodnum << " is "
<< min << " defects." << endl;
break ;
case 6:
for (i=0; i < days; i++)
{
var[i] = maxdefects[i]-mindefects[k];
}
minvar=var[0];
for (k=0; k< days; k++)
{
if ( var[k] < minvar)
minvar = var[k];
}
cout<<"The minimum variance is "
<<minvar<<endl;
break ;
Nov 11, 2010 at 6:23pm UTC
1 2 3 4
for (i=0; i < days; i++) // no 'k' here
{
var[i] = maxdefects[i]-mindefects[k]; // what is 'k'?
}
What that supposed to be mindefects[i] ?
Topic archived. No new replies allowed.