CSC 102 Exercize Question


I'm doing some exercises from a cpp book and the question is

"Consider the following function prototype:

int test(int,char,double,int);

Write a C++ statement that prints the value returned by the function test with the actual
parameters 5, 5, 7.3, and ' z'."

Would the following code I just wrote be appropriate/accurate?


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
#include <iostream>
#include <cmath>

using namespace std; 

int main(void);
int test (int, char, double, int);



int main()
{
	int actual;
	int a = 5;
	char b = 'z';
	double c = 7.3;
	int d = 5;

	actual = test(a, b, c, d);
	printf ("The value is   ");

	return 0;

}

int test(int first, char second, double third, int fourth)
{

	int value;
	cout << first << "," << second<< ","<< third << ","<< fourth << endl;
	cin >> value ;
	return value;



}
No, because the function itself likely shouldn't be printing anything nor ask for user input.
Edit: I should clarify -- what you did technically works, but your function was not really using the parameters to influence the result. If you had a comment near the function (or near the prototype in your case) explaining what the function was supposed to do, it'd be clearer.

main() is like the user of the function. The user calls it with some parameters, gets a result, and if he wants to print out the result then he'll print it.

You might also want to convert tabs to spaces in your editor ;D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std; 

int test(int a, char b, double c, int d)
{
    return a + (int)b*c / d;
}

int main()
{
    int a = 1;
    char b = '=';
    double c = 2.05;
    int d = 3;

    int actual = test(a, b, c, d);
    cout << actual << endl;

    return 0;
}

output:
42
Last edited on
Yes, except that you forgot to print the value of the returned value stored in actual.
@icy1 & @ peter87

ah yes!, I meant to remove that

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
#include <iostream>


using namespace std; 

int main(void);
int test (int, char, double, int);



int main()

{
	int actual;
	int a = 5;
	char b = 'z';
	double c = 7.3;
	int d = 5;

	actual = test(a, b, c, d);

}

int test(int first, char second, double third, int fourth)

{
	int value;
	cout << first << "," << second<< ","<< third << ","<< fourth << endl;
	cin >> value ;
	return value;

}
Topic archived. No new replies allowed.