Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello

So I'm trying to create program which will ask the user to enter 10 integers and then check if the numbers that they enter are odds or evens by using functions like int numOfOdd , int numOfEven or something similar to that. After that I want to find the sum of the total amount of odds and the sum of the total amount of evens and then print everything out with a function maybe like this one void outputResults ( int numOfOdd, int oddTotal, int numOfEven, int evenTotal ); If there's anyone can help with this, I thank you in advance. Also, I said that I want to check the odds and evens using functions but down here you can see that I used loops as I found making functions is kind of confusing, but if someone can show me how to do it with function, that would be awesome too.

Down here is my code

What I have tried:

C++
<pre>
#include<iostream>

#include<string>

using namespace std;

int main()
{
    int numbers[10], even = 0, odd = 0, i;
    cout << "Please enter 10 integers ";
    for (i = 0; i < 10; i++)
        cin >> numbers[i];
    for (i = 0; i < 10; i++)
    {
        if (numbers[i] % 2 == 0)
            even++;
        else
            odd++;
    }
    cout << "The amount of even numbers: " << even;
    cout << "The amount of odd numbers: " << odd;
    cout << endl;
    return 0;
}
Posted
Updated 15-Dec-21 20:35pm
Comments
Richard MacCutchan 15-Dec-21 11:44am    
In the loop where you check for even or odd numbers you can add each number to the respective sums at the same time.

Create a function called numOfEven which accepts and array and which returns an integer.
In the function, count the even numbers - you have code for that - and return it.
Do the same for a numOfOdd function.
Then call them, save the results, and sum the results to a total varaibale
Print the results.

Simple.
 
Share this answer
 
You are almost there. Try this :
C++
#include<iostream>

#include<string>

using namespace std;

inline bool isEven( int value )
{
    return (value  % 2 == 0);
}


int main()
{
    const int numberCount = 10;
    int sumEven = 0;
    int sumOdd = 0;
    int numbers[numberCount], even = 0, odd = 0, i;

    cout << "Please enter " << numberCount << " integers ";
    for (i = 0; i < numberCount; i++)
        cin >> numbers[i];

    for (i = 0; i < numberCount; i++)
    {
        if( isEven( numbers[i] )
        {
            even++;
            sumEven += numbers[i];   // update the sum of even values
        }
        else
        {
            odd++;
            sumOdd += numbers[i];    // update the sum of odd values
        }
    }

    cout << "The count of even numbers: " << even << "their sum is " << sumEven;
    cout << "The count of odd numbers : " << odd  << "their sum is " << sumOdd;
    cout << endl;
    return 0;
}
If you want, and I usually do, you can put all of your input values in a file and then run the program with input redirected from that file. This way you don't have to enter the values every time. You can do this by typing this at the command line : MyProgram << InputData.txt. With the appropriate names for your program and the input file. The input file should have one value per line. This will make things much easier I think.
 
Share this answer
 
v2
Comments
CPallini 15-Dec-21 16:14pm    
Hey Rick, have a second look at your isEven function.
Also, have a second look at your the summing up code.
Tiến Dũng Đỗ 15-Dec-21 21:45pm    
Yes sir, there're 4 errors in there
Rick York 15-Dec-21 23:57pm    
You are progressing if you can see the errors - or did the compiler tell you? :)
Rick York 15-Dec-21 23:58pm    
It's a puzzle - let's see if he can fix it. :)
Tiến Dũng Đỗ 16-Dec-21 0:57am    
Yes sir, I did fix them :) But for the summing up code, I tried both your way and this: sumOfEven = sumOfEven+i; it will only print out 10 no matter what
Let's focus on odd values.
'Traditional' implementation
C++
<iostream>
using namespace std;

enum { N = 10 };

bool odd(int i);
int count_of_odd(int a[], int size);
int sum_of_odd(int a[], int size)


int main()
{
  int a[N];
  cout << "please enter " << N << " integers\n";

  for (int n=0; n<N; ++n)
  {
    cin >> a[n];
    if ( odd(a[n] ))
      cout << a[n] << " is odd\n";
  }

  cout << "count of odd integers " << count_of_odd(a, N) << "\n";
  cout << "sum of odd integers " << sum_of_odd(a, N) << "\n";
}

bool odd(int i) {return (i & 1) == 1;}

int count_of_odd(int a[], int size)
{
  int count = 0;
  for (int n = 0; n<size; ++n)
    count += odd(a[n]) ? 1 : 0;

  return count;
}

int sum_of_odd(int a[], int size)
{
  int sum = 0;
  for (int n = 0; n<size; ++n)
    sum += odd(a[n]) ? a[n] : 0;

  return sum;
}


'Less-traditional' implementation
C++
#include <iostream>
#include <array>
#include <algorithm>
#include <numeric>

using namespace std;

enum { N = 10 };
int main()
{

  auto odd = [] (int i) { return ((i & 1) == 1); };
  auto add_odd =  [odd] (int sum, int i) { return (sum + (odd(i) ? i : 0)); };

  array<int, N> a;
  cout << "please enter " << N << " integers\n";

  for (auto & x : a)
  {
    cin >> x;
    if ( odd(x) )
      cout << x << " is odd\n";
  }

  cout << "count of odd integers " << count_if( a.begin(), a.end(), odd) << "\n";
  cout << "sum of odd integers " << accumulate( a.begin(), a.end(), 0, add_odd ) << "\n";

}


Choose your poison...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900