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
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 didnt include the stuff before that generally so not to bore you but i will now make it clear.

Code: Select all

#include"stdafx.h"
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
	vector<double>temps;
	double temp;
	while((cin>>temp) && (temps.size() <2))
		 temps.push_back(temp);
	double sum=0;
	for (unsigned 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;
	if(temps[0]<temps[1] && temps[2])
		cout<<temps[0]<<" is the least.\n";
	else if (temps[1]<temps[0] && temps[2])
		cout<<temps[1]<<" is the least.\n";
	else 
		cout<<temps[2]<<" is the least.\n";
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
it prints the medean line but the if statements must be interupted by the warning.
I'm not modest, I'm brutally honest.
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 »

Were you trying to do:

Code: Select all

if(temps[0]<temps[1] && temps[0]<temps[2])
which will compare temps[0] to temps[1] and temps[2]?

Either way, you should not do the if statements at all, as you already sorted the array, which means that temps[0] will always the lowest after the sort that you did.

Now, you really need to learn about naming variables and proper spacing:

Code: Select all

#include"stdafx.h"
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    vector<double> tempArray;
    double temp;
    double sum=0;

    while((cin>>temp) && (tempArray.size() <2))
    {
       tempArray.push_back(temp);
    }

    for (unsigned int i=0;i<tempArray.size();++i)
    {
        sum+= temps[i];
    }
    cout << "average temurature: " << sum/tempArray.size() << endl;

    sort(tempArray.begin(), tempArray.end());
    cout << "medean temurature: " << tempArray[tempArray.size()/2] << endl;

    cout << tempArray[0] << " is the least.\n";
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();

    return 0;
}
That makes your code just a bit more readable.
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: push_back, square( )ect.ect.

Post by Maevik »

How many data are you trying to take as input? Currently you prompt the user 3 times, but only store the first two values into your vector.

Your first while loop should look something more like

Code: Select all

    while( tempArray.size() < x )
    {
		cin >> temp;
        tempArray.push_back(temp);
    }
where x is the number of values you want to get from the user.
My love is like a Haddoken, it's downright fierce!
Post Reply