returning numbers place in array instead of the number

Hello.
How can I return the place of a number in the array instead of returning the whole number?

Thanks!
Code:

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
//----------------------------------
void Skaito(int &n, int A[]);
int Geriausias(int n, int A[]);
int main ()
{
    ofstream rf("rez.txt");
    int n, A[100];

    Skaito(n, A);
    if(Geriausias(n, A) == 0) rf << "Nera" << endl;
    else rf << Geriausias(n, A) << endl;

return 0;
}
void Skaito(int &n, int A[])
{
    ifstream df("duom.txt");
    int i;
    df >> n;
    for(i = 0; i < n; i++) df >> A[i];
    df.close();
}
int Geriausias(int n, int A[])
{
    ofstream rf("rez.txt");
    int did = 0, i;

    for(i = 0; i < n; i++)
    if(A[i] > did && A[i] != A[i - 1] && A[i] != A[i + 1]) did = A[i];
    return did;
}
Last edited on
What you are trying to do is not clear and understandable. Are you asking for returning the index of a given number of the array instead of the stored element within the array?

And.....
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
Yes, I am trying to return the index instead of the stored element withing the array.
Loop through your array, to find the wanted value. When found the loop should have the index of that value. Return/store that index value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
   // create an array
   int arr[7] { 5, 15, 18, 3, 8, 19, 125 };

   // what number do you want to find?
   const int num_to_find { 8 };

   // the found number index, -1 no match
   int index { -1 };

   for (int i { }; i < 7; ++i)
   {
      if (num_to_find == arr[i])
      {
         index = i;
         break;
      }
   }
   std::cout << num_to_find << " was found at array index: " << index << '\n';
}
8 was found at array index: 4
Topic archived. No new replies allowed.