C++ Algorithm problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 .

10th Jun 2020, 1:05 PM
Mr Robot
Mr Robot - avatar
24 Answers
+ 12
#include <algorithm> #include <iterator> std::all_of(std::begin(array), std::end(array), [](int x) { return x > 0; });
10th Jun 2020, 1:22 PM
Schindlabua
Schindlabua - avatar
+ 9
int count =0; for (int i =0; i < arr.length; i l++) { if(arr[i] > 0) count++; } if(count == arr.length) cout>>"success"
10th Jun 2020, 1:19 PM
_yaroslavv [online_everyday]
_yaroslavv [online_everyday] - avatar
+ 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.
10th Jun 2020, 1:35 PM
Schindlabua
Schindlabua - avatar
+ 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";
10th Jun 2020, 7:47 PM
Brian
Brian - avatar
+ 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.
10th Jun 2020, 2:25 PM
Schindlabua
Schindlabua - avatar
+ 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()`.
10th Jun 2020, 2:01 PM
Schindlabua
Schindlabua - avatar
+ 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
10th Jun 2020, 2:05 PM
Mr Robot
Mr Robot - avatar
+ 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
10th Jun 2020, 6:05 PM
Cihad Jasem Alhaji
Cihad Jasem Alhaji - avatar
+ 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
10th Jun 2020, 7:56 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
+ 3
#include<algorithm> #include<iterator> std::all_of(std::begin(array), std::end(array), [](int x) {return x > 0;});
11th Jun 2020, 11:54 AM
Codewithsawann
Codewithsawann - avatar
+ 2
Oh...sure. you can use: int length = (sizeof(arr)/sizeof (*arr))
10th Jun 2020, 1:32 PM
_yaroslavv [online_everyday]
_yaroslavv [online_everyday] - avatar
+ 2
Schindlabua thanks for your guidance . It really helps me alots.
10th Jun 2020, 2:53 PM
Mr Robot
Mr Robot - avatar
+ 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";
12th Jun 2020, 7:47 AM
Honorable Con 🇸🇳
Honorable Con 🇸🇳 - avatar
+ 1
the loop is not suitable?
10th Jun 2020, 1:12 PM
_yaroslavv [online_everyday]
_yaroslavv [online_everyday] - avatar
+ 1
_yaroslavv [online_everyday] asking or telling ^_^
10th Jun 2020, 1:14 PM
Mr Robot
Mr Robot - avatar
+ 1
You are talking about that?
10th Jun 2020, 1:19 PM
_yaroslavv [online_everyday]
_yaroslavv [online_everyday] - avatar
+ 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
10th Jun 2020, 1:28 PM
Mr Robot
Mr Robot - avatar
+ 1
Schindlabua I don't understand your code any reference you know Or explain little
10th Jun 2020, 1:29 PM
Mr Robot
Mr Robot - avatar
+ 1
_yaroslavv [online_everyday] Int length = sizeof(*arr); Why cannot simply write this?
10th Jun 2020, 1:41 PM
Mr Robot
Mr Robot - avatar
10th Jun 2020, 1:58 PM
Mr Robot
Mr Robot - avatar