|
Hey,
Try this:
if (area1 > area2) // area 1 greater than area 2 { cout << "blablah"; } else { // the else here is so that if the first if checksthrough as true, the elses will be ignored. if (area2 > area1) { cout << "blahblah"; } else // both areas are equal { cout << "blahblah"; } }
Should work with no problems, for each "if" your allowed one else statement in C++
|
On October 08 2010 05:15 KiLL_ORdeR wrote: These are the last problems. Updating OP.
The Greatest and Least of These *Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all of the numbers have been entered, the program should display the largest and smallest numbers entered.
This will do the greatest and least. The getch at the end is to prevent the program from closing right away. You can change "int _tmain(int argc, _TCHAR* argv[])" to "int main()".
#include "stdafx.h" #include <iostream> #include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { int nInputNumber = 0; int nCounter = 0; int anNumbers[100000]; int nMax = INT_MIN; int nMin = INT_MAX;
cout << "This program will input a series of numbers and show the maximum and minimum\n";
cout << "Enter a number(-99 to finish): "; cin >> nInputNumber;
while(nInputNumber != -99) { anNumbers[nCounter] = nInputNumber; nCounter++;
cout << "Enter a number(-99 to finish): "; cin >> nInputNumber; }
for(int i = 0; i < nCounter; i++) { int nNum = anNumbers[i];
if(nNum > nMax) nMax = nNum; if(nNum < nMin) nMin = nNum; }
if(nCounter == 0) cout << "No numbers entered"; else cout << "Maximum: " << nMax << endl << "Minimum: " << nMin;
cout << "\nEnter any key to quit . . .";
_getch(); return 0; }
|
On October 08 2010 08:17 HowitZer wrote:Show nested quote +On October 08 2010 05:15 KiLL_ORdeR wrote: These are the last problems. Updating OP.
The Greatest and Least of These *Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all of the numbers have been entered, the program should display the largest and smallest numbers entered. This will do the greatest and least. The getch at the end is to prevent the program from closing right away. You can change "int _tmain(int argc, _TCHAR* argv[])" to "int main()". #include "stdafx.h" #include <iostream> #include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { int nInputNumber = 0; int nCounter = 0; int anNumbers[100000]; int nMax = INT_MIN; int nMin = INT_MAX;
cout << "This program will input a series of numbers and show the maximum and minimum\n";
cout << "Enter a number(-99 to finish): "; cin >> nInputNumber;
while(nInputNumber != -99) { anNumbers[nCounter] = nInputNumber; nCounter++;
cout << "Enter a number(-99 to finish): "; cin >> nInputNumber; }
for(int i = 0; i < nCounter; i++) { int nNum = anNumbers[i];
if(nNum > nMax) nMax = nNum; if(nNum < nMin) nMin = nNum; }
if(nCounter == 0) cout << "No numbers entered"; else cout << "Maximum: " << nMax << endl << "Minimum: " << nMin;
cout << "\nEnter any key to quit . . .";
_getch(); return 0; }
Thanks a lot. It's really helpful to look at the program done out like this. Since I know we won't have the exact same problems at the exam, it's really helpful to be able to see how these are structured.
|
I just did the oceans level one, but I fucked up somewhere.
+ Show Spoiler [Code so Far] + #include <iostream> using namespace std;
int main()
{ int years = 0; cout << "Years Growth\n"; cout << "_____________________\n"; while (years <= 25) { cout << years << "\t\t" << (years + 1.5) <<endl; years++; } return 0; }
This one isn't doing what it's supposed to do. Instead of adding 1.5 each loop, it's instead starting at 1.5 and adding one. Gonna look back I think I just need to change years++
|
I think you just need to change the (years + 1.5) to (years * 1.5) for it to work like you want. Putting the arithmetic into a cout stream isn't going to actually change the variable's value.
|
Not contributing anything but I program in Java, some of it looks very similar.
|
On October 08 2010 09:44 KiLL_ORdeR wrote:I just did the oceans level one, but I fucked up somewhere. + Show Spoiler [Code so Far] + #include <iostream> using namespace std;
int main()
{ int years = 0; cout << "Years Growth\n"; cout << "_____________________\n"; while (years <= 25) { cout << years << "\t\t" << (years + 1.5) <<endl; years++; } return 0; }
This one isn't doing what it's supposed to do. Instead of adding 1.5 each loop, it's instead starting at 1.5 and adding one. Gonna look back I think I just need to change years++
Years * 1.5 instead of year + 1.5 should do it, but that's the messy way I'd probably do this
int years = 1; float growth = 1,5; while (years <= 25) { cout << "Year " << years << " - " << growth << " millimeters" << endl; years++; growth += 1.5; }
|
On October 08 2010 05:32 Scorcher2k wrote:Show nested quote +On October 08 2010 05:21 Tenrou wrote: Please do not give OP codes to do his program. This is not helping him at all. OP is still having trouble with the syntax. I would suggest rereading the book and look at the examples in it.
Because examples don't give him code? I'm sorry but seeing code that works for problems I'm having or people pointing things out to me is the fastest way that I learn. Simply because you think that it might not help him does not mean that it is actually the case.
Programming is a creative process similar to creating a new art work. You CANNOT teach someone how to program. You can teach him the syntax and semantics within a program, but you cannot teach him how to program. He must learn how to use these tools creatively and create something useful. By giving him code that already does his programming assignment, you are starving him from knowing the whole process of how he should use those tools to create that program.
On October 08 2010 09:44 KiLL_ORdeR wrote:I just did the oceans level one, but I fucked up somewhere. + Show Spoiler [Code so Far] + #include <iostream> using namespace std;
int main()
{ int years = 0; cout << "Years Growth\n"; cout << "_____________________\n"; while (years <= 25) { cout << years << "\t\t" << (years + 1.5) <<endl; years++; } return 0; }
This one isn't doing what it's supposed to do. Instead of adding 1.5 each loop, it's instead starting at 1.5 and adding one. Gonna look back I think I just need to change years++
years is a variable of type int so it can only hold whole number. + Show Spoiler + int years = 0; years = years + 1.5; //years will become 1 because it truncates the decimal part away
years++ post increment the variable by 1 every time which is not what you want.
|
On October 08 2010 12:03 Tenrou wrote:Show nested quote +On October 08 2010 05:32 Scorcher2k wrote:On October 08 2010 05:21 Tenrou wrote: Please do not give OP codes to do his program. This is not helping him at all. OP is still having trouble with the syntax. I would suggest rereading the book and look at the examples in it.
Because examples don't give him code? I'm sorry but seeing code that works for problems I'm having or people pointing things out to me is the fastest way that I learn. Simply because you think that it might not help him does not mean that it is actually the case. Programming is a creative process similar to creating a new art work. You CANNOT teach someone how to program. You can teach him the syntax and semantics within a program, but you cannot teach him how to program. He must learn how to use these tools creatively and create something useful. By giving him code that already does his programming assignment, you are starving him from knowing the whole process of how he should use those tools to create that program. Show nested quote +On October 08 2010 09:44 KiLL_ORdeR wrote:I just did the oceans level one, but I fucked up somewhere. + Show Spoiler [Code so Far] + #include <iostream> using namespace std;
int main()
{ int years = 0; cout << "Years Growth\n"; cout << "_____________________\n"; while (years <= 25) { cout << years << "\t\t" << (years + 1.5) <<endl; years++; } return 0; }
This one isn't doing what it's supposed to do. Instead of adding 1.5 each loop, it's instead starting at 1.5 and adding one. Gonna look back I think I just need to change years++ years is a variable of type int so it can only hold whole number. + Show Spoiler + int years = 0; years = years + 1.5; //years will become 1 because it truncates the decimal part away
years++ post increment the variable by 1 every time which is not what you want.
I agree with you about programming being similar to an art. I do understand how to develop a program in my head and I can look at a problem and understand the steps I need to take to solve it, but it's the syntax that is giving me trouble, since I've only written maybe 10 programs, focusing on a wide variety of things, and we're going at a pretty fast pace imo. It was weird, one day I started doing homework, and realized that I had no idea what was going on. when people post code, that's very helpful to me because I can look at how they write it, then go to Wascana and copy it in, and get a good grasp of all of the pieces.
for the second part, the growth needs to start at 0 at year 0, and increase by 1.5 each time the year increases by 1, which I think seems to be the opposite of what people are telling me to do.
|
My advice is to read your book and every time you see something new, create a test program to use the new code. Check to see what it does and can't do.
|
for the second part, the growth needs to start at 0 at year 0, and increase by 1.5 each time the year increases by 1, which I think seems to be the opposite of what people are telling me to do.
Use the code I posted above but initialize the variables to 0 instead.
|
been doing some crazy serious work. I decided to go back to the basics and do all of the book problems, by chapter, until I have a good feel of all of the concepts, syntax, and types of problems. It's working really well and I wish I did this from the start, since now I pretty much have to stay up until I finish this, and it's currently 3:15 here
Here's the new code I wrote for the Math tutor program. I forgor to mention that we were supposed to includ a cin.getline code so that the "student" could press enter to view the answer.
+ Show Spoiler + #include <iostream> #include <cstdlib> #include <ctime> using namespace std;
int main ()
{ unsigned seed = time (0); srand(seed); char ch; int randa, randb, total; randa = rand ()%50; randb = rand ()%50; total = randa+randb;
cout << " " << randa << endl; cout << "+ " << randb << endl; cout << "_____\n"; cout << "Press the [ENTER] key to see the right answer\n"; cin.get(ch); cout << total << endl;
return 0; }
So this will take two random numbers between 0 and 50, and then add them in the right format.
I'll update op with the others as I hit them. Thanks for all the help guys!
|
Finally got the area of rectangles one to work. Didn't stay up working all night, although I only got like 4 hours of sleep, and now I'm really tired.
+ Show Spoiler [Final Code] + #include <iostream> using namespace std;
int main()
{ int length, width, area1, area2;
// First Rectangle cout << "What is the length of the first rectangle? "; cin >> length; cout << "What is the width of the first rectangle? "; cin >> width; area1 = length * width; cout << "The area of the first rectangle is " << area1 << ".\n";
// Second Rectangle cout << "What is the length of the second rectangle? "; cin >> length; cout << "What is the width of the second rectangle? "; cin >> width; area2 = length * width; cout << "The area of the second rectangle is " << area2 << ".\n";
//Comparison if (area1>area2){ cout << "The first triangle is larger\n"; } else if (area1<area2){ cout << "The second rectangle is larger\n"; } else if (area1==area2){ cout << "They are equal\n"; }
return 0; }
|
Another helpful site to keep in mind is WolframAlpha, especially for any math problems you may have to do. For example you don't need to use a loop to add the numbers 1 to n.
Also, for the min and max numbers of a given series you can just compare the entered number to the current max / min number, there's no need to store all numbers.
|
For the ocean level rising example, you want to use two variables: one for the level, and a second one for the loop. The loop variable describes the years and is incremented by 1 in each loop, until 25. The level increases by 1.5 in each loop. You need a very exact approach to thinking for programming which feels unnatural and difficult at first. You need to figure out exactly what you want the program to do, step by step, and translate that into code.
For the last example, you may need a while(true) {...} loop, and when the input equals -99, you use the break; command to exit the loop. Just in case you didn't know about break. Or you use something like while(input != -99), which is the more elegant solution. You see, there are often multiple ways to achieve the same goal. This is why some people call programming a creative process.
|
Typing on my phone right now, but here's the general idea of how you do it
Int years = 0 Double waterLevel = 0
While years < 25 years++ waterLevel = waterLevel + 1.5 Cout << years << " " << waterLevel << endl
|
I agree with some of the previous posters that we are giving away these answers too freely. We should let the student try and work through the problem on their own.
|
he's shown the initiative and i'd rather have someone show me exactly how to do it rather than lead me to it...idk probably just different methods of learning for some people
|
Canada8028 Posts
My god Wascana Eclipse is bloated. Dev C++ is 9MB compared to this 150MB monster.
Anyway, I thought I'd show you how to debug simple programs. I'm gonna use your ocean levels program as an example and go over your mistake in there at the same time.
+ Show Spoiler [Warning: really long] +To begin with, your screen should look something like this: Click on the text editor window (the window with all your code in it). Now go to Run > Debug As > Local C/C++ Application This message box might come up. Choose yes to change perspectives. Your screen should now look like this: Here's an overview of the various important screens: - This kinda tells you what the system's doing. Right now it's saying that the program is suspended at a breakpoint. I'll explain this in a minute. You can probably ignore this for the most part.
- This is the variables screen. It tells you about variables are used in the program as they are declared and changed.
- This is your program. Note the line I've circled, and how there's a tiny blue arrow on the left hand side. This means the program is currently stopped at that line.
- This is the console. It will show you the output of your program as it would normally appear in the command prompt. You've probably used this section before.
So here's what debug mode does: it stops your program at certain locations called breakpoints, and allows you to step through your program one line at a time. This way, you can observe the changes in your variables and locate any problems easier. Normally, you'd be able to set breakpoints wherever you'd like in your source file, but I can't figure out how to properly use this stupid program. Even the online help wasn't any use. -_- When you enter debug mode using the steps I've described above, the breakpoint is automatically set at the first line of the program. Thus, your program is currently stopped at the line int years = 0; Look at the variables window. Notice how years has shown up because it has been declared, but that the value is all screwed up. This is because the value has not been assigned to the variable. Let's hit step into to advance the code one line: Notice that the little blue arrow has gone down one line. Also notice that the variable years now has a value of 0. I'm gonna keep going. Now we're at the loop. Take a look at the variable screen. years is 0, which is less than or equal to 25, so we enter the loop. Stepping further... Look at the console and notice that we now have output. The reason why the first two cout statements didn't show up before this is that text is sometimes stored in the buffer instead of immediately being displayed. Suffice it to say that this is nothing to worry about. Anyway, this is the part where your program screws up. This line: cout << years << "\t\t" << (years + 1.5) <<endl; Since years is 0, (years +1.5) gives you an output of 1.5 instead of 0. As others have mentioned in this thread, having (years * 1.5) would fix this. Strictly speaking, the expression (years +1.5) is bad since type int isn't meant to contain decimal values. However, the program is smart enough to typecast the integer as a float or double to compensate for this. Stepping some more... years has now incremented, as you can see. The while loop has also ended, so we're now reevaluating the condition right now. years is still under 25, so we'll end up entering the loop again. Stepping more... Here we see that the output for 1 year is 2.5 instead of 1.5 Again, using the expression (years * 1.5) would solve this problem, giving an output of 1.5 Hit resume (outlined in green) to make the program run to the end without further stoppages. Hit terminate (outlined in red) to make the program stop running completely (means no more stepping) at its current location.
So that's a basic overview of how to debug your programs. Using debug mode will give you a better understanding of what your program is doing at every step, and help you locate those nasty bugs. It's a really nice feature you should learn to use.
Edit: By the way, I really hate Wascana Eclipse. It's not very intuitive at all. Given a choice, I'd use another development environment, but you guys might not get a choice in that. =/
|
Amazing guide, Eclipse looks about the same as MSVC as far as the debug features go, so this guide should translate well.
|
|
|
|