Like Tree1Likes

programme, prime factorisation

This is a discussion on programme, prime factorisation within the C Programming forums, part of the General Programming Boards category; Hey! I'm a complete beginner and I want to write a programme which does the prime factorisation of numbers. The ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    programme, prime factorisation

    Hey! I'm a complete beginner and I want to write a programme which does the prime factorisation of numbers. The numbers are in a file. The prime factorization should be in a new file. I have written everything and the programme successfully debugged but the window opened and closed. I don't know what's wrong with it.

    This is a part of my code
    Code:
    {
                          int number_ofnumbers=0;
                          char c;
                          fscanf(f,"%c", &c);
                          if (c=='\0')
                          number_ofnumbers++;
    
                          int a[number_ofnumbers];
                          int i;
                          for (i=0; i<number_ofnumbers; i++)
                          {
                              int j;
                              int k=0;
    
    
                              int b[100];
    
                              if (a[i]>1)
                              {
                                  for(j=2; j<=sqrt(a[i]); j++)
                                    while ((a[i]% j) == 0)
                                      {
                                         if (sqrt(a[i]) != j)
                                         {
                                             k++;
                                             b[k] = a[i];
                                         }
                                         else
                                         {
                                          k++;
                                          b[k] = j;
    
                                         }
                                      }
    Could anyone look whether it's ok or not? I'd be very thankful.
    Attached Images Attached Images  

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    You mean that the output window closes too soon? You could write a getchar(); before the end of main, so that the window waits for a key to be pressed before closes.

    But you should look at your file and not your window I guess

    Also, welcome to the forum!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    well hard to duplicate what is going on, if you dont post the entire code so we can run it, kinda common sense there.....

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Here's the whole code:
    Code:
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    int main()
    {
        FILE *f; 
        f = fopen("we.txt","r"); 
    
        if (f) {
                printf ("\The file connot be opened.\n");
                return -1;
                }
        while (!feof(f)) {
                          int number_ofnumbers=0;
                          char c;
                          fscanf(f,"%c", &c);
                          if (c=='\0')
                          number_ofnumbers++;
    
                          int a[number_ofnumbers];
                          int i;
                          for (i=0; i<number_ofnumbers; i++)
                          {
                              int j;
                              int k=0;
    
    
                              int b[100];
    
                              if (a[i]>1)
                              {
                                  for(j=2; j<=sqrt(a[i]); j++)
                                    while ((a[i]% j) == 0)
                                      {
                                         if (sqrt(a[i]) != j)
                                         {
                                             k++;
                                             b[k] = a[i];
                                         }
                                         else
                                         {
                                          k++;
                                          b[k] = j;
    
                                         }
                                      }
    
         FILE *f2;
         int z;
        f2 = fopen("output.txt", "r");
        for(z = 0; z < k; z++){
        fputs("b[z]",f2);
        }
                              }
                           }
        }
    
        fclose (f);
        return 0;
    }

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Using feof to control a loop is not a good a idea. Reason.

    Also, you have two pointers for the files but you close only one. Close both.

    Moreover, you didn't explain what to you mean, even though you followed the good advice of Crossfire, you should help us understand
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    I open a file with numbers. Then I count how many numbers there are in the file. Then goes the prime factorisation. The programme should show the prime factorisation in the 'output' file.

    I changed it but now I get 3 errors and 1 warning.
    Code:
     
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    int main()
    {
        FILE *f;
        f = fopen("input.txt","r");
    
        if (!f) {
                printf ("\The file connot be opened.\n");
                return -1;
                }
        while (feof(f)==0) {
                          int number_ofnumbers=0;
                          char c;
                          fscanf(f,"%c", &c);
                          if (c=='\0')
                          number_ofnumbers++;
    
                          int a[number_ofnumbers];
                          int i;
                          for (i=0; i<number_ofnumbers; i++)
                          {
                              int j;
                              int k=0;
    
    
                              int b[100];
    
                              if (a[i]>1)
                              {
                                  for(j=2; j<=sqrt(a[i]); j++)
                                    while ((a[i]% j) == 0)
                                      {
                                         if (sqrt(a[i]) != j)
                                         {
                                             k++;
                                             b[k] = a[i];
                                         }
                                         else
                                         {
                                          k++;
                                          b[k] = j;
    
                                         }
                                      }
        fclose (f);
    
        FILE *f2;
        int z;
        f2 = fopen("output.txt", "r");
        for(z = 0; z < k; z++){
        fputs("b[z]",f2);
        }
                              }
                           }
        }
    
        fclose (f2);
        return 0;
    }

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    You pass as parameter of fputs the string "b[z]" and not the actual element of b[z]!

    Maybe it would be easier to use fprintf.

    It will be a good idea to post your errors and warnings from now one. Also don't forget to read why feof is bad for loop controlling. You will find that in post #2.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    What do you mean by the actual element of b[z]? I want the output file to show all elements of b[z].

    Could you explain in your own words why feof is bad for loop controlling?

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Yes, you want to send the elements of the array b, but what you send is just the same string every time. You have "b[z]", which is a string!

    With feof, the chance of reading the last line twice is high, because the function checks for the EOF indicator and not the stream.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    985
    Look at this part of the code:
    Code:
    int number_ofnumbers=0;
                          char c;
                          fscanf(f,"%c", &c);
                          if (c=='\0')
                          number_ofnumbers++;
     
                          int a[number_ofnumbers];
    This says to read one byte from the file stream, and if it is '\0', create an array "int a[1]", otherwise it will create an array "int a[0]"
    What are you trying to do with this part?

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    I want to know how many numbers there are in the file by adding spaces.

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    \0 is the null terminator for strings.
    If you have a .txt which is like this :
    1
    2
    3
    4
    then you could do that by checking for the newline character ('\n')
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    You know what, I think you have done the difficult part, the algorithmic one.

    So I made some minutes ago an example of how to copy a file to another. You can found the example here.
    If I were you I would try to customize the code found in the example in my needs.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Could you look at the algorythm please? The more I look at it, the more doubts I have.
    Last edited by Lisa91; 01-19-2013 at 04:26 PM.

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    The algorithm you used for primes? Isn't this your code? :O

    Or are you talking about my 'algorithm' for copying a file to another?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is a number Prime or not prime?
    By ImaCnoob in forum C Programming
    Replies: 27
    Last Post: 10-08-2012, 11:47 PM
  2. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  3. prime factorisation program
    By szpengchao in forum C Programming
    Replies: 4
    Last Post: 04-07-2009, 04:30 PM
  4. Replies: 3
    Last Post: 03-29-2005, 03:24 PM

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