Author Topic: Basic C++ program (Good place to start)  (Read 2264 times)

0 Members and 1 Guest are viewing this topic.

Offline Live Wire

  • Knight
  • **
  • Posts: 189
  • Cookies: 4
  • Up on your Net
    • View Profile
Basic C++ program (Good place to start)
« on: September 25, 2012, 11:30:45 am »
Hey guys. Made this piece of code this for my TI for the upcoming SAT. Then I thought that it has a lot of good intro c++ stuff. So I'm going to do a break-down of the code. Hope you enjoy!!
 
First, the whole code.
 
Code: [Select]

#include <iostream.h>
 
int main(){
   
    //vars
    int inNum;
    bool check = true;
   
    //setup
    cout << "\nPlease enter the number: ";
    cin >> inNum;
   
    // actual calcs
         
    for (int i = 2; i < inNum && check;){
        if (inNum%i != 0){
           i++;
           
        }else{
            check = false;
        }
             
    } //end for
   
    if (check == false){
          cout << "\nNumber is not prime!\n\n";
    }else if (check == true){
          cout << "\nNumber is prime!\n\n";
    }else{
          cout << "\n\n\nWTF?\n\n";
    }
   
   return 0;   //for syntax only!!!   
}//end of main

Okay, #1
Code: [Select]
#include <iostream.h>

This just tells the program that we want to be able to process basic input. This is used for a lot of things, but I'm just getting stuff from the keyboard.
 
#2
Code: [Select]
int main(){
   
    //vars
    int inNum;
    bool check = true;

We declare the main method, of the int type. We then declare the variables we want to use in the program. Personally, I like to declare all of my vars upfront. Here we use an int (basically a simple number) and a bool (logic statement (true/false)).
 
#3
Code: [Select]
    //setup
    cout << "\nPlease enter the number: ";
    cin >> inNum;

This is a VERY important step! This is where we use the IO functions of C++. cout tells the program to print the specified text, and cin takes the inputed data, and stores it in the variable we already declared (int inNum;).
 
*the \n is used to make a new line. The // is used for comments, and not used in the actual program.


#4
Code: [Select]
   for (i = 2; i < inNum && check;){
        if (inNum%i != 0){
           i++;
           
        }else{
            check = false;
        }
             
    } //end for
 

Okay, now we're moving into real programming. We used a for loop here. In a nutshell, I instructed the program to create a new variable (int i), and set its value to 1. Then, I told the program to keep running while i < inNum AND check is still true. After this, I could have created a number of other statements, but I only needed two. This is the real use of the for loop. Normally, it is used to declare, check, and increment a value. Since you can do all of this from a single statement, it is better for this than a while loop.
 
After that, we introduce the if/else statment. These take a statment, check if they are true, and execute the code depending on the outcome.
 
 Next, I use the mod (%) sign. This is just returning the remainder of inNum / the current value of i. We then check to see if the remainder does not equal (!=) 0. This tells us that the number is not prime, so we can keep checking for higher values of i (i++ (adding 1 to the value of x) ). If we find that it ever DOES equal 0, it cannot be prime and we set the boolean logic variable check to false. This means that the loop can no longer continue, and we stop checking for values.
 
#5
Code: [Select]
    //if/else
 
    if (check == false){
          cout << "\nNumber is not prime!\n\n";
    }else if (check == true){
          cout << "\nNumber is prime!\n\n";
    }else{
          cout << "\n\n\nWTF?\n\n";
    }

This is the basic programming logic statement. Rember we set check to false if the inputed number is not prime. In *MOST* programming, you will use  =  to assign a value, and  ==  to compare something's value to another value.
 
Here, we look to see if check is false or true. If check is false, the number is not prime, and we report this to the user. If check is true, we tell the user. If a magnet was used to wipe a specifc point of your memory in the split second it took to check this, it outputs this to the user. Can't be to prepared, can you?
 
#6
Code: [Select]
return 0;   //for syntax only!!!   
}//end of main

We then end to code. The return of 0 isn't required, but is good for syntax.
 
 
 
 
I tried to do the best I could with the syntax. This is the first tut I've ever done, so any pointers would be helpful.
 
As usual, any feedback appriciated!
« Last Edit: September 25, 2012, 11:57:16 am by Live Wire »
"There is no right or wrong, there is only fun and boring."

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Basic C++ program (Good place to start)
« Reply #1 on: September 25, 2012, 03:29:22 pm »
Here's an improved algorithm and style if you don't mind ^^

