MrNoob 24 Posting Whiz in Training

I just downloaded Code blocks today since alot of people here said its cool etc I also have also msv8 it have everything msv8 has + it opens faster and got gcc compiler so I vote code blocks for msv8 you need like fast computer i got alrdy 3 mb of ram at my laptop and it still opens slow

MrNoob 24 Posting Whiz in Training

thanks a lot guys for helping me out everytime

MrNoob 24 Posting Whiz in Training

but fgets reads strings can it read ints 2 ?maybe if i read a string and convert to int by atoi ?

MrNoob 24 Posting Whiz in Training

when i remove fflush(stdin) in some the scanf gets missed it moves to other instruction also thanks man I dont wanna use fflush but sometimes when i have many puts or printf some scanf gets missed is there a way to fix it without fflush(stdin) and for dollar variable i named it dollar coz i convert the pounds to dollar i m gonna fix that function 2 since i m calculating wrong for the shipping

MrNoob 24 Posting Whiz in Training

i mean the artichokes i know i did it 3 times in printf but now i fixed it it keeps reading 1st only either than that it doesnt read the other option

MrNoob 24 Posting Whiz in Training

it doesnt even read other input either than first input

MrNoob 24 Posting Whiz in Training

in my switch program keeps reading wrong input also i know i used way too much fflush(stdin); because stupid printf keeps inputting to the screen so scanf gets bad but i m sure i desinged program nice i dunno why when i like enter num 5 it keeps reading artichokes even though i made the default break;

