Thread: How do I adapt a snippet to this program?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Question How do I adapt a snippet to this program?

    I have a program that tells if a number from 1 to 100 is "happy" or not. (look it up if you don't know what that is).
    I want to change it so it gives the prime happy numbers and doesn't say the ones that aren't happy and/or prime. I found a "snippet" of code that tests if a number is prime, and I need to know how to adapt it into the code...

    Note: The parts with the opening and closing of folders isn't important to look at. It's not really as much code as it seems.
    This is the Happy Numbers Prgm:
    Code:
     
    
    #include <stdio.h>                                              
    #include <stdlib.h>                                             
    #include <errno.h>                                              
    #include <string.h>                                             
                                                                    
     FILE *outputFile;                                              
     FILE *inputFile;                                               
     char buffer[81];                                               
     int bytesWritten;                                              
                                                                    
    main()                                                          
    {                                                               
     void isHappy(int);                                             
     int addSquareDigits(int);                                      
                                                                    
      int i;                                                        
                                                                    
                                                                    
     /*************************************/                        
     /*     Open file to write output     */                        
     /*************************************/                        
     outputFile = fopen("DD:OUTPUT", "w");                          
     if (outputFile == NULL)                                        
     {                                                              
      printf("open error:   %d/%s\n", errno, strerror(errno));      
      exit(99);                                                     
     }                                                              
                                                                    
     /*************************************/                        
     /*     Open file to read input       */                        
     /*************************************/                        
     inputFile = fopen("DD:INPUT", "r");                            
     if (inputFile == NULL)                                         
     {                                                              
      printf("open error:   %d/%s\n", errno, strerror(errno));      
      exit(98);                                                     
     }                                                              
                                                                    
     /*************************************/                        
     /*     Run program                   */                        
     /*************************************/                        
                                                                    
      while (fscanf(inputFile,"%d",&i) != EOF) {                    
        isHappy(i);                                                 
      }                                                             
                                                                    
     /*************************************/                        
     /*     Close output file             */                        
     /*************************************/                        
     fclose(outputFile);                                            
                                                                    
     /*************************************/                        
     /*     Close input file              */                        
     /*************************************/                        
     fclose(inputFile);                                             
                                                                    
      return 0;                                                     
    }                                                               
    int addSquareDigits (int myInt)                                 
    {                                                               
     int digit = 0;                                                 
     int addMe = 0;                                                 
     int newInt = 0;                                                
     do                                                             
     {                                                              
      digit = myInt % 10;                                           
      addMe = digit*digit;                                          
      newInt = newInt + addMe;                                      
      myInt = (myInt - digit)/10;                                   
     } while (myInt>0);                                             
     return newInt;                                                 
    }                                                               
                                                                    
    void isHappy (int myInt)                                        
    {                                                               
     int phases[50];                                                
     int loopy=0;                                                   
     int newInt = myInt;                                            
     phases[1]=myInt;                                               
     int i,j;                                                       
                                                                    
                                                                    
     for (i = 2; (i < 50 && loopy==0); i++)                         
     {                                                              
      newInt = addSquareDigits(newInt);                             
      phases[i]=newInt;                                             
      if (newInt==1) {                                              
       bytesWritten = sprintf(buffer,"%d is happy! :)\n",myInt);    
       fwrite(buffer, 1, bytesWritten, outputFile);                 
       loopy=1;                                                     
      }                                                             
      else {                                                        
      for (j=1; j<i; j++)                                           
      {                                                             
       if (newInt==phases[j])                                       
       {                                                            
        bytesWritten = sprintf(buffer,"%d is unhappy :(\n",myInt);  
        fwrite(buffer, 1, bytesWritten, outputFile);                
       /* writeToFile(myInt+" is unhappy :("); */                   
        loopy=1;                                                    
       }                                                            
      }                                                             
      }                                                             
     }                                                              
     return;                                                        
    }
    I'm not an expert at this type of code, so I'm not sure how to do much of this. I know I need to get rid of where it prints if it's happy and replace that with the test for being prime code. I also need to get rid of the printing of unhappy. The following snippet for the prime # test needs to replace the printing of "# is happy" and test it.

    Prime Test code:
    Code:
    #include <stdio.h>
    #include <getopt.h>
    #include <stdlib.h>
    
    int isprime(int value);
    
    int main(int argc, char *argv[]) {
     int number = 0; 
    
     if(argc != 2) {
      fprintf(stderr, "Usage: isprime INT\n");
      return 1;
     } else
      number = atoi(argv[1]);
    
     if(isprime(number) == 0)
      printf("%2d is NO prime\n", number);
     else
      printf("%2d is a prime...\n", number);
    
     return 0;
    }
    
    int isprime(int value) {
     int retval = -1;
     int i = 0;
    
     for(i = 2; i <= value / 2; i++) {
      if(value % i == 0) {
       retval = 0;
       break;
      }
     }
    
     return retval;
    }
    Notice the variables are different... those need to be adapted too. Any help?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Step 1: Read the snippet.
    Step 2: Understand what each line of the snippet does.
    Step 3: Use that understanding, and your knowledge of what needs to be done in your main program, to decide whether (and if so, where) that bit of code belongs in your program.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Well put tabstop!
    Excalty fully understand each line, condition,loop, and variable that applies to your design.. Test the snippet with features you don't want(by deleting them) .. then use that code to mesh into your own code..

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    just add the isprime function in ur file and instead of calling isHappy just call the function isprime

    Code:
    main()                                                          
    {                                                               
    // void isHappy(int);  
    isprime() //// this will be working i hope:)

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    It has to call it after it is determined to be a happy number.
    What if I delete the printing of the unhappy's (the whole 'else' statement), and before it prints '# is happy', I call the isprime() code...
    Also: "myInt" is what I have to see if it's prime, so do I put "isprime(myInt)", or do I change it to "myInt" in the isprime() procedure, or both? And if I copy the procedure code from the isprime test, do I get rid of the "#include" stuff at the beginning of it b/c it's already in there, and do I have to rename the main() procedure to something else b/c there's already a main() procedure?
    Should it look like this?:

    Code:
    for (i = 2; (i < 50 && loopy==0); i++)                         
     {                                                              
      newInt = addSquareDigits(newInt);                             
      phases[i]=newInt;                                             
      if (newInt==1) {                                              
       isprime(myInt) ////or isprime()?//
       bytesWritten = sprintf(buffer,"%d is a happy prime! :)\n",myInt);    
       fwrite(buffer, 1, bytesWritten, outputFile);                 
       loopy=1;                                                     
      }
    I still have to probably change the code in the isprime(), so would "int value" be the thing to change to "myInt"? Am I getting this?
    Please try to answer all the questions if you can.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    nvr mnd. I just need to fugure out how to change it to add three header lines before the output now.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    1
    mbomb007,
    did you figure it out? can you share the solution?

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Talking

    Do you think I'm stupid? Your username says "master - the - main". This is obviously referring to the mainframe contest that I, too, am competing in! (If I am wrong, and it was coincidental, just tell me.)
    None of the posts were helpful, and so I proceeded to figure it out using the available resources and my own skill. The answer is not important.
    I pretty much found enough info online and used my own skill to fugure it out pretty well.

    Sorry for being so harsh, but this IS a contest that tests your OWN skills. If you want your resumé submitted (which is the reward for Part 2), then you need to deserve the job.

    I won't post the code of my answer just in case someone would use it for their own advantage. And the answer isn't really close to what I was getting at anyway. Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM