3. Using User Defined Functions do the following question:

my_array = { 3, 61, -5, 38, -17, 21, 92, -64, 41, -6}

Find the sum of odd and even numbers in the array


!!please help me to answer this question!!

3. Using User Defined Functions do the following question:

my_array = { 3, 61, -5, 38, -17, 21, 92, -64, 41, -6}

Find the sum of odd and even numbers in the array


!!please help me to answer this question!!

Do you want help or do you want someone to do it for you? If you want help, you need to post some sort of an attempt or a more specific question.

#include <iostream>
using namespace std;

int my_arrayLength(int sumOfEven,int sumOfOdd)
int main()

{

int my_array = {3, 61, -5, 38, -17, 21, 92, -64, 41, -6};

int sumOfEven=0;
int sumOfOdd=0;
int sum(my_array);

public void sum(int arr[])
	{
	for(int i=0;i<my_arrayLength; i++)
		{
	
		int sumOfEven=0;
		int sumOfOdd=0;
		
		if(my_array[i]%2==0)
		{
			sumOfEven+=sumOfEven;
		}
		else
		{
			sumOfOdd+=sumOfOdd;
		}
		cout<<"Sum of even numbers is"<<sumOfEven;
		cout<<"Sum of odd numbers is"<<sumOfOdd;
		}
	}
return 0;
}

I wrote this but it don't work.What must I add it to work? or if you can do it in another way,please write for me

#include <iostream>
using namespace std;
int my_arrayLength(int sumOfEven,int sumOfOdd)
int main()
{
int my_array = {3, 61, -5, 38, -17, 21, 92, -64, 41, -6};
int sumOfEven=0;
int sumOfOdd=0;
int sum(my_array);
public void sum(int arr[])
{
for(int i=0;i<my_arrayLength; i++)
{
int sumOfEven=0;
int sumOfOdd=0;
if(my_array%2==0)
{
sumOfEven+=sumOfEven;
}
else
{
sumOfOdd+=sumOfOdd;
}
cout<<"Sum of even numbers is"<<sumOfEven;
cout<<"Sum of odd numbers is"<<sumOfOdd;
}
}
return 0;
}

I wrote this but it don't work.What must I add it to work? or if you can do it in another way,please write for me

One, you can't have a nested function in C++. You have a function inside main. It needs to go after or before main, not inside main.

Two, what is my_arrayLength ? There's no function in C++ that can retrieve the length of an array. You need to pass the array length to the function as an integer parameter.

can you write the correct form of code for me? I cannot understand how to do this

Before you get strangled with compiler errors. Please note that in c++
you write public: Then all things below are public (until either the end of the class or one of private: or protected: The reason I am flagging THIS error up, is that the compilation error you generate is likely to be completely indecyferable.

Complete diversion : [P.I.M.P please ignore (sorry)].
VernonDozier said:

There's no function in C++ that can retrieve the length of an array.

Now I would have agreed for the last 20 years BUT last week I saw this:

template<typename T, int size>
int getArrayLength(T(&)[size]){ return size; }

And that works in a simpler context (ie the given situation)

#include <iostream>

template<int size>
int
func(int (&X)[size])
{
  std::cout<<"X == "<<size<<std::endl;
 return 0;
}

int main()
{
  int A[50];
  int B[80];

  func(A);
  func(B);
  return 0;
}
commented: Nice. +11

No. No-one will write code for you, unless you're their respectable employer, teacher, or project partner.

First function is missing a semicolon.
Why do you declare and define another function inside of a function?
Why not use a container if you don't know how to use an array?
Why are you using the class keyword public?

You can also just check the right-most bit of a value, to see if it's an odd or even.

Thank you all..

Before you get strangled with compiler errors. Please note that in c++
you write public: Then all things below are public (until either the end of the class or one of private: or protected: The reason I am flagging THIS error up, is that the compilation error you generate is likely to be completely indecyferable.

Complete diversion : [P.I.M.P please ignore (sorry)].
VernonDozier said:


Now I would have agreed for the last 20 years BUT last week I saw this:

template<typename T, int size>
int getArrayLength(T(&)[size]){ return size; }

And that works in a simpler context (ie the given situation)

#include <iostream>

template<int size>
int
func(int (&X)[size])
{
  std::cout<<"X == "<<size<<std::endl;
 return 0;
}

int main()
{
  int A[50];
  int B[80];

  func(A);
  func(B);
  return 0;
}

Well I'll be damned. Learn something new every day. I was just taking other people's word for it on this forum in previous threads that you can't figure out the length of an array without keeping track of it. But your program certainly works. I did a forum search and a few very experienced people who I had thought said it was impossible offered very similar solutions to yours. I must have not read them closely enough before. Thanks for the heads up.

But P.I.M.P., I think it would still be better to pass the size of the array to the function. No, I won't write it for you, but I'll give you a bit of a skeleton that could work. Note the parameters of the function and that it returns an integer. Also note the array declaration.

#include <iostream>
using namespace std;

int sum (int array[], int arraySize, bool sumEvenNumbers);


int main ()
{
    int my_array[] = {3, 61, -5, 38, -17, 21, 92, -64, 41, -6};


    // code

    return 0;
}


int sum (int array[], int arraySize, bool sumEvenNumbers)
// if sumEvenNumbers is true, add up the even numbers
// otherwise, add up the odd numbers
{
    // return an integer
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.