functions

Good day everyone, need your help cause I'm stuck with one task of my HW,
I have to write a code in a specific way (using functions) which will

count how many times the result of an equation(a+b-c) was smaller than 10. - what i wrote it displays always 1. Please check lines 39-42 and 86-87

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
#include <iostream>
 #include <cstdlib>
 #include <ctime>
 #include <cstring>
 #include <conio.h>
 #include <iomanip>
 #include <windows.h>

 using namespace std;

 int insertINT(char mes[256])
 {
 int value;
 cout<<mes;
 cin>>value;

 return value;
 }


 int abc(int a, int b, int c) // a+b-c
 {
 return a+b-c;
 }


 void showINT(char mes[256],int R)
 {
 cout<<mes<<R<<endl;
 }


 int sum(int R, int s) //sum of the results a+b-c
 {
 return R+s;
 }


int contor(int co) // counter
{
	 co++;
	return co;
}


int parity(int R, int even, int odd) // parity check
{
 if(R%2==0)
 {
 even++; cout<<"even";
 }
 else
 {
 odd++; cout<<"odd";
 }

return R%2==0? even: odd;	
	
}



 int main()
 {
 srand(time(0));


 int p, even=0, odd=0, co=0, cp, suma=0, a,b,c, n = insertINT("Enter n=");
 
 
 for(int i=0; i<n; i++)

 {

 showINT("\n iteration nr.=",i+1);

 a = insertINT("Enter a=");

 b = insertINT("Enter b=");

 c = insertINT("Enter c=");


 suma = sum( abc(a,b,c) ,suma);

 if( suma < 10 ){
 cp=contor( co); }



 showINT("Result of a+b-c=", abc(a,b,c) );
 showINT(" a+b-c ", parity(abc(a,b,c), even,odd));
 
 }


 cout << " suma = "<<suma<<endl;
 cout << " The results of equation were smaller than 10 for "<<cp<<" times"<<endl;

 
 return 0;

 }
Last edited on
L84, 86 suma is the cumulative sum of abc(). So you are testing that the cumulative sum is < 10 - not the result of abc()

L87 contor(co) returns the value of its argument + 1. As co is never changed, cp will always be 1.
#seeplus, even if change Line 86 to

1
2
if( abc(a,b,c) < 10 ){
 cp=contor( co); }


when I display the counter it shows 1, can you please be more specific
L87 contor(co) returns the value of its argument + 1. As co is never changed, cp will always be 1.


L86 - I was trying to condition the statement to check if the result (doesn't matter cumulative sum or the result abc()) < 10.

L39-42. And I used the counter function contor( co) to count how many times it should be < 10.
As co is never changed,
- That's the issue, I can't handle why function will never count ?!
Remove the unecessary cp variable and use only co.
#coder777
I thought a function should return its value to a variable and that's why I stored it into cp.
anyway even if I remove the cp it doesn't work properly....sorry to be a pain in the ass, I am new to this...
1
2
3
4
5
6
7
8
9
10
11
int contor(int co) // counter
{
	 co++;
	return co;
}
...
int main()
{
    int co=0, cp;
    ...
    cp=contor( co); 
Do you realize that the co parameter defined in fuction contor() is different from the co variable defined inside main()? So when you increment co in contor(), you are not incrementing co in main(). I'd change line 87 to
co = contor(co);
and change line 98 to print co instead of cp.

Your parity function is wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int parity(int R, int even, int odd) // parity check
{
 if(R%2==0)
 {
 even++; cout<<"even";
 }
 else
 {
 odd++; cout<<"odd";
 }

return R%2==0? even: odd;	// If R%2 is 0, this returns 1. If R%2 is 1, it returns 1.
	
}

Also, even and odd should be local variables.
I'd change parity() so it doesn't output anything to cout and it returns 0 for even parity and 1 for odd parity. That turns out to be incredibly simple:
1
2
3
4
int parity(int R) // parity check
{
    return R%2;
}


I strongly urge you to indent your code according to show the block structure of the program. Otherwise it's just too easy to misplace a } and spend hours trying to find it. There's a good chance that your editor can indent the code for you.

#dhayden thank you master!

I can't understand how I forgot that the variables in main function are different from the ones used in functions.

And thanks a lot about your advice with parity,



Topic archived. No new replies allowed.