Thread: A Complete Beginner With a Big Probelm

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    A Complete Beginner With a Big Probelm

    so i have to make a program that displays only prime numbers that are palindromes, im supplied with this code
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <errno.h> 
    #include <string.h>
                       
     FILE *outputFile; 
     char buffer[81];  
     int bytesWritten; 
                       
    main()             
    {                  
     int isPrime(int); 
    
     int i,j;                                                
                                                             
                                                             
    /*************************************/                  
    /*     Open file to write output     */                  
    /*************************************/                  
    outputFile = fopen("DD:OUTPUT", "w");                    
    if (outputFile == NULL)                                  
    {                                                        
     printf("open error:   %d/%s\n", errno, strerror(errno));
     exit(99);                                               
    }                                                        
                                                             
    /*************************************/                  
    /*     Run program                   */                  
    /*************************************/                  
                                                             
     for (i=1; i<15000; i++)                                 
     
      {                                                     
        if (isPrime(i)==1)                                  
        {                                                   
         bytesWritten = sprintf(buffer,"%d is prime!!\n",i);
         fwrite(buffer, 1, bytesWritten, outputFile);       
        }                                                   
      }                                                     
                                                            
     /*************************************/                
     /*     Close output file             */                
     /*************************************/                
     fclose(outputFile);                                    
                                                            
      return 0;                                             
    }                                                       
                                                            
    int isPrime (int myInt)                                 
    {                                                       
    
     int loop;                               
                                             
     for (loop = 2; loop < myInt/2+1; loop++)
     {                                       
       if (myInt%loop==0)                    
         return 0;                           
     }                                       
       return 1;                             
    }
    now my problem is i have literally never used C before and the reference material supplied is about 1000 pages long and i have no clue where to begin looking, when the program runs this only displays prime numbers i need to modify the code so that it it to only display prime numbers that are palindromes ANY help at all would be appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    So write a function that checks if a number is a palindrome.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Code:
     
    int is_palindrome(unsigned long orig)
    {
      unsigned long reversed = 0, n = orig;
    
      while (n > 0)
      {
        reversed = reversed * 10 + n % 10;
        n /= 10;
      }
    
      return orig == reversed;
    }
    okay well this lists all the palindromes now how do i go about combining them so it lists only the palindromic prime numbers

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So you want to check if a number is prime and it is a palindrome. How might you put that into code?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This looks like a likely spot doesn't it?

    Code:
    /*************************************/                  
    /*     Run program                   */                  
    /*************************************/                  
                                                             
     for (i=1; i<15000; i++)                                 
     
      {                                                     
        if (isPrime(i)==1)                                  
        {                                                   
          < cough, cough >
         bytesWritten = sprintf(buffer,"%d is prime!!\n",i);
         fwrite(buffer, 1, bytesWritten, outputFile);       
        }                                                   
      }

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Quote Originally Posted by anduril462 View Post
    So you want to check if a number is prime and it is a palindrome. How might you put that into code?
    I'm not sure that's exactly my problem

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    I will try this when I get home thank you
    Quote Originally Posted by Adak View Post
    This looks like a likely spot doesn't it?

    Code:
    /*************************************/                  
    /*     Run program                   */                  
    /*************************************/                  
                                                             
     for (i=1; i<15000; i++)                                 
     
      {                                                     
        if (isPrime(i)==1)                                  
        {                                                   
          < cough, cough >
         bytesWritten = sprintf(buffer,"%d is prime!!\n",i);
         fwrite(buffer, 1, bytesWritten, outputFile);       
        }                                                   
      }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ayoonisito View Post
    I'm not sure that's exactly my problem
    I am. That's exactly what your problem description said in your first post:
    Quote Originally Posted by ayoonisito View Post
    so i have to make a program that displays only prime numbers that are palindromes
    It would be equivalent to Adak's solution, but expressed in one if statement with a compound condition, instead of two nested if statements. The hard part of determining whether something is prime and whether something is a palindrome is done. You just have to combine those two conditions.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    i get that i have to combine the two conditions but how exactly do i do that i put the palindrome condition where Adak said to put it but it just returns 0 when i do
    Quote Originally Posted by anduril462 View Post
    I am. That's exactly what your problem description said in your first post:

    It would be equivalent to Adak's solution, but expressed in one if statement with a compound condition, instead of two nested if statements. The hard part of determining whether something is prime and whether something is a palindrome is done. You just have to combine those two conditions.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Post your updated code so we can see what you did, and then we can tell you why it doesn't work and how to fix it.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    heres what i tried it doenst return anything just 0. i
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <errno.h> 
    #include <string.h>
                        
     FILE *outputFile; 
     char buffer[81];  
     int bytesWritten; 
                        
    main()             
    {                  
     int isPrime(int); 
     
     int i,j;                                                
                                                              
                                                              
    /*************************************/
    /*     Open file to write output     */
    /*************************************/
    outputFile = fopen("DD:OUTPUT", "w");                    
    if (outputFile == NULL)                                  
    {                                                        
     printf("open error:   %d/%s\n", errno, strerror(errno));
     exit(99);                                               
    }                                                        
                                                              
    /*************************************/
    /*     Run program                   */
    /*************************************/
                                                              
     for (i=1; i<15000; i++)                                 
      
      {                                                     
        if (isPrime(i)==1)                                  
        {
          
    int is_palindrome(unsigned long orig)
    {
      unsigned long reversed = 0, n = orig;
     
      while (n > 0)
      {
        reversed = reversed * 10 + n % 10;
        n /= 10;
      }
     
      return orig == reversed;
    }
    
                                                 
         bytesWritten = sprintf(buffer,"%d is a palindromic prime!!\n",i);
    
         fwrite(buffer, 1, bytesWritten, outputFile);       
        }                                                   
      }                                                     
                                                             
     /*************************************/
     /*     Close output file             */
     /*************************************/
     fclose(outputFile);                                    
                                                             
      return 0;                                             
    }                                                       
                                                             
    int isPrime (int myInt)                                 
    {                                                       
     
     int loop;                               
                                              
     for (loop = 2; loop < myInt/2+1; loop++)
     {                                       
       if (myInt%loop==0)                    
         return 0;                           
     }                                       
       return 1;                             
    }

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You can't put functions inside other function in C. Adak did not intend for you to put the whole function there, just to call the function at that point.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    what exactly do you mean by call? im sorry for these incredibly basic quesitons but like i said im a complete beginner.
    Quote Originally Posted by anduril462 View Post
    You can't put functions inside other function in C. Adak did not intend for you to put the whole function there, just to call the function at that point.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ayoonisito View Post
    what exactly do you mean by call? im sorry for these incredibly basic quesitons but like i said im a complete beginner.
    Then get out your class notes, crack open your text book and start reading. Work some examples. Look at some tutorials online. We have one here, and Google will turn up lots more. Use the code you were given as an example of what to do.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    no class notes or textbook i actually have no idea why this was assigned other then it pertains to computers and the class im taking pertains to computers, if i had any background at all it would help but i dont have any background in c so im pretty lost. he literally assigned it and hasnt talked about it since, its an information and technology course NOT a programming course
    Quote Originally Posted by anduril462 View Post
    Then get out your class notes, crack open your text book and start reading. Work some examples. Look at some tutorials online. We have one here, and Google will turn up lots more. Use the code you were given as an example of what to do.
    Last edited by ayoonisito; 10-25-2011 at 04:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Complete beginner needs help customising sorting code
    By Ian SH in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2007, 07:09 PM
  2. Complete Beginner.
    By nubby in forum C Programming
    Replies: 14
    Last Post: 10-02-2007, 07:00 AM
  3. Help out a complete beginner
    By Jmcrofts in forum C++ Programming
    Replies: 7
    Last Post: 07-28-2007, 12:09 PM
  4. Complete Beginner [Need Help]
    By Vintik in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2005, 05:08 PM
  5. Complete Beginner, HELP!!
    By britneyspy in forum C++ Programming
    Replies: 19
    Last Post: 06-12-2003, 11:01 AM