Page 1 of 2 12 LastLast
Results 1 to 10 of 20
  1. #1
    Join Date
    Nov 2008
    Location
    41° 28′ 56″ N, 81° 40′ 11″ W
    Posts
    2,398

    Using what you've learned from the first C++ 101 Session...

    Now that the first session of C++ 101 is over, it is now time to use what you have learned to create a simple program. For example, here is a program that I wrote that utilizes variables, an if/else statement, operators, and user input:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int sum;
    	int number;
    
    	cout << "Select a three-digit number\n";
    	cout << "All three digits the same...\n";
    	cout << "\nAdd the three digits together...\n";
    	cout << "\nWhat is the sum of the three digits? ";
    	cin >> sum;
    
    	if (sum < 3 && sum > 27)
    	{
    		cout << "Sorry, but the end number must have three equal digits.\n";
    	}
    	else
    	{
    		number = 37 * sum;
    		cout << "\n\nYour Number Is:  " << number;
    	}
    
    	cin.get();
    	cin.ignore();
    	return 0;
    }
    Which results in this:



    Now with this in mind, let's see what you can come up with based on yesterday's or Monday afternoon's class sessions, and be creative with it, yet stick to what you have learned so far. Good luck!

    I just thought that "Well, since it was an introductory lesson, and there wasn't any homework, that doesn't mean that we can't prove what was learned in some form." So that's how I came up with this thread, in case you were curious.
    Last edited by WedgeBob; 04-14-2012 at 07:20 AM.
    PC Specs: Antec 900 ATX Case, Gigabyte 790GX Mobo, AMD Phenom II x4 955, Corsair HX620W PSU, Corsair Vengeance 16GB (4x4GB) DDR3-1600, EVGA GTX 680 2GB GDDR5, 3D Vision Kit 2, LG 8x Blu-ray Burner, Intel X25-M SSD, 500GB+640GB WD Caviar Blacks, Win 7 Pro x64, DAS Pro K/B, MX518 Mouse, Logitech Z-2300 Speakers, Toshiba 32TL515U 32" 3DTV, Wacom Bamboo Fun Large Tablet.

  2. #2
    Join Date
    Apr 2012
    Location
    Seaside, California
    Posts
    57
    Well I wanted to work towards making the "FizzBuzz" interview test. Two warnings, first of all, this is NOT a solution to FizzBuzz. Second, it uses the Modulus operator(%), which hasn't been discussed in the class.

    Modulus (%) simply returns the remainder of 2 numbers. For example, 5 % 2 would be 1.

    So here we go!

    "Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"."


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int number = 0;
    
    	cout << "Please enter a number: ";
    
    	cin >> number;
    
    	if(number % 3 == 0 && number % 5 == 0)
    	{
    		cout << "FizzBuzz";
    	}
    	else
    	{
    		cout << number;
    	}
    
    	cin.ignore();
    	cin.get();
    	return 0;
    }
    As the class goes along, I'll do more to actually solve this problem, but this is a good, simple first step

    As a side note: I decided to include modulus, because I'm assuming we will not be going over it in the class. If this ruined a grand plan for a class dedicated to the modulus operator, I'm sorry :-p
    Last edited by Xietep; 04-14-2012 at 12:03 PM.

  3. #3
    Join Date
    Dec 2002
    Location
    Norway
    Posts
    450
    I also immediately wrote out a FizzBuzz program after reader the link Nelson posted in the other thread. Took me a good 3 minutes, but I did it in C#. Still haven't tried anything in C++ though, but there will be homework soon enough You're not far from a complete implementation, Xietep. It's just more of the same logic, and you need the "counting from 1-100" part (which hasn't been covered) instead of user input. Btw: I think you meant 3 % 2 = 1

  4. #4
    Join Date
    Oct 2009
    Location
    The In Between. US
    Posts
    2,025
    Quote Originally Posted by Metanol
    Btw: I think you meant 3 % 2 = 1
    nope he is right 5 % 2 = 1;
    elevating to new levels of knowledge (that's the plan at least )

  5. #5
    Join Date
    Dec 2002
    Location
    Norway
    Posts
    450
    Quote Originally Posted by elevating View Post
    nope he is right 5 % 2 = 1;
    Of course he is *doh!* My bad

  6. #6
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    Quote Originally Posted by Xietep
    For example, 5 % 2 would be 1.
    Quote Originally Posted by Metanol
    Btw: I think you meant 3 % 2 = 1
    Quote Originally Posted by elevating View Post
    nope he is right 5 % 2 = 1;

    Ha, ha, ha - too funny. ANYTHING odd % 2 = 1

  7. #7
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,349
    Quote Originally Posted by WedgeBob View Post
    Now that the first session of C++ 101 is over, it is now time to use what you have learned to create a simple program. For example, here is a program that I wrote that utilizes variables, an if/else statement, operators, and user input
    A few notes on style for that program: Don't use using namespace std; at file scope. It potentially pulls a ton of identifiers into the global namespace, which you don't want to be happening at large scales. Instead, only use using at function scope:
    Code:
    int main() {
         using namespace std;
         ...
    }
    In particular, *never* use using in a header file.
    Also, consider use using declarations (like using std::cout;) since they offer a more fine-grained control over what is pulled in. Alternatively, just use the fully qualified name everywhere. It's not that much more to type and it becomes crystal clear when your code is using something from the standard library.

    Also, don't declare all of your variables at the top of the function, but closest to their first point of use. In your example, the variable 'number' only needs to live inside the else-block, so you should also declare it there. Limited lifetime of variables helps you avoid errors and allows the compiler to generate more efficient code. This becomes even more important once you use objects, where the lifetime of a variable is often tied with the use of an expensive system resource.

    Quote Originally Posted by Xietep View Post
    Well I wanted to work towards making the "FizzBuzz" interview test. Two warnings, first of all, this is NOT a solution to FizzBuzz. Second, it uses the Modulus operator(%), which hasn't been discussed in the class.
    To solve this issue (and make the problem a little more interesting ), try to solve the FizzBuzz without using modulus.

  8. #8
    Join Date
    Oct 2009
    Location
    The In Between. US
    Posts
    2,025
    Quote Originally Posted by ComicSansMS View Post
    A few notes on style for that program: Don't use using namespace std; at file scope. It potentially pulls a ton of identifiers into the global namespace, which you don't want to be happening at large scales. Instead, only use using at function scope:
    I have heard this before and have debated it with others who say I'm nuts. They say putting it at the top isn't a problem, and I must say almost all if not all books I have ever read do this as well which to me is teaching a very poor habit for no reason or one I don't understand why yet.
    elevating to new levels of knowledge (that's the plan at least )

  9. #9
    Join Date
    Nov 2008
    Location
    41° 28′ 56″ N, 81° 40′ 11″ W
    Posts
    2,398
    Well, Nelson taught to use the using namespace std; just after the #include <iostream>, so that's what I did.
    We'll see what he says about that come tomorrow afternoon, ComicSansMS. I mean, at first he showed us the std::line bit throughout, but he admitted that got kind of annoying to type out std:: line after line after line, so he threw on the "master switch" if you will. So that's how I got into that.
    Last edited by WedgeBob; 04-15-2012 at 06:51 AM.
    PC Specs: Antec 900 ATX Case, Gigabyte 790GX Mobo, AMD Phenom II x4 955, Corsair HX620W PSU, Corsair Vengeance 16GB (4x4GB) DDR3-1600, EVGA GTX 680 2GB GDDR5, 3D Vision Kit 2, LG 8x Blu-ray Burner, Intel X25-M SSD, 500GB+640GB WD Caviar Blacks, Win 7 Pro x64, DAS Pro K/B, MX518 Mouse, Logitech Z-2300 Speakers, Toshiba 32TL515U 32" 3DTV, Wacom Bamboo Fun Large Tablet.

  10. #10
    Join Date
    Apr 2012
    Location
    Seaside, California
    Posts
    57
    I was under the impression that using namespace std may increase compile time slightly, but would do absolutely nothing different to the compiled code. I know this is true for C#.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •