Need Help!!

Pages: 12
I need to know if this code will compare two values in arrays at the same index and if it is above a certain value assign True to a third array and false if otherwise.

the three arrays are: float sales[], bool meet[] (this is the one the true/false values need to be put into), and float targetNumber[] (which has predefined values that the sales array must equal or be greater than.

I just need a counter to count up all the values that are equal to a target number, so it has to access each index in the array and check if its above or equal too the target number but I cant get it to work.

Thanks,

TECK

1
2
3
4
5
6
7
8
9
10
11
12
13
  float compareArrays(float targetNumber[], float sales[], bool meet[],float SIZE)
{
  int counter = 0;
  for (int i = 0; i < SIZE; i++)
    {
      if(sales[i] < targetNumber[i])
        {
         counter++;
        }
    }
      return counter;
}
That code does not assign anything to any meet[i]. It only counts the cases, where sales < targetNumber, i.e. neither the >= nor == (which is futile on floats anyway).
how do i get it to check if the current index in the sales array is equal to or greater than the targetNumber array? Ive been stumped for about a day on this.

Thanks for the quick reply
TECKSPEED wrote:
how do i get it to check if the current index in the sales array is equal to or greater than the targetNumber array?

If you can explain what line 6 of your code snippet is doing, you can answer that question.
if the current index of the sales array is less than the current index of the targetNumber array then it adds one to the counter but when i compile and run i get 0. . .so what do i need to return to get the right value
I assume you want counter to keep count of "How many times does sales meet or exceed targetNumber?" In that case, you'll want to make one minor change. Your code as is says "increment counter each time sales is LESS THAN the targetNumber". You actually want the opposite: "increment counter each time sales is GREATER THAN OR EQUAL TO the targetNumber." So line 6 would become:
if(sales[i] >= targetNumber[i])
I need both if it is above an expected value and if it is less than an expected value, i use each as a separate function. Here is all of my code, I just output the two function i am asking about to test if i get the correct value. Maybe all of my code will help you.

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void displayHeader();
void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE);
float computeAverage(float sales[], float SIZE, float &Numsales);
float compareAboveArrays(float targetNumber[], float sales[], bool meet[], float\
 SIZE);
float compareBelowArrays(float targetNumber[], float sales[], bool meet[], float\
 SIZE);
void displayResults(float totalAverage, float comparingInfoAbove, float comparin\
gInfoBelow);

int main()
{
  float Numsales, salesInfo, totalAverage, results, comparingInfoAbove, comparin\
gInfoBelow;
  const int SIZE = 12;
  float targetNumber[] = {23.0, 33.1, 21.0, 23.5, 54.0, 34.3, 35.0, 45.0, 56.3, \
45.6, 34.0, 55.0};
  float sales[SIZE];
  bool meet[SIZE];


  ifstream fp;
  fp.open("sales.dat");
  if(!fp)
    {
      cout <<"Error opening the file!" <<endl;
    }

  displayHeader();

  while(fp.eof()==false)
    {
      readIntoSales(fp, Numsales, sales, SIZE);
      totalAverage = computeAverage(sales, SIZE, Numsales);
      comparingInfoAbove = compareAboveArrays(targetNumber, sales, meet, SIZE);
     }
  displayResults(totalAverage, comparingInfoAbove,comparingInfoBelow);
}

void displayHeader()
{

  cout<< "Store Statistics"<<endl;
  cout<<"                   "<<endl;
  cout<<"  Dept  "  <<  "  Average  "  <<  "  Above  "  <<  "  Below  "  <<  "  \
Performance  " <<endl;
  cout<<"----------------------------------------------------------------" <<end\
l;

}


void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE)
{
  fp >>Numsales;
  for (int i = 0; i < Numsales; i++)
    sales[i] = Numsales;

}


float computeAverage(float sales[], float SIZE, float &Numsales)
{
  float sum = 0;
  for(int i = 0; i <SIZE; i++)
    {
      sum += sales[i];
    }
  return sum/11;
}

float compareAboveArrays(float targetNumber[], float sales[], bool meet[],float \
SIZE)
{
int counter = 0;
  for (int i = 0; i < SIZE; i++)
    {
      if(sales[i] > targetNumber[i])
        {
         counter++;
        }
    }
      return counter;
}

float compareBelowArrays(float targetNumber[], float sales[], bool meet[], float\
 SIZE)
{
  int counter = 0;
  for (int i = 0; i < SIZE; i++)
    {
      if(sales[i] < targetNumber[i])
        {
          counter++;
        }
    }
  return counter;
}

void displayResults(float totalAverage, float comparingInfoAbove, float comparin\
gInfoBelow)
{
  cout <<totalAverage <<endl;
  cout <<comparingInfoAbove <<endl;
  cout <<comparingInfoBelow <<endl;
}
I still have not figured this out...
You print out only the results of the last iteration.
than how do I get it to print out each line from the text file I have. THe text file is just decimal numbers, 12 on each line and there are 7 lines.
Here is my code. My average function isn't computing how it should. How do I get the array to only take the first 12 values in a text file and use those 12 values to compute the average?

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

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void displayHeader();
void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE);
float computeAverage(float sales[], float SIZE, float &Numsales);
float compareAboveArrays(float targetNumber[], float sales[], bool meet[], float\
 SIZE);
float performanceInfo(float compareInfoBelow, float SIZE);
void displayResults(float totalAverage, float comparingInfoAbove, float infoPerf\
ormance);

