|
What have you done yet to try to solve this _very_ simple school problem ? did you even try to write some code for it ? or were you expecting someone else to do it for you ?
anyway, should be quite easy with a simple loop, a couple of std::cin and std::cout , an accumulator, a counter, * and / ...
I'm certain if you try to do it by yourself, you will be so proud that you will feel ashame to even have posted this question.
M.
Watched code never compiles.
|
|
|
|
|
Since the question is just to write a function, NO, I didn't try to run a program.
I Think the answer to this is:
Double average (int size, double values []);
{return (a);} {return (a + b) / 2;} {return (a + b + c) /3;} {return (a + b + c + d / 4;}
{return (a + b + c + d + e / 5;}
I really don't have a clue what I'm doing so I'm winging it to say the least. I'm thinking the above is in the ball park because in my text book, there's a program called Average.c that goes like this:
#include <stdio.h>
double average (double a, double b);
int main(void)
{
double x, y, z;
printf("Enter three numbers: ");
scanf("%1f%1f%1f", &x, &y, &z);
printf("average of %g and %g: %g\n", x, y, average (x, y));
printf("average of %g and %g: %g\n", x, y, average (y, z));
printf("average of %g and %g: %g\n", x, y, average (x, z));
return 0;
}
double average (double a, double b)
{
return (a + b) / 2;
while(1);
}
Am I in the ball-park for the function part???
Oh ya, I have no idea what Maximilien was talking about with all the
"simple loop, a couple of std::cin and std::cout , an accumulator, a counter, * and / ..." Sounds like what adults sound like on Peanuts cartoons.
|
|
|
|
|
Tim Dappen wrote: I really don't have a clue what I'm doing
So maybe some time with your text books would be time well spent.
It's time for a new signature.
|
|
|
|
|
Don't you want to use a std::vector to hold the numbers entered by the user?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
no need to keep an history of the input, just "accumulate" and divide by the count (number of inputs).
Watched code never compiles.
|
|
|
|
|
Maximilien wrote: no need to keep an history of the input, just "accumulate"...
accumulate() has to have an array of some sort to work with.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
If he's that clueless, he's not ready for templates.
Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.
|
|
|
|
|
The basic idea is simple: keep a running total and a count of the number of numbers entered. After each number is entered the average is the total divided by the number of numbers entered.
Steve
|
|
|
|
|
One thing to remember when doing programming assignments: Accademic computer scientists love recursion. You should try and introduce it to everything you do. So the best way to implement the function you're trying to write is:
double sum_array( int size, double values[] )
{
if( size > 0 ) return values[0] + sum_array( size - 1, values + 1 );
return 0;
}
double average( int size, double values[] )
{
return sum_array( size, values ) / size;
}
Another cool thing is that the solution doesn't have any side effects so it's pure within the mathematical idea of a function and the CS ideas of functional programming.
You can test it with something like:
int main()
{
double values[ 10 ] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
std::cout << average( 10, values ) << std::endl;
}
The I/O should be easy to sort out.
Cheers,
Ash
|
|
|
|
|
Aescleal wrote: Accademic computer scientists love recursion.
They do seem to. I'm not the biggest fan myself.
Steve
|
|
|
|
|
I prefer divide and conquer:
double sum(std::size_t size, double values[]) {
if (0==size)
return 0.0;
else
return sum(size/2, values) + sum(size-size/2, values+size/2);
}
double average(std::size_t size, double values[]) {
return sum(size, values)/size;
}
Of course, if your goal is to repeatedly calculate the 'running average' of a stream of numbers, a more efficient implementation might look like this:
double incremental_average(double pre_avg, std::size_t new_size, double new_value) {
if (new_size > 1)
return (new_value + pre_avg*(new_size-1))/new_size;
else
return new_value;
}
int main () {
double values[] = {3.5, 5, 7, -2, 3.14, 2.71, 6, 7, 8, 9};
double avg = 0;
for (std::size_t i = 0; i < sizeof(values)/sizeof(double); ++i) {
avg = incremental_average(avg, i+1, values[i]);
std::cout << "average[" << i+1 << "] = " << avg << std::endl;
}
}
modified on Tuesday, September 28, 2010 8:52 AM
|
|
|
|
|
Hi everyone, I want to write an application that can analyse data given in grids like those in Excel. I want to write the application in the Win32 API. How can I add such grid tools in this application? Does using this tool require learning other API before doing it? I will also be glad if I am directed to tutorials and documentations about doing this. Thanks.
|
|
|
|
|
|
I do not know MFC, and going into it means possibly learning another language, although it is in C++. Please do you have any idea of how I can write one on my own if there are no other ways. Brief ideas and sources will be appreciated. Thanks.
|
|
|
|
|
how much effort it needs and LOC it takes to develop such a control in plain win32 without using MFC, just guess. 
|
|
|
|
|
I would probably write the GUI in .NET and if you need any high performance calculation, then implement this in a standard unmanaged dll.
|
|
|
|
|
Hi,
I am currently digging through heaps of articles about various mechanisms for parallel programming, such as OpenMP, OpenCL, and the new Parallels library in Visual Studio 2010. In the short run, I intend to modify our application to take advantage of multicore systems, and it seems the VS libraries will make this really easy to implement.
But I am looking further, and would like to draw on the power of GPU processing as well, so I'd like to make my changes in a way that are not limited to just one method. What I'm thinking of is an interface that can be adapted through settings to use multiple cores, GPU power, or whatever other method I might think of in the future (cloud computing maybe?).
I have no idea where to start though. I've found some very helpful articles (with examples) for using the VS integrated libraries, so I'm good for these. I am pretty much dangling in thin air with respect to GPU processing though: the Wiki articles on OpenMP or OpenCL are pretty much academic, and provide very little in the way of examples or 'how-to' advice.
Can anyone point me to a good article or two to get me started?
P.S.: The application this is all about is a product sold to individual customers with varying hardware, so information about specific GPU programming such as CUDA (I've already found a good article about this on CP) doesn't really help all that much, as it's limited to just one family of graphic cards.
|
|
|
|
|
This is not easy to answer. It's like if you asked: 'Oh, I like the idea of quantum physics, I tried to read a book but it was too academic, please help...'
Parallel computing is a very wide field, divided into many distinct disciplines. I suggest you first try to clarify what you go into first: Utilizing multicore CPU systems is mostly about 'task parallelism' (= pieces of your program code are running concurrently, possibly sharing same data), GPU computing is mainly about 'data parallelism' (= data is processed by several independent units performing the same step on different data). Often, these two kinds of parallelism overlap. Technically, however, programming GPU parallelism versus concurrent programming on a (multicore) CPU system are two different worlds...
Wikipedia articles are not a bad point to start with. I also suggest to have a look at the MSDN magazine articles and the tutorials on msdn.com. You also mentioned OpenCL and OpenMP - there is a vast amount of introductory material available in printed and online format. With regard to concurrent programing I'd recommend to have a look at frameworks which abstract concurrency, like Microsofts TPL or CCR. With regard to general purpose GPU computing, I liked http://www.mathematik.uni-dortmund.de/~goeddeke/gpgpu/index.html[^]
modified on Sunday, September 19, 2010 5:15 AM
|
|
|
|
|
Thank you for your response.
To clarify, I am actually not exactly new to programming, I already dabbled a bit into it in the 80s, then using the OCCAM programming language. However, what we did was largely academic because we never managed to lay our hands on actual parallel hardware, we just simulated it to see how it works.
The problem I have is that since then I never really had a need to care about multithreading or any other form of parallelism until, some 5 years ago, I joined a team that had a master-slave pattern implemented for remote data base queries. This was the first time I've seen the problem of parallelism molded into an object-oriented pattern, and it was completely different from my first experiences with OCCAM.
What I am trying to accomplish now is two things:
First, get a mechanism into place to deal with massive data paralellism: we have multiple time-consuming numerical algorithms that work on dozens, hundreds, or sometimes thousands of sets of independent data at the same time. Multicore paralleliism like what is supported by the VS parallel libraries would be a first step, but I'd like to be sufficiently abstract to be able to use GPUs instead, if the graphics card supports it.
Second, add 'task-parallelism', as you named it. I'm currently not clear how much this would help, but fear it will take a lot of work for little gain. The areas that I've determined so far which could benefit from this are also in a way data-parallel, although on a much more complex level, and possibly would require some work to prevent race conditions.
From what I've read - confirmed by you're saying - my first goal might be tough to fit into one framework. Anyway, the link you posted looks like it might give me a better idea of that. I'll be content if I can at the very least design an interface that I don't have to completely rewrite once I add in GPU programmming. It's just too much work that depends on this interface, i. e. too much code I need to adapt, to do it twice over. I just want to do that once, and then concentrate on the crunchy bits.
|
|
|
|
|
I see what you're up to. Currently, I do not know of any framework (planned or existing) which would sufficiently abstract 'parallelisms' (huch, this plural sounds strange) of both kinds.
Well, there is something which comes close: the deferred expression/query technique from microsoft's .net framework, also known as LINQ. You define an expression which is basically converted into AST. This expression can be 'compiled' into executable code in an application specific manner. The expression is compiled on demand yielding a 'callable' entity which can be executed with various parameters. What the expression is actually compiled into is an implementation detail.
For example, there is a framwork called 'brahma' (go google 'brahma' = 'gpu computing'). Brahma takes as input an algebraic expression consisting of matrix or other vector operations and compiles it into a program which can execute on a GPU. Equally well, you could compile the expression into a program which uses TPL (Task Parallel Library) or something else...
|
|
|
|
|
What constitutes a good programmer?
Do you know any links which say something about this matter? e.g. skills etc.
best regards.
|
|
|
|
|
Flying_Dutchman wrote: What constitutes a good programmer?
That's almost an impossible question to answer (compounded by it being asked in the wrong forum). What constitutes good?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
thanks david. do u know a forum where to ask it??
let's try, good - highly paid? ...
best regards.
|
|
|
|
|
Strong programming skills.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
very funny
not saying anything about the post below etc....
best regards.
|
|
|
|
|