Advertisement
Lawnknome

evenOdd.cpp

Nov 3rd, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sumArray (int oddEven[], int arraySize);
  5.  
  6. int main ()
  7. {
  8.    
  9.     int array_size = 0;
  10.     int evenOdd [array_size];
  11.  
  12.     cout << "Enter the size of the array you wish to create: ";
  13.  
  14.     while (!(cin >> array_size))
  15.     {
  16.         cin.clear();
  17.         cin.ignore(1000, '\n');
  18.         cout << "\nPlease enter a valid integer: ";
  19.     }  
  20.  
  21.     for(int count = 0; count < array_size; count++)
  22.     {
  23.         cout << "\nPlease enter the value of the element at position " << count << ": ";
  24.  
  25.         while (!(cin >> evenOdd[count]))
  26.         {
  27.             cin.clear();
  28.             cin.ignore(1000, '\n');
  29.             cout << "\nPlease enter a valid integer: ";
  30.         }  
  31.     }  
  32.  
  33.     cout << "\n\nThe result of the sum of the even values minus the sum of the odd values is ";
  34.     cout << sumArray(evenOdd, array_size) << ".\n\n";
  35.  
  36.     return 0;
  37. }
  38.  
  39. int sumArray (int oddEven[], int arraySize)
  40. {
  41.     int result;
  42.     int sumEven = 0;
  43.     int sumOdd = 0;
  44.     int count;
  45.    
  46.     for(count = 0; count < arraySize; count++)
  47.     {
  48.         if((oddEven[count] % 2) == 0)
  49.             sumEven += oddEven[count];
  50.  
  51.         else
  52.             sumOdd += oddEven[count];
  53.     }
  54.  
  55.     result = sumEven - sumOdd;
  56.  
  57.     return result;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement