Searching and storing

Hello, I am creating a program that's supposed to take input from the user in the form below and output it in a certain style and this involves storing the variables from all the test cases in order to output them at the same time:


Input:
2 //number of test cases(how many the entire program will be repeated)
4 //array size
1 2 3 4 //array elements
3 //element to be searched for

5 //again, array size
10 90 20 30 40 //array elements
40 //element to be searched for

Output:

2 4 //the indexes of both the number found in the 1st test case and the 2nd number found in the 2nd test case:



My output is this:


Number of test cases:
2
Array size
4
1 2 3 4
Element to search for:
3
Index: 2
Array size
5
10 90 20 30 40
Element to search for:
40
Index: 4


My 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
37
38
39
40
#include <iostream>

using namespace std;

int main()
{
    int test_case, x, size;
    int a[256];


    cout << "Number of test cases:" << endl;

    cin >> test_case;
    //int temp[256];

    for(int i = 1; i <= test_case; i++)
    {
        cout << "Array size" << endl;
        cin >> size;

        for(int i = 0; i < size; i++)
        {
            cin >> a[i];
        }

        cout << "Element to search for: " << endl;
        cin >> x;

        for (int i = 0; i < size; i++)
        {
            if(a[i] == x)
            {
                cout << "Index: " << i << endl;
                break;
            }
        }

    }
    return 0;
}


I want the output to NOT be separate. i.e to output all indexes at once.
Thanks in advance <3
Last edited on
Use dynamic arrays if you can't use std::vector:
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
#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    std::cout << "How many arrays? \n";
    size_t numArrays{};
    std::cin >> numArrays; //data-validation
    size_t* indexFind = new size_t[numArrays];//to store the index numbers from search
    for (auto i = 0; i < numArrays; ++i)
    {
        std::cout << "Enter size of array " << i+1 << ": \n";
        size_t sizeArray{};
        std::cin >> sizeArray;
        int* p = new int[sizeArray];
        for (size_t j = 0; j < sizeArray; ++j)
        {
            std::cout << "Enter array element " << j+1 << " of " << sizeArray << "\n";
            //writing it out like this shows a running total of array entries
            std::cin >> p[j];
        }
        std::cout << "Enter element to search for: \n";
        int searchTerm{};
        std::cin >> searchTerm;
        indexFind[i] = std::distance(p, std::find(p, p + sizeArray, searchTerm));
        //http://en.cppreference.com/w/cpp/iterator/distance
        //http://en.cppreference.com/w/cpp/algorithm/find
        if(indexFind[i] == sizeArray)std::cout << "Element not found \n";
        delete [] p;
    }
    for (auto i = 0; i < numArrays; ++i)std::cout << indexFind[i] << " ";
    delete [] indexFind;
}

Topic archived. No new replies allowed.