+ 2
C++ Algorithm problem
Int arr = {1,5,9,9}; I want to print success if all numbers in array > 0 Array size may be hundred or thousand how can I do that . In javascript I use every() for this purpose .
24 Answers
+ 12
#include <algorithm>
#include <iterator>
std::all_of(std::begin(array), std::end(array), [](int x) { return x > 0; });
+ 9
int count =0;
for (int i =0; i < arr.length; i l++) {
if(arr[i] > 0)
count++;
}
if(count == arr.length)
cout>>"success"
+ 7
`all_of` is C++'s equivalent of Javascript's `every`.
Whereas in JS we would write
array.every(x => x > 0)
in C++ that is
all_of(
begin(array),
end(array),
[ ] (int x) { return x > 0; }
);
A key difference is that you can choose from where to where you iterate. In our case we just take the beginning and the end of our array using `std::begin` and `std::end`—in other words, the whole array. (All functions inside <algorithm> work this way).
The third parameter is a lambda, in case you haven't seen it before. It should be familiar from javascript, however C++ lambdas do work a bit differently. Not in this simple case though.
Here's a reference for `std::all_of`: http://www.cplusplus.com/reference/algorithm/all_of/
Hope that helped.
+ 6
An optimization would be to break out of the loop upon finding the first element that disqualifies the array:
int i;
for (i =0; i < arr.length && arr[i] > 0; i++);
if(i == arr.length)
cout << "success";
+ 5
Saad Mughal You're welcome :P
I don't read too many tutorials these days so I don't have many links, sorry!
Though I did come across this talk a year or so ago that was kind of cool, it's about everything inside <algorithm>: https://youtu.be/2olsGf6JIkU
Maybe that's something to check out. cplusplus.com is great for looking up things too.
std::vectors are a must-know, you will come across them a LOT. Since as you probably know, plain arrays cannot change size, and vectors can.
In my opinion the best way to improve is to push yourself and program something big, you will learn lots. Tutorials should accompany coding and not replace it.
But yeah I am not the best person to talk to about that kind of stuff.
+ 4
Saad Mughal That works for all collections, like vectors etc, but a plain array does not have `arr.begin()` sadly, as arrays are not objects.
So it's `std::begin(a)` if you are using plain arrays.
Or use
std::vector<int> vec {1,2,5,29};
instead. Then you can `vec.begin()`.
+ 3
Schindlabua OK bro thanks. you help me so much. I mostly know the lessons sololearn provide me and I have a good practice in it
But I don't know the advanced concept of cpp like vectors where do I find these advanced concept of c++ what things I have to do next . Plz tell me I shall be very thankful to you
+ 3
In c++17 , you can use std::size(arr) to get the length of an array and then loop as usual. But if it was me i would do like this (see attached code)
https://code.sololearn.com/ccm8UfYQIcXL/?ref=app
+ 3
bool cond=true;
for(int i=0;i<arr.length && cond; i++)
if(!(arr[i]>0))cond=false;
if(cond)cout<<"success";
// A bit faster algorithm if you need one
+ 3
#include<algorithm>
#include<iterator>
std::all_of(std::begin(array),
std::end(array), [](int x) {return x > 0;});
+ 2
Oh...sure. you can use:
int length = (sizeof(arr)/sizeof (*arr))
+ 2
Schindlabua thanks for your guidance . It really helps me alots.
+ 2
int c=0;
int array[]={1,5,9,9};
for(int n: array)
if (n<0) break;
else c++;
if (c == sizeof(array)/sizeof(int))
cout<<"success";
+ 1
the loop is not suitable?
+ 1
_yaroslavv [online_everyday] asking or telling ^_^
+ 1
You are talking about that?
+ 1
_yaroslavv [online_everyday] I think arr.length is not working in c++ but the logic you tell me help me very much in similar cases Thanks
+ 1
Schindlabua I don't understand your code any reference you know
Or explain little
+ 1
_yaroslavv [online_everyday]
Int length = sizeof(*arr);
Why cannot simply write this?
+ 1
https://code.sololearn.com/c7R41P1J5HDZ/?ref=app
Schindlabua what is the problem here?