function not returning true false,how to cout index of array

c) Which searches the first n elements of an array for an element with a given value. If the value is found then the function should return true and also return the index
// of the element in the array. If not found then the function should return false.


The problem is with the "bool isSame(int number,int arraysize)"
it's not returning true false i also want to ask how to print the index.
If someone can help me till tomorrow.
Thanks


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
  #include<iostream>
using namespace std;
int minArrayFunc(int arraysize);
int correspondingOnes(int arraysize);
bool  isSame(int number,int arraysize);
int main()
{

cout <<"ENTER CHOICE"<<endl;
int choice;
cin >>choice;

   switch(choice)
   {
       case 1 :
      cout <<"PART A"<<endl;
      cout <<"ENTER array size";
      int array_size1;
      cin >>array_size1;
      minArrayFunc(array_size1);
      break; //optional

       case 2 :
      cout <<"PART B"<<endl;
      cout <<"ENTER array size";
      int array_size2;
      cin >>array_size2;
      correspondingOnes(array_size2);
      break; //optional

       case 3 :
      cout <<"PART C"<<endl;
      cout <<"ENTER NUMBER AND SIZE"<<endl;
    int num,siz;
    cin >>num>>siz;
    isSame(num,siz);
      break; //optional

   }

}

int minArrayFunc(int arraysize)
{
    int array_[arraysize], minimum, size_, c, location = 1;


    cout << "Enter integers" <<endl;

    for ( c = 0 ; c < arraysize ; c++ )
        {
            cin>>array_[c];
        }
            minimum = array_[0];

    for ( c = 1 ; c < arraysize; c++ )
    {
        if ( array_[c] < minimum )
        {
           minimum = array_[c];
           location = c+1;
        }
    }

    cout <<"Minimum element is " <<minimum;
    return 0;

}

// (b) To return a count of the number of corresponding elements which differ in the first n        elements of two arrays of the same size.
int correspondingOnes(int arraysize)
{
        int arr1[arraysize] ;
        int arr2[arraysize] ;

        for ( int c = 0 ; c < arraysize ; c++ )
        {
            cin>>arr1[c];
        }
        cout <<"SECOND"<<endl;
        for ( int c = 0 ; c < arraysize ; c++ )
        {
            cin>>arr2[c];
        }


int unique_ = 1; //incase we have only one element;&nbsp;it is unique!
  for(int i = 0; i < arraysize;i++)
    {
     if(arr1[i]==arr2[i])
       continue;
     else
       unique_++;
    }
  cout<<"The number of unique elements is "<<unique_ <<endl;



    }


//    (c) Which searches the first n elements of an array for an element with a given value. If the value is found then the function should return true and also return the index
//     of the element in the array. If not found then the function should return false. In these functions incorporate error testing for a number of elements greater than the array size.
//

     bool  isSame(int number,int arraysize)
     {

         int array1[arraysize];
         cout << "ENTER"<<endl;
          for ( int c = 0 ; c < arraysize ; c++ )
        {
            cin>>array1[c];
        }

         for(int i=0;i<arraysize;i++)
         {

             if(number == array1[i])
             {
                 return true;
             }
             else
             {
                    return false;
             }


         }


     }



Pretty sure you are misunderstanding the problem. Your function should probably be more like this:

bool search(int array[], int arraySize, int searchTarget, int &targetIndex);
try this ...

cout << "Value returned by the function is : " << isSame(num,siz);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool isSame(int number,int arraysize)
    {
        bool match = false;
        int array1[arraysize];
        
        cout << "ENTER" << endl;

        for (int c = 0; c < arraysize; c++)
            cin >> array1[c];

        for (int i = 0; i < arraysize; i++)
        {
            if (array1[i] == number)
            {
                cout << "Number matches at index number " << i << endl;
                match = true;
            }
        }
        return (match == true) ? true : false;
    }
Topic archived. No new replies allowed.