Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
wow you guys are so nice :cool:thanks a lot guys, haha im trying so hard, but my professor doesnt speak a lick of english so all he does is write programs on the board and cant really explain them and if you have specific questions its impossible to get an answer. i find that i learn a lot more in lab for two reasons, my lab instructor is a nice english speaking lady and shes very hands on (the whole point of the lab, i guess) i do enjoy it but it gets so frustrating!
 
wow you guys are so nice :cool:thanks a lot guys, haha im trying so hard, but my professor doesnt speak a lick of english so all he does is write programs on the board and cant really explain them and if you have specific questions its impossible to get an answer. i find that i learn a lot more in lab for two reasons, my lab instructor is a nice english speaking lady and shes very hands on (the whole point of the lab, i guess) i do enjoy it but it gets so frustrating!

One thing that may help is make sure when you look at his programs, that you can comment what every line of code is doing. If you can't, or are stuck on a line make sure you either come here or ask someone for help.

Its a pain but commenting each line of code when learning can really help solidify the ideas.
 
i more or less understand a code when it is given to me.. just like when im givin a problem and have to write a code, it takes me about 5 times to do it before i do it successfully (or i cant get it at all!) theres just so many options haha but i got a 19/20 on my first assignment so thats a good start
 
Think, practice, loop ...

Was yesterdays task the second assignment?

yup. well those two codes were 2/3 i had to do, then theres short answer types (determine the errors etc) and multiple choice type. i have another question where i have to add 4% a year on to 2500 for 6 years. i think i can manage that one myself.. its not due until feb 7th but im a keener haha
 
Well keep in mind school is about learning to think and learning how to learn.

Just keep at it and you'll come to enjoy your new found abilities to solve problems instead of panicking when confronted by them.

Oh, and congratulations!
 
Last edited:
Well keep in mind school is about learning to think and learning how to learn.

Just keep at and you'll come to enjoy your new found abilities to solve problems instead of panicking when confronted by them.

Oh, and congratulations!

hahahaha i know, this stuff is just so different than anything else ive done in my university career to date yano? and thank you! my midterm is 2 weeks from today so im going to buckle down and try to learn what we were taught so far really well :eek::eek:
 
hahahaha i know, this stuff is just so different than anything else ive done in my university career to date yano? and thank you! my midterm is 2 weeks from today so im going to buckle down and try to learn what we were taught so far really well :eek::eek:

and remember you can always come back for questions.....:)
 
lol you guys dont have to tell me twice.. ill be here. haha im going to attempt that last program now.. any suggestions on which loop to use?


14 A country club, which currently charges $2,500 per year for membership, has announced it will increase its membership fee by 4% each year for the next six years. Write a program that uses a loop to display the projected rates for the next six years. Sample output:
Membership for year 1: $aaaa.aa
….
Membership for year 6: $ffff.ff
 
'for(;;) {}' and 'while() {}' are very similar but only happen if the initial condition is non false. While 'do {} while()' happens at least once and will repeat only if the condition is non false.

Personal preferences really ...
 
okay ummm i think i got it, and it outputs fine, except that for the last two outputs, my calculations had me rounding up and the outputs rounded down, or vice versa i dont remember exactly. is there anyway to fix this or what? im going to post the code here, and if theres anything unnecessary that i dont need, could someone let me know :)?

Code:
#include <iostream>
using namespace std;

#include <iomanip>
using std::setprecision;

int main ()
{
	double sum = 0, membership = 2500;

	cout << setprecision (2) << fixed;
	
	for (int year = 1; year <=6; year++)
		{
		membership = membership * 1.04;
		sum = membership;
		cout << "Membership for year " << year << " is $" << sum << endl;
		}
		
	return 0;
}
 
Code:
#include <iostream>
using namespace std;

#include <iomanip>
using std::setprecision;

int main ()
{
	double sum = 0, membership = 2500;

	cout << setprecision (2) << fixed;
	
	for (int year = 1; year <=6; year++)
		{
		membership = membership * 1.04;
		sum = membership;
		cout << "Membership for year " << year << " is $" << sum << endl;
		}
		
	return 0;
}
[/QUOTE]

Two things:

1. Do you want to add the 6% on for the first year? If not then you will need to move the
Code:
membership = membership * 1.04
to after your
Code:
cout
statement.
2. It looks like you copied your previous program and retained some of it's logic. Think about what "sum" did in your previous program and check if it's still needed in this one.
 
That's what I wasn't really sure about.. like its initially 2500, so 4% added on would be 2600 dollars for year one, right?

"A country club, which currently charges $2,500 per year for membership, has announced it will increase its membership fee by 4% each year for the next six years. Write a program that uses a loop to display the projected rates for the next six years. Sample output:
Membership for year 1: $aaaa.aa
….
Membership for year 6: $ffff.ff"

is the question and i find it's kind of vague..

Code:
	double membership = 2500;

	cout << setprecision (2) << fixed;
	
	for (int year = 1; year <=6; year++)
		{
		membership = membership * 1.04;
		cout << "Membership for year " << year << " is $" << membership<< endl;
		}
		
	return 0;

would work right? realzing i dont need sum..im silly sometimes
 
That's what I wasn't really sure about.. like its initially 2500, so 4% added on would be 2600 dollars for year one, right?

"A country club, which currently charges $2,500 per year for membership, has announced it will increase its membership fee by 4% each year for the next six years. Write a program that uses a loop to display the projected rates for the next six years. Sample output:
Membership for year 1: $aaaa.aa
….
Membership for year 6: $ffff.ff"

is the question and i find it's kind of vague..

...

would work right? realzing i dont need sum..im silly sometimes

Exactly right! sum isn't needed.

For the question itself I would interpret that to mean that you need to have a year "0" which is now and then there will be six years following that where the cost will increase. It is rather vague however, so I could be wrong.
 
Exactly right! sum isn't needed.

For the question itself I would interpret that to mean that you need to have a year "0" which is now and then there will be six years following that where the cost will increase. It is rather vague however, so I could be wrong.

yay me! hmm yeah i know what you mean, maybe ill ask around in class and see what other people interpreted it as, im sure its not that big of a deal either way since the point of the question is to complete the code for it, which i did so. thank you :eek::eek:
 
guys im kind of confused on when i need to initialize a variable to 0... and what the difference is between = and ==? any tips?
 
guys im kind of confused on when i need to initialize a variable to 0... and what the difference is between = and ==? any tips?

Just my $0.02.

It's generally a good idea to initialize variables, whether you do it to zero or some other initial value depends on the variable.

In C related languages:

= is assignment, put the value on the right into the variable on the left

== is comparison, compare the two sides and let me know if they are equal.

This is why you will see a lot of experienced coders doing comparisons like:

Code:
 if (0 == myNumber) ...
as putting the constant on the left will have the compiler help you with an error if you inadvertently left out a "=".

B
 
guys im kind of confused on when i need to initialize a variable to 0... and what the difference is between = and ==? any tips?

If you don't initialize a variable, it will have some random value that was in that segment of memory before.

= is assignment. You use this to assign values to variables. == is a Boolean operation which evaluates equivalency. So, x = y, assigns the value of y to x, whereas x == y asks if x and y are equal and will evaluate to either 1 or 0 (true or false).
 
im fuzzy on the difference between post incrememnt and pre increment... the examples he used to demonstrate the differencedont make much sense! any help pals?
 
im fuzzy on the difference between post incrememnt and pre increment... the examples he used to demonstrate the differencedont make much sense! any help pals?

Try running this code and looking at the output. Can you explain what seems to be happening?:)

Code:
#include <iostream>

using namespace std;

int main (int argc, char* argv[])
{
  int a = 1, b = 1, x = 0, y = 0;

  cout << "a: " << a << "\nx: " << x << endl;
  x = ++a;
  cout << "a: " << a << "\nx: " << x << endl;

  cout << endl;

  cout << "b: " << b << "\ny: " << y << endl;
  y = b++;
  cout << "b: " << b << "\ny: " << y << endl;

  return 0;
}
 
With post decrement/increment the value is evalutated then modified.

With pre decrement/increment the value is modified then evaluated.

Demonstration:

Code:
int main()
{
    int loop_count;
    
    loop_count = 1;
    while ( counter-- )
    {
        std::cout << "In \'loop1\'!" << std::endl;
    }
    
    loop_count = 1;
    while ( --counter )
    {
        std::cout << "In \'loop2\'!" << std::endl;
    }

    return 0;
}
 
Last edited:
i understand it more so now i think.. its hard to wrap my head around though haha so like when it is evaluated, then modified, if x = 0 and then it is evaluated THENmodified, would it output say x = 1 the next time? CONFUSE
 
i understand it more so now i think.. its hard to wrap my head around though haha so like when it is evaluated, then modified, if x = 0 and then it is evaluated THENmodified, would it output say x = 1 the next time? CONFUSE

Let's say a = 1, and x = 0.

If you wrote x = ++a, a would be incremented THEN assigned to x. So after executing x = ++a, x would equal 2 and a would equal 2.

If you wrote x = a++, a would be assigned to x THEN incremented. So after executing x = a++, x would equal 1 and a would equal 2.

If you take the example of the while loop above (and fix the bugs):

Code:
int counter;
    
    counter = 1;
    while ( counter-- )
    {
        std::cout << "In \'loop1\'!" << std::endl;
    }
    
    counter = 1;
    while ( --counter )
    {
        std::cout << "In \'loop2\'!" << std::endl;
    }

In 'loop2'! will not get output, can you figure out why?
 
i understand it more so now i think.. its hard to wrap my head around though haha so like when it is evaluated, then modified, if x = 0 and then it is evaluated THENmodified, would it output say x = 1 the next time? CONFUSE

Your professor should be the one teaching this, but:
Code:
int x = 5;
int a = 0;
int b = 0;
int c = 0;
a = --x;
//a now contains 4, x also contains 4
b = x--;
//b now contains 4, x contains 3
c = --b + a++;
// c contains 7, b contains 3, a contains 5
The pre and post increment/decrement will alter the value stored in the variable they operate on. The issue is when this change occurs relative to the rest of the containing expression. If it is a prefix operator, this happens before the rest of the expression. This means the value used in the containing expression will be the value after the change. With a postfix the expression is evaluated with the original value and the variable is modified afterwards. If the pre or postfix operator appears in expression by itself (on its own line, say) which you use is irrelevant. Try this yourself, a lot. Keep going, documenting what you think will happen then checking what does when you run it. Do this until you know what will happen when you run the code.

-Lee

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.