int main()
{
  float Numsales, salesInfo, totalAverage, results, comparingInfoAbove, infoPerf\
ormance;
  const int SIZE = 12;
  float targetNumber[] = {23.0, 33.1, 21.0, 23.5, 54.0, 34.3, 35.0, 45.0, 56.3, \
45.6, 34.0, 55.0};
  float sales[SIZE];
  bool meet[SIZE];

  displayHeader();

  ifstream fp;
  fp.open("sales.dat");
  if(!fp)
    {
      cout <<"Error opening the file!" <<endl;
    }

  while(fp.eof()==false)
    {
      readIntoSales(fp, Numsales, sales, SIZE);
      totalAverage = computeAverage(sales, SIZE, Numsales);
      comparingInfoAbove = compareAboveArrays(targetNumber, sales, meet, SIZE);
      infoPerformance = performanceInfo(comparingInfoAbove, SIZE);
     displayResults(totalAverage, comparingInfoAbove, infoPerformance);
    }
return 0;
}

void displayHeader()
{

  cout<< "Store Statistics"<<endl;
  cout<<"                   "<<endl;
  cout<<"  Dept  "  <<  "  Average  "  <<  "  Above  "  <<  "  Below  "  <<  "  \
Performance  " <<endl;
  cout<<"----------------------------------------------------------------" <<end\
l;

}


void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE)
{
  fp >>Numsales;
  for (int i = 0; i < Numsales; i++)
    sales[i] = Numsales;

}


float computeAverage(float sales[], float SIZE, float &Numsales)
{
  float sum = 0;
  for(int i = 0; i < 12; i++)
    {
      sum += sales[i];
    }
  return sum/12;
}

float compareAboveArrays(float targetNumber[], float sales[], bool meet[],float SIZE)
{
  int counter = 0;
  for (int i = 0; i < SIZE; i++)
    {
      if(sales[i] >= targetNumber[i])
        {
          meet[i] = 'T';
        }
      else
        {
          meet[i] = 'F';
        }
      counter++;
    }
}

float performanceInfo(float comparingInfoBelow, float SIZE)
{
  for(int i = 0; i < SIZE; i++)
    {
        if(comparingInfoBelow > 4)
          cout<<"unsatisfactory"<<endl;
        else
          cout<<"satisfactory"<<endl;
    }
}

void displayResults(float totalAverage, float comparingInfoAbove,float infoPerformance)
{
  cout<< "   1        " <<totalAverage  <<"     " <<comparingInfoAbove<<"     " \
<<infoPerformance<<endl;
}

Does the compiler give any warnings when you compile your program?
No it compiles, but my average function is just returning the values of the text document, which are these numbers:

23 33.5 21 23 25 56 54 43 34.2 35.4 34 69.5
24 35.2 24 26 43 56.7 54 32 43 34 34 57.9
24 42 43 35 52 56 67 54 56 45.3 32 32
20 32 45 72 45.4 63.2 45 56 52 65 53 65
34 35 37.5 32 23 45 31 43 52 43 76 65
35 56 63.4 45.2 45.6 56 67.3 45 56.3 67 78 76
34.2 45 62 19 45 39 38 37 82 74 45 58.4


Those numbers are what show up when I run the program. So my average function isnt doing anything and I can figure out why.
The first line of the text document is 12 numbers, I need the average of just those 12 numbers then I need to take those numbers and check if each on is above a certain number and if it is you add one to a counter. But my average function is wrong and I have no clue why. It return every value in the text document and doesn't stop at the end of the first line.
for (i=0;i<SIZE;i++)
{ if (float sales[i] < float targetNumber[i]) meet[i]=false; else meet[i] = true;}

That what you're looking for?
There is nothing wrong in the average calculation.

Your read function ...
I figured it out but now my function to see if the number is above the target number is acting up, Ive been staring st this thing for hours and i know im making simple/dumb mistakes.

here is the code segment

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
void readIntoSales(ifstream &fp, float Numsales, float sales[], float SIZE)
{
  fp >>Numsales;
  for (int i = 0; i < SIZE; i++)
    sales[i] = Numsales;

}


float computeAverage(ifstream &fp, float sales[], float SIZE, float &Numsales)
{
  float sum = 0, salesAverage;
  for(int i = 0; i < SIZE; i++)
    {
      fp>>Numsales;
      sum += Numsales;
    }
  salesAverage = sum/SIZE;
  return salesAverage;
}

float compareAboveArrays(float targetNumber[], float SIZE, float &salesAverage)
{
  int counter = 0, Above;
  for (int i = 0; i < SIZE; i++)
    {
      if(salesAverage >= targetNumber[i])
        {
          Above += counter;
        }
    }
  return Above;
}

for (i=0;i<SIZE;i++)
{ if (float sales[i] < float targetNumber[i]) meet[i]=false; else meet[i] = true;}

That what you're looking for?



I have that code typed in but then what do I return?? Im confused because I cant return the array meet[i] can I? after the true/false values get assigned to all of the indexes I need to tally up the true's and output that integer number.

compareAbove:
at start counter==0 and Above is what?
each iteration adds 0 to Above
then, you return your int Above as float?

you don't need to return array meet. You don't return sales from read either and that seems ok, doesn't it? You can increase counter within the same if that sets the meet.
Last edited on
This is what I have now, I have confused myself tons

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int compareAboveArrays(float &Numsales, float targetNumber[], bool meet[], float\
 SIZE, float &salesAverage)
{
  int counter = 0, Above;
  for (int i = 0; i < SIZE; i++)
    {
      if(salesAverage <= targetNumber[i])
        {
          meet[i] = 'f';
          Above++;
        }
      else
        {
          meet[i] = 't';
          Above++;
        }
      return Above;
    }
}
Pages: 12