Code: (c++) [Select]
#include <iostream.h>
#include <math.h>
 
int main(void)
{   
    //vars
    int inNum;
    bool check = true;
   
    //setup
    cout << "\nPlease enter the number: ";
    cin >> inNum;
   
    int limit = sqrt(inNum);
    // actual calcs
    for (int i = 2; i < limit; i++){
        if (inNum % i == 0){
            check = false;
            break;
        }
    } // end for
   
    if (check == false)
        cout << "\nNumber is not prime!\n\n";
    else
        cout << "\nNumber is prime!\n\n";
   
   return 0;   //for OS, everything was fine
}//end of main
Easter egg in all *nix systems: E(){ E|E& };E

Offline Live Wire

  • Knight
  • **
  • Posts: 189
  • Cookies: 4
  • Up on your Net
    • View Profile
Re: Basic C++ program (Good place to start)
« Reply #2 on: September 25, 2012, 06:07:04 pm »
Here's an improved algorithm and style if you don't mind ^^

Code: (c++) [Select]
#include <iostream.h>
#include <math.h>
 
int main(void)
{   
    //vars
    int inNum;
    bool check = true;
   
    //setup
    cout << "\nPlease enter the number: ";
    cin >> inNum;
   
    int limit = sqrt(inNum);
    // actual calcs
    for (int i = 2; i < limit; i++){
        if (inNum % i == 0){
            check = false;
            break;
        }
    } // end for
   
    if (check == false)
        cout << "\nNumber is not prime!\n\n";
    else
        cout << "\nNumber is prime!\n\n";
   
   return 0;   //for OS, everything was fine
}//end of main

hmmm, didn't even think of doing it that way. Thanks. But what did you think of the tut?
"There is no right or wrong, there is only fun and boring."

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Basic C++ program (Good place to start)
« Reply #3 on: September 26, 2012, 01:21:01 am »
Well, the tutorial was alright for absolute beginner, but I think you need to learn more about C++ though in order to teach good style and correct technique :P

The way you include <stuff> is deprecated, and is C style, not C++. In C++ you use "using namespace", learn about that. Also "endl" when using "cout" for newline is something to research about. Also no need for {} bracelets when only having one statement after a condition, makes code easier to read as long as you are indenting your code.

I salute the fact that you are trying to teach people, but make sure you know the modern C++ style first.
« Last Edit: September 26, 2012, 01:21:28 am by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline Live Wire

  • Knight
  • **
  • Posts: 189
  • Cookies: 4
  • Up on your Net
    • View Profile
Re: Basic C++ program (Good place to start)
« Reply #4 on: September 26, 2012, 07:35:09 am »
oh, I know how to use namespace, I'm just using my schools ass old compilers, which for some reason freak out whenever I use it.
 
and yeah, I'm a java guy. But the best way to learn is to teach, right?
"There is no right or wrong, there is only fun and boring."

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Basic C++ program (Good place to start)
« Reply #5 on: September 26, 2012, 09:18:34 am »
What is the point of teaching deprecated programming? :P
Also some of your statements can lead people wrong way, like saying that returning code 0 to the OS is not needed, and only for style. You declared main as returning an int, so you must return and int. Also "If a magnet was used to wipe a specifc point of your memory in the split second it took to check this, it outputs this to the user. Can't be to prepared, can you?" that's just too paranoid for this simple boolean variable :P
Basically, in order to teach others correct information, you need to know that information first.
Easter egg in all *nix systems: E(){ E|E& };E

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Basic C++ program (Good place to start)
« Reply #6 on: September 27, 2012, 03:44:00 am »
But the best way to learn is to teach, right?

......................................................................no.

You need to understand it first before you teach it otherwise you're teaching incorrectly.  Teaching a deprecated style is acceptable if it is left as a footnote and not the focus.  And the "return 0" statement is not 'just for syntax' or 'not required'; it most certainly is required.  In teaching, you should be more stringent on style for students than on yourself.  When the students know the rules, they can decide for themselves when to bend or break them.  The point is that they need to know the rules, and the correct ones at that.  Furthermore, they should understand the reasons behind those rules so they're not blindly stepping into something without understanding its purpose.

It's fine to share what you're learning or what you've recently discovered or what you think you know and we welcome such sharing here on EvilZone.  But 'sharing' and 'teaching' are different things.  Bottom line: don't teach what you don't know through & through.  I'll grant you that that statement likely means that the majority of your instructors at college aren't qualified to teach but that is beside the point.

Sorry if I seem rude; I happen to teach programming.
-Xires