c programing help

This is a discussion on c programing help within the C Programming forums, part of the General Programming Boards category; The Palindromes A string is said to be palindrome, if it reads same from both ends, i.e, from left-to-right and ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Cool c programing help

    The Palindromes
    A string is said to be palindrome, if it reads same from both ends, i.e, from left-to-right and from
    right-to-left. For example, “MALAYALAM” is a palindrome string. The letters are symmetrical at
    both ends.
    Similarly, a numeral palindrome has corresponding digits from the both ends symmetrical. So, on
    reversing the digits of the number, we get the same number itself. Thus, 23432 is a palindrome
    number. Further, some numbers have the property that they are palindrome in both decimal, and
    binary bases. One such example is the number 58510 = 10010010012
    We see that both 585 and 1001001001 are palindromes.
    Task
    In a given range, you have to find out those numbers which are palindrome in both decimal and
    binary bases, and report their decimal sum.
    Input
    The first line of the input will have a single integer t (1 <= t <= 50), the number of test cases. Then,
    t test cases will follow. Each test case will consist of two numbers m and n, which will define the
    lower limit and the upper limit of the range (m, n <= 10000000 and m < n).
    Output
    The output will contain t lines with exactly one number on each line, the sum of all the palindromes
    (in both decimal and binary bases) that lie in the given range [m, n].

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Please read and follow the homework policy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    void paliendrome(int a,int b)
    {
    int m;
    for (i=a;i<=b;i++)
    {
    m=convertdecimaltobinary(i);//converts the decimal no to it s binary equivalent
    checkpaliendrome(m);//checks whether the binary no is a paliendrome or not
    }
    }
    int  convertdecimaltobinary(int x)
    {//***************please help me with this part **************
    }
    void checkpaliendrome(int y)
    {
    int k=y,new=0;
    int r=y;
    int u,v,c=0;//c counts the number of digits 
    while(k>0)
    {
    u=k%10;
    k=k/10;
    c=c+1;
    }
    while (r>0)
    {
    u=r%10;
    v=pow(10,c-1)*u ;
    new=new+v;
    c--;
    }
    if (new ==y)
    { printf("%d",y);
    }
    }
    
    //both decimal and binary equivalents have to be paliendromes
    
    void main()
    {
    int t,u,l;
    scanf("%d",&t);
    int i ;
    //t stands for the no of cases
    for(i=0;i<=t;i++)
    {
    scanf("upperrange",&u);
    scanf("lowerrange",&l);
    paliendrome(u,l);
    }

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    This problem is quite simple . . .

    Assuming you're working a maximum size of, say, 32. (unsigned long int, in other words)


    (yes, I *know* that long is the same thing as int on most modern systems.)
    Code:
    char *convert_to_binary(unsigned long number) {
        unsigned long bitmasks[32] = {0x01};
        int x;
        char *result = malloc(sizeof(char) * 33);
        for(x = 1; x < 32; x ++) {
            bitmasks[x] = bitmasks[x-1] * 2;
        }
        for(x = 0; x < 32; x ++) {
            result[x] = (number & bitmasks[x])?'1':'0';
        }
        return result;
    }
    Not that there is insufficient error checking etc. in there.

    Also, I haven't tried it, so I'm not 100% certain if it'll work, but that should work.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Code:
    there is one error in your code ---  type mismatch in redeclaration of checkpaliendrome
    
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    void paliendrome(int a,int b)
    {
    int m;
    for (i=a;i<=b;i++)
    {
    m=convertdecimaltobinary(i);//converts the decimal no to it s binary equivalent
    checkpaliendrome(m);//checks whether the binary no is a paliendrome or not
    }
    }
    int  convertdecimaltobinary(int x)
     {
        unsigned long bitmasks[32] = {0x01};
        int x;
        char *result = malloc(sizeof(char) * 33);
        for(x = 1; x < 32; x ++) {
            bitmasks[x] = bitmasks[x-1] * 2;
        }
        for(x = 0; x < 32; x ++) {
            result[x] = (number & bitmasks[x])?'1':'0';
        }
        return result;
    }
    
    void checkpaliendrome(int y)
    {
    int k=y,new=0;
    int r=y;
    int u,v,c=0;//c counts the number of digits 
    while(k>0)
    {
    u=k%10;
    k=k/10;
    c=c+1;
    }
    while (r>0)
    {
    u=r%10;
    v=pow(10,c-1)*u ;
    new=new+v;
    c--;
    }
    if (new ==y)
    { printf("%d",y);
    }
    }
    
    //both decimal and binary equivalents have to be paliendromes
    
    void main()
    {
    int t,u,l;
    scanf("%d",&t);
    int i ;
    //t stands for the no of cases
    for(i=0;i<=t;i++)
    {
    scanf("upperrange",&u);
    scanf("lowerrange",&l);
    paliendrome(u,l);
    }
    
    
    please tell me how will correct it
    Last edited by hot love; 03-20-2009 at 04:09 PM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    i have added indentetion to the code and some questions in the comments
    Code:
    //conio.h is not standard
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    
    void paliendrome(int a,int b)
    {
    	int m;
    	for (i=a;i<=b;i++)
    	{
    		m=convertdecimaltobinary(i);//converts the decimal no to it s binary equivalent
    		checkpaliendrome(m);//checks whether the binary no is a paliendrome or not
    	}
    }
    int  convertdecimaltobinary(int x)
    {
    	unsigned long bitmasks[32] = {0x01};
    	int x; /* this local var hides the past parameter */
    	/* do you see the type of result? */
    	char *result = malloc(sizeof(char) * 33);
    	for(x = 1; x < 32; x ++) {
    		bitmasks[x] = bitmasks[x-1] * 2; /* what do you think this is doing? */
    	}
    	for(x = 0; x < 32; x ++) {
    		result[x] = (number & bitmasks[x])?'1':'0';
    	}
    	return result; /* you trying to return char* while return type is int */
    }
    /* are you checking binary or decimal palindrom? */
    void checkpaliendrome(int y)
    {
    	int k=y,new=0;
    	int r=y;
    	int u,v,c=0;//c counts the number of digits 
    	while(k>0)
    	{
    		u=k%10;
    		k=k/10;
    		c=c+1;
    	}
    	while (r>0)
    	{
    		u=r%10;
    		v=pow(10,c-1)*u ;
    		new=new+v;
    		c--;
    	}
    	if (new ==y)
    	{ printf("%d",y);
    	}
    }
    
    //both decimal and binary equivalents have to be paliendromes
    
    /* main should be int main(void) - read FAQ */
    void main()
    {
    	int t,u,l;
    	scanf("%d",&t);
    	int i ;
    	//t stands for the no of cases
    	for(i=0;i<=t;i++)
    	{
    		scanf("upperrange",&u); /* you have no format specifier here */
    		scanf("lowerrange",&l);
    		paliendrome(u,l); 
    	}
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing for DNA...
    By S16 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 10:25 AM
  2. Programing microcontrollers in c
    By gorabhat in forum C Programming
    Replies: 2
    Last Post: 09-09-2008, 11:40 AM
  3. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 05:48 PM
  4. Non programing Question
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 06-21-2002, 07:55 AM
  5. C++ Programing
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2001, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21