Thread: For loop fibonacci and prime number getting program not working

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    For loop fibonacci and prime number getting program not working

    I have two programs, a program that outputs the Fibonacci sequence with for loops, and a program that determines if a entered number is prime. The Fibonacci one prints the incorrect result, and the prime number one has incorrect output and the result seems to be based on the return statements. Here is my code:

    For Fibonacci:
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int count = 0;
        cout << "This program lists the Fibonacci sequence." << endl;
        cout << "How many terms? ";
        cin >> count;
        int tracker = 0;
                cout << "F(0) = " << tracker << endl;
                tracker = 1;
                cout << "F(1) = " << tracker << endl;
        for(int track = 2; track < count; track++) {
                int output = tracker + track;
                int ovar = track;
                ovar = tracker;
                output = ovar + tracker;
                cout << "F(" << track;
                cout << ") = " << output;
                cout << "" << endl;
                
                }
                }
    for Prime numbers:
    Code:
    #include <iostream>
    using namespace std;
    bool isPrime(int prime) {
         while(true) { 
             int track;
             track++;
             if(prime > track) {
                    if(prime / track - 1 == 0) {
                             return 1; // I don't know what to return, this is a placeholder          
                             }
                             else {
                                  return 0; // placeholder
                                  }
                                  
                                  }
                                  
                                  else {
                                       break;
                                       }
                                       }
                                       }
         int main ( ) {
             int number;
             cout << "Enter number: ";
             cin >> number;
             cout << "" << endl;
             cout << isPrime(number);
             if ( isPrime(number) ) {
             cout << number << " is not prime.";
             }
             else {
             cout << number << " is prime.";
             return 0;
             }
             
             
             }
    Please help. Also, do NOT post complete code as a reply, only help.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your code is clearly based on guesswork, rather than systematic approach. Throw it out. Write down on paper (i.e. without going near a keyboard) a precise step-by-step description of how you would address each problem (producing Fibonacci sequence, or testing if a value is prime). Once you have that description in a clear form (i.e. you could give it to someone else who knows nothing about the problems, and they could follow it) then work out how to translate that description into code.

    If you are going to post code, make sure that it is sensibly formatted (indentation, etc). That doesn't matter for a compiler, but does matter to people who might try to help you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Any other help?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about you do what was asked of your in the previous post? If your code doesn't make sense, then we need to make sure you understand the problem. Throwing a bunch of random stuff together isn't going to help. Once you show that you have a clear understanding of the problem, we can help you turn it into code.
    If you don't fully understand the problem, then that's fine too. We can help you get an understanding.
    What we won't do is give you a solution. You need to show us that you are willing to put in the effort needed to solve this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I don't fully understand the problem.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you need to elaborate on what you don't understand. Do you understand the algorithm itself? Do you understand what fibonacci and primes are?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Have you learn recursion yet? This would be a great problem for that!!!!

    If you have not learned that yet get a piece of paper and write down what is happening in the loop or use breaks in your compiler.

    If you do not fully understand the problem you are solving you will waste your time looking for an easy to find solution to jump out at you on google.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with prime number program
    By luisgonzalo4 in forum C Programming
    Replies: 1
    Last Post: 10-22-2014, 11:01 PM
  2. C++ prime number loop?
    By uly235 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2010, 11:54 AM
  3. Why Errors in Prime Number Loop
    By wco5002 in forum C++ Programming
    Replies: 15
    Last Post: 03-22-2008, 10:49 PM
  4. Fibonacci number program problem(assembly)
    By ok_good in forum Tech Board
    Replies: 7
    Last Post: 04-07-2006, 07:27 PM
  5. Replies: 3
    Last Post: 03-29-2005, 04:24 PM