|
nono it has to be consecutive sums..
input=11
1+2+3+4+5=15
2+3+4+5=14
3+4+5+6=12
4+5+6=14
5+6=11
you have to increase it by 1 each time is which i have done
but what should i place after sums=sums+j ??
to make sure that after it finds the nubers added and it went over 11 to check again?
|
|
|
|
|
Ah... I get it now.
Does it have to be a sequence of a particular maximum size ? Do you have to approach the problem this way ? I'd approach it by saying if I use a sequence of n numbers to add up to x, then x%n = n-1, so that's what I would look for within a loop. To do it the way you are seems to me more complex, although it may be the way your teacher wants you to do it for the sake of the exercise.
LMK if you must do it the way you are, I'm going to have a play with it now.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
Assuming you're allowed to do it this way, here is a solution:
#include "stdafx.h"
#include <iostream>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
int main(int argc, char* argv[])
{
int input;
cout <<"please enter a number ";
cin >> input;
for (int i = 2; i < 10; ++i)
{
if (input%i == i-1) break;
}
if (i == 10)
{
cout << "No solution found\n";
return 0;
}
int lowest = floor(input/i);
for (;i>=0;--i)
{
cout << lowest++;
if (i > 0) cout << " + ";
}
cout << " = " << input << endl;
return 0;
}
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
Damn - this does not work. Give me a minute...
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
This will work but has IMHO ceased to be elegant.
#include "stdafx.h"
#include <iostream>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
int main(int argc, char* argv[])
{
int input;
cout <<"please enter a number ";
cin >> input;
for (int i = 2; i < 10; ++i)
{
if ((input%i) == (i-1)) break;
}
if (i == 10)
{
cout << "No solution found\n";
return 0;
}
int lowest = floor(input/i);
if (lowest < 1) lowest = 1;
int sequence = 0;
for (;i>0;--i)
{
sequence += lowest;
if (sequence > input) break;
cout << lowest++;
if (i > 1 && sequence + lowest <= input) cout << " + ";
}
cout << " = " << input << endl;
return 0;
}
I'm at work so I can't devote any more time to it, but hopefully you can see what direction I'm heading. Right now I am managing cases my algorithm won't catch after the fact, which is *bad*. Maybe you can see where I have gone wrong. Try putting 6 into the first code I posted and you'll see it does not work, and it's pretty clear why. I just can't quickly wrap my head around itwhile other people are waiting for me to do some work so they can also continue with theirs.
If you don't get it sorted today I'll be happy to play with it more tonight.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
Here we go - this works every time and uses while loops to get to where it's going.
#include "stdafx.h"
#include <iostream>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
int main(int argc, char* argv[])
{
int input;
cout <<"please enter a number ";
cin >> input;
int result = 1;
int low = 1, high = 1;
while (result < input)
{
result += ++high;
}
while (result != input)
{
while (result > input)
result -=low++;
while (result < input)
result += ++high;
}
for (int i = low; i < high; ++ i)
{
cout << i << " + ";
}
cout << high << " = " << result << endl;
return 0;
}
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
I don't see it listed in any of the project settings.
Cathy
|
|
|
|
|
It's not in the settings. Source help files are visible in the FileView, just like .cpp's and .h's.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Thanks!
Cathy
|
|
|
|
|
I have problem to implement a flashwindow within timer
method, somebody have a silimar code to flashwindow
like "MSN Messenger" when arrived a message.
thanks! bye
|
|
|
|
|
|
Hi,
I've got a question about non-type template parameters. I'm passing a global char* as a parameter to a template (in addition to other stuff). Something like this:
In RecordTemplate.h
template<class record_type,="" char*="" record_name="">
RecordTemplate
{
// Methods and other neat stuff go here
};
Then in Record1.h
#include "RecordTemplate.h"
// Declare the string literals for record name
char _Record1RecordName[];
// Declare the struct type for use with the Adi record
struct Record1Struct
{
double TimeDate;
// other stuff . . .
};
// Typdef a specialization
typedef RecordTemplate<record1struct, _record1recordname=""> Record1;
And then in Record1.cpp
// Define the record name
char _Record1RecordName[] = "The name of the record";
This is working OK, and I am able to stamp out specializations of the template class. But, I want to be able to declare the char arrays as const, and initialize them in the declaration, like this (in the header file):
const char _Record1RecordName[] = "The name of the record.";
Then, I don't need the extra source file, and the records are safe from modification. I tried to use the const keyword, and I get a strange compiler error about template parameters may not be static. Apparently the const keyword affects the linkage, but I became a little confused at that point.
Has anyone tried this before? Basically, I just want to be able to pass a global const array of characters (or any other sort of const array for that matter) as a template parameter.
Thanks,
Aaron
|
|
|
|
|
Const objects have internal linkage by default (meaning the object is only visible in that translation unit [.CPP file]). Template params cannot have internal linkage.
You may be able to fix the error by adding extern to the declaration, or putting the array in an unnamed namespace.
Also, check out my C++ FAQ - I just added a link to a template FAQ that covers this particular issue.
--Mike--
http://home.inreach.com/mdunn/
Ford: How would you react if I said that I'm not from Guildford after all, but from a small planet somewhere in the vicinity of Betelguese?
Arthur: I don't know. Why, do you think it's the sort of thing you're likely to say?
|
|
|
|
|
OK, Mike. The extern keyword takes care of the problems with the template parameters.
Thanks for the tip
Aaron
|
|
|
|
|
I have written an MDI application with visual C++ 6, overrided
the functions : OnSaveDocument and OnOpenDocument to use my own serialization and filters.
The debug version works correctly, and I have a serious problem with my release version.
When I save my document, I quit the application without any prompt (The document is all the same saved...)
Problem of leak of memory, or initialization ,...?
How can do to find out error(s) and what tool can use ?
Thank in advance for your help.
Leo.
|
|
|
|
|
Problem of leak of memory, or initialization ,...?
This may be an initialization problem - in the release build local variables of primitive types are not initialized.
Also, you may have vital code inside ASSERTs - in the release build the code disappears; if this is the case, use VERIFY instead.
How can do to find out error(s) and what tool can use ?
BoundsChecker (www.numega.com) or Purify (www.rational.com). Note that you'll have to create new configuration based on Release, but with debug info on.
If your OnSave/OnOpenDocument code isn't very long, post it here. You can consider CodeProject forum to be another semi-automatic diagnostic tool
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
Hello folks,
I have a question with regards to printing in Win32 to printers who's name exceed the DEVMODE.dmDeviceName[32] limit.
In our product, we allow the user to set a default printer. We populate the possible printer list by using the following code:
flags = PRINTER_ENUM_LOCAL;
if ( IsWindows95 () )
level = 5;
else
flags |= PRINTER_ENUM_CONNECTIONS;
pbuf = (PBYTE) NULL;
EnumPrinters ( flags, NULL, level, pbuf, 0, &size, &num );
if ( asize > 0 )
{
pbuf = (PBYTE ) calloc ( size, 1 );
if ( pbuf == (PBYTE) NULL )
goto exit_func;
size = num = 0;
EnumPrinters ( flags, NULL, level, pbuf, asize, &size, &num );
if ( size == 0 || num == 0 )
goto exit_func;
if ( IsWindows95 ( ) )
pinf5 = (PRINTER_INFO_5 *) pbuf;
else
pinf4 = (PRINTER_INFO_4 *) pbuf;
retval = TRUE;
/* Now fill the listbox with the printers available. */
SendDlgItemMessage ( hwnd, id, CB_RESETCONTENT, 0, 0 );
for ( i = 0; i < num; i++ )
{
if ( IsWindows95 () )
{
SendDlgItemMessage ( hwnd, id, CB_ADDSTRING,
(WPARAM) 0, (LPARAM) pinf5->pPrinterName );
pinf5++; /* Next structure */
}
else
{
SendDlgItemMessage ( hwnd, id, CB_ADDSTRING,
(WPARAM) 0, (LPARAM) pinf4->pPrinterName );
pinf4++; /* Next structure */
}
}
No problems here, we simply store the pPrinterName in one of our variables such that when the time comes to print, we build the DEVMODE structure, then create our dc.
The problem comes when the user selects a printer that exceeds the 32 character maximum, (ie, network printer), where the name could be "\\nt200\FOO\FOO\PRINTER1010101". If the variable we stored was something like "\\nt200\laser1", everything would be fine, the document would print without any problems.
In the MSDN documentation it notes the following on the dmDeviceName variable:
Specifies the the "friendly" name of the printer; for example, "PCL/HP LaserJet" in the case of PCL/HP LaserJet®. This string is unique among device drivers. Note that this name may be truncated to fit in the dmDeviceName array.
How does Windows handle the case where the printer name exceeds the 32character max? Is there a field I could put the server name?
Mike.
|
|
|
|
|
There's an extenssion to win32API that comes with Win2000/XP
that allows to impose some restrictions on single process or
process group. The flags that are used to define constraints
allow to achieve a lot , but I couldnt find how to force
process /group of processes to use not more than some amount
of procesor computing power, measured in % (percentage).
For example: I want a media player which decodes some
video file (ie divx format) to use not more than 50% of
computing power of my CPU. If the program wants to consume more
CPU procesing power (than the specyfied amount)it would simply not receive it. When viewing the performance tab of task manager , the upper graf (of the cpu) shouldn't exceed 50% (provided that no other applications use processor).
I know that it is possible , since the IIS (that comes with
Win2000) allows to specify whether or not to use 'process throttling' , and to specify how many CPU computing power a web application (running some executable code / scripts) within the server may consume
Thanx in advance
Michal Januszczyk
Michal Januszczyk
|
|
|
|
|
I have created an MFC Extension DLL with the intention to export a FormView. I edited my resource.h file to force the Resource Objects to start at 9000. This allowed my form view to be created with the correct resource. Is this OK or is there a better way to export the resources that go along with my exported FormView?
|
|
|
|
|
|
We are using MS Access Database. Is it possible to execute a macro in the access database from VC++ program. My macro consists of several sqls. I understand you could do CDaoQueryDef to execute sqls, but my macro also has a transfer text that imports a txt file and creates a table in the database. I want to know if it is possible to directly execute the macro from the program. Any suggestions or ideas would be greatly appreciated.
Thanks,
|
|
|
|
|
Is Imposible, to do that, but you can try to execute the macro from a Query into the Access database, and in that case, you can execute the query...
Cheers!!!
Carlos Antollini.
|
|
|
|
|
Are you saying that I create a Query in Access database to execute the Macro. If that is what you are saying, how do I do that. When I create a new query within access database, it doesn't even give me an option to include a macro. Please advise.
Thanks,
|
|
|
|
|
Yes, I don't Know How you can execute a Macro from a Query, but is true that you can not execute a Access Macro from VC++. I am not an expert in Access, but I think that you need to execute the macro from the Query.
But at this moment I have an Idea: You can execute the macro with OleAutomation. I think that this last one is the best...
Cheers!!!
Carlos Antollini.
|
|
|
|
|
I don't know a whole lot about OleAutomation. Would you direct me to some place where I can find sample code.
Thanks,
|
|
|
|
|