Look over some coding problems?

Hello everyone. I just finished answering these coding problems and was wandering if you guys could look over them to see if they are done correctly. Thanks so much!

In the following problems, what does the given function return?

Problem 1
1
2
3
4
5
6
7
8
9
10
11
12
 int foo(void) 
{ 
	int i, j, k, x = 0; 
	 
	for ( i = 5; i >0 ,i -= 3)
		for (j = 3; j*2 < 10; j++)
			for (k=7; k+j < 14; k++)
			{	x += 2;
				x += 3;
			}
	return (x);
}

foo returns : x=40


Problem 3

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
int bar(void) 
{ 	
   // return ((x+y)*z); (18*12) // return x //9

	if (foo(8,10,12) > foobar(9,8,7)) //
	//	return (foo(2,4,6)); 
	else 
		return (foobar(1,3,5)); 
} 

int foo (int x, int y, int z) 
{ 
	if ((x > y) && (y > z)) 
		return (x); 
	else 
		return ((x+y)*z); 
}


int foobar (int x, int y, int z) 
{ 
	if ((x > y) && (y > z)) 
		return (x); 
	else 
		return ((x*y)+z); 
}

bar returns : 36


Last edited on
Compile... run. Check answers?
It wont compile as is :(

How do I set them up so I can see the return value of the function? Thanks.
Last edited on
you can not overload the return value
return ((x*y)+z);
set a variable = to that value and then return that value.
you can not overload the return value
return ((x*y)+z);
set a variable = to that value and then return that value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string> 
using namespace std;
int foo (void);
int n;
int z;
int main()
{
	cout<< z<< endl;
}
 int foo(void) 
{ 
	int i, j, k, x = 0; 
	 
	for ( i = 5; i >0 ,i -= 3;)
		for (j = 3; j*2 < 10; j++)
			for (k=7; k+j < 14; k++)
			{	x += 2;
				x += 3;
			}
	x = z;
	return (z);
}


I set it up so that return =z but it said z equals 0 when i ran it. Could this possibly be right?
Last edited on
1
2
3
you can not overload the return value
return ((x*y)+z);
set a variable = to that value and then return that value. 


I don't know what you think this does, but it doesn't overload the return value and is perfectly legal.
It wont compile as is :(

How do I set them up so I can see the return value of the function? Thanks.


In the code above where you attempted to do this, you never called foo and output the value of a global variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string> 
using namespace std;
int foo (void);


int main()
{
	cout<< foo() << endl;
}

 int foo(void) 
{ 
	int i, j, k, x = 0; 
	 
	for ( i = 5; i >0 ,i -= 3;)
		for (j = 3; j*2 < 10; j++)
			for (k=7; k+j < 14; k++)
			{	x += 2;
				x += 3;
			}

	return (x);
}
Last edited on

In the code above where you attempted to do this, you never called foo and output the value of a global variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24



#include <iostream>
#include <string>
using namespace std;
int foo (void);


int main()
{
cout<< foo() << endl;
}

int foo(void)
{
int i, j, k, x = 0;

for ( i = 5; i >0 ,i -= 3;)
for (j = 3; j*2 < 10; j++)
for (k=7; k+j < 14; k++)
{ x += 2;
x += 3;
}

return (x);
}



I tried running it the way you wrote it above^^. It ran but gave me no output. It was just blank. Does anyone know what I need to add or do to see what x is equal to?
I copy/pasted your code and there's a typo in it.

for ( i = 5; i >0 ,i -= 3;)

should be:

for ( i = 5; i >0 ;i -= 3)

If that typo was part of the original question, it would be the same as:

for( i = 5; i -= 3; ), so the outer loop would stop only when i was equal to 0.
Last edited on
But when I take away the colon it gives me a syntax error. Does anyone know how I can run the function and just see the return value? That is all I really want to check. There must be a way to do it. Thanks.
There was no colon. There was a semi-colon and a comma. Look more closely, grasshopper.
Topic archived. No new replies allowed.