MrNoob 24 Posting Whiz in Training

thanks i understand it now...

MrNoob 24 Posting Whiz in Training

Hello i m reading chapter in K&R about bit operation whish totally confuses me i understand what all operators does but i dont understand the code itself

unsigned getbits(unsigned x, int p, int n)
{
    return (x >> (p+1-n)) & ~(~0 << n);
}

it will right shift p+1-1 then after that i dont understand

MrNoob 24 Posting Whiz in Training

Well in that case say you have 6 characters in str array,then assuming that you overwrite first three characters in the exact way in which you want even then you are doing nothing with the left over's.
Well change your code as this :

#include <stdio.h>
void Squeeze2(char *str,char *save,char *required)
{
     int i,x;
     int buffer=0;//used to save stuff
// And ya make an habit of using null character rather than simple 0
     for( i=0 , x=0 ; str[i]!='\0' && save[x]!='\0' ; i++, x++ ) {
          if( str[i]!=save[x] ) {
              required[buffer]=str[i];
              buffer++;
              }
          }
}
int main(void)
{
    char name[]="NAME";
    char req[10];
    Squeeze2(name,"NAM",req);
    puts(req);
    getchar();
    return 0;
}

oh thanks man but coudlnt i change direct to buffer?

MrNoob 24 Posting Whiz in Training

and yes i wrote it
well first i read both string then after that make a test saying every characters thats not in search and i do new string in str[buffer].

MrNoob 24 Posting Whiz in Training

well i m trying to delete every character from String2 and delete that in string1

MrNoob 24 Posting Whiz in Training

i dunno im sure this output the good result i dont understand why it got not the output that i want

#include <stdio.h>
void Squeeze2(char *str,char *save)
{
     int i,x;
     int buffer=0;//used to save stuff
     for( i=0 , x=0 ; str[i]!=0 && save[x]!=0 ; i++, x++ ) {
          if( str[i]!=save[x] ) {
              str[buffer]=str[i];
              buffer++;
              }
          }
}
int main(void)
{
    char name[]="NAME";
    Squeeze2(name,"NAM");
    puts(name);
    return getchar();
}
MrNoob 24 Posting Whiz in Training

nice code mate yah i also wanted to ask i see alot people doing while(*s!=0) that mean it will keep running till it reach 0 ?

MrNoob 24 Posting Whiz in Training

fixed it

unsigned int htoi(char *HString)
{
     int i,Hexo,n=0;
     for(i=0;HString[i]!=0;i++) {
         if(HString[i]<='A' && HString[i]>='F') {
			Hexo= HString[i] - '0';
			n = 16 * n + Hexo;
        }
		else if(HString[i] >='a' && HString[i] <='f') {
			Hexo= HString[i] -'a' + 10;
			n = 16 * n + Hexo;
        }
		else if(HString[i] >='A' && HString[i] <='F') {
			Hexo= HString[i] -'A' + 10;
			n = 16 * n + Hexo;
            }
      }
     return n;
}
MrNoob 24 Posting Whiz in Training

srry i didnt notice your talking about indexed search i think i know how to do it now...

MrNoob 24 Posting Whiz in Training

i think i made it but its not so compact just bunch of test will work only from A to F but if you add like AA or 2 FF or others wont work but i hope i will fix it l8er

#include <stdio.h>
int htoi(char *HString)
{
    int i;
    int n=0,n2;
    for(i=0;HString[i]!=0;i++) {
        if(HString[i]=='A'|| HString[i]=='a')
           n+=10;
        if(HString[i]=='B' || HString[i] == 'b')
           n+=11;
        if(HString[i]=='C' || HString[i]=='c')
           n+=12;
        if(HString[i]=='D' || HString[i]=='d')
           n+=13;
        if(HString[i]=='E' || HString[i]=='e')
           n+=14;
        if(HString[i]=='F' || HString[i]=='f')
           n+=15;
        else if(HString[i]<='0' && HString[i]>='9')
            n+=HString[i];
        }
    return n;
}
int main(void)
{
    char name[]="F";
    int x;
    x=htoi(name);
    printf("%d",x);
    return getchar();
}
MrNoob 24 Posting Whiz in Training

but is there a equation to change the characters hex to decimal because sometimes if a string got F or A it will just change that to its ascii value.

MrNoob 24 Posting Whiz in Training

thanks i will try

MrNoob 24 Posting Whiz in Training

i m trying to code htoi function in chapter 2.3 for K&R but problem i dunno where to start should i first read all string then after that change each character to its decimal and then add them ? or any better ideas?

MrNoob 24 Posting Whiz in Training

thanks alot guys for helping me out

MrNoob 24 Posting Whiz in Training

thnaks man but i got one more question i made also a find biggest and smallest but it alawys get last element

#include <stdio.h>
#define BIGGEST 1
#define Smallest 0
int FindBiggest(int *arr,int choice)
{
    int i;
	int num=0;
	for(i=0;i<3;i++) {
		if(choice){//find BIGGEST NUM
		    if(arr[i]>num)
			    num=arr[i];
		}
		else
		{
			num=1000;
			if(arr[i]<num)
				num=arr[i];
		}

	}
	return num;
}    
int main(void)
{
    int arr[]={0,5,3};
    int result;
	result=FindBiggest(arr,Smallest);
	printf("%d\n",result);
    getchar();
    return 0;
}
MrNoob 24 Posting Whiz in Training

i made it work now but what the diff between array and just normal i?

#include <stdio.h>
int FindBiggest(int *arr)
{
    int i;
	int num=0;
	for(i=0;i<3;i++) {//used to be for(i=0;array[i<3;i++)
		    if(arr[i]>num)
			    num=arr[i];
	}
	return num;
}    
int main(void)
{
    int arr[]={0,5,3};
    int result;
	result=FindBiggest(arr);
	printf("%d\n",result);
    getchar();
    return 0;
}

in first line in the for loop ?

MrNoob 24 Posting Whiz in Training

yah i changed that i also changed in function the if better and still not working

#include <stdio.h>
int FindBiggest(int *arr)
{
    int i;
	int num=0;
	for(i=0;arr[i]<=3;i++) {
		    if(arr[i]>num)
			    num=arr[i];
	}
	return num;
}    
int main(void)
{
    int arr[]={0,5,3};
    int result;
	result=FindBiggest(arr);
	printf("%d\n",result);
    getchar();
    return 0;
}

now ofcourse 1st num is bigger than 0 whish should set num to 5 whats wrong now :S?

MrNoob 24 Posting Whiz in Training

I m trying to find the biggest number in the array but problem is that it keeps returning one

#include <stdio.h>
int FindBiggest(int *arr,int choice)
{
    int i;
	int num=0;
	for(i=0;arr[i]<=sizeof arr;i++) {
		    if(num<arr[i])
			    num=arr[i];
	}
	return num;
}    
int main(void)
{
    int arr[]={0,5,3};
    int result;
	result=FIND_NUM(arr,BIGGEST);
	printf("%d\n",result);
    getchar();
    return 0;
}

even though in my function i set the number to 0 first then after that we check with the if num<array whish ofcourse it is now it we set the num to array untill there no we found our biggest num i dunno its problem :s