Write a program that reads a set of integers (can be +ve or -ve) and then finds and prints the sum of the positive odd integers, negative even integers, and all integers not belonging to the previous two groups. (Use the following as test data: 1,2,3,4,5,-5,-4,-3,-2,-1,18,-18). You must make use a function named sumofNumbers that take an integer number that indicate how many numbers are in the test data and return the results (sum of positive odd integers, sum of negative even integers and sum of all integers which are not belonging to the previous two groups.
1
Expert's answer
2012-07-12T08:36:13-0400
#include <tchar.h> #include <iostream> using namespace std;
int* sumofNumbers(int n){ int* data = new int[n]; for(int i = 0; i < n; i++) & cin >> data[i]; int* result = new int[3]; for(int i = 0; i < 3; i++) & result[i] = 0; for(int i = 0; i < n; i++) & if((data[i] > 0)&&(data[i]%2 ==1)) & result[0] += data[i]; & else if((data[i] < 0)&&(data[i]%2 == 0)) & result[1] += data[i]; & else & result[2] += data[i]; return result; }
int _tmain(int argc, _TCHAR* argv[]) { int* result = sumofNumbers(12); for(int i = 0; i < 3; i++) & cout << result[i] << endl; }
Comments
Leave a comment