#include <stdio.h>
//everything here is per pound ofcourse
#define artichokes 1
#define beets 2 
#define carrots 3
int ParseFees(int dollars,int type)
{
    char shipping;
    int pounds=dollars;
    dollars*=type;//to get dollar amount
    fputs("do you want the stuff shipped ?:_\n",stdout);
    fflush(stdin);
    shipping=getchar();
    toupper(shipping);
    if(shipping=='Y') {
         if(type==1) 
             dollars=(dollars + 8) + (type * 2);
         switch(pounds) {
            case 5:    
                 dollars+=3;
                 break;
            case 10:
                 dollars+=10;
                 break;
            }
         }
    return dollars;
}
int main(void)
{
    int groc;//variable used for gocery
    int pounds;
    int dollars=0;
    for(;;) {
         puts("Hello Sir May I help you with gocery?\n"
              "Here is the Menu\n"
              "1)artichokes for $1 per pound\n"
              "2)beets for 2 $ per pound\n"
              "3)carrots for 3 $ per pound\n"
              "\nNotice: We give 5 % discount for orders over 100 $\n"
              "\n2nd Notice: enter other value to quit the programe and know how many dollars you owe\n"
              );
         
         switch(scanf("%d",&groc)) {
              case 1:
                   fputs("how many pounds you want? of artichokes",stdout);
                   fflush(stdin);
                   scanf("%d",&pounds);
                   dollars+=ParseFees(pounds,artichokes);//rest of here will handle groceries etc
                   continue;
              case 2:
                   fputs("how many pounds you want? of artichokes",stdout);
                   fflush(stdin);
                   scanf("%d",&pounds);
                   dollars+=ParseFees(pounds,artichokes);//rest of here will handle groceries etc
                   continue;
              case 3:
                   fputs("how many pounds you want? of artichokes",stdout);
                   fflush(stdin);
                   scanf("%d",&pounds);
                   dollars+=ParseFees(pounds,artichokes);//rest of here will handle …
MrNoob 24 Posting Whiz in Training

you can some of its algothrim for your mp3 compression

MrNoob 24 Posting Whiz in Training

why dont you research an algothrim for image i think zip and one other is open source

MrNoob 24 Posting Whiz in Training

also if your using win its better to use dev or msv6

jephthah commented: no. sorry. -2
tux4life commented: absolutely wrong -2
MrNoob 24 Posting Whiz in Training

lolz nvm i did wrong stuff acctually i didnt intilise sum to anything so it was += the memory it had lolz

#include <stdio.h>
#define SIZE 8
void Parsethem(int *num,int *sum,int i)
{
     int x=i;
     for(x=0;x<i;x++) 
          sum[i]+=num[i];
}
int main(void)
{
    int num[SIZE];
    int sum[SIZE]={0};
    int i;
    int x=2;
    for(i=0;i<8;i++) {
        num[i]=x;
        x*=2;
    }
    for(i=0; i< 8 ;i++)
        Parsethem(num,sum,i);
    puts("thats sum\n");
    for(i=0 ; i<8 ; i++)
       printf("%d ",sum[i]);
    puts("\nthose are normal nums\n");
    for(i=0;i<8;i++)
        printf("%d ",num[i]);
    getchar();
    return 0;
}
       
/* with normal fashion we do this by lets say array[0] we just that so array[1] sum[1]=array[0] + array[1] */

i m sorry for this lame post :P

MrNoob 24 Posting Whiz in Training

hello i coding a program that need to sum array according to its num
like for example num[0] will only do that and num[1]
sum[1]=num[0] +num[1];
sum[2]=num[0] +num[1] +num[2]; etc
i got one idea first i will fill the num array with numbers
then i go through after that with a for loop to fill the nums so like 0 it will only run 1 time and 1 will run 2 times etc
but code i think it returns memory addr not nums i think
anyways here my code

#include <stdio.h>
#define SIZE 8
void Parsethem(int *num,int *sum,int i)
{
     int x=i;
     for(x=0;x<i;x++) 
          sum[i]+=num[i];
}
int main(void)
{
    int num[SIZE];
    int sum[SIZE];
    int i;
    int x=2;
    for(i=0;i<8;i++) {
        num[i]=x;
        x*=2;
    }
    for(i=0; i< 8 ;i++)
        Parsethem(num,sum,i);
    for(i=0 ; i<8 ; i++)
       printf("%d\n",sum[i]);
    getchar();
    return 0;
}
       
/* with normal fashion we do this by lets say array[0] we just that so array[1] sum[1]=array[0] + array[1] */
MrNoob 24 Posting Whiz in Training

why dont you put an array with stuff you want and extract that to new array?

MrNoob 24 Posting Whiz in Training

well thats just small program since i m coding that in dev but with big programs i code in other compilers like msv8 i return 0;

MrNoob 24 Posting Whiz in Training

problem that i did x=i; whish set x to the same num as i everytime the loop counts

MrNoob 24 Posting Whiz in Training

ohh thanks alot man i got it to work yah i will divide stuff into smaller stuff so i get the result in the end

#include <stdio.h>
int main(void)
{
    int i,x;
    char c;
    for(i=0;i<6;i++) {
        c='F';
        for( x=0 ; x<=i; x++ ) {
            printf("%c ",c);
            c--;
            }
        puts("");
        }
    return getchar();
}
MrNoob 24 Posting Whiz in Training

Hello people i got a h.w to do Using nested loops to produce the following pattern:
F

FE

FED

FEDC

FEDCB

FEDCBA


I had idea to do it but that will require array and strlen(char first int of the outer loop) and the thing says that I must do it with char only
but here my code i made problem is that it need to intilise c='F' then its bigger than or = 'A' and i decrement it so it shows from F to A problem that there wont be any thing to intilise the inner loop to the outer integer or that may seem true to me maybe its wrong since i dont have much programming experience and i alawys sucks in nested loop anyways here code now

#include <stdio.h>
int main(void)
{
    int i,x;
    char c;
    for(i=0;i<6;i++) {
        for(c='F',x=i;c>='A' && x<=i;c-- , x++) {
            printf("%c ",c);
            }
        puts("");
        }
    return getchar();
}

as i think in inner loop i intilise c to f then c is bigger or = to a then i make x<=i whish will like first time 1 2 etc but shouldnt that also print
F then FE etc ? since x will be incremeted each time ?

MrNoob 24 Posting Whiz in Training

oh thanks

MrNoob 24 Posting Whiz in Training

shoudlnt it be like this "%s %s %s\n",string,string[28],string[num]); ?

MrNoob 24 Posting Whiz in Training

either than that i understand it all

MrNoob 24 Posting Whiz in Training

thanks mate but there 1 thing i dont understand when you sprintf the num whish is bigger than 100 it should like print 2 variables not 3 ? how it printed 3?

MrNoob 24 Posting Whiz in Training

i can make couple if if statement to fix that but that will take alot of tests since i want a converter till 999

MrNoob 24 Posting Whiz in Training

also making num % 20 will solve the problem but that will stay only till 119 !! after that it wont work

MrNoob 24 Posting Whiz in Training

no i acctually dont quite understand that i understand the daviding part but whats if h? and why would you - it with the h after that? anyways my code till now

#include <stdio.h>
#include <string.h>
#define DEBUG
char *string[]= {
     "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
     "sixten","sevten","eighteen","nineteen","twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","one hundreed",
     "two hundered","three hundered"
     };
//from int to array and return char string

/* n will be the  index of the array like if its twenty thirty fourty etc*/
/*n2 will be the reminder*/
#define LESSTHAN100 0
#define MORETHAN100 1
void ParseString(char *buffer1,char **buffer2,int n,int n2,int choice)
{
     if(!choice) {
         strcpy(buffer1,buffer2[n]);
         strcat(buffer1," ");
         strcat(buffer1,buffer2[n2]);
         }
     else {
         strcat(buffer1,buffer2[n]);
         strcat(buffer1," ");
         strcat(buffer1,"and");
         strcat(buffer1," ");
         strcat(buffer1,buffer2[n2]);
         }
}
char *IntToarray(int num)
{
     char *pch;
     int j;
     int z;
     static char temp[100];
     if(num>0 && num<=19){
              puts("we returned");
              return pch=string[num];
              }
     else if(num>19 && num <100) {
              
              j=num % 10;
              num=( num /10) +18;
              if(j==0) 
                  return pch=string[num];
              ParseString(temp,string,num,j,LESSTHAN100);
              return pch=temp;
              }
     else if(num>100 && num<=109) {
             j=num % 10;
             num= (num/10) +18;
             if(j==0)
                 return pch=string[num];
             ParseString(temp,string,num,j,MORETHAN100);
             return pch=temp;
             }
}
int main(void)
{
    char *pch;
    pch=IntToarray(109);
    puts(pch);
    getchar();
    return 0;
}
MrNoob 24 Posting Whiz in Training

but why when i made it static it worked i didnt return the acctual temp i return the char * ptr i returned ptr = ?

MrNoob 24 Posting Whiz in Training

even it start before 111 it start at 110 when i % it it will return the same num so there no search list with that num so it mess up the print

MrNoob 24 Posting Whiz in Training

yah i also made a mistake of || instead of && i wanted now i m stuck
after 111 it will as i said before get the reminder whish is like 111 reminder is 1 so it will print for example 111 one hundered and one I dunno how can i fix this...

MrNoob 24 Posting Whiz in Training

i did till now but i got problem now now it when i get to like 111 or more i will get reminder 1 etc so it wont be from one hundered eleven it will be one hundered 1

#include <stdio.h>
#include <string.h>
#define DEBUG
char *string[]= {
     "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
     "sixten","sevten","eighteen","nineteen","twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","one hundreed",
     "two hundered","three hundered"
     };
//from int to array and return char string

/* n will be the  index of the array like if its twenty thirty fourty etc*/
/*n2 will be the reminder*/
#define LESSTHAN100 0
#define MORETHAN100 1
void ParseString(char *buffer1,char **buffer2,int n,int n2,int choice)
{
     if(!choice) {
         strcpy(buffer1,buffer2[n]);
         strcat(buffer1," ");
         strcat(buffer1,buffer2[n2]);
         }
     else {
         strcat(buffer1,buffer2[n]);
         strcat(buffer1," ");
         strcat(buffer1,"and");
         strcat(buffer1," ");
         strcat(buffer1,buffer2[n2]);
         }
}
char *IntToarray(int num)
{
     char *pch;
     int j;
     int z;
     char temp[100];
     if(num==0 || num<=19){
              puts("we returned");
              return pch=string[num];
              }
     else if(num==20 || num <100) {
              j=num % 10;
              num=( num /10) +18;
              if(j==0) 
                  return pch=string[num];
              ParseString(temp,string,num,j,LESSTHAN100);
              puts(temp);
              }
     else if(num==100 || num<=999) {
             j=num % 10;
             num= (num/10) +18;
             if(j==0)
                 return pch=string[num];
             ParseString(temp,string,num,j,MORETHAN100);
             puts(temp);
             }
}
int main(void)
{
    char *pch;
    pch=IntToarray(41);
    //puts(pch);
    getchar();
    return 0;
}

anyways i will go sleep now i hope tommrow i will fix those errors.

MrNoob 24 Posting Whiz in Training

okay i almost done it for but when i return it to char * it messed it up

#include <stdio.h>
#include <string.h>
#define DEBUG
char *string[]= {
     "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
     "sixten","sevten","eighteen","nineteen","twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","hundreed"
     };
//from int to array and return char string

/* n will be the  index of the array like if its twenty thirty fourty etc*/
/*n2 will be the reminder*/
void ParseString(char *buffer1,char **buffer2,int n,int n2)
{
     strcpy(buffer1,buffer2[n]);
     strcat(buffer1," ");
     strcat(buffer1,buffer2[n2]);
}
char *IntToarray(int num)
{
     char *pch;
     int j;
     int z;
     char temp[100];
     if(num>0 || num<=19)
              pch=string[num];
     if(num==20 || num <100) {
              j=num % 10;
              num=( num /10) +18;
              if(j==0) 
                  return pch=string[num];
              ParseString(temp,string,num,j);
              //puts(temp); this work i dunno why it get messed  
              return pch=temp;//this doesnt work
              }
}
int main(void)
{
    char *pch;
    pch=IntToarray(21);
    puts(pch);
    getchar();
    return 0;
}
MrNoob 24 Posting Whiz in Training

i didnt read the other posts after i posted just now but i think my codes are just sloppy now i was thinking of like searching 20 30 40 50 60 but that will take alot of functions maybe i should write that into a function

MrNoob 24 Posting Whiz in Training

till now i made this

char *IntToarray(int num)
{
     char *pch;
     int j;
     char temp[100];
     if(num>0 || num<=19)
              pch=string[num];
     if(num>=20 || num<=100) {
              if(num>=20 || num <=29) {
                  j=num %10;
                  strcpy(temp,string[20]);
                  strcat(temp," ");
                  strcat(temp,string[j]);
                  puts(temp);
                  }
              }    
}
MrNoob 24 Posting Whiz in Training

i dunno how its possible to do it with 1 string only like if i get 20 or thirty fourty fifty etc etc i think that should be in a array itself so i get the reminder by j then after that cancnate it to the string2
then as for 100 and up i have to do extra work but i will figure that out i just wanna fix from 20 to 99 first then do from 100 to 999

MrNoob 24 Posting Whiz in Training

till now i made but i tried with 21 it didnt work out

#include <stdio.h>
#include <string.h>
#define DEBUG
char *string[]= {
     "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
     "sixten","sevten","eighteen","nineteen"
     };
char *string2[]= {
     "twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","hundreed"
     };
//from int to array and return char string
char *IntToarray(int num)
{
     char *pch;
     int j=0;
     if(num>0 || num<=19)
              pch=string[num];
     else if(num>=20 || num<=100) {
              j=num %10;
#ifdef DEBUG
              printf("number of j is %d",j);//it should out here 1 and it didnt 
              printf("number of j is %d",j);
              if(j==1)
                  puts("dude so cool");
#endif
              getchar();
              num=num %10;
              strcat(string2[num]," ");
              strcat(string2[num],string[j]);
              }    
}
int main(void)
{
    char *pch;
    pch=IntToarray(21);
    //puts(pch);
    getchar();
    return 0;
}
MrNoob 24 Posting Whiz in Training

but i still i would have to do tests from 1 to nineteen
whish would be alot of tests i thought of this idea but is there another way i could do it by

MrNoob 24 Posting Whiz in Training

I m trying to find idea to change like from 1 to one and 2 to two and 3 three from 1 to 999 i m had an idea to do like a string with information etc ? but i want better idea so i dunno how to do it :S lolz.

MrNoob 24 Posting Whiz in Training

c right ?
and you want to assign them to a buffer?

MrNoob 24 Posting Whiz in Training

thanks again mate.

MrNoob 24 Posting Whiz in Training

why dont you use strstr then use its return value to print that line ?

MrNoob 24 Posting Whiz in Training

here example if you wanna read 3 characters

#include <stdio.h>
#define REQUIRED 3
int main(void)
{
    char name[]="NAME";
    int i;
    for(i=0; i<REQUIRED ;i++)
        printf("%c",name[i]);
    //or if you want read full name
    for(i=0;name[i]!=0;i++)//read untill null terminator is reached
       printf("%c",name[i]);
    getchar( );
    return 0;
}

remeber it will only read it wont change to 3 characters only

MrNoob 24 Posting Whiz in Training

yah i felt really cool when i was able to solve it :D.btw do you advice me to continue with K&R or get another book to continue with ?

MrNoob 24 Posting Whiz in Training

but seriously man K&R need to write the excerises in better way it took me long time to understand the question itself

MrNoob 24 Posting Whiz in Training

i solved it :D thanks alot mate

#include <stdio.h>
unsigned int getx(int x,int p,int n)
{
     return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n)))));
}
unsigned int gety(int y,int p,int n)
{
    return ((y & ~(~0 << n)) << (p + 1 - n));
}
unsigned int setbits(int x,int y,int p,int n)
{
     x=getx(x,p,n);
     y=gety(y,p,n);
     return (unsigned int) x | y;
}
int main(void)
{
    printf("result is %d",setbits(85,238,6,5));
    return getchar();
}

