|
when somebody input some number,you need give out the number of middle.my code is follow:#include <iostream>
using namespace std;
void swap(int &a,int &b);
void main()
{
int n=0,k,m,j;
int*p=new int[n];
cin>>n;
k=n;
j=n;
m=n;
for(;n>0;n--)
{
cin>>p[n-1];
}
int x=1;
while(x)
{
x=0;
for(;k>0;k--)
{
if(p[k-1]
|
|
|
|
|
youbo wrote: the number of middle.
AKA the median.
youbo wrote: my code is follow
Congratulations.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
|
Is dash space dash exclamation exclamation an abbreviation for something?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Here you go
void swap(int &a,int &b);<br />
void main()<br />
{<br />
int array_size=0,k,m,j;<br />
<br />
<br />
cin>>array_size;<br />
if(array_size < 1)<br />
return;<br />
int*p=new int[array_size];<br />
for(int i=0;i<array_size;i++)<br />
{<br />
cin>>p[i];<br />
}<br />
bool changed = true;<br />
int bubble_end = array_size - 1;<br />
while(changed)<br />
{<br />
changed = false;<br />
for(int i=0;i<bubble_end;i++)<br />
{<br />
if(p[i] > p[i+1])<br />
{<br />
swap(p[i],p[i+1]);<br />
changed = true;<br />
}<br />
}<br />
}<br />
if(array_size > 1)<br />
{<br />
if(array_size%2==1)<br />
cout<<p[array_size/2+1];<br />
else cout<<p[array_size/2]<<p[array_size/2+1];<br />
}<br />
else<br />
cout<<p[0];<br />
<br />
}<br />
void swap(int &a,int &b)<br />
{<br />
int c;<br />
c=a;<br />
a=b;<br />
b=c;<br />
}
-- modified at 8:50 Saturday 15th September, 2007
|
|
|
|
|
thank you first,but i couldn't find out the differences from your code to mine.i
have run it ,it appeared the same wrong that is not i need. my code in the same with yours is looked right....
|
|
|
|
|
Look more closely
void main()<br />
{<br />
int n=0,k,m,j;<br />
<br />
int*p=new int[n];<br />
<br />
cin>>n;<br />
k=n;<br />
j=n;<br />
m=n;<br />
for(;n>0;n--)<br />
{<br />
cin>>p[n-1];<br />
}<br />
int x=1;<br />
while(x)<br />
{<br />
x=0;<br />
for(;k>0;k--)<br />
{<br />
<br />
if(p[k-1] {<br />
swap(p[k-1],p[k]);<br />
x=1;<br />
<br />
}<br />
<br />
}<br />
}<br />
<br />
if(m%2==1){cout<<p[j/2+1];}<br />
else cout<<p[j/2]<<p[j/2+1];<br />
<br />
}<br />
void swap(int &a,int &b)<br />
{int c;<br />
c=a;<br />
a=b;<br />
b=c;} get this,it is also wrong.please have
|
|
|
|
|
I have problem with Casting ...
i've create class named Instrument { ... }
and in Dialog based MFC application
i have TreeView Control and in that at some point i am settin ItemData as "Instument" classs' Object
treeCtrl->SetItemData(instObject)//Instrument* to DWORD_PTR casting is done here
now at some point i need this data(object) back
so to take data
Instrument *instObject = treeCtrl->GetItemData(hItem); // error:
Please help me to cast DWORD_PTR back to Instrument Type ...
|
|
|
|
|
To reverse the effect of the first cast, cast the value back to the original type:
Instrument *instObject = (Instrument*) treeCtrl->GetItemData(hItem);
|
|
|
|
|
hey its not working ...
otherwise i would have done that ...
|
|
|
|
|
Then you've obviously done something else wrong, as Michael's suggestion is correct. How are you verifying that it does not work?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I'm guessing you are getting cannot convert DWORD_PTR to Instrument *.
Oh and allways try and use eplicit casting i.e. static_cast rather than C-Style casts.
Try this
Instrument *instObject = static_cast<Instrument *>((static_cast<void *>(treeCtrl->GetItemData(hItem))));
or if not
Instrument *instObject = dynamic_cast<Instrument *>((dynamic_cast<void *>(treeCtrl->GetItemData(hItem))));
Could you copy the exact compiler error into the thread.
|
|
|
|
|
reinterpret_cast is the right one for this situation, since you're converting between a pointer and a non-pointer type.
|
|
|
|
|
oh sorry its working now ... it was my mistake ...
Thanks a lot ...
|
|
|
|
|
Hi,
in the process of running my program is allocating a lot of memory (gigabytes). After it reaches a certain size strange things start to happen so I'm trying to debug it. I have some questions.
1. To my surprise new operator does not return NULL on error. It seems to throw an exception. When did this happen? What's the exception object? I'm using VS 8.
2. Is new a thread safe operator? What if I'm using some libraries that most likely use malloc. Is memory allocation still thread safe? I'm using dynamic link run time for my program and hopefully all the libraries. If some library is using a different run-time but I don't free its memory is that fine?
3. What is the memory allocation limit for a process in Windows XP? Is it 2 GB? Or RAM + swap space?
4. Any free GUI profilers out there?
Thanks for your help.
|
|
|
|
|
Budric B. wrote: To my surprise new operator does not return NULL on error. It seems to throw an exception. When did this happen?
I thought it was a lot longer ago, but according to the docs:
"In Visual C++ .NET 2002, the new function in the Standard C++ Library will support
the behavior specified in the C++ standard, which is to throw a std::bad_alloc exception
if the memory allocation fails. In Visual C++ 2005, the C Runtime Library's new function will also throw a std::bad_alloc
exception if the memory allocation fails.
If you still want the non-throwing version of new for the C Runtime Library, link your program
with nothrownew.obj. However, when you link with nothrownew.obj, new in the Standard C++
Library will no longer function."
Budric B. wrote: 2. Is new a thread safe operator?
In the Multithread CRT, Yes. Any third-party library, well...
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Sounds to me like you have some serious memory leakage. Make sure you call delete on every time you allocate using new.
Try this[^] utility, it's excellent for finding leaks.
As for your questions:
1. use try catch blocks.
2. yes.
3. Usually RAM, but not too sure...
4. www.google.com
Waldermort
|
|
|
|
|
Budric B. wrote: 1. To my surprise new operator does not return NULL on error. It seems to throw an exception. When did this happen? What's the exception object? I'm using VS 8.
That's the way it is supposed to work - it throws bad_alloc. If you want to get a NULL instead, use nothrow[^]
Budric B. wrote: 2. Is new a thread safe operator? What if I'm using some libraries that most likely use malloc. Is memory allocation still thread safe? I'm using dynamic link run time for my program and hopefully all the libraries. If some library is using a different run-time but I don't free its memory is that fine?
Yes. Yes. Yes.
Budric B. wrote: 3. What is the memory allocation limit for a process in Windows XP? Is it 2 GB? Or RAM + swap space?
In practice, less then 2GB[^] (BTW, who would vote down that text and why )
Budric B. wrote: 4. Any free GUI profilers out there?
Try GlowCode.
|
|
|
|
|
thanks for the heads up on the flag, I'll try it with my code.
|
|
|
|
|
Hello, I wanted to know if there's a way to embed databases into an executable file. How do you do it (I'd really appreciate it if you explain the way for both Sql Server and MySql, if they're different ways)? Is it recommended?
Thanks.
Perrin.
|
|
|
|
|
The engine ? Me think not really, but i'm no DB expert.
|
|
|
|
|
What I mean is to have a resulting exe that does not depend on the server (MySql or SQL Server) being installed in the user's computer, but still makes use of those databases.
|
|
|
|
|
SQL Server 2005 Express apperently can be embedded with an application. Look for the details at MSDN.
As for MySQL, it is easy: Embedded MySQL[^]
|
|
|
|
|
|
Hi everybody,
is it possible to get the console cursor position?
To set it, it works with SetConsoleCursorPosition(...)
Any ideas?
Thanks
|
|
|
|