C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-09-2009, 08:34 PM   #76
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Question Thats not right?!?!?!

Quote:
Originally Posted by Elysia View Post
Solution 1:
Code:
bool flag = true;
while (!flag)
{
    switch (...)
    {
        // ...
        default:
            flag = false;
    }
}
Solution 2:
Code:
while (...)
{
    switch (...)
    {
        // ...
        default:
            continue;
    }
}
It is not at all that hard as you make it out to be. Learn how to use flowcharts and pseudo code.
I tried them both out but they didn't seem to work, i may be doing something wrong but that doesn't really matter right now, ill learn it later I'm just friggin confused with this. When i input this code it's supposed to find the average of the integers or something.

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();
}
It's not the best code in the world but im still learning. Anyways, a while back i made a code that multiplied two seperate integers that were input (aren't i amazing), this is relavent i guess because whenever i try to run this code, it keeps popping up my multiplication code. I mean WTF!!!!
__________________
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   Reply With Quote
Old 01-10-2009, 03:23 AM   #77
Kiss the monkey.
 
CodeMonkey's Avatar
 
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   Reply With Quote
Old 01-10-2009, 03:26 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 01-10-2009, 03:38 AM   #79
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Old 01-10-2009, 12:50 PM   #80
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by Elysia View Post
Interesting solution, but are we going to confuse the newbie?
...Ya i have no idea what you guys are saying so that might meen that im a bit confused

Quote:
Originally Posted by laserlight View Post
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;
}
You code works, it gives me the average of the number, but im just wondering how my code
----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   Reply With Quote
Old 01-10-2009, 12:55 PM   #81
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
You code works, it gives me the average of the number, but im just wondering how my code
----ed up so badly?
Which code are you talking about, and how does it not work?
__________________
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   Reply With Quote
Old 01-10-2009, 01:00 PM   #82
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
Which code are you talking about, and how does it not work?
This is the code that i am having trouble with. it says the problem at the end of the quote.

Quote:
Originally Posted by Ryan0773 View Post
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();
}
It's not the best code in the world but im still learning. Anyways, a while back i made a code that multiplied two seperate integers that were input (aren't i amazing), this is relavent i guess because whenever i try to run this code, it keeps popping up my multiplication code. I mean WTF!!!!
__________________
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   Reply With Quote
Old 01-10-2009, 01:04 PM   #83
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
This is the code that i am having trouble with. it says the problem at the end of the quote.
What do you mean by "it keeps popping up my multiplication code"? The program looks okay, compiles okay, and gives the correct results when run.
__________________
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   Reply With Quote
Old 01-10-2009, 01:10 PM   #84
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
What do you mean by "it keeps popping up my multiplication code"? The program looks okay, compiles okay, and gives the correct results when run.
Well whenever i run it, instead of running this code, it runs my multiplication code:

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;
}
That is the code it runs instead of the other one. I dont know why it keeps running this one instead.
__________________
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   Reply With Quote
Old 01-10-2009, 01:36 PM   #85
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
Well whenever i run it, instead of running this code, it runs my multiplication code:
How are you compiling and running your test programs?
__________________
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   Reply With Quote
Old 01-10-2009, 02:23 PM   #86
Kiss the monkey.
 
CodeMonkey's Avatar
 
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   Reply With Quote
Old 01-10-2009, 07:05 PM   #87
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
How are you compiling and running your test programs?
Quote:
Originally Posted by CodeMonkey View Post
Yes, you must ensure that your IDE is building the correct project.

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   Reply With Quote
Old 01-10-2009, 07:39 PM   #88
Kiss the monkey.
 
CodeMonkey's Avatar
 
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   Reply With Quote
Old 01-10-2009, 08:07 PM   #89
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by CodeMonkey View Post
Create a new project with only the above code, and tell us what happens.
...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   Reply With Quote
Old 01-10-2009, 08:19 PM   #90
C++ noob
 
Ryan0773's Avatar
 
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   Reply With Quote
Reply

Tags
basic, basics, c++, lots, noob

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22