arrays and functions

Hi, i'm new here and i have this homework and i really need your help


The following program asks the user to type 10 integers of an array and an integer V. The program searchs if V is in the array and writes "V is in the array" or "V is not in the array".

Modify this program so as it asks the user to type 10 integers in a first array array1 and 20 integers in a second array array2 and an integer V. The program searchs if V is in one of the arrays array1 and array2 and writes "V is in the first array" or "V is in the second array" or "V is not in the arrays".


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
#include <iostream>
using namespace std;
const int X = 10;
void outputFunc(int array[], int V);
bool testFunc(int array[], int V);
int inputFunc(int array[]);
int main()
{  int V, array[X];
   V = inputFunc(array);
   outputFunc(array, V);
   return 0;
}
int inputFunc(int array[])
{  
int V;
cout << "Please enter " << X << " integer elements of an array. " << endl;
for(int count = 0; count < X; ++count)
   {  
   cout << "array[" << count << "]: ";
   cin >> array[count];
   }
cout<<endl<<"Enter the integer to find:";
cin >> V;
return V;
}
bool testFunc(int array[], int V)
{  int isIn = 0, count = 0;
   while ((isIn==0) && (count < X))
   {
   if(array[count] == V)
         isIn = 1;
   ++count;
   }
   if(isIn == 1)
      return true;
   else
      return false;
}
 
void outputFunc(int array[], int V)
{  cout <<endl<< V<<" is ";
 
   if(! (testFunc(array, V) ) )
      cout << "not ";
   cout << "in the array. " <<endl<< endl;
}
Last edited on
Can you be specific about what it is you're having trouble with? That code you've been given already shows you:

- how to declare an array
- how to ask the user a question
- how to read values in that the user types
- how to search an array for a specific value
- how to tell the user whether or not the value was in an array

I suggest you take the time to read and understand the code you've been given, and learn from it.
read and understand the code you've been given, and learn from it.

That's fair, up to a point. Though I'd be tempted to rewrite most of it, at the very least I would change the function signatures so that the array size is one of the parameters passed, rather than depending on the global X.

For example the input might look like this:
1
2
3
4
5
6
7
8
9
10
void inputFunc(int array[], int size)
{  
    cout << "Please enter " << size << " integer elements of an array. " << endl;
    
    for (int count = 0; count < size; ++count)
    {  
       cout << "array[" << count << "]: ";
       cin >> array[count];
    }
}

Then it could work for any array, regardless of size.

Though I'm not sure how helpful this is - it makes the code cleaner and more re-usable, but doesn't directly solve the problem.

An alternative would be to change the inputFunc to handle both arrays,
 
int inputFunc(int array1[], int array2[] );
and that might be what is expected.

Because the code for each array is very similar, this function could then call the version I suggested (maybe with a changed name) rather than duplicating the code.
Topic archived. No new replies allowed.