program effciency

Hi there:) I had to create an array that can hold ten integers, and get input from user. Then I had to ask the user for an integer, search for that integer and count the number of times it is found.
The solution I found online is different to mine. Both of them work but I was wondering which one is more efficient.

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

using namespace std;

int main()

{
    int i, arr[10], n,c=0;
    cout<<"Enter your number:";
    cin>>n;
    cout<<"Enter elements of array:";
    for(i=0; i<10; i++)
        cin>>arr[i];
    for(i=0; i<10; i++)
        if (arr[i]==n)
            c++;
    cout<<"The number was found"<<" "<<c<<" "<<"times";
    return 0;
}



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
#include<iostream>
using namespace std;


int count(const int arr1[10], int numb)
{
    int cont=0;

    for (int u=0; u<10; u++)
    {

        if (arr1[u]==numb)
            cont++;
    }
    return cont;
}
int main()
{

    int arr[10];
    int num;
    cout<<"Enter 10 values in array:";

    for(int i = 0; i < 10; i++)
        cin >> arr[i];
    cout << "Values in array are now:";

    for(int i = 0; i < 10; i++)
        cout << " " << arr[i];
    cout<<"\n"<< "Enter value to find";
    cin >> num;
    cout<< num<< " was found " << count(arr,num)<<"times";
    return 0;
}




Last edited on
I guess that the code with the count function might be a bit slower(few millis) because calling a function causes some overhead.

BTW The best way to write this code IMO is to use either std::array or std::vector and std::count since in 2017 we should use C++11
> I was wondering which one is more efficient.

The version with a separate count function is more programmer efficient.

There would be no difference between the two as far as machine efficiency is concerned (compilers know how to auto inline functions).
Thomas, you say I should get rid of using namespace std; and put std:: every time it's needed? If so , I have another question. Why would you do that? I find it kinda hard to do so, especially in a long code.
theoretically you can't do this (with an array type structure) without at least looking at each number one time, so a loop that touches each item once is about as good as it gets. From there any tweaking is going to have minimal effect until you have billions of entries.

if you know that the data is in a small range (say, 0 to 100) you can use the bucket sort type algorithm to store the values as they are entered and get the answer in a single operation. But for general purpose data, that can't happen. You can do something similar using a map structure, though... where the key is the number input, and the element stored is the # of times. Map is not limited by the range.
Last edited on
> I should get rid of using namespace std; and put std:: every time it's needed?
> I find it kinda hard to do so, especially in a long code.

While certain rules are universally accepted (for example: do not place using declarations and directives in header files), there are two valid points of view:

One which says that using declarations and directives are perfectly acceptable in implementation files, especially if it is local to a scope; if you perceive that it aids readability, use it.

In short: You can and should use namespace using declarations and directives liberally in your implementation files after #include directives and feel good about it. Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable.
...
But using declarations and directives are for your coding convenience, and you shouldn't use them in a way that affects someone else's code. In particular, don't write them anywhere they could be followed by someone else's code: Specifically, don't write them in header files (which are meant to be included in an unbounded number of implementation files, and you shouldn't mess with the meaning of that other code) or before an #include (you really don't want to mess with the meaning of code in someone else's header).

- Sutter and Alexandrescu in 'C++ Coding Standards: 101 Rules, Guidelines, and Best Practices'



SF.6: Use using namespace directives ... for foundation libraries (such as std) ...

using namespace can lead to name clashes, so it should be used sparingly. However, it is not always possible to qualify every name from a namespace in user code (e.g., during transition) and sometimes a namespace is so fundamental and prevalent in a code base, that consistent qualification would be verbose and distracting.

CoreGuideLines https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rs-using


The other, which says that using explicit namespace prefixes, even in implementation files, makes the code clearer, and more portable.

In implementation files (e.g. .cpp files), the rule is more of a stylistic rule, but is still important. Basically, using explicit namespace prefixes makes the code clearer, because it is immediately obvious what facilities are being used and where they are coming from. And more portable, because namespace clashes cannot occur between LLVM code and other namespaces. The portability rule is important because different standard library implementations expose different symbols (potentially ones they shouldn't), and future revisions to the C++ standard will add more symbols to the std namespace. As such, we never use 'using namespace std;' in LLVM.
- LLVM Coding Standards



Think about it, and then as a general practise, follow what appeals to you more.
Hi. I notice "using namespace std" has come up again, the old threads have been closed down that i'd have liked to comment on, and so can't. I appreciate this is off topic but can anyone point me in the direction of how to open it up for comment again please or reopen the coversation on std::cout etc. I've had some thoughts, that miraculously i haven't been able to find anywhere else on the internet. . .Thanks
If you have something new to say, there's no harm in creating another topic.

I don't believe there's any way to re-open archived topics. Just open a new one, and reference the old threads as required.
Topic archived. No new replies allowed.