Issues writing functions

My program keeps having problems initializing variables, it's pulling the error code "unable to resolve identifier." The pseudocode seems right to me so I don't really understand why I'm having so many problems with this. Help is really appreciated. :)


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>
using namespace std;
/* Write the following functions and provide a program to test them.
 * a. double smallest(double x, double y, double z), returning the smallest 
 * of the arguments
 * b. double average(double x, double y, double z), returning the average 
 * of the arguments
 */
int main() {
    double smallest_value (double x, double y, double z)
    {
        double smallest; 
        if (x < y && x < z)
        {
            x = smallest;
        }
        else (y < x && y < z)
        {
            y = smallest;
        }
        else (z < x && z < y)
        {
            z = smallest;
        }
        cout << smallest;   
    }
    double average(double x, double y, double z)
    {
        int sum = x + y + z
        int average = sum /3
        cout << sum;
    }
    void smallest_value (1,2,3);
    void average (1,2,3);
    return 0;
}
Your functions are inside of your main. They should first be declared outside of your main, before main is ran. At the end of main, as is common coding practice, you should define the functions. In main itself, you simply call the functions.
In addition, you never initialize "smallest" yet you use it to assign a value to x.
Yeah... I think that in the smallest_value function, you have the assignment operator mixed up- it assigns the value to the right of it to the variable to the left. Try flipping each variable around with "smallest" and see if it works then.
closed account (zb0S216C)
Quite a few things are noteworthy:

1) Functions cannot be defined within another function. However, functions can be declared inside a function, but common (and preferred) practice is to declare functions outside a function.

2) Inside the definition of "smallest_value( )", "else" statements cannot test conditions. The way conditionals are formed in C++ is the following:

1
2
3
4
5
6
7
8
9
10
11
12
if( Something )
{
}

else if( Something_Else... )
{
}

else
{
   // None of the above...
}

Because you have two adjacent "else" statements, the compiler will complain that the second "else" does not have a corresponding "if".

3) In "average( )", "sum" cannot hold single-/double-precision values, so, on line 29, the values of "x", "y" and "z" are rounded to the lowest whole number so that "sum" can hold the value of the computation. It would be better to be consistent and declare "sum" as a "double".

4) On lines 33 and 34, you're trying to call "smallest_value( )" and "average( )", respectively, but the compiler thinks otherwise. To invoke, or call, a function, you need to do the following:

1
2
3
4
int main( )
{
  FunctionName( Arguments );
}

Wazzak
Last edited on
I'm not sure if I flipped the variables the way you were thinking of. But here's my code so far, I'm having errors between 12 and 20.

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
#include <iostream>
using namespace std;

    double smallest_value (double x, double y, double z)
    {
        int smallest = 0; 
        if (x < y && x < z)
        {
            smallest = x;
        }
        else (y < x && y < z)
        {
            smallest = y;
        }
        else (z < x && z < y)
        {
            smallest = z;
        }
        cout << smallest;   
    }
    
    double average_number (double x, double y, double z)
    {
        int sum = x + y + z;
        int average = sum /3;
        cout << average;
    }
    int main() {
    smallest_value (1,2,3);
    average_number (1,2,3);
    return 0;
}
Line 11:
You forgot what Framework mentioned about if/else statements.
 
else if (y < x && y < z)
My bad, I didn't refresh the page before posting and missed his comment. I'm not sure if I did the right thing with calling the functions respectively. The program runs now but I'm getting an output of 12.
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
#include <iostream>
using namespace std;


    double smallest_value (double x, double y, double z)
    {
        int smallest = 0; 
        if (x < y && x < z)
        {
            smallest = x;
        }
        else if (y < x && y < z)
        {
            smallest = y;
        }
        else if (z < x && z < y)
        {
            smallest = z;
        }
        cout << smallest;   
    }
    
    double average_number (double x, double y, double z)
    {
        double sum = x + y + z;
        double average = sum /3;
        cout << average;
    }
    int main() 
    {
    {
    smallest_value (1,2,3);
    }
    {
        average_number (1,2,3);
    }
    return 0;
}
Last edited on
The output is correct. Your cout statements simply display the answers next to each other. You can add "<< endl" or "<< ' ' " so you can see the difference.

Your answers are "1" being the smallest number and "2" being the average, both being correct.
Last edited on
That was really silly of me xb. Thanks so much for the help. :D
But what happens when
(a) two of the numbers are the same - what is output as the smallest?
for example smallest_value (1,3,1);

(b) what about the average when it isn't a whole number?
for example average_number (1,3,4);
Last edited on
Topic archived. No new replies allowed.