Function that searches for the key in an array

OK so i'm trying to make this function that asks the user to input a key, then search for the key in array A and inform the user about the existence (true/ false) of the key in the array. I know that this function returns true or false as a result so we're not dealing with a void function

This program involves an input file called data2.txt that involves the following ten integers
20 25 27 89 90 95 27 23 99 33

This is the last function I need to finish my program, I have no idea how to do it, help please

Here's what I have so far so you have an idea on how the program is, read the comments, the program doesn't run properly



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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


//This function needs to read the ten integers from the input file data2.txt into array A
void readArray(const int a[], int SIZE)
{
  int i;
  for(int i = 0; i < SIZE; i++)
    {
      cout << a[i];
    }
}

//This function needs to copy array A into array B in reverse order
void reverseArray(const int a[], int SIZE)
{
  int i;
  for(int i = 9; i > 0; i--)
    {
      cout << a[i];
    }
}


//This function needs to print array A
void printA(const int a[], int SIZE)
{
  int i;

  for(i = 0; i < 10; i++)
    cout << a[i] << " ";
}


//This function needs to print array B
void printB(const int a[], int SIZE)
{
  int i = 9;
  while(i <= 0)
    {
      cout << a[i] << " ";
      i--;
    }
}

//This function needs to find the number of elements in array A that are >= 80 & <=100
int Elements(int a[], const int SIZE)
{
  int counter = 0;

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

  return counter;
}

//this function needs to find the number of the elements in array A in which their contents are divisible by 5
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;
}

//This function needs to find the index of the elements in array A in which their contents are divisible by 5
void index(int a[], const int SIZE)
{
  for(int i = 0; i < SIZE; i++)
    {
      if(a[i] % 5 == 0)
        cout << i;
    }
}

////This function needs to find the mean(average) in array A and return the mean(average)
float findAvg(const int a[], int SIZE)
{
  int total = 0;

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

  return (float)total / SIZE;
}

//This function needs to find the minimum number in array A and return the minimum number (which is 20), since we're returning something this function is not a void
int findMin(const int a[], int SIZE)
{
  int i;
  int min = a[0];

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

  return min;
}

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

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

 if(!fin)
    cout << "The input file doesn't exist" << endl;
  else
    {
      while(!fin.eof())
        {
          readArray(a, SIZE); //function call

          reverseArray(a, SIZE); //function call

          printA(a, SIZE); //function call

          printB(a, SIZE); //function call

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

          div = divisible(a, SIZE); //function call
          cout << "Number of elements in array A in which their content is divisible by 5: " << div << endl;

          index(a, SIZE); //function call

          avg = findAvg(a, SIZE); //function call
          cout << avg;

          min = findMin(a, SIZE); //function call
          cout << "The minimum is: " << min << endl;
        }

      fin.close();
    }
  return 0;
}
You'd use cin to get keyboard input, then loop through the array comparing 'key' with each array element.

Return true or 1 if you find a match, false or 0 if you reach the end of the array without finding the key.
can you anyone show me an example, I need to see how it's formatted?

cin >> key;

1
2
3
4
5
6
for( int i = 0; i < 10; i++)
{
      if( array[i] = key;
           return true;
}
return false;
Topic archived. No new replies allowed.