Quote:
Originally Posted by Rafael Olarte
Hello,
Thanks for all your help.
I was finally able to figure this out.
I had to create two more additional conditions for the output that did not contain any data on the last column. This was cuasing some formating issue, but in reality it had to do with the conditions code.
Now, I can use whatever value on the tempInput.txt file, and the results, and the formatting look perfect.
This is how it works correctly:
if (tempDif1 < 4)
outData << setw(10) << "close" << endl;
else if (tempDif1 > 4.3)
outData << setw(15) << "big change" << endl;
else if (tempDif1 > 4)
outData << setw(10) << " " << endl;
else if(tempDif1 < 4.3)
outData << setw(15) << " " << endl;
Hi,
Although what you did will work, is was unnecessary complex. Think about it this way. You have five regions:
-
number line indicating possible values of x:
-
-
Group 2 Group 4
-
Group 1 | Group 3 | Group 5
-
x < 4 <######################| |
-
x > 4.3 | |#######################>
-
| |
-
x <= 4 && x >= 4.3 ##############################
-
(i.e everything else) | |
-
_______________________|______________ _____________|________________________
-
4 4.3
-
Group 1 is x < 4
-
Group 2 is x = 4
-
Group 3 is 4 < x && x < 4.3
-
Group 4 is x = 4.3
-
Group 5 is 4.3 < x
-
- less than 4 (Group 1)
- greater or equal to 4 but less or equal to 4.3 (Group 2, 3 and 4)
- greater than 4.3 (Group 5)
In each of these cases, you want (respectivly)
- print out "close"
- don't print anything
- print out "big change"
In every case, you want to have a endl to come out. I don't think you really care if there are a bunch of spaces after the line if you don’t print “close” or “big change”. So, all you need are the conditions that output “close” or “big change” excluding the endl and then at the end of it all, output an endl.
Now, let us take a look at what you wrote here. You have 4 conditions, two of which overlap the same region:
-
number line indicating possible values of x:
-
-
Group 2 Group 4
-
Group 1 | Group 3 | Group 5
-
x < 4 <####################| |
-
x > 4.3 | |###################>
-
x > 4 |###############################################>
-
x < 4.3 <################################################|
-
_____________________|________________ __________|____________________
-
4 4.3
-
Group 1 is x < 4
-
Group 2 is x = 4
-
Group 3 is 4 < x && x < 4.3
-
Group 4 is x = 4.3
-
Group 5 is 4.3 < x
-
Your if statement will be executed sequencially, and once a true condition is found, the execution will stop looking for truths in the other else if statements and start at the next statement after them.
So, lets ‘execute’ this:
- Is tempDif1 < 4? If true, then you have found Group 1. So output “close” with endl. Start executing after if/else if statement.
- Otherwise is tempDif1 > 4.3? If true, then you have found Group 5. So output “big change” with endl. Start executing after if/else if statement.
- Otherwise, is tempDif1 > 4? If true, you have found Group 3, 4 and 5. Though you have already found group 5 in point 2 so in reality you have just found Group 3 and 4. So Output 10 spaces with endl. Start executing after if/else if statement.
- Otherwise, is tempDif1 < 4.3? If true, you have found Group 1, 2 and 3. But you have already found Group 1 in point 1, and Group 3 in point 3, so in reality you have just found Group 2. So output 15 spaces with endl. Start executing after if/else if statement.
Do you see what I am getting at? You can merge three of these groups (Groups 2, 3 and 4) into one, and not print any spaces.
If you insist, you can output an endl at the end of each “close” and “big change”, but in that case you should just have one else as a catchall to just output an endl. But it is unnecessary since when you find any of these groups (Group 1, 2, 3, 4 or 5) each require a endl. So you can put the endl after the if/elseif statement without having a final else statement.
I hope that I am not talking to the wind and you think about this. It is an important concept to grasp in computing theroy when generating good code.
Adrian
What I have just described is called group theroy, which is the analysis of grouping sets (stuff) in order to find set intersections (commonalities or overlapping sections) or set disjunctions (inverse intersections where there is no overlapping area), and other interesting things and can be applied to writing efficent, effective and less buggy computer programmes.