#include <iostream>
usingnamespace std;
int sumofpositive (int x) // Sum of Positive
{
int z;
if (x > 0) z = (z + x);
return z;
}
int sumofnegative (int x) // Sum of Negative
{
int z;
if (x < 0) z = (z + x);
return z;
}
int positive (int x) // Positive value
{
int z;
if (x > 0) z++;
return z;
}
int negative (int x) // Negative value
{
int z;
if (x < 0) z++;
return z;
}
int zero (int x) // Zero value
{
int z;
if (x == 0) z++;
return z;
}
int odd (int x) // Odd value
{
int z;
if (x %2==!0) z++;
return z;
}
int even (int x) // Even value
{
int z;
if (z %2==0) z++;
return z;
}
int noteo (int x) // Not Even nor Odd value
{
int z;
if (x < 0) z++;
return z;
}
int addition (int x, int y) // Sum of All Inputs
{
int z;
z = (x + y);
return z;
}
int main ()
{
int ctr=0, unk=1; // For loop
int n, num; // For looping input/output
int a, b; // For the Sum of all Positive and Negative integers
int c, d, e; // For Positive, Negative and Zero value
int f, g, h; // For Odd, Even and Not Even nor Odd value
int i; // For the sum all input integers
cout << "Please enter input size: ";
cin >> n;
cout << endl;
cout << endl;
for (ctr=0; ctr<n; ctr++)
{
cout << "Enter the integer value " << (unk++) << ": ";
cin >> num;
a = sumofpositive (num);
b = sumofnegative (num);
c = positive (num);
d = negative (num);
e = zero (num);
f = odd (num);
g = even (num);
h = noteo (num);
i = addition (num, num);
}
cout << endl << "Number of Positive Integer(s): " << c;
cout << endl << "Number of Negative Integer(s): " << d;
cout << endl << "Number of Zero(es): " << e;
cout << endl;
cout << endl << "Number of Odd Integer(s): " << f;
cout << endl << "Number of Even Integer(s): " << g;
cout << endl << "Number of Not Even nor Odd Integer(s): " << h;
cout << endl;
cout << endl << "The sum of all Positive Integers: " << a;
cout << endl << "The sum of all Negative Integers: " << b;
cout << endl << "The sum of all input integers: " << i;
cout << endl;
cout << endl;
system ("pause");
}
I've been coding these for hours, I'm deadbeat already.
#include <iostream>
usingnamespace std;
int main()
{
int ctr=0, unk=1; // For loop
int n, num; // For looping input/output
int sump=0, sumn=0; // For the sum of positive and negative integers
int a=0, b=0, c=0, d=0, e=0, f=0; // For Positive, Negative, Zero, Odd, Even and not Even nor Odd
float g; // For the Sum of all integers
float h; // For the average of the sum of all integers
cout << "Please enter input size: ";
cin >> n;
cout << endl;
cout << endl;
for (ctr=0; ctr<n; ctr++)
{
cout << "Enter the integer value " << (unk++) << ": ";
cin >> num;
if (num > 0) sump = sump + num;
if (num < 0) sumn = sumn + num;
if (num > 0) a++;
elseif (num < 0) b++;
else c++;
if (num < 0) f++;
elseif (num %2== 0) e++;
else d++;
g = sump + sumn;
h = g/n;
}
cout << endl << "Number of Positive Integer(s): " << a;
cout << endl << "Number of Negative Integer(s): " << b;
cout << endl << "Number of Zero(es): " << c;
cout << endl;
cout << endl << "Number of Odd Integer(s): " << d;
cout << endl << "Number of Even Integer(s): " << e;
cout << endl << "Number of Not Even nor Odd Integer(s): " << f;
cout << endl;
cout << endl << "The sum of all Positive Integer(s): " << sump;
cout << endl << "The sum of all Negative Integer(s): " << sumn;
cout << endl;
cout << endl << "The sum of all kind of Integer(s): " << g;
cout << endl << "The average of the sum of all kind of Integer(s): " << h;
cout << endl;
cout << endl;
system ("pause");
}
And it worked perfectly fine, howerver I need to do the same thing using functions only.
I understand it. It is you who understands nothing.
Do not show me your various code snips. Answer the simple question what is the value of variable z in expression
it was not initialized. So it can contain any arbitrary value more precisely any garbage that was in memory at the moment of the variable definition.
So the next expression
Uninitialized local variables have no any predetermined values. And moreover if a local variable had value 0 in this case you code has no sense because it simply returns the argument it got.
int sump=0, sumn=0; // For the sum of positive and negative integers
// other statements are skipped
for (ctr=0; ctr<n; ctr++)
{
cout << "Enter the integer value " << (unk++) << ": ";
cin >> num;
if (num > 0) sump = sump + num;
if (num < 0) sumn = sumn + num;
First of all sump and sumn were initialized by zeroes
int sump=0, sumn=0;
Moreover statements
if (num > 0) sump = sump + num;
if (num < 0) sumn = sumn + num;
use previous values of sump and sumn to get new values of sump and sumn.
The corressponding functions could look the following way
1 2 3 4 5 6 7 8 9 10
int sumofpositive (int z, int x ) // Sum of Positive
{
return ( ( x > 0) ? z + x : z );
}
int sumofnegative ( int z, int x) // Sum of Negative
{
return ( ( x < 0 ) ? z + x : z );
}