0

Anybody know this?,I'm tired of searching.

Q/ Write a C++ function named it (count) to count the even numbers in the array and return it. In the main method declare an int array of size 5 and ask user to input elements in to it, call your count function to output the number of even numbers in the array.

18th Mar 2020, 5:09 PM
Rovan Xalil
Rovan Xalil - avatar
6 ответов
+ 2
Rovan Xalil , Welcome 😀 keep learning!
18th Mar 2020, 8:20 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
If you need help show your attempt. If you get stuck while solving problem , feel free to ask. Link your code in description of this question. It's not very hard you just need to know about operators and if-else statement. Try it.
18th Mar 2020, 6:49 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
Thanks for your suggestion. Here is my code I did it with Void Type ,but i want it in Return Type. i tried a lot i couldn't. #include <iostream> using namespace std; void Count(int TestArr[]); int main() { int TestArr[5]; cout << "Hi User enter the array elements? " << endl; for (int i = 0; i < 5; i++) { cin >> TestArr[i]; } Count(TestArr); return 0; } void Count(int TestArr[]) { cout << "These are the even numbers that you want: " << endl; for (int i = 0; i < 5; i++) { if (TestArr[i] % 2 == 0) { cout << TestArr[i] << endl; } } }
18th Mar 2020, 7:31 PM
Rovan Xalil
Rovan Xalil - avatar
+ 2
Rovan Xalil , Thanks for understanding. Often people ask questions but don't understand that efforts from their side matter the most. back to question, You can return only one value at a time from a function. You could use pointers , raw arrays but that's more of C way. https://www.sololearn.com/discuss/2183600/?ref=app In C++ we have vectors , tuples and many more classes to make coding efficient. #include <iostream> #include <vector> using namespace std; vector<int> Count(int TestArr[]) { vector <int> v; for (int i = 0; i < 5; i++) { if (TestArr[i] % 2 == 0) { v.push_back(TestArr[i]); } } return v; } int main() { int TestArr[5]; cout << "Hi User enter the array elements? " << endl; for (int i = 0; i < 5; i++) { cin >> TestArr[i]; } cout<<"Here are even numbers\n"; auto vec = Count(TestArr); for(int elem : vec) cout<<elem<<" "; return 0; }
18th Mar 2020, 7:54 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
You can see how to return raw arrays in post I shared in previous answer. Using vector will be much easier. I have declared return type as vector<int> which means vector of integers. Vector can be resized at runtime. You'll understand <int> is template argument if you have seen this in tutorial. class templates. push_back is method of vector class that inserts new element at end of collection. If you still have doubts then search official documentation , if still don't understand then ask here.
18th Mar 2020, 8:02 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
🇮🇳Omkar,Thank you man I understand now i didn't know about Vectors. You did a lot to me Thank you very much.
18th Mar 2020, 8:18 PM
Rovan Xalil
Rovan Xalil - avatar