![]() |
| | #76 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
Code: #include <cstdarg>
#include <iostream>
using namespace std;
double average ( int num, ... )
{
va_list arguments;
double sum = 0;
va_start ( arguments, num );
for ( int x = 0; x < num; x++ )
sum += va_arg ( arguments, double );
va_end ( arguments );
return sum / num;
}
int main()
{
cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;
cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;
cin.get();
}
__________________ If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date. | |
| Ryan0773 is offline | |
| | #77 |
| Kiss the monkey. Join Date: Sep 2001
Posts: 810
| Got me thinking.... Code: #include <vector>
#include <iostream>
template<typename T>
class average
{
public:
average(T in, T null_in = 0) : null(null_in)
{ nums.push_back(in); }
const T null;
average & operator()(T n)
{ nums.push_back(n); return *this; }
long double value()
{
T sum = null;
for(std::size_t i = 0; i < nums.size(); ++i)
sum += nums[i];
return (long double)sum / (long double)nums.size();
}
private:
std::vector<T> nums;
};
template<typename T>
average<T> avg(T first)
{
return average<T>(first);
}
template<typename T>
std::ostream & operator << (std::ostream & s, average<T> & a)
{
s << a.value();
return s;
}
int main(int argc, char* argv[])
{
std::cout << avg(3)(22.3345)(76)(2) << std::endl; //bad: 22.3345 will be added as 22
std::cout << avg(22.3345)(3)(76)(2) << std::endl; //good: this is what you want.
//Incidentally, you could just implement it as a double, without the templates.
return 0;
}
__________________ "If you tell the truth, you don't have to remember anything" -Mark Twain |
| CodeMonkey is offline | |
| | #78 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Interesting solution, but are we going to confuse the newbie?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #79 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Frankly, it seems much easier to write: Code: #include <numeric>
#include <cstddef>
#include <iostream>
int main()
{
double numbers[] = {5, 3.3, 2.2, 1.1, 5.5, 3.3};
const std::size_t size = sizeof(numbers) / sizeof(numbers[0]);
double average = std::accumulate(numbers, numbers + size, 0.0) / size;
std::cout << average << std::endl;
}
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #80 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| ...Ya i have no idea what you guys are saying so that might meen that im a bit confused Quote:
----ed up so badly?
__________________ If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date. | |
| Ryan0773 is offline | |
| | #81 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #82 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| This is the code that i am having trouble with. it says the problem at the end of the quote. Quote:
__________________ If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date. | |
| Ryan0773 is offline | |
| | #83 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #84 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
Code: #include <iostream>
using namespace std;
int mult (int x, int y);
int main ()
{
int x;
int y;
cout<<"Please input two numbers to be multiplied, after inserting the first number, press enter. Then input the second number and press enter: ";
cin>> x >> y;
cin.ignore ();
cout<<"The product of you two numbers is " << x * y <<"\n";
cin.get ();
}
int mult ( int x, int y )
{
return x * y;
}
__________________ If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date. | |
| Ryan0773 is offline | |
| | #85 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #86 |
| Kiss the monkey. Join Date: Sep 2001
Posts: 810
| Yes, you must ensure that your IDE is building the correct project.
__________________ "If you tell the truth, you don't have to remember anything" -Mark Twain |
| CodeMonkey is offline | |
| | #87 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
umm...Like everyone else? I think? I write the code and then press the "Compile & Run" button. Is there another way to compile it? This hasn't ever happenned before with my other projects.
__________________ If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date. | |
| Ryan0773 is offline | |
| | #88 |
| Kiss the monkey. Join Date: Sep 2001
Posts: 810
| Create a new project with only the above code, and tell us what happens.
__________________ "If you tell the truth, you don't have to remember anything" -Mark Twain |
| CodeMonkey is offline | |
| | #89 |
| C++ noob Join Date: Jan 2009
Posts: 43
| ...which "above code", the average one?
__________________ If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date. |
| Ryan0773 is offline | |
| | #90 |
| C++ noob Join Date: Jan 2009
Posts: 43
| Alright, nevermind, it seems to be working. I'm still wondering how it read the other program. I did everything the same but this time it gave me the averages (which are 13 and 3.08, by the way) I have a question about learning C++. Would it be a better idea to get a book on the subject (if so, please suggest some) or should i just keep doing the C++ tutorials on the site, but they dont explain things all that well.
__________________ If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date. |
| Ryan0773 is offline | |
![]() |
| Tags |
| basic, basics, c++, lots, noob |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Several Questions, main one is about protected memory | Tron 9000 | C Programming | 3 | 06-02-2005 07:42 AM |
| Trivial questions - what to do? | Aerie | A Brief History of Cprogramming.com | 23 | 12-26-2004 09:44 AM |
| Questions. | anonytmouse | A Brief History of Cprogramming.com | 4 | 05-19-2004 02:16 PM |
| C++ test questions | Mister C | C++ Programming | 9 | 09-08-2002 12:05 PM |