|
I think you have some errors in your code. Can you post the correct code? Also post a more complete definition of what you're trying to accomplish.
-Sean
----
Shag a Lizard
|
|
|
|
|
Sory for the error.
I'm interested in how to enhence the calculation speed of the for loop.
Now the correct code:
==========================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ZeroOneDiff(char *str, int length, char sbit)
{
int i;
char tmp;
char backup[60];
int iZero, iOne;
int iDiff;
tmp = sbit;
memset(backup, 0, 28);
for(i=0; i<length; i++){
if( (str[i] == '1') && (tmp == '0')) {
backup[i] = '1';
}
else if( (str[i] == '1') && (tmp == '1')){
backup[i] = '0';
}
else if((str[i] == '0') && (tmp == '0')){
backup[i] = '0';
}
else if((str[i] == '0') && (tmp == '1')) {
backup[i] = '1';
}
tmp = backup[i];
}
iZero = 0;
iOne = 0;
iDiff = 0;
for(i=0; i<length; i++){
if(backup[i] == '0') iZero++;
else iOne++;
}
iDiff += (iZero - iOne);
return iDiff;
}
void main()
{
char str[60] = "010001000100100";
int diff = ZeroOneDiff(str, 15, '1');
printf("%d\n", diff);
}
============================================
chen
|
|
|
|
|
By what gauge are you using to measure its current speed?
Why are you only setting the first 28 bytes of backup, which is 60 bytes, to 0?
The only change I could possibly make to this would be something like:
iOne = 0;
for (i = 0; i < length; i++)
{
if ('1' == backup[i])
iOne++;
}
return (length - iOne);
You could actually do this counting in the other 'for' loop. This would save having to iterate through the string again
You could also change the first loop to something like the following, but any gain would be negligible. If this function is only called a few times, this is really a moot exercise.
if ('1' == str[i])
{
if ('0' == tmp)
backup[i] = '1';
else
backup[i] = '0';
}
else
{
if ('0' == tmp)
backup[i] = '0';
else
backup[i] = '1';
}
BTW, read the FAQ regarding how to format code when certain reserved symbols are used. The '<' symbol should be "& lt" and the '>' should be "& gt", minus the space. This is evident in the 'for' loop of your code.
|
|
|
|
|
It's obvious that below method is shorter but I am NOT sure its performance is better. (Depond how big is your binary string)
Looks like it's CS freshman's assignment
int ZeroOneDiff2(char *str, int length, char sbit)
{
int nZero = 0, nOne = 0, nNext = 0, nTmp = atoi(&sbit);
char c;
for(int i = 0; i < length ; i++)
{
c = str[i];
nNext = atoi(&c);
nTmp ^= nNext;
if(nTmp)
nOne++;
else
nZero++;
}
return nZero - nOne;
}
|
|
|
|
|
This indeed counts but I don't see the "translation" code.
|
|
|
|
|
>>> I don't see the "translation" code.
What is that???????????
If you like to see the result of "backup[]", just add this line in the end of for loop
printf("%d",nTmp);
If it's what you mean "translation".
Come on, this should be the assignment for the "C" 2-month-experience beginner....
|
|
|
|
|
Anonymous wrote:
What is that???????????
The code that translates "010001000100100" into "011110000111000." The code snippet that you provided does indeed count 1s and 0s but it does not appear to do the OP's translation requirement. Do you concur?
|
|
|
|
|
???? The method only return Zero - one as integer.
Does the method print/compute "translatioin" for user????????
I am out of here. It's not worth to "chat"...........
|
|
|
|
|
You obviously missed Chen's original post. It had TWO 'for' loops. The first one translated the string. The second one counted the 0s and 1s.
|
|
|
|
|
I have a popup in my pc that have me very tired.
Every time that I start my explorer it starts and I am very closer to format my machine....
any body knows where I can look in the registry to find the dll that is making that. The popup is the green lotery of USA. Please any body can help me. May be Michael Dunn knows where the dll is....
Thanks in Advance
Carlos Antollini.
Pi Five[^]Creator
Sonork ID 100.10529 cantollini
|
|
|
|
|
Unfortunately, IE launching popup-ad windows is an all too common occurence these days. Its not a DLL that is the culprit but embedded script and/or an ActiveX control.
One thing you can do is to change your browser's security settings. By default, most items are set to Enable, where they should actually be Disable or Prompt.
Another is to install an ad-blocker program. These are just BHOs (Browser Helper Objects). Some are good while others aren't.
|
|
|
|
|
Haven't you been visiting pr0n sites lately ? or serialz sites ?
It's very likely the plugin registers as the default IE/Explorer search extension :
check out weird values in HKCR\Software\Microsoft\Internet Explorer and
HKLM\Software\Microsoft\Internet Explorer.
Look up those subkeys :
- Main
- Search
- Toolbar
- Extensions
|
|
|
|
|
I have these two lines in my code but the second line throws an exception saying that the recordset is writeprotected, what could be the problem>?
<br />
CRecordset myRecordSet.Open(CRecordset::dynaset, NULL, CRecordset::appendOnly);<br />
myRecordSet.AddNew();<br />
|
|
|
|
|
Upon failure, what is the value of CDBException.m_nRetCode?
|
|
|
|
|
I found the problem, it was caused by one of the columns in the table having the identity field set so that it automatically filled in the id for the row. Apparantly the CRecordSet class cannot handle that ...
|
|
|
|
|
Hi!
I have a vector<int> which could be {0 2 53 84 962 1120}, i.e. it is sorted.
Now assume I have a value x, i'd like to get the highest value which is lower than x in the list. (i.e. if x =250, the result would be 84, since 84<250 and 250>962)
Of course, I already have a solution -- iterating through the vector, but i'd like to know if there is a way to do it using algorithm (like defining a predicate less<int,250> and using the max_element algorithm with that predicate (which would throw the max value of the vector which is lower than 250 Ouf !). Is that possible ? If yes, what would be the exact code for it ?
Thanks !
~RaGE();
|
|
|
|
|
You could use lower_bound() to get the position of first equal or greater element and then move 1 place back in the vector (after checking you didn't get the begin or end iterator).
|
|
|
|
|
clever ! Thanks !
~RaGE();
|
|
|
|
|
A very quick response - not much thought went into it. Please check it before using.
Sort the vector descending
ie, {1120, 962, 84, 53, 2, 0}
Now look for the first value less than 250
comparison fn:
bool compare(value1, value2)
{
return value1 < value2;
}
std::lower_bound(vec.begin(), vector.end(), 250, compare);
returns the required iterator.
Thomas
My article on a reference-counted smart pointer that supports polymorphic objects and raw pointers
modified 29-Aug-18 21:01pm.
|
|
|
|
|
thanks, very good !
~RaGE();
|
|
|
|
|
If your looking for a little more speed on the lookup, you could use a 'set', as it will be pre-sorted. Only thing is there is an increased overhead in loading the items.
Of course a 'set' is for unique values. If you require the abbility to store duplicates then you could use a 'multiset'.
All you need to do then, is find the value you are looking for or the nearest one, and search one place in the correct direction.
Shameless Plug - Distributed Database Transactions in .NET using COM+
|
|
|
|
|
|
Note the use of reverse iterators and binders.
vector<int> v;
vector<int>::reverse_iterator iter;
iter = find_if(v.rbegin(), v.rend(), bind2nd(less<int>(),250));
|
|
|
|
|
|
Dear all,
I have two template functions for displaying books and members respectively from arrays.
<br />
template<class Object><br />
void List<Object>::displayBook(char* type)<br />
{<br />
if (num_elements == 0)<br />
cout<<"No "<<type<<" is found in the "<<type<<" array.\n";<br />
else<br />
for(int element=0; element<this->num_elements; element++)<br />
{<br />
cout<<'\n';<br />
this->element_list[element]->display(association_list.get_member(element_list[element]));<br />
}<br />
}<br />
<br />
template<class Object><br />
void List<Object>::displayMember(char* type)<br />
{<br />
if (num_elements == 0)<br />
cout<<"No "<<type<<" is found in the "<<type<<" array.\n";<br />
else<br />
for(int element=0; element<this->num_elements; element++)<br />
{<br />
cout<<'\n';<br />
this->element_list[element]->display(association_list.get_book(element_list[element]));<br />
}<br />
}<br />
The function prototypes are:
<br />
Member* get_member(Book* book);<br />
Book* get_book(Member* member);<br />
and the functions implementation code is:
<br />
template<class Book,class Member><br />
Member* AssociationList<Book,Member>::get_member(Book* book)<br />
{<br />
Member* member=0;<br />
bool searching=true;<br />
int index=0;<br />
<br />
while(searching)<br />
{<br />
if (this->association_list[index])<br />
if (this->association_list[index]->linked_book()==book)<br />
{<br />
member=this->association_list[index]->linked_member();<br />
searching=false;<br />
}<br />
else<br />
index++;<br />
else<br />
index++;<br />
if (searching && (index == LIST_SIZE))<br />
{<br />
searching=false;<br />
}<br />
}<br />
return member;<br />
}<br />
<br />
template<class Book,class Member><br />
Book* AssociationList<Book,Member>::get_book(Member* member)<br />
{<br />
Book* book=0;<br />
bool searching=true;<br />
int index=0;<br />
<br />
while(searching)<br />
{<br />
if (this->association_list[index])<br />
if (this->association_list[index]->linked_member()==member)<br />
{<br />
book=this->association_list[index]->linked_book();<br />
searching=false;<br />
}<br />
else<br />
index++;<br />
else<br />
index++;<br />
if (searching && (index == LIST_SIZE))<br />
{<br />
searching = false;<br />
}<br />
}<br />
return book;<br />
}<br />
Could someone tell me how can I use One Template Function Instead of Two (if this is possible)?
Apologies for posting the similar question many times.
grscot
|
|
|
|