Thread: Hello about my program

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    Hello about my program

    Hello,

    I am making a program for a listing prime numbers in a sequence starting from 1 to 50.

    I want to use for loop statement for this program as:


    --------------------------------------------------------------------------------------------------------

    Code:
    int A[40]; // array for storing the prime numbers/
    
    int y; //variable for taking values from users/
    
    int i; //
    Code:
    for(y=1;y<50;y++)
    
    cout<<" pls enter a number"<<;
    
    cin>>"y";
    
    
    if (y==50)
    
    {
    break; 
    }

    ---------------------------------------------------------------------------------------------

    Can I use the find() method inside this "for loop"?

    If I want to add the number found from the find() method, how can I add that number into an array A[40] that I have declared in this program?

    Thanks

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You want to store 50 primes, but your array is of size 40! It should be 50, shouldn'it

    You have
    Code:
    cin>>"y";
    but what you meant to write is
    Code:
    cin >> y;
    I do not see any find() method in your post.

    You can access the i-th element of an array, with the [] operator.
    This code is storing value 5 in the first cell of an int array of size 10.
    Code:
    int A[10]
    A[0] = 5;
    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
    Nov 2011
    Posts
    96
    Sorry for the mistakes that I did,

    Yes I'll correct them but is it true that if I take any number from a user as input using the "cin", do I have to manually enter each value taken from the user in the array?




    Thanks
    Last edited by student111; 11-14-2013 at 03:31 AM. Reason: correcting.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You could do this
    Code:
    #include <iostream>
    
    int main()
    {
    
            int N = 50;
            int A[N];
            std::cout << "Input " << N << " integers\n";
            for(int i = 0 ; i < N ; ++i)
                    std::cin >> A[i];
            std::cout << "Output\n";
            for(int i = 0 ; i < N ; ++i)
                    std::cout << A[i] << "\n";
            return 0;
    }
    Here, we read N integers into the array and then we print the array.
    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

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    thanks for your reply,

    I have some questions about the program that you have posted.

    What is the difference between "++i" and "i++"?

    Also If we have initialized any variable eg."i" in "for loop", so we don't need to declare it outside of the main function?

    Thanks

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In this point, it is the same.
    In other words
    Code:
    for(i = 0 ; i < 5 ; ++i)
    is the same (for us) with
    Code:
    for(i = 0 ; i < 5 ; i++)
    Recall, that in the 1st piece of code, the incremental operator is prefix and in the 2nd, postfix!

    In order to take a smell of what this means, consider this simple example.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int i = 0 ;
            // First print i and then increment i
            printf("%d\n", i++);
    
            // First increment i and then print i
            printf("%d\n", ++i);
    
            return 0;
    }
    First think what the output will be and then run the code to test your thought.
    Notice that incremental (or decremental) operators can be a caveat. If you are not sure if it is correct to use it in a line of code, avoid it.

    I declared variable "i" in the for loop. In other words, I initialized this variable in the scope of the for loop. As a result, this variable leaves inside the for statement and its body only.
    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

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    thanks for the reply,

    Is it true that we use variables and arrays outside of the main function only when we make classes?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by student111
    Is it true that we use variables and arrays outside of the main function only when we make classes?
    No, e.g., when you write a non-member function other than the main function, you will probably use one or more variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM