Click here to Skip to main content
15,900,563 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionIs there a fast method for doing this? Pin
chen30-Apr-03 3:11
chen30-Apr-03 3:11 
AnswerRe: Is there a fast method for doing this? Pin
David Crow30-Apr-03 3:23
David Crow30-Apr-03 3:23 
GeneralRe: Is there a fast method for doing this? Pin
chen30-Apr-03 3:35
chen30-Apr-03 3:35 
GeneralRe: Is there a fast method for doing this? Pin
David Crow30-Apr-03 6:03
David Crow30-Apr-03 6:03 
GeneralRe: Is there a fast method for doing this? Pin
chen30-Apr-03 6:18
chen30-Apr-03 6:18 
GeneralRe: Is there a fast method for doing this? Pin
Rage30-Apr-03 6:19
professionalRage30-Apr-03 6:19 
AnswerRe: Is there a fast method for doing this? Pin
Sean Cundiff30-Apr-03 5:14
Sean Cundiff30-Apr-03 5:14 
GeneralRe: Is there a fast method for doing this? Pin
chen30-Apr-03 5:49
chen30-Apr-03 5:49 
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
GeneralRe: Is there a fast method for doing this? Pin
David Crow30-Apr-03 5:56
David Crow30-Apr-03 5:56 
GeneralRe: Is there a fast method for doing this? Pin
Anonymous30-Apr-03 9:05
Anonymous30-Apr-03 9:05 
GeneralRe: Is there a fast method for doing this? Pin
David Crow30-Apr-03 10:08
David Crow30-Apr-03 10:08 
GeneralRe: Is there a fast method for doing this? Pin
Anonymous1-May-03 7:27
Anonymous1-May-03 7:27 
GeneralRe: Is there a fast method for doing this? Pin
David Crow1-May-03 7:52
David Crow1-May-03 7:52 
GeneralRe: Is there a fast method for doing this? Pin
Anonymous2-May-03 8:10
Anonymous2-May-03 8:10 
GeneralRe: Is there a fast method for doing this? Pin
David Crow2-May-03 8:24
David Crow2-May-03 8:24 
GeneralA question about IE Pin
Carlos Antollini30-Apr-03 2:52
Carlos Antollini30-Apr-03 2:52 
GeneralRe: A question about IE Pin
Anonymous30-Apr-03 3:10
Anonymous30-Apr-03 3:10 
GeneralRe: A question about IE Pin
Stephane Rodriguez.30-Apr-03 3:17
Stephane Rodriguez.30-Apr-03 3:17 
GeneralProblem with CRecordSet Pin
Poul Haahr Klemmensen30-Apr-03 1:46
Poul Haahr Klemmensen30-Apr-03 1:46 
GeneralRe: Problem with CRecordSet Pin
David Crow30-Apr-03 2:50
David Crow30-Apr-03 2:50 
GeneralRe: Problem with CRecordSet Pin
Poul Haahr Klemmensen30-Apr-03 2:59
Poul Haahr Klemmensen30-Apr-03 2:59 
GeneralSTL algorithm Pin
Rage30-Apr-03 1:36
professionalRage30-Apr-03 1:36 
GeneralRe: STL algorithm Pin
markkuk30-Apr-03 2:10
markkuk30-Apr-03 2:10 
GeneralRe: STL algorithm Pin
Rage30-Apr-03 5:42
professionalRage30-Apr-03 5:42 
GeneralRe: STL algorithm Pin
User 988530-Apr-03 2:18
User 988530-Apr-03 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.