Creating if, if...else and switch case program

I was asked to create a program that calculate heat energy[Q=mc(delta)T] using if, if...else and switch case but I don't know how to do it. Can anyone help me?(One program for each case)
Last edited on
But the tutorial only shows the simple program without calculation.
We're not going to do your homework for you.

Make an attempt and post what you've written and we will try and help with questions.
so you dont want help with if else and switch, you want help with the maths?
I've done this so far. But I don't know what I should put in the if() .

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

using namespace std;

int main()
{
    int m;
    cout << "Enter mass: ";
    cin >> m;
    
    int c;
    cout << "Enter specific heat: ";
    cin >> c;

    int t1;
    cout << "Enter initial temperature: ";
    cin >> t1;
    
	int t2;
    cout << "Enter final temperature: ";
    cin >> t2;



     if ()
	 {
      answer = m*c*(t2-t1);
      cout << "Q = " << answer << endl;;
   
    return 0;
    
}
Last edited on
(One program for each case)

This implies you aren't telling us stuff.

What, exactly are these programs meant to do? If it's just calculating energy then why do you need 2 programs?

The way you've described the problem i dont see why you'd have to have any if/else/switch statements in there. Unless you are talking in terms of validation. for example:
(psuedocode:

1
2
3
4
5
6
7
if(t1 > t2)
{
    // if this is true your subtraction on line 30 will come out as negative
    // so here you might want to multiply t2 - t1 by minus 1 to get a positive
    // result.
}



OR you could tell the user if your mass is cooling or heating up say.
psuedocode:

if(t1 < t2)
{
    print "body is getting hotter" 
}
else
{
    print "body is cooling"
}
Last edited on
It's the program for if case.

I was asked write a program that will calculate heat energy automatically by using selection that include: if, if-else, if-else-if & switch-case.
Well i can't help anymore sorry, as i have no idea why you'd need conditional testing in a program that just needs to multiply 3 numbers together.
I don't know how to do it that's why I ask here. Thanks anyway.
Do you have a more detailed description of the problem that you can post here? Are you supposed to be running the calculation for different materials, different conditions? It's not clear from what you've posted so far why an if/else or switch statement would be needed.
This is the task:

Write a program that will calculate heat energy automatically by using c++.The system MUST consist of the following:

(i) Functions that include: pass by value & pass by reference
(ii) Selection that include: if, if-else, if-else-if & switch-case

I already done with the first part.
Thanks. Honestly - it's still not clear to me why they want you to include selection.
It's ok.
Topic archived. No new replies allowed.