Need help for my program ! Urgent PLEASE!!

This is my program to calculate the emitted power for two type of light.
After I change the normal if else statement to function , it cant run and I have no idea how to fix the problem. I think the problem is from the function.
PLEASE HELP ME :(

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
134
135
 

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
const double h = 6.626e-34;
const double c = 2.998e+08;
const double k = 1.381e-23;
const double pai = 3.14159;

char validType(char type)
{	
	
	do
	{
		cout << "Enter the type of radiation spectrum\n";
		cout << "Visible light" << setw(5) << "-1\n";
		cout << "Ultraviolet light" << setw(5) << "-2\n";
		cout << "Type of spectrum\n";
		cin >> type;
	} while (!(type == '1' || type == '2'));
	
}

double validateLimits(char type)
{
	int lowerL, upperL;
	if (type == '1')
	{
		do
		{
			cout << "Enter the wavelength in <nm> for visible light\n";
			cout << "400-700\n";
			cout << "Lower limit: \n";
			cin >> lowerL;

			if (lowerL < 400)
			{
				cout << "Lower limit must>=400 \n";
			}
		} while (lowerL < 400);

		do
		{
			cout << "Upper Limit: \n";
			cin >> upperL;

			if (upperL > 700)
			{
				cout << "Upper limit must <= 700 \n";
			}
		} while (upperL > 700);
	}
	if (type == '2')
		do
		{
			cout << "Enter the wavelength in <nm> for Ultraviolent light\n";
			cout << "400-700\n";
			cout << "Lower Limit :\n";
			cin >> lowerL;
			if (lowerL < 10)
			{
				cout << "Lower limit must >= 10 \n";
			}
		}while (lowerL < 10);
		do
		{
			cout << "Upper limit : \n";
			cin >> upperL;
			if (upperL>400)
			{
				cout << "Upper limit must <= 400 \n";
			}
		} while (upperL > 400);

}

int temp()
{
	int temp;
	cout << "Enter the temperature in <K>:";
	cin >> temp;
	return temp;
}

int surfaceArea()
{
	int area;
	cout << "Enter the radiating surface area in <mm>:";
	cin >> area;
	return area;
}

double power(double wavelength, double temp)
{
	double power;
	power = ((2 * pai*h*c*c) / (pow(wavelength, 5.0)))*(1 / (exp((h*c) / (wavelength*k*temp)) - 1));
	return power;
}

double callPower(double LL, double UL, double temp, double area)
{
	double sumpower = 0;
	for (int x = LL; x <= UL; x++)
	{
		sumpower += (power((x*(1e-9)), temp))*area*pow(10.0, -15.0);	
	}
	return sumpower;
}




int main()
{
	char type;
	double wavelength;
	double area, emittedpower , temp;
	int UL, LL;
	
	type = validType(type);

	validateLimits(type);
	temp = temp();
	area = surfaceArea();
	emittedpower = callPower(LL, UL, temp, area);
	cout << "Emitted power\n";
	cout << "----------------\n";
	cout << emittedpower << "Watts"<<endl;

	system("pause");
	return 0;

}
JunTioh

You cannot write lines like
temp = temp();
I'm sure that your compiler tells you this. Either the variable or the function name will have to be changed.

You call callPower from main without having set the values of LL and UL: presumably you meant to set these in validateLimits, but you didn't return them as parameters.

In main, the arguments LL and UL are integers, yet in the function definition they are double. These must be made consistent. Given that they are used as the ends of a loop they should probably be integers (despite their physical meaning).


Other things:
Although they are major physical constants and hence candidates for global variables, Planck's constant h, Boltzmann constant k etc. are only used in power() - might be better to define them there.

Please write 1.0e-15, not pow(10.0, -15.0). Is this correct, anyway?

Presumably surface area is measured in mm squared, not mm. (Actually, unless there is a good reason to do otherwise I would be inclined to enter it in metres squared.)

Is there any good physical reason why you separate visible and ultraviolet spectra here?

I would watch out for significant over-runs with your exp() function in line 98. You could re-express the spectral density to avoid this if necessary.



Last edited on
closed account (48T7M4Gy)
Forgetting about the physics you have a few compatibility, function call and variable naming problems:

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
134
135
136
#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

const double h = 6.626e-34;
const double c = 2.998e+08;
const double k = 1.381e-23;
const double pai = 3.14159;

char validType(char type)
{
    do
    {
        cout << "Enter the type of radiation spectrum\n";
        cout << "Visible light" << setw(5) << "-1\n";
        cout << "Ultraviolet light" << setw(5) << "-2\n";
        cout << "Type of spectrum\n";
        cin >> type;
    } while (!(type == '1' || type == '2'));
    
    return type; // <-- at least
}

double validateLimits(char type)
{
    double xyz = 0; //<--- at least so can have a return value
    
    int lowerL, upperL;
    if (type == '1')
    {
        do
        {
            cout << "Enter the wavelength in <nm> for visible light\n";
            cout << "400-700\n";
            cout << "Lower limit: \n";
            cin >> lowerL;
            
            if (lowerL < 400)
            {
                cout << "Lower limit must>=400 \n";
            }
        } while (lowerL < 400);
        
        do
        {
            cout << "Upper Limit: \n";
            cin >> upperL;
            
            if (upperL > 700)
            {
                cout << "Upper limit must <= 700 \n";
            }
        } while (upperL > 700);
    }
    if (type == '2')
        do
        {
            cout << "Enter the wavelength in <nm> for Ultraviolent light\n";
            cout << "400-700\n";
            cout << "Lower Limit :\n";
            cin >> lowerL;
            if (lowerL < 10)
            {
                cout << "Lower limit must >= 10 \n";
            }
        }while (lowerL < 10);
    do
    {
        cout << "Upper limit : \n";
        cin >> upperL;
        if (upperL>400)
        {
            cout << "Upper limit must <= 400 \n";
        }
    } while (upperL > 400);
    
    return xyz; // <--- need a return value
}

double temp() // <--- returns a double ???
{
    double temporary; // <--- must have another name than temp
    cout << "Enter the temperature in <K>:";
    cin >> temporary;
    
    return temporary;
}

int surfaceArea()
{
    int area;
    cout << "Enter the radiating surface area in <mm>:";
    cin >> area;
    return area;
}

double power(double wavelength, double temp)
{
    double power;
    power = ((2 * pai*h*c*c) / (pow(wavelength, 5.0)))*(1 / (exp((h*c) / (wavelength*k*temp)) - 1));
    return power;
}

double callPower(double LL, double UL, double temp, double area)
{
    double sumpower = 0;
    for (int x = LL; x <= UL; x++)
    {
        sumpower += (power((x*(1e-9)), temp))*area*pow(10.0, -15.0);
    }
    return sumpower;
}

int main()
{
    char type;
    double wavelength;
    double area, emittedpower , temporary;
    int UL, LL;
    
    type = validType(type);
    
    validateLimits(type);
    
    temporary = temp(); // <---- temporary is a double
    area = surfaceArea();
    emittedpower = callPower(LL, UL, temporary, area); // <--- temporary, not temp
    cout << "Emitted power\n";
    cout << "----------------\n";
    cout << emittedpower << "Watts"<<endl;
    
    system("pause");
    return 0;
}
Last edited on
closed account (48T7M4Gy)
Still no physics but a little more rationalisation and simplification:

Hint: use meaningful variable names, initialise variables, don't be afraid to use line space for clarity.

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

using namespace std;

const double h = 6.626e-34;
const double c = 2.998e+08;
const double k = 1.381e-23;
const double pai = 3.14159;

void getLimits(int &LL, int &UL)
{
    char type = ' ';
    
    do
    {
        cout << "Enter the type of radiation spectrum\n";
        cout << "Visible light" << setw(5) << "-1\n";
        cout << "Ultraviolet light" << setw(5) << "-2\n";
        cout << "Type of spectrum: ";
        cin >> type;
    } while (!(type == '1' || type == '2'));
    
    if (type == '1')
    {
        do
        {
            cout << "Enter the wavelength in <nm> for visible light\n";
            cout << "400-700\n";
            cout << "Lower limit then upper limit: " ;
            cin >> LL >> UL;
        } while (LL < 400 or UL > 700 or LL > UL);
        return;
    }
    
    if (type == '2')
    {
        do
        {
            cout << "Enter the wavelength in <nm> for visible light\n";
            cout << "10-400\n";
            cout << "Lower limit then upper limit: " ;
            cin >> UL >> LL;
        } while (LL < 10 or UL > 400 or LL > UL);
        return;
    }
    
    return;
}

double getTemperature()
{
    double temperature;
    cout << "Enter the temperature in <K>: ";
    cin >> temperature;
    
    return temperature;
}

int getSurfaceArea()
{
    int area;
    cout << "Enter the radiating surface area in <mm>: ";
    cin >> area;
    return area;
}

double power(double wavelength, double aTemperature)
{
    return ((2 * pai*h*c*c) / (pow(wavelength, 5.0)))*(1 / (exp((h*c) / (wavelength*k*aTemperature)) - 1));
}

double totalPower(double LL, double UL, double temp, double area)
{
    double sumpower = 0;
    for (int x = LL; x <= UL; x++)
    {
        sumpower += (power((x*(1e-9)), temp))*area*pow(10.0, -15.0);
    }
    return sumpower;
}

int main()
{
    double wavelength = 0;
    double area = 0;
    double emitted_power = 0;
    double temperature = 0;
    int UL = 0;
    int LL = 0;
    
    getLimits(LL, UL);
    
    temperature = getTemperature();
    area = getSurfaceArea();
    
    emitted_power = totalPower(LL, UL, temperature, area);
    
    cout << "Emitted power\n";
    cout << "----------------\n";
    cout << emitted_power << " Watts"<<endl;
    
    return 0;
}
Topic archived. No new replies allowed.