Help with Functions and Loop

Pages: 12
Code is working, all outputs are wrong. Please help me correct these.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>

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<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.
Your functions have no sense because they use local variables in calculations that were not initialized. For example

i
1
2
3
4
5
6
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.


1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

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<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++;
           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

1
2
    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.
Let consider your working code

1
2
3
4
5
6
7
8
9
10
11
12
    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 );
}


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

1
2
3
4
5
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