dam i rlly hated this chapter took me 2 days to solve it after reading your post like 30 times

jephthah commented: good job keeping at it. +11
MrNoob 24 Posting Whiz in Training

but see now problem i tried to do the other exerises but i alawys end up with -1 as result

#include <stdio.h>
unsigned getbits(unsigned x, int p, int n)
{
    return (x >> (p+1-n)) & ~(~0 << n);
}
unsigned int invert(int x,int p , int n) {
    x=getbits(x,8,3);
    
    return x=~x;
}
int main(void)
{
    int result;
    result=invert(5,8,3);
    printf("%d",result);   
    return getchar();
}

first i get the bits according then i changed that to invert them then i returned it but i alawys get -1 as result

MrNoob 24 Posting Whiz in Training

i think i did it

#include <stdio.h>
int setbits(int x, int y,int p,int n)
{
    x = (x >> (p+1-n)) & ~(~0 << n);
    y = y & (~0 << n);
    return x | y;
}
int main(void)
{
    int x;
    x=setbits(10,9,6,3);
    printf("%d",x);
    return getchar();
}
MrNoob 24 Posting Whiz in Training

also in the previous example of the K&R you specify the num you read in the bits but this example you have to get from n till like 0
lets say you said from number 5 in the bits you have to read till num 0 but in the other example of k&r you specify from 5 to 2 but how i know when it ends lets say you put num 5 how i know if i end at 1 or 0 or whatever..
i know i dont make much sense but this is rlly confusing me normally im good with normal coding....

MrNoob 24 Posting Whiz in Training

but how can i assign those rightmost bits of y to rightmost of x ? is there somekind of specail loop i can go through to do that or i have to do it with bitwise operators?

MrNoob 24 Posting Whiz in Training

average =sum/howmany why dont you after you read the array davide the array directly by i instead of just writing up the number yourself will make it more compact

MrNoob 24 Posting Whiz in Training

okay i understand all bitwise operators but in the chapter of k&r at bitwise operators i m really lost at there question i have no idea how to do them
Exercise 2-6. Write a function setbits(x,p,n,y) that returns x with the n bits that begin at
position p set to the rightmost n bits of y, leaving the other bits unchanged.
Exercise 2-7. Write a function invert(x,p,n) that returns x with the n bits that begin at
position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.

3rd one was easy so i did it but i have no idea how to do those could anyone give me any idea how to code them ?

MrNoob 24 Posting Whiz in Training

nvm i didnt notice something in your quote thanks

MrNoob 24 Posting Whiz in Training

~(~0 << n) makes a bitmask of ones equal to the number 'n'. does this by first filling the integer full of ones, then shifting them to the left 'n' bits (filling lsb's with zeros), then inverts all the bits.

for instance, n=5 makes a bitmask = '...00000011111' x >> (p+1-n) shifts the original number to the right 'p+1-n' bits.

the whole point is to extract the bits from the original value, starting with the zero-indexed bit # at 'p' (pointer) and extracting 'n' number of bits moving to the right.

the two are ANDed together to get the new, extracted, value

example value = 0x55 (01010101) with p=6 and n=5 will extract bits 6 through bit 2 (total of 5 bits), to find the new value 0x15, (dec 21, or '10101')

01010101
p=6  ^
n=5  .....
     10101   val = 0x15 (d 21)

but srry 1 more question why would it exract to 2 shoudlnt it exract from 6 till the end ?