As Dr. Carminati recommended me to do, I have proposed myself a new challenge. In order to make my algorithm more productive, I will try to create a new one so that the user can choose a given number of prime numbers and they will appear on the screen.
During the Activity
As everything when coming to programming. Ideas are simpler in your head than when being typed. It is very difficult for me to put my ideas and my thoughts into code because you are restricted to the nomenclature and to the terminology as well as to the structure you must use in order to transmit your ideas to the computer. I was stuck during a long time and decided I did not have the required level of programming in C++ to accomplish the challenge. Nonetheless, I did not surrender! I went to YouTube and found a series of courses in C++ and during some long hours I just learned and learned more techniques of programming. I think I figured out a way to come up with a good algorithm by basing me on other algorithms which I found on internet and my previous one.
I have also been thinking it is something curious to be searching for stuff in the internet here at CERN. It may be the most special place to use the internet because it was at CERN where it was invented!
After the Activity
I finally did it. Once I had fixed all the glitches in the code (and oh weren't they many) I sighed deeply. I had succeeded in the challenge. I am very, very happy. It has taken my two weeks to write and make perfect this piece of code which is my own work. Two weeks ago, I knew nothing about programming and now I can fluently program codes by myself, without looking up for help in YouTube or from other person. I feel very proud of myself!
Below are the CAS objectives I have met so far with this activity:
2. Undertaken new challenges (Programming is something
completely new for me)
Now, the good part. Here is my code:
// Prime Number Factory Nº2
#include <iostream>
using namespace std;
int PrimeList()
{
int num;
bool prime;
cout << "Type in a positive whole number please." << endl;
cin >> num;
for(int i = 3; i <= num; i++) { //i increases form 3 (1, and 2 are not included...)until it reaches the number selected.
prime = true;
for(int n = 2; n <= i - 1; n++) {// n increases until it reaches one number less than i (an even one), so i will be odd.
if(i % n == 0){ // If i is perfectly divisible by n this means it is an even number so it cannot be prime (excluding two).
prime = false;
}
}
if(prime) {
cout << i << " is a prime number!" << endl;}
}
return 0;
}
No comments:
Post a Comment