C++
  Home arrow C++ arrow Page 8 - First Steps in (C) Programming, introducti...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

First Steps in (C) Programming, introduction
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2005-11-17

    Table of Contents:
  • First Steps in (C) Programming, introduction
  • What Is a Variable?
  • Variables That Store Numbers
  • Try It Out: Using More Variables
  • Naming Variables
  • Initializing Variables
  • Basic Arithmetic Operations
  • Try It Out: Division and the Modulus Operator

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    First Steps in (C) Programming, introduction - Try It Out: Division and the Modulus Operator


    (Page 8 of 8 )

    Suppose you have a jar of 45 cookies and a group of 7 children. You’ll share out the cookies equally among the children and work out how many each child has. Then you’ll work out how many cookies are left over.

    /* Program 2.6 Cookies and kids */
    #include <stdio.h>
    void main()
    {
     
    int cookies = 45;             /* Number of cookies in the jar */
     
    int children = 7;             /* Number of children          */
     
    int cookies_per_child = 0;    /* Number of cookies per child */
     
    int cookies_left_over = 0;    /* Number of cookies left over */
     
    /* Calculate how many cookies each child gets when they are divided up */ 
      cookies_per_child = cookies/children; /* Number of cookies per child */
      printf("You have %d children and %d cookies", children, cookies);
      printf("\nGive each child %d cookies.", cookies_per_child);
     
    /* Calculate how many cookies are left over */
      cookies_left_over = cookies%children; 
      printf("\nThere are %d cookies left over.\n", cookies_left_over);
    }

    When you run this program you’ll get this output:

    --------------------------------------------
    You have 7 children and 45 cookies
    Give each child 6 cookies.
    There are 3 cookies left over.
    --------------------------------------------

    HOW IT WORKS

    Let’s go through this program step by step. Four integer variables,cookies,children,cookies_per_child, andcookies_left_over, are declared and initialized with the following statements:

    int cookies = 45;          /* Number of cookies in the jar */
    int children = 7;          /* Number of children           */
    int cookies_per_child = 0; /* Number of cookies per child */
    int cookies_left_over = 0; /* Number of cookies left over */

    The number of cookies is divided by the number of children by using the division operator/to produce the number of cookies given to each child:

    cookies_per_child = cookies/children; /* Number of cookies per child */

    The next two statements output what is happening, including the value stored incookies_per_child:

    printf("You have %d children and %d cookies", children, cookies);
    printf("\nGive each child %d cookies.", cookies_per_child);

    You can see from the output thatcookies_per_childhas the value 6. This is because the division operator always produces an integer result when the operands are integers. The result of dividing 45 by 7 is 6, with a remainder of 3.

    You calculate the remainder in the next statement by using the modulus operator:

    cookies_left_over = cookies%children;

    The expression to the right of the assignment operator calculates the remainder that results when the value ofcookiesis divided by the value ofchildren.

    Finally, you output the reminder in the last statement:

    printf("\nThere are %d cookies left over.\n", cookies_left_over);

    .............................................

    More on Division with Integers

    Let’s look at the result of using the division and modulus operators where one or the other of the operands is negative. With division, if the operands have different signs, the result will be negative. Thus, the expression –45/7 produces the same result as the expression 45/–7, which is –6. If the operands in a division are of the same sign, then the result is positive. Thus, 45/7 produces the same result as –45/–7, which is 6.

    With the modulus operator, the sign of the result is always the same as the sign of the left operand. Thus, 45%–7 results in the value 3, whereas –45%7 results in the value –3.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Beginning C," published by Apress. We hope...
       · Worthwhile reading for the beginner. BUT... I'm torn between praising the publishing...
       · Lol! Yes, I understand your ambivalence about it being a marketing ploy. On the...
       · That's a good idea. We tend to forget that C is still very capable and popular....
       · Thank you Pantaz! I'm glad you liked the article, and we'll keep your suggestion in...
     

    Buy this book now. This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out today at your favorite bookstore. Buy this book now.

    C++ ARTICLES

    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class
    - Iostream Library and Basic I/O in C++
    - Introduction to Streams







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT