Sum of even and odd values in an array – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-CBSE12A-2018-03A #CPP#5270    siteicon   siteicon  

Problem Statement - Sum of even and odd values in an array

Write the definition of a function SumEO(int VALUES[], int N) in C++, which should display the sum of even values and sum of odd values of the array separately.

Example: if the array VALUES contains
25 20 22 21 53

Then the functions should display the output as:
Sum of even values = 42 (i.e 20+22)
Sum of odd values = 99 (i.e 25+21+53)

Solution

TC++ #5270

void SumEO(int VALUES[], int N)
{
  int SE = 0, SO = 0;
  for (int I=0;I<N;I++)
  {
    if(VALUES[I] %2 == 0)
      SE += VALUES[I];
    else
      SO += VALUES[I];
  }
  cout<< "Sum of even values = " << SE<<endl;
  cout<< "Sum of odd values = " << SO<<endl;
}

Notes

int SE = 0, SO = 0; //Sum of Even SE and Sum of Odd SO has to be initialized with 0 so that final additions can be done in these variables.
for (int I=0;I<N;I++) { //This loop runs till array length passed as N
if(VALUES[I] %2 == 0) SE += VALUES[I]; // This check is for even numbers, by testing remainder after dividing by 2, which is successively added to SE.
else SO += VALUES[I]; //Otherwise assuming odd numbers will be successively added to SE
}
cout<< “Sum of even values = ” << SE<<endl;
cout<< “Sum of odd values = ” << SO<<endl;
Relevant values are printed.
}


Share

CSKC| Created: 4-Dec-2018 | Updated: 5-Dec-2018|CBSE12A-2018






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back