|
Member 13501021 wrote: ...i have to find the maximun range When I hear the phrase "maximum range" I think of things like: 1) what's the farthest that ball can be hit, 2) what's the longest distance that bullet will travel in 50% humidity, 3) what's the farthest that car can go on a full tank. What is your definition of maximum range?
Member 13501021 wrote: +--------------------------+
| 100 | -10 | -10 | -10 | 100 | --> this sould return the range 0-4
+--------------------------+
+-----------------+
| -7 | 5 | -1 | 9 | -6 | --> this sould return the range 1-3
+-----------------+
+-----------------+
| -7 | 5 | -6 | 9 | -6 | --> this sould return the range 3-3
What's the pattern?
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Pretty sure that the "maximum range" should be the segment of the array where the sum of its values is the highest of all possible segments.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
This surely is your homework, so I won't rob you of the opportunity to find the solution yourself.
But I'll try to nudge you onto the right track:
- Take a pen and paper, write down some random value sequences as input arrays and solve them manually. Try to do this in an algorithmic way. Use some longer sequences than you showed here.
- You can't have nested loops over n for O(n), obviously. So you will have to make do with one pass over the array. But you're free to use more or other variables.
- The solution is surprisingly easy and succinct - don't think too complicated.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Calculate sum of M natural numbers starting from N, where N and M should be read from standard input (separated by space). User FOR statement.
Example
Input:
3 4
Output:
18
So my code is:
#include <stdio.h>
int main(){
int i,n,m,sum;
scanf("%d %d",&n,&m);
sum=n;
for(i=0;i<=m;i++)
sum=sum+i;
printf("%d",sum);
}
I set i=0 because I figured since the sum is supposed to start from N itself then that's the way to do it. The problem is though it doesn't work the way it should. What's wrong with my code? :sigh:
|
|
|
|
|
Member 13478986 wrote: sum=n;
for(i=0;i<=m;i++)
sum=sum+i;
1. you are trying to sum (M+1) natural numbers: starting with 0 and ending with M...
2. You are trying to sum not was required
(i.e. N + N+1 + N+2 ... + N+M)
but
(N + 0 + 1 + ... + M)
|
|
|
|
|
that helped a lot, i changed it to
sum=0;
for(i=0;i<m;i++)
sum=sum+n+i;
works as it should, thanks!
|
|
|
|
|
You need no iteration, actually.
Since (see 1 + 2 + 3 + 4 + ⋯ - Wikipedia[^])
1 + 2 + ... + n = n * (n + 1) / 2
In your case
SUM = ((N + M - 1) * (N + M) - (N - 1) * (N)) / 2
Let's try the formula with the input
N=3, M=4
SUM = ((3 + 4 -1) * (3 + 4) - (3) * (2)) / 2 = (6 * 7 - 3 * 2) / 2 = (42 - 6) / 2 = 18
|
|
|
|
|
yeah I mean it's just an arithmetic sequence so the easiest way would be to just use the formula. the thing is the task requires using the "for" loop so I had to.
|
|
|
|
|
error :
24 no operator = for thie
104 string to TCHAR warning
i have a 'player.bmp' in the same map
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
bool key[ 256 ] ;
int winx , winy ;
#define FRAME_TIMER 1
class Sprite
{
public :
int x , y , w , h ;
bool visable ;
BITMAP bmp ;
Sprite(){;}
void loadbmpfromfile( HINSTANCE hinstance , TCHAR * file )
{
bmp = LoadBitmap( hinstance , _T( file ) ) ;
w = bmp.bmWidth ;
h = bmp.bmHeight ;
visable = true ;
}
void show( HDC hdc )
{
if ( visable )
{
BitBlt( hdc , x , y , w , h/2 , hdc , 0 , 0 , MERGECOPY ) ;
BitBlt( hdc , x , y , w , h/2 , hdc , 0 , h/2 , MERGEPAINT ) ;
}
}
bool hit( Sprite spr )
{
if ( x + w < spr.x ) return false ;
if ( x > spr.x + spr.w ) return false ;
if ( y + h < spr.y ) return false ;
if ( y > spr.y + spr.h ) return false ;
return true ;
}
} ;
Sprite player ;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
_T("sprite 1"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
) ;
ShowWindow (hwnd, nCmdShow);
player.loadbmpfromfile(hThisInstance,"player.bmp") ;
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paint ;
HDC hdc = BeginPaint( hwnd , &paint ) ;
RECT rect ;
switch (message)
{
case WM_CREATE :
player.x = 0 ;
player.y = 0 ;
SetTimer( hwnd , FRAME_TIMER , 40 , NULL ) ;
case WM_KEYDOWN :
key[ wParam ] = true ;
switch( wParam )
{
case VK_ESCAPE :
PostQuitMessage( 0 ) ;
break ;
default : ;
}
break ;
case WM_KEYUP :
key[ wParam ] = false ;
break ;
case WM_TIMER :
switch( wParam )
{
case FRAME_TIMER :
if ( key[ VK_UP ] ) player.y-- ;
if ( key[ VK_DOWN ] ) player.y++ ;
if ( key[ VK_LEFT ] ) player.x-- ;
if ( key[ VK_RIGHT ] ) player.x++ ;
if ( player.y > winy ) player.y = 0 ;
if ( player.y < 0 ) player.y = winy ;
if ( player.x > winx ) player.x = 0 ;
if ( player.x < 0 ) player.x = winx ;
InvalidateRect( hwnd , NULL , true ) ;
break ;
default : ;
}
break ;
case WM_PAINT :
GetClientRect( hwnd , &rect ) ;
winx = rect.right ;
winy = rect.bottom ;
player.show( hdc ) ;
EndPaint( hwnd , &paint ) ;
case WM_DESTROY :
PostQuitMessage( 0 ) ;
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0 ;
}
|
|
|
|
|
Please stop doing this. Do not just dump a load of code with a vague comment and expect someone to figure out what the problem is and then fix it for you. Edit your question, remove the code not relevant to the problem, and explain clearly what is going wrong, and where it happens.
|
|
|
|
|
The _T() macro is for constant strings (and characters) only:
bmp = LoadBitmap(hinstance, file);
When supporting Unicode and non-Unicode builds, all constant strings must be embedded in that macro:
player.loadbmpfromfile(hThisInstance, _T("player.bmp"));
|
|
|
|
|
Both errors look like they could be coming from the call to LoadBitmap() , but since you failed to include line numbers, it's hard to say for sure. That function returns a BOOL not a BITMAP . Also, the _T() macro is for string literals.
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
a error font by the compler
can have a source elsewere so i have to post al the code
i don't know how to include the line numbers
my english in writing is not great
so i have to be short
|
|
|
|
|
ok i try it
i changed the folowing :
player.loadbmpformfil( ,_T("player.bmp)) ;
my sprite looks like this now :
class Sprite
{
public :
int x , y , w , h ;
bool visable ;
HBITMAP hbmp ;
HDC bmphdc ;
Sprite(){;}
void loadbmpfromfile( HINSTANCE hinstance , TCHAR * file )
{
hbmp = LoadBitmap( hinstance , file ) ;
w = hbmp.i ;
h = hbmp.i ;
visable = true ;
}
void show( HDC hdc )
{
if ( visable )
{
BitBlt( hdc , x , y , w , h/2 , bmphdc , 0 , 0 , MERGECOPY ) ;
BitBlt( hdc , x , y , w , h/2 , bmphdc , 0 , h/2 , MERGEPAINT ) ;
}
}
bool hit( Sprite spr )
{
if ( x + w < spr.x ) return false ;
if ( x > spr.x + spr.w ) return false ;
if ( y + h < spr.y ) return false ;
if ( y > spr.y + spr.h ) return false ;
return true ;
}
} ;
Sprite player ;
i know this is wrong . how do i do this ?
my wm_paint :
case WM_PAINT :
GetClientRect( hwnd , &rect ) ;
winx = rect.right ;
winy = rect.bottom ;
player.bmphdc = CreateCompatibleDC(hdc);
player.show( hdc ) ;
DeleteDC( player.bmphdc ) ;
EndPaint( hwnd , &paint ) ;
this can be wrong . how do i do this ?
|
|
|
|
|
I told you a week ago, you must call BeginPaint inside your WM_PAINT code. So it needs to be:
case WM_PAINT :
hdc = BeginPaint( hwnd , &paint );
EndPaint( hwnd , &paint ) ;
break;
|
|
|
|
|
ok done that
how do i proseed next ?
case WM_PAINT :
hdc = BeginPaint( hwnd , &paint ) ;
GetClientRect( hwnd , &rect ) ;
winx = rect.right ;
winy = rect.bottom ;
player.bmphdc = CreateCompatibleDC( hdc ) ;
player.show( hdc ) ;
DeleteDC( player.bmphdc ) ;
EndPaint( hwnd , &paint ) ;
break ;
case WM_DESTROY :
|
|
|
|
|
bluatigro wrote: how do i proseed next ? What do you mean? As far as I can see the above code does not do anything.
|
|
|
|
|
by me pc its giving error's
if i knew what to do i won't ask
this is new to me
what do i ad / change so it does work as expected ?
it shoot show a sprite 'player.bmp' whit cursor control
|
|
|
|
|
bluatigro wrote: by me pc its giving error's What errors? Where do they occur? What is your code trying to do? Please provide proper information so we can try to help. Also, if this is all new to you then you would do well to study some tutorials on Windows programming. Here are two that are very useful:
EFNet #Winprog[^]
Win32 Programming - FunctionX[^]
|
|
|
|
|
i m trying as coding exersise
a verry simple VR system
error :
the picture isn't Always right
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <vector>
#include <math.h>
int winx , winy ;
class d3d
{
public :
double x , y , z ;
d3d()
{
x = 0.0 ;
y = 0.0 ;
z = 0.0 ;
}
d3d( double a , double b , double c )
{
x = a ;
y = b ;
z = c ;
}
} ;
d3d operator + ( d3d a , d3d b )
{
return d3d( a.x+b.x,a.y+b.y,a.z+b.z) ;
}
d3d operator / ( d3d a , double b )
{
return d3d( a.x/b,a.y/b,a.z/b) ;
}
d3d pnt[256] ;
void setpoint( int no ,
double a ,
double b ,
double c )
{
pnt[ no ] = d3d(a,b,c) ;
}
class triangle
{
public :
d3d punt[ 3 ] , mid ;
COLORREF color ;
triangle()
{
;
}
triangle( d3d p1 , d3d p2 , d3d p3 ,
COLORREF kl )
{
punt[ 0 ] = p1 ;
punt[ 1 ] = p2 ;
punt[ 2 ] = p3 ;
mid = ( p1 + p2 + p3 ) / 3 ;
color = kl ;
}
void show( HDC hdc )
{
POINT p[ 3 ] ;
int i ;
if ( mid.z > -900)
{
for ( i = 0 ; i < 3 ; i++ )
{
p[i].x=(LONG)(winx/2+punt[i].x/
(punt[i].z+1000.0)*1000) ;
p[i].y=(LONG)(winy/2-punt[i].y/
(punt[i].z+1000.0)*1000) ;
}
HBRUSH brush = CreateSolidBrush( color ) ;
HPEN pen = CreatePen( PS_SOLID , 0 , color ) ;
SelectObject( hdc , pen ) ;
SelectObject( hdc , brush ) ;
Polygon( hdc , p , 3 ) ;
SelectObject( hdc , pen ) ;
DeleteObject( pen ) ;
SelectObject( hdc , brush ) ;
DeleteObject( brush ) ;
}
}
} ;
const COLORREF red = RGB( 255 , 0 , 0 ) ;
const COLORREF green = RGB( 0 , 255 , 0 ) ;
const COLORREF yellow = RGB( 255 , 255 , 0 ) ;
const COLORREF blue = RGB( 0 , 0 , 255 ) ;
const COLORREF magenta = RGB( 255 , 0 , 255 ) ;
const COLORREF cyan = RGB( 0 , 255 , 255 ) ;
bool key[ 256 ] ;
triangle triangles[100] ;
int tritel ;
#define FRAME_TIMER 1
void show_al( HDC hdc )
{
int i , j ;
triangle h ;
for ( i = 1 ; i < tritel ; i++ )
{
for ( j = 0 ; j < i - 1 ; j++ )
{
if ( triangles[i].mid.z
> triangles[j].mid.z )
{
h = triangles[i] ;
triangles[i] = triangles[j] ;
triangles[j] = h ;
}
}
}
for ( i = 0 ; i < tritel ; i++ )
triangles[i].show( hdc ) ;
}
void tri( int p1 , int p2 , int p3 , COLORREF kl )
{
if ( tritel >= 100 ) return ;
triangles[ tritel ] = triangle(pnt[p1],
pnt[p2],
pnt[p3],
kl) ;
tritel++ ;
}
void quad( int p1 , int p2 , int p3 , int p4
, COLORREF kl )
{
d3d p = ( pnt[p1]+pnt[p2]+pnt[p3]+pnt[p4])/4;
setpoint( 255 , p.x,p.y,p.z ) ;
tri( 255 , p1 , p2 , kl ) ;
tri( 255 , p2 , p3 , kl ) ;
tri( 255 , p3 , p4 , kl ) ;
tri( 255 , p4 , p1 , kl ) ;
}
d3d rotatey( d3d a , double b )
{
double s = sin( b ) , c = cos( b ) , x , z ;
x = c * a.x - s * a.z ;
z = s * a.x + c * a.z ;
return d3d( x , a.y , z ) ;
}
triangle roty( triangle t , double b )
{
t.mid = rotatey( t.mid , b ) ;
t.punt[2] = rotatey( t.punt[2] , b ) ;
t.punt[1] = rotatey( t.punt[1] , b ) ;
t.punt[0] = rotatey( t.punt[0] , b ) ;
return t ;
}
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
_T("Rotating cube 1.0"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int t ;
PAINTSTRUCT paint ;
HDC hdc ;
RECT rect ;
GetClientRect( hwnd , &rect ) ;
switch (message)
{
case WM_CREATE :
tritel = 0 ;
setpoint( 0 , 100 , 100 , 100 ) ;
setpoint( 1 , 100 , 100 , -100 ) ;
setpoint( 2 , 100 , -100 , 100 ) ;
setpoint( 3 , 100 , -100 , -100 ) ;
setpoint( 4 , -100 , 100 , 100 ) ;
setpoint( 5 , -100 , 100 , -100 ) ;
setpoint( 6 , -100 , -100 , 100 ) ;
setpoint( 7 , -100 , -100 , -100 ) ;
quad( 0 , 1 , 3 , 2 , red ) ;
quad( 7 , 6 , 4 , 5 , cyan ) ;
quad( 0 , 1 , 5 , 4 , green ) ;
quad( 7 , 6 , 2 , 3 , magenta ) ;
quad( 0 , 2 , 6 , 4 , blue ) ;
quad( 7 , 5 , 1 , 3 , yellow ) ;
SetTimer(hwnd,FRAME_TIMER,40,NULL) ;
break ;
case WM_TIMER :
if ( wParam == FRAME_TIMER )
{
for ( t = 0 ; t < tritel ; t++ )
{
triangles[t] = roty(triangles[t],.1);
}
InvalidateRect( hwnd , NULL , true ) ;
}
break ;
case WM_KEYDOWN :
if ( wParam == VK_ESCAPE )
PostQuitMessage( 0 ) ;
break ;
case WM_PAINT :
GetClientRect( hwnd , &rect ) ;
hdc = BeginPaint( hwnd , &paint ) ;
winx = rect.right ;
winy = rect.bottom ;
show_al( hdc ) ;
EndPaint( hwnd , &paint ) ;
break ;
case WM_DESTROY :
PostQuitMessage( 0 ) ;
break ;
default :
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0 ;
}
|
|
|
|
|
bluatigro wrote:
the picture isn't Always right And that means what exactly?
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
And you think we can guess what all that code is supposed to do, and what it isn't doing right?
|
|
|
|
|
the code shoot show a colored rotating cube
i want to get rid of showing drawing
it gives ugly effects
i want double buffering
i have no idea where / what i have to change in the code
to that effect
i got now : WS_POPUP | WS_MAXIMIZE
how do i get fulscreen ?
|
|
|
|
|
bluatigro wrote: i have no idea where / what i have to change Well you cannot expect anyone here to understand all that code in a few minutes. You need to narrow down where the problem is and gather some more information about what is happening.
Your Window style should be based on WS_OVERLAPPEDWINDOW , not WS_POPUP . Google for "set Window full screen".
|
|
|
|
|
If you wrote the code to display the cube, then surely you can also remove it. If not, can you ask on the site where you procured the code from? Grabbing code from other sites, blindly throwing it into a compiler, and then wondering why it doesn't work hardly defines the software development process.
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|