### Help with Functions and Loop

Pages: 12

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133`` ``````#include using namespace 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> 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"); }``````

Your functions have no sense because they use local variables in calculations that were not initialized. For example

i
 ``123456`` ``````nt sumofpositive (int x) // Sum of Positive { int z; if (x > 0) z = (z + x); return z; }``````

what is the value of z that is being added with x? Moreover the function returns nothing in case when x <= 0.
 `` `` ``what is the value of z that is being added with x? Moreover the function returns nothing in case when x <= 0.``

 ``12345678`` ``````int sumofpositive (int x) // Sum of Positive { int z; if (x > 0) z = (z + x); return z; } a = sumofpositive (num)``````

shouldn't it do the input value of num?

and the final output to a?
I wrote very clear. I do not understand why you are asking your silly questions.
That's why I am asking. I don't even understand what's wrong, how could I answer your question? It's my first time using functions fyi.

I did the same thing using

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include using namespace 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> num; if (num > 0) sump = sump + num; if (num < 0) sumn = sumn + num; if (num > 0) a++; else if (num < 0) b++; else c++; if (num < 0) f++; else if (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.
One more what is the value of variable z in this expression used in the function I pointed out

if (x > 0) z = (z + x);
Last edited on
Well according to my working code, that I mentioned

Input 25
Input 25

if (num > 0) sump = sump + num;

output = 50

If I take sump away from the equation,

Input 25
Input 25

if (num > 0) sump = num;

output 25

Therefore I placed "z" in that expression

if (x > 0) z = (z + x);

Hope you understand it.
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

 ``12`` `````` int z; if (x > 0) z = (z + x);``````

that is present in the function definition.
Last edited on
Well the value of z is the value of summed z and then + x until all are summed up I guess. If my answer is wrong then I honestly don't know its value.
Expression z = (z + x); uses subexpression ( z + x ). What is the value of z in this subexpression?
Zero I guess.
Shortly speaking you defined local variable z

int z;

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

z = (z + x);

has no any sense becuase its result is undefined.
Last edited on
 @Gencaerus Zero I guess.

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.

 ``123456789101112`` `````` int sump=0, sumn=0; // For the sum of positive and negative integers // other statements are skipped for (ctr=0; ctr> 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

 ``12345678910`` ``````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 ); }``````

And should be called with two arguments.
That code is beyond what I currently know.
Last edited on
The statement

return ( ( x < 0 ) ? z + x : z );

is equivalent to

if ( x < 0 ) return z + x;
else return z;

Or you can write for example

 ``12345`` ``````int sumofnegative ( int z, int x) // Sum of Negative { if ( x < 0 ) z += x; return ( z ); }``````

Last edited on
But what will I put in

 a = sumofnegative (num);

Last edited on
First of all you should initialize a

int a = 0, b = 0; // For the Sum of all Positive and Negative integers

and use the following calls

a = sumofpositive ( a, num );
b = sumofnegative ( b, num );
Holy, atlast atleast 2 functions worked.

Thank you very much for spending your time with me. If you can just spend more time helping me with the others I'd be very much glad.
Try to do something yourself. You have already enough information.
Pages: 12