Sequential Search

there's an error on this program supposedly how can I utilize it to become a linked list and array and to identify how many comparisons would be necessary to find the key '18'?

Lastly, Modifying this program so that it returns the count of repeating instances for a given search element ‘k’
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
 #include <iostream>

int main ()
{
    const int numlist[10] = [15,18,2,19,18,0,8,14,19,14];
    const int Item = 10, n= 5;
    int value;
    int i = 0, j= 0;


    std::cout <<"The Elements within the Array : \n" std::<<endl;
    std::cin>> value;


    for (i=0; i<n ; i++)
    {
        std::cout << i <<numlist[i];

    }

    while (j<n){
        if(numlist[j]==Item){
        break;
        }
        j=j +1;
    }
    std::cout<<"The elements at position " <<Item <<j+1;
}
Well to 'fix' the program, consider something like this:

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
#include <iostream>

int main() {
	const size_t noElems { 10 };
	const int numlist[noElems] { 15, 18, 2, 19, 18, 0, 8, 14, 19, 14 };
	int toFind {};

	std::cout << "The Elements within the Array : \n";

	for (size_t i {}; i < noElems; ++i)
		std::cout << i + 1 << "  " << numlist[i] << '\n';

	std::cout << "\nEnter element to find: ";
	std::cin >> toFind;

	size_t j {};

	while (j < noElems) {
		if (numlist[j] == toFind)
			break;

		++j;
	}

	std::cout << toFind;

	if (j < noElems)
		std::cout << " at position " << j + 1;
	else
		std::cout << " not found";

	std::cout<< '\n';
}


to become a linked list and array


??

A linked list isn't an array. The code above uses an array. If you want a linked list then you need to implement one using dynamic memory.

Assuming this is an assignment, what exactly are you being asked to do?
Last edited on
there's an error on this program supposedly

Supposedly? As in you were given "wrong code" and you should fix it, or
you have written code but suspect that it does not do the right thing?


What is below is not the correct answer to the "count comparisons and instances by modifying the 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
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <algorithm>
#include <list>

int main ()
{
    const int numlist[] {15,18,2,19,18,0,8,14,19,14};
    std::cout << "Pos\tValue\n";
    for ( int i = 0; int num : numlist )
    {
        std::cout << i++ << '\t' << num << '\n';
    }
    int value {};
    std::cout <<"Value to seek : \n";
    std::cin >> value;

    size_t count = 0;
    auto it = std::find_if( std::begin(numlist), std::end(numlist),
    [&count, value](int x){ ++count; return x == value; } );
    if ( it != std::end(numlist) ) {
        auto index = std::distance( std::begin(numlist), it );
        std::cout << "The value " << value << " is at position " << index << '\n';
    }
    else {
        std::cout << "The value " << value << " not found\n";
    }
    std::cout << count << " comparisons\n\n";

    
    std::list<int> numar( std::begin(numlist), std::end(numlist) );
    for ( int i = 0; int num : numar )
    {
        std::cout << i++ << '\t' << num << '\n';
    }
    count = 0;
    auto it2 = std::find_if( begin(numar), end(numar),
    [&count, value](int x){ ++count; return x == value; } );
    if ( it2 != end(numar) ) {
        auto index = std::distance( begin(numar), it2 );
        std::cout << "The value " << value << " is at position " << index << '\n';
    }
    else {
        std::cout << "The value " << value << " not found\n";
    }
    std::cout << count << " comparisons\n\n";


    auto occ = std::count( std::begin(numlist), std::end(numlist), value );
    std::cout << occ << " occurrences of " << value << '\n';
}
count of repeating instances for a given search element ‘k’


Maybe something like:

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
#include <iostream>

int main() {
	const size_t noElems { 10 };
	const int numlist[noElems] { 15, 18, 2, 19, 18, 0, 8, 14, 19, 14 };
	int toFind {};

	std::cout << "The Elements within the Array : \n";

	for (size_t i {}; i < noElems; ++i)
		std::cout << i + 1 << "  " << numlist[i] << '\n';

	std::cout << "\nEnter element to find: ";
	std::cin >> toFind;

	size_t fnd {};

	for (size_t j {}; j < noElems; ++j)
		if (numlist[j] == toFind) {
			std::cout << "Found at position " << j + 1 << '\n';
			++fnd;
	        }

	if (!fnd)
		std::cout << "Not found\n";
	else if (fnd > 1)
		std::cout << fnd << " instances found\n";
}

Last edited on
using std::list, then as a starter consider:

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

int main() {
	const std::list<int> numList { 15, 18, 2, 19, 18, 0, 8, 14, 19, 14 };
	int toFind {};

	std::cout << "The Elements within the Array : \n";

	for (size_t i {}; const auto& e : numList)
		std::cout << ++i << "  " << e << '\n';

	std::cout << "\nEnter element to find: ";
	std::cin >> toFind;

	size_t comps {};

	if (const auto fnd { std::ranges::find_if(numList, [&](auto e) {++comps; return e == toFind; }) }; fnd != numList.end())
		std::cout << "Found at position " << std::distance(numList.begin(), fnd) + 1 << " with " << comps << " comparisons\n";
	else
		std::cout << "Not found\n";
}

@seeplus

there's an error in line 20, it says no member named 'ranges' in namespace 'std'
seeplus tends to use the latest standard when answering questions, which is C++20. If you click the Edit & run on cpp.sh link you will see that the C++20 standard is selected in the list on the bottom left of the page.

You should update your compiler to support at least C++17. (But if you are updating, might as well get the latest and support C++20.)

Thereafter you need to compile with the proper flags to use the latest standard. Fortunately that is pretty easy:

  • Add /std:c++20 to MSVC’s command-line
  • Add -std=c++20 to GCC’s and Clang’s command-line

If you are using an IDE like Code::Blocks or VSCode or something, you need to muck around the compiler settings to find the command-line options and change them.
Thank you guys for the help!
Topic archived. No new replies allowed.