|
Hi all,
I've noticed over the year or so that i've been a member of Team Liquid that there are quite a few Computer Science people in the community. I'm a digital art major in University so I have to take Computer Science classes to supplement the Digital part.
Right now I'm taking an intro to C++ class and I'm trying to prepare for the midterm, which involves going into class and writing programs. I'm a little over my head I think. I understand the real basic stuff, like compiling, a few basic programs, and conceptually how the software works, but I'm having trouble making programs do what I want them to do. It would be really awesome if people here who know more than I do could help me out a bit, since I'm running out of time.
We use Wascana Eclipse, and out textbook is Starting Out with C++: From Control Structures through Objects by Tony Gaddis, if that matters to anyone. What I'm going to do is write down each separate problem, and then include code i've written so far, and what I specifically need help with. Here are the problems I am having trouble with:
Area of Rectangles *Write a Program that asks the user for the length and width of two rectangles. The program should tell the user which rectangle has a larger area, or if they are the same.
+ Show Spoiler [Code so far] + #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; }
Finally got it to work
Math Tutor *Write a program that displays two random numbers in this format:
74 + 12
The program should wait for the student to enter the correct answer. If the answer is correct, a message of congratulations should be printed. If incorrect, the message should show the correct answer.
+ Show Spoiler [Code so Far] + #include <iostream> #include <cstdlib> #include <ctime> using namespace std;
int main()
{ unsigned seed = time(0); srand (seed);
int randa, randb, total, answer; randa = rand ()%50; randb = rand ()%50; total = randa+randb;
cout << " " << randa << endl; cout << "+ " << randb << endl; cout << "______\n"; cout << "please enter what you think is the correct answer:\n"; cin >> answer;
if (answer==total){ cout << "you are correct :)"; } else{ cout << "Sorry, you're wrong this time :(\n"; cout << "The correct answer is " << total << endl; cout << "Double check your answer, then try again with new numbers"; }
return 0; }
Finally got this one right. I also think I'm finally getting the hang of an if else statement.
EDIT: These are the last problems
Ocean Levels *Assuming that the Oceans level is rising 1.5 millimeters per year, write a program that displays a table showing the number of millimeters that the Ocean will have risen each year for the next 25 years.
+ 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++
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.
+ Show Spoiler [Code so Far] +
I'm obviously like an E- computer noob at programming, but I actually kinda like it, and I really like the feeling of making a program actually work. If anyone is able to help me either with advice, useable code, etc. it would be very much appreciated.
|
Hyrule18918 Posts
On October 08 2010 04:10 KiLL_ORdeR wrote:Hi all, I've noticed over the year or so that i've been a member of Team Liquid that there are quite a few Computer Science people in the community. I'm a digital art major in University so I have to take Computer Science classes to supplement the Digital part. Right now I'm taking an intro to C++ class and I'm trying to prepare for the midterm, which involves going into class and writing programs. I'm a little over my head I think. I understand the real basic stuff, like compiling, a few basic programs, and conceptually how the software works, but I'm having trouble making programs do what I want them to do. It would be really awesome if people here who know more than I do could help me out a bit, since I'm running out of time. We use Wascana Eclipse, and out textbook is Starting Out with C++: From Control Structures through Objects by Tony Gaddis, if that matters to anyone. What I'm going to do is write down each separate problem, and then include code i've written so far, and what I specifically need help with. Here are the problems I am having trouble with: Area of Rectangles*Write a Program that asks the user for the length and width of two rectangles. The program should tell the user which rectangle has a larger area, or if they are the same. + Show Spoiler [Code so far] + #include <iostream> using namespace std;
int main()
{ int area1, area2;
{ int length, width, area1, area2;
// First Rectangle cout << "This program calculates the areas of two"; cout << "rectanlges, and then compares the two and"; cout << "displays which one is larger.\n"; 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";
} if (area1 > area2); { cout << "The first rectangle is larger than the second one.\n"; else (area1 < area2); cout << "The second rectangle is larger than the first one.\n"; return 0; }
I really thought that this one would work, I have no idea why it won't compile I get an error message that says "else without previous if" and "expected '}' at end of input" both of which are things I have. Math Tutor*Write a program that displays two random numbers in this format: 74 + 12The program should wait for the student to enter the correct answer. If the answer is correct, a message of congratulations should be printed. If incorrect, the message should show the correct answer. + Show Spoiler [Code so Far] +I don't have anything written for this one yet, but I do know how to make two random number, within any range I want. I also understand that I need to use an if-else statement to complete the program. What I can't figure out how to do is to get the number to print in that format, and how to add the two random numbers. Running the Race*Write a prgoram that asks for the names of 3 runners, and the time it took for them to finish a race. The program should display who cam in 1st, 2nd, and 3rd, and times should not have negative values. + Show Spoiler [Code so Far] +We did something similar to this in class, but it was only using two names. When I was trying to do this with three names, it seems that the logic is much different. Additionally, I was unsure how to get the names to bind with the times. Sum of Numbers*Write a program that asks the user for a positive integer. The program should contain a loop to get the sum of all of the numbers from 1 to the number the user entered. For example, if the user enters fifty, the program should get the sum of all numbers from 1 through 50. Do not accept negative numbers. + Show Spoiler [Code so Far] +#include <iostream> using namespace std;
int main () { { int number;
cout << "Please enter a number." << endl; cout << "Make sure that your number is a positive integer."; cin >> number; {
int x = number; while (x > 2) x = x - 1; } }
return 0; } So ya. I'm really stumped on this one. I know how to retrieve a number, and I think I understand the mechanics of a while loop, but I have know idea how to get the sum, or prevent the use of negative numbers. I addition to these, there are two more problems I'm trying to work through, I'll update with questions as they arise. I'm obviously like an E- computer noob and programming, but I actually kinda like, and I really like the feeling of making a program actually work. If anyone is able to help me either with advice, useable code, etc. it would be very much appreciated.
Okay, for area of rectangles: braces should match. You randomly tossed in an opening brace between some declarations. Also, if you do if(blah) { you need to use } else. There are two ways to do if-else blocks. 1: No braces, 2: Braces. When not using braces, you can only have 1 statement between the condition and the else. When using braces, you can do as much as you want. You can mix and match as well, to do something like
if(a == 1) cout << "stuff"; else { cout << "OMG"; cout << "WTF"; cout << "BBQ"; }
Math Tutor
int a = rand() int b = rand() int sum = a+b
cout << " " << a << endl; cout << "+ " << b << endl; cout << "------" << endl;
It's only pseudocode, so replace rand() with your real number generator.
Running the Race Use 3 variables for times and 3 variables for runner names (strings or char arrays, however you've been taught). runnera/timea, runnerb/timeb, etc... The logic for comparing 3 variables is exactly the same as for comparing 2, it just looks harder.
Summing Numbers
while(counter < number) sum += counter++; Also, you REALLY need to stop throwing braces around all willy-nilly.
|
ah thanks, that does actually make it a bit easier.
|
The else statement is contained within the the if block. The syntax is supposed to be like this:
if (statement) { (block of code) } else { (block of code) }
edit: Also, you don't want the semicolons at the end of the line for your if and else statements.
I would also expect an error because you're re-declaring area1 and area2.
|
int x, i, sum; cin >> x while (x < 0 ) { cout << " please enter pos number" << endl; cin >> x; }
for (i = 0; i < x+1; i++) { sum = sum + i; )
cout << sum << endl;
number 4, if i didnt make any dumb mistakes
|
Hyrule18918 Posts
On October 08 2010 04:25 Shivaz wrote: int x, i, sum; cin >> x while (x < 0 ) { cout << " please enter pos number" << endl; cin >> x; }
for (i = 0; i < x+1; i++) { sum = sum + i; )
cout << sum << endl;
number 4, if i didnt make any dumb mistakes Same with OP (probably should mention this in my reply...), but if they enter a negative number your program will loop forever. Also, instead of using i < x + 1, you should use i <= x. You can also do sum++ instead of sum = sum + 1
|
I don't see the point of telling you how to do these simple programs. You seem to make basic syntacs error in the most basic if and if else loops. Have you actually tried debugging and reading error messages ? Or tried running in debug mode.
The point I'm making you either didn't study the basic syntax well or just didn't study or practice enough. I'm not just talking about the syntacs but also proper programming You have a spamline of 10 couts while you can just write your message in one line instead of splitting it up in 10. You can also request the input of both rectanguals in either one message or 2 if you really want to split both up.
A few small notes. Why restric the size of those rectangulars to ints they could have any size.
Your else loop is wrong. You enter another statement namely area2 > area1. Else loops are run without restraints if the if fails. What you were like for is a else if loop. Even in that case your program could be wrong. What happens if both areas are the same.
|
Hyrule18918 Posts
What are you smoking? "Else loop"?
|
Disclaimer: Haven't done C++ in a while so you'll have to double check for errors etc, code meant to be used as examples not directly implemented.
// rectangle one: if (area1 > area2); { cout << "The first rectangle is larger than the second one.\n"; } else (area1 < area2); { cout << "The second rectangle is larger than the first one.\n"; } return 0;
For the random numbers one, something like this
int num1 = rand(); int num2 = rand(); cout << num1 << "\n+" << num2 << endl; cin >> answer;
if ( answer == (num1+num2) ) { cout << "gz"; } else { cout << "Sorry the wrong answer is: " << num1+num2 << endl; }
For the runners there are several methods, but I think your prof wants you to take a look at sorting algorithms, maybe? As an itroduction... There are very very many different ones. Maybe he just straight wants you to compare them, like so:
if (runner1 > runner 2 && runner1 > runner3) { runner 1 wins! }
Or hell maybe he wants you to make a runner class... I doubt it though.
class Runner { int time; }
Runner jack; jack.time = 10;
As for storing the names + times, you could use a 2d array.
int runners[name][time] = { A, 1, B, 2, C, 3 };
You'll have a matrix looking like this
0,0 | 0,1 ----------- 1,0 | 1,1 ----------- 2,0 | 2,1
or
A | 1 ------- B | 2 ------- C | 3
The thing about "not negative values" either means you have to validate the input (i.e. check if it's a positive integer or not, for this you could use an ascii chart and check if each character in the input string is in the area you want, if that makes sense...) OR it could mean that the user will only input positive integers, in which case don't worry about it, and I assume that's what he means as these are beginner tasks.
Now, let's have a look at the loop one. First off cin >> int is dangerous, but I think you shouln't worry about that now as in the above question the user will probably only input positive integers so let's just assume that (not good practice but w/e).
your loop should look something like this:
cin >> number; int x = 0; while (x < number) { x++; } // this will loop until X = number
alternatively a more pretty loop would be
for (int x = 0; x < number; x++) { // do whatever here. }
for loops are cool -
for ( variables; condition; increment/decrement )
these can be mixed and matched however you want, and you can leave some of them blank if you want, here's an infinite loop:
for (;;).
|
On October 08 2010 04:27 tofucake wrote:Show nested quote +On October 08 2010 04:25 Shivaz wrote: int x, i, sum; cin >> x while (x < 0 ) { cout << " please enter pos number" << endl; cin >> x; }
for (i = 0; i < x+1; i++) { sum = sum + i; )
cout << sum << endl;
number 4, if i didnt make any dumb mistakes Same with OP (probably should mention this in my reply...), but if they enter a negative number your program will loop forever.
yeah exactly keep asking until they enter a positive number. isn't that what the program wants
|
On October 08 2010 04:34 Marradron wrote: I don't see the point of telling you how to do these simple programs. You seem to make basic syntacs error in the most basic if and if else loops. Have you actually tried debugging and reading error messages ? Or tried running in debug mode.
The point I'm making you either didn't study the basic syntax well or just didn't study or practice enough. I'm not just talking about the syntacs but also proper programming You have a spamline of 10 couts while you can just write your message in one line instead of splitting it up in 10. You can also request the input of both rectanguals in either one message or 2 if you really want to split both up.
A few small notes. Why restric the size of those rectangulars to ints they could have any size.
Your else loop is wrong. You enter another statement namely area2 > area1. Else loops are run without restraints if the if fails. What you were like for is a else if loop. Even in that case your program could be wrong. What happens if both areas are the same. I seriously doubt he has been shown how to debug. Reading error statements or even knowing where you can read them isn't second nature to people just starting out. His code wasn't that bad and it could be just how his professor wanted him to do it.
On October 08 2010 04:42 Shivaz wrote:Show nested quote +On October 08 2010 04:27 tofucake wrote:On October 08 2010 04:25 Shivaz wrote: int x, i, sum; cin >> x while (x < 0 ) { cout << " please enter pos number" << endl; cin >> x; }
for (i = 0; i < x+1; i++) { sum = sum + i; )
cout << sum << endl;
number 4, if i didnt make any dumb mistakes Same with OP (probably should mention this in my reply...), but if they enter a negative number your program will loop forever. yeah exactly keep asking until they enter a positive number. isn't that what the program wants It doesn't matter if they user intentionally tries to muck the program. The instruction is to only account for users who follow instructions so there is no need to program it for the 'what if'. This is really just a very easy intro class level program.
|
TossFloss
Canada606 Posts
>_< C++ is such a terrible language to learn programming!
If you have time, pick up The C Programming Language. It's only 274 pages and you can read it in a week.
The above posters have already given good suggestions. For the math tutor question you want to use a for loop.
|
Thanks a lot, these are all very helpful, I knew TL would pull through. I know I'm not very practiced at this, and that's the exact problem (I believe I also said that in OP, but i digress).
Anyway, the syntax seems to be what's messing me up the most, so examples of the correct syntax are actually a lot more helpful than the literal code, since i'm positive that we won't be getting the same problems on the exam.
On October 08 2010 04:40 Adeny wrote: // rectangle one: if (area1 > area2); { cout << "The first rectangle is larger than the second one.\n"; } else (area1 < area2); { cout << "The second rectangle is larger than the first one.\n"; } return 0;
I tried fixing the syntax for this part, and I'm still getting a error message of 'else' without previous 'if.' Why is this?
On October 08 2010 04:40 Adeny wrote:For the runners there are several methods, but I think your prof wants you to take a look at sorting algorithms, maybe? As an itroduction... There are very very many different ones. Maybe he just straight wants you to compare them, like so: if (runner1 > runner 2 && runner1 > runner3) { runner 1 wins! }
When we did a problem similar to this previously, we used stream compare, so I think this is just supposed to be building off of that.
On October 08 2010 04:40 Adeny wrote:Or hell maybe he wants you to make a runner class... I doubt it though. class Runner { int time; }
Runner jack; jack.time = 10;
As for storing the names + times, you could use a 2d array. int runners[name][time] = { A, 1, B, 2, C, 3 };
You'll have a matrix looking like this 0,0 | 0,1 ----------- 1,0 | 1,1 ----------- 2,0 | 2,1 or A | 1 ------- B | 2 ------- C | 3
I sure as shit hope this isn't what I'm supposed to do, because I've never seen anything like this before...
On October 08 2010 04:40 Adeny wrote:The thing about "not negative values" either means you have to validate the input (i.e. check if it's a positive integer or not, for this you could use an ascii chart and check if each character in the input string is in the area you want, if that makes sense...) OR it could mean that the user will only input positive integers, in which case don't worry about it, and I assume that's what he means as these are beginner tasks. Now, let's have a look at the loop one. First off cin >> int is dangerous, but I think you shouln't worry about that now as in the above question the user will probably only input positive integers so let's just assume that (not good practice but w/e). your loop should look something like this: cin >> number; int x = 0; while (x < number) { x++; } // this will loop until X = number
alternatively a more pretty loop would be
for (int x = 0; x < number; x++) { // do whatever here. }
for loops are cool - for ( variables; condition; increment/decrement )
these can be mixed and matched however you want, and you can leave some of them blank if you want, here's an infinite loop: for (;;).
I'm 99% sure that he wants us to write the program so that if the user tries to enter a negative number, it will be invalid. The only way I can think of to do this that we have learned is to set up an if else statement that says if number is greater than zero, it will do the subsequent code, but if it's less than zero, it will display an error message.
|
These are the last problems. Updating OP.
Ocean Levels *Assuming that the Oceans level is rising 1.5 millimeters per year, write a program that displays a table showing the number of millimeters that the Ocean will have risen each year for the next 25 years.
+ Show Spoiler [Code so Far] +This looked really easy at first and I'm going through it right now, I'll post some code in a minute
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.
|
// rectangle one: if (area1 > area2); { cout << "The first rectangle is larger than the second one.\n"; } else (area1 < area2); { cout << "The second rectangle is larger than the first one.\n"; } return 0;
You shouldn't have semi colon after your if(area1>area2) or else(area1 < area2).
On October 08 2010 05:05 KiLL_ORdeR wrote:
I'm 99% sure that he wants us to write the program so that if the user tries to enter a negative number, it will be invalid. The only way I can think of to do this that we have learned is to set up an if else statement that says if number is greater than zero, it will do the subsequent code, but if it's less than zero, it will display an error message. x=-1; while (x<0){ cout<<"Please enter a positive number"; cin>>x; }
Something like that should work.
|
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.
Show nested quote +On October 08 2010 04:40 Adeny wrote: // rectangle one: if (area1 > area2); { cout << "The first rectangle is larger than the second one.\n"; } else (area1 < area2); { cout << "The second rectangle is larger than the first one.\n"; } return 0;
I tried fixing the syntax for this part, and I'm still getting a error message of 'else' without previous 'if.' Why is this?
The compiler is expecting an "if" after that else because of the extra condition you addressed (area1 < area2).
You should also want to have a condition where area1 == area2.
If there are things that you have never seen (class, arrays, etc.), then do not worry about them. Your professor want you to do his assignment with the things you already know in class and through the assigned readings.
|
On October 08 2010 04:56 TossFloss wrote:>_< C++ is such a terrible language to learn programming! If you have time, pick up The C Programming Language. It's only 274 pages and you can read it in a week. The above posters have already given good suggestions. For the math tutor question you want to use a for loop.
He's actually just learning C. Hardly anything he does is C++ related. Though I would recomend reading that C tutorial
|
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.
|
On October 08 2010 05:05 KiLL_ORdeR wrote:Thanks a lot, these are all very helpful, I knew TL would pull through. I know I'm not very practiced at this, and that's the exact problem (I believe I also said that in OP, but i digress). Anyway, the syntax seems to be what's messing me up the most, so examples of the correct syntax are actually a lot more helpful than the literal code, since i'm positive that we won't be getting the same problems on the exam. Show nested quote +On October 08 2010 04:40 Adeny wrote: // rectangle one: if (area1 > area2); { cout << "The first rectangle is larger than the second one.\n"; } else (area1 < area2); { cout << "The second rectangle is larger than the first one.\n"; } return 0;
I tried fixing the syntax for this part, and I'm still getting a error message of 'else' without previous 'if.' Why is this?
I'm sorry, let me try that again:
if (area1 > area2) // area 1 greater than area 2 { cout << "blablah"; } else if (area2 > area 1) // the else here is so that if the first if checks through as true, the elses will be ignored. { cout << "blahblah" } else // both areas are equal { cout << "blahblah" }
Note that the if's shouldn't have semi colons.
As for the other ones this is becoming quite the clusterfuck, hang on...
|
This works fine...
int x = 0; cin >> x; if (cin.good() > 0) { cout << "user input is: " << x << endl; } else { cout << "user input is bad" << endl; }
LOLOL DISREGARD THIS ENTIRE MESSAGE
+ Show Spoiler +Ok, let's validate the input so that only digits between 0 and 9 will be accepted. If you use cin >> on an integer, your program will blow up if someone starts typing anything but strictly numbers. Because of that we have to jump hoops... HUH, DISREGARD THIS LOL. It seems if you cin >> to an int, with characters and nonsense, the int stays 0... God damnit it.+ Show Spoiler + #include <string> #include <iostream> #include <conio.h>
using namespace std;
int main() { cout << "Input number or w/e\n"; string input; cin >> input; for (int x = 0; x < input.size(); x++) { if ( input[x] < 48 && input[x] > 57 ) { cout << "Only 0-9"; break; } }
int validated = atoi(input.c_str()); if (validated > 0) { cout << validated; } else { cout << "Number must be over 0\n"; }
getch(); return 0; }
What it does is take the users input, places it in to a string. Now it dissects the string, and checks each character seperately if its ASCII value is above or below 48-57, which is 0-9 on an ASCII table ( ASCII Chart). Then convers the string to an integer using atoi(), and finally checks if the ints value is above 0. Chances are though, your professor doesn't expect that of you and just poorly worded the assignment, maybe he wants you to assume that a user will always input numbers, in which case simply doing the following should work: + Show Spoiler + int input; cin >> input; if (input > 0) { // do whatever here } else { // tell the user his input was bad. }
Also god damn conversions in C++, jeez.
|
|
|
|