Functions not working properly in my program

Hey everyone, I have this program here that runs properly but somehow 3 of my functions(divisible, index, and userKey) aren't giving me the proper numbers. -The function divisible function suppose to give me 4 not 6
-The function index suppose to give me 0 1 4 5 not 0 1 3 4 5 8
-The function userKey is suppose to give me true not false when I enter 99 since 99 is found in the array, 10 seems to give me true when I enter that number however.
Help Please!

Here's the input file I'm using data2.txt, it contains the following 10 integers: 10 15 27 89 90 95 27 13 99 33

Here's what the program outputs when I run it:

Array A:
10 15 27 89 90 95 27 13 99 33
Array B/reverse:
33 99 13 27 95 90 89 27 15 10
Numbers of elements that are greater or equal to 80 and less than or equal to 100: 4

Number of elements in array A in which their content is divisible by 5: 6

Index of the elements in Array A in which their content in divisible by 5:
0 1 3 4 5 8
The mean average in Array A is: 52.5

The minimum is: 10

Please enter a key:
99
The number entered is found in the array:
False




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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

//This function reads the ten integers from the input file data2.txt into array \
A
void readArray(int a[])
{
  //fill the array with #'s read from input file

  ifstream fin;
  fin.open("data2.txt");
  int num;

  if(!fin)
    cout << "The input file doesn't exist " << endl;
  else //file exists
    {
      int i = 0;
      while(!fin.eof())
        {
          fin >> num;
          a[i] = num;
          i++;
        }
    }
  fin.close();
}

//This function copies array A into array B in reverse order
void reverseArray(const int a[], int b[], int s)
{
  for(int i = 0;i <s;i++)
    {
      b[i]= a[s-i-1];

    }
}

//This function prints array A and displays it
void print( int a[], int SIZE)
{
  for(int i = 0; i < SIZE; i++)
    {
    cout << a[i] << " ";
    }
}


//This function finds the number of elements in array A that are >= 80 and <= 10\
0
int Elements(int a[], int SIZE)
{
  int counter = 0;

  for(int i = 0; i < SIZE; i++)
    {
      if((a[i] >= 80) && (a[i] = 100))
        counter ++;
    }

  return counter; //value that is returned to the main function
}

//This function finds the number of the elements in array A in which their conte\
nts are divisible by 5 and returns the number of elements to the main function
int divisible(int a[], const int SIZE)
{
  int counter = 0;

  for(int i = 0; i < SIZE; i++)
    {
      if(a[i] % 5 == 0)
        counter ++;
    }

  return counter; //value that is returned to the main function
}

//This function finds the index of the elements in array A in which their conten\
ts are divisible by 5 and prints the elements
void index(int a[], const int SIZE)
{
  for(int i = 0; i < SIZE; i++)
    {
      if(a[i] % 5 == 0)
        cout << i << " ";
    }
}

//this function finds the mean(average) in array A andd returns the mean(average\
) to the main function
float findAvg(const int a[], int SIZE)
{
  float sum, ava;

  for(int i = 0; i < SIZE; i++)
    {
      sum += a[i];
    }

  ava = sum / SIZE;

  return ava; //the value that is returned to the main function
}

//This function finds the minimum number in array A and returns the minimum numb\
er to the main function
int findMin(int a[], int SIZE)
{
  int min = 0;

  for(int i = 0; i < SIZE; i++)
    {
      if(a[min] > a[i])
         min = i;
    }

  return a[min]; //the value that is returned to the main function
}

bool userKey(int a[], int SIZE)
{
  int key;

  cout << "Please enter a key: " << endl;
  cin >> key;

  for(int i = 0; i < SIZE; i++)
    {
      if(a[i] == key)
        return true;
      else
        return false;
    }
}

int main()
{
  const int SIZE = 10;
  int a[SIZE];
  int b[SIZE];
  float avg;
  int min;
  int elem;
  int div;
  int key;

          readArray(a); //function that reads array A gets called here

          reverseArray(a, b, SIZE); //function that reads the reverse of array A\
 gets called

          cout << "Array A: " << endl;
          print(a, SIZE); //function that prints array A gets called

          cout << endl;

          cout << "Array B/reverse: " << endl;
          print(b, SIZE); //function that prints array B gets called

          cout << endl;

          elem = Elements(a, SIZE); //function that finds # of elements >=80 and\
 100 <= gets called
          cout << "Numbers of elements that are greater or equal to 80 and less \
than or equal to 100: " << elem << endl;

          cout << endl;

          div = divisible(a, SIZE); //function that finds the elements that are \
divisible by 5 gets called
          cout << "Number of elements in array A in which their content is divis\
ible by 5: " << div << endl;

          cout << endl;

          cout << "Index of the elements in Array A in which their content in di\
visible by 5: " << endl;
          index(a, SIZE); //function that finds the index of the elements that a\
re divisible by 5

          cout << endl;

          avg = findAvg(a, SIZE); //function that finds the average in array A g\
ets called
          cout << "The mean average in Array A is: " << avg << endl;

          cout << endl;

          min = findMin(a, SIZE); //function that finds the minimum in array A g\
ets called
          cout << "The minimum is: " << min << endl;

          cout << endl;

          key = userKey(a, SIZE);
          cout << "The number entered is found in the array: " << endl;
          if(key == 1)
            {
              cout << "True " << endl;
            }
          else
            {
              cout << "False " << endl;
            }

  return 0;
}

It's working as programmed, just not as you want it.

1
2
3
4
5
6
7
  for(int i = 0; i < SIZE; i++)
    {
      if(a[i] == key)
        return true;
      else
        return false;
    }


This is causing the function to return after the first comparison. Since the first number does not equal the key, it returns from the function with the value of false. It doesn't bother to test the other values in the array, since you instructed it to return a value of true or false on every and any comparison, so it ends the loop and exits the function after the first comparison. If you were to type in a 10, you would get a true value return, since 10 is the first number in the array.

You want to return true on the first value that tests true against the key, but false if you stepped through the entire array without finding the key.
Last edited on
Would having a counter++ in the if statement help so it doesn't leave the loop immediately?
No.
Not necessary. You have i++ to index into the array. You merely have to test the values in the array against key, return a value of true on the first value in array that tests true against the key.

If you go through the entire array and haven't found the key, the loop will end. If the loop goes to the end, you obviously haven't found the key, so you're free to return a false. It's a fall-through system.
Last edited on
Topic archived. No new replies allowed.