How do I define the index within the array?

Storing array index into another array in c++?

  • So im just trying to store an array's indexes into another array so if there are two of the same number I can show where the two indexes are located What I have: #include ...show more

  • Answer:

    b[x]=i; Where are you setting the value of x? Edit: What you are doing is called searching. You don't need the b array for that. cin >> key; for (i=0; i<8; i++) if (a[i]==key) cout << " " << i; return 0; Edit: >>> "how would I later call to the 2 indexes of 7?" You didn't say anything about a later call. AND, the main point of this assignment is for you to figure things out, not ask others for a complete solution.

6JLYP2VSQVNS74W5U3XRI3OWEM at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Try: #include <iostream> using namespace std; int main() { int a[8]= { 1, 2, 3, 4, 5, 7, 7, 42 }, b[8], key, x, i; cout << "Enter your key: "; cin >> key; for ( x= i= 0; i < 8; ++i ) if ( a[i] == key ) b[x++]= i; for ( i= 0; i < x; ++i ) cout << ' ' << b[i]; cout << endl; return 0; }

Jonathan

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.