push_back, square( )ect.ect.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: push_back, square( )ect.ect.

Post by Ginto8 »

kostiak2 wrote:

Code: Select all

for (unsigned int i=0;i<temps.size();++i)sum+= temps[i];
for my style, I changed some things:

Code: Select all

for(unsigned i = 0; i < temps.size(); ++i)
    sum += temps[i];
just pointing out that you can shorten "unsigned int" to "unsigned". I changed the style so my head wouldn't explode. ;) :lol:
Last edited by Ginto8 on Wed Apr 01, 2009 9:27 pm, edited 1 time in total.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
kostiak2
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 74
Joined: Tue Mar 24, 2009 4:08 pm

Re: push_back, square( )ect.ect.

Post by kostiak2 »

I actualy prefer:

Code: Select all

for (unsigned int i=0;i<temps.size();++i)
{
    sum+= temps[i];
}
I just didn't want to bitch about it :lol:
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: push_back, square( )ect.ect.

Post by K-Bal »

Yeah and what happens if you type in

unsigned x = 0.5f; ? ;)

I guess, I'll try that out later.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: push_back, square( )ect.ect.

Post by Ginto8 »

K-Bal wrote:Yeah and what happens if you type in

unsigned x = 0.5f; ? ;)

I guess, I'll try that out later.
The program will have to do a runtime conversion, making x = 0. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: push_back, square( )ect.ect.

Post by MarauderIIC »

Actually, it doesn't return an unsigned int. It returns a size_type (vector::size_type, super-specifically). Which happens to be an unsigned int. But "it might not be later." The same way that NULL "might not be 0 later."

Also, I don't write size_type either, but I think your loop should (technically, again) look like this:

Code: Select all

for (vector::size_type i = 0;i < myVector.size();++i)
But... that's slow (you're recalculating the size every time!). So you shouldn't be doing that, anyway. You should at least be doing this:

Code: Select all

const unsigned int max = myVector.size();
for (unsigned int i = 0; i < max;++i)
But this is faster:

Code: Select all

for (vector<myVecType>::iterator i = myVector.begin(); i != myVector.end();++i)
Where myVector is defined as

Code: Select all

vector<myVecType> myVector;
And you can probably go even farther and (I don't go this far) :

Code: Select all

const vector<myVecType>::iterator max = myVector.end();
for (vector<myVecType>::iterator i = myVector.begin(); i != max; ++i)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

ok this does work and sorry again schools been loading on the homework and I have to make money now and then too so for the lateness my appologies.

Like I said earliar this does work but when all nubers are written in a box pops up with a big explination point and says debug assertion failed.

why?
ps.If it matters I am using microsft visual c++ express edition.
I'm not modest, I'm brutally honest.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: push_back, square( )ect.ect.

Post by MarauderIIC »

What line?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: push_back, square( )ect.ect.

Post by MadPumpkin »

you should change the post name if possible, because the abbreviation you're looking for it etc. NOT ect.
ect. makes it look like your trying to say eccetera, exetera, ecetera... but, the phrase Et Cetera, is what it should stand for, so etc. is correct. It is Latin it means "And So On(ward)"

:) thats all : D
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

first off i will enter the data and it will say the average and median tempurature and then the debug asertion warning thing.
second thank you for cerrecting me im all for that. :)
I'm not modest, I'm brutally honest.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: push_back, square( )ect.ect.

Post by MarauderIIC »

Okay, so what comes after the median temperature line?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

I think nothing but, then again their might be some spaces in there.Anyway pretty much nothing comes after the median tempurature line other than the warning that pops up.

and thank you :)
I'm not modest, I'm brutally honest.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: push_back, square( )ect.ect.

Post by MarauderIIC »

I meant code-wise.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
cplusplusnoob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sun Mar 08, 2009 2:40 pm
Location: Iowa city, Iowa

Re: push_back, square( )ect.ect.

Post by cplusplusnoob »

I am not sure if this is what you meant but it could be between line 19 and line 24. ;) :lol:
I'm not modest, I'm brutally honest.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: push_back, square( )ect.ect.

Post by dandymcgee »

cplusplusnoob wrote:I am not sure if this is what you meant but it could be between line 19 and line 24. ;) :lol:
Lmao.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: push_back, square( )ect.ect.

Post by MarauderIIC »

It's like trying to pull teeth from a chicken.

Look, this is all the source of yours I found in this thread:

Code: Select all

using namespace std;
int main()
{
   vector<double>temps;
   double temp;
   while(cin>>temp)
      temp.push_back(temp);
   double sum=0;
   for (int i=0; i<temps.size();++i)sum+= temps[i];
   cout<<"average temurature: "<<sum/temps.size()<<endl;
   sort(temps.begin(),temps.end());
   cout<<"medean temurature: "<<temps[temps.size()/2]<<endl;
What comes after the last line here? It seems like there wouldn't be an error in this code.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply