Thread: need help explaining this code and comments please here

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    need help explaining this code and comments please here

    all i can think i will type here and please explain the others.thank you
    this is just to enhance my knowledge about oop.The info i got now isn't enough...
    q1
    Write a simple program to ask user to input five digits number. Using the input, output the five digits number in reverse order.
    solution
    Code:
    #include<stdio.h>
    
    # include<iostream.h>
    
    void main()
    {
    int a,n,s=0,t;
    cout<<"Enter the number=";
    cin>>a;
    for(n=a;n!=0;n=n/10)
    //////this is the loop i think to get it in reverse orders correct??/////
    {
    t=n%10;<<to get the last digit
    s=(s*10+t);
    /////
    to get output question and make it in  order but i am unsure
    ///////
    }
    cout<<"The Reverse No. is "<< s<<endl;
    }
    i think using modulus 10 gets the last digit in the digit we inputed and doing modulus 10 again get the second last digit.
    am i correct?

    q2
    Write a program that ask user to input an integer. Pass the input to functions toBase2(), toBase4(), toBase8(), for which each function will output the equivalent of the integer in Base 2, Base 4, and Base 8 respectively.
    the equation i guess
    89
    in base 8 is:
    1 * 8 ^ 0 + 3 * 8 ^ 1 + 1 * 8 ^ 2
    in base 4:
    1 * 4 ^ 0 + 2 * 4 ^ 1 + 1 * 4 ^ 2 + 1 * 4 ^ 3
    in base 2:
    1 * 2 ^ 0 + 0 * 2 ^ 1 + 0 * 2 ^ 2 + 1 * 2 ^ 3 + 1 * 2 ^ 4 + 0 * 2 ^ 5 + 1 * 2 ^ 6
    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    void tobase2(int a)
    {
    	int z;
    	cout<<a<<" in base 2: ";
    	while(a>0)
    	{
    		z=a%2;
    /////this is to get the remainder digit left of divide by 2  of 1 or 0//
    		a/=2;
    ////i wonder what's this use?to make it more efficient?
    /////
    		cout<<z;
    	}
    	cout<<endl;
    	return;
    }
    void tobase4(int a)
    {
    	int z;
    	cout<<a<<" in base 4: ";
    	while(a>0)
    	{
    		z=a%4;
    /////to get the  remainder of divide by 4 i guess?///
    		a/=4;>>
    ////please explain/////
    		cout<<z;
    	}
    	cout<<endl;
    	return;
    }
    void tobase8(int a)
    {
    	int z;
    	cout<<a<<" in base 8: ";
    	while(a>0)
    	{
    		z=a%8;
    ////to  get the  remainder of divide by 8 i guess?////
    		a/=8;
    ////please explain///
    		cout<<z;
    	}
    	cout<<endl;
    	return;
    }
    
    
    int main()
    {
    	int a;
    	cout<<"Enter an integer: ";
    	cin>>a;>>normal input by user
    	tobase2(a);
    	tobase4(a);
    	tobase8(a);
    	return 0;
    }
    q3
    Modify Question 2 so that only one function is created called toBase() which will receive two arguments which is the integer to convert and the base to which the integer will be converted. This function can be used to convert any number to any base. Ask the user to input an integer. Then using the function, iteratively call the function toBase() which will output the integer from Base 2 until Base 10.

    so this program receives only base and integer user and outputs of the desired integer in the base is it?
    and then it displays the values of the received integer from 2 to 10.
    this is wat i understand.
    i only got the integer from 2 to 10.
    any idea on doing for the desired

    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    void toBase( int a, int b)
    {
    	int z;
    	cout<<a<<" in base "<<b<<": ";
    	while(a>0)
    	{
    		z=a%b;
    ////to get the remainder and make it divide again./////
    		a/=b;
    ///////not sure need help here///////
    		cout<<z;
    	}
    	cout<<endl;
    	return;
    }
    int main()
    {
    	int a, b;
    	cout<<"Enter an integer: ";
    	cin>>a;
    	for(b=2;b<11;b++)
    ///////the loop from base 2 to 10///////
    	{
    		toBase(a, b);
    	}
    	return 0;
    }
    this only input from 2- 10 base of the required integer.

    but i need more understanding of this coding.your help is so appreciated
    and this is my first assignment for in c++..
    no OOP here..alll output are working.
    Last edited by MyRedz; 01-29-2009 at 06:48 AM. Reason: correct the comments tag.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MyRedz
    i think using modulus 10 gets the last digit in the digit we inputed and doing modulus 10 again get the second last digit.
    am i correct?
    I have not checked your code, but yes, that is a correct idea.

    Quote Originally Posted by MyRedz
    so this program receives only base and integer user and outputs of the desired integer in the base is it?
    Yes, but you might want to clarify with your teacher just exactly how the representation of bases greater than 10 should be. For example, we often use the letters of the (English/Roman) alphabet to express "digits" greater than nine, but this certainly cannot account for all bases.

    By the way, this programming assignment is about simple problem solving in C++. It is not about object oriented programming.
    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

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    yes..well we have just finish the intro to oop which has these assignment from c++.
    sorry for that..
    can anyone explain about this please
    Code:
    while(a>0)
    	{
    		z=a%b;
    ////to get the remainder and make it divide again./////
    		a/=b;
    ///////not sure need help here///////
    		cout<<z;
    	}
    i dun understand it's formula.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    i dun understand it's formula.
    I do not understand your question at all
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Sounds alot like somebody hasn't paid attention in class?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    sorry..now i get it
    q2 and 3 i get it.
    just basically use modulo to get it's remainder and divide with integer again until it's finish correct???
    now q 1 i dun understand
    on this part
    Code:
    for(n=a;n!=0;n=n/10)
    
    {
    t=n%10;
    s=(s*10+t);
    }
    which is modulo 10 and then divide by ten.
    i think modulo ten is to get the last digit output first?or just to get it's remainder??
    but what's divide by ten purpose?is it for a loop to make it reverse order or what?
    but then
    s=(s*10+t);<<is this one to make it in reverse order ones???like change places from last to first and vice versa?
    now the loop...
    for(n=a;n!=0;n=n/10)
    first n = is equal to our inputed digit then n is not same or equal to zero and n is equal again to n divide by 10..<<can explain me clearly here please.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MyRedz View Post
    sorry..now i get it
    q2 and 3 i get it.
    just basically use modulo to get it's remainder and divide with integer again until it's finish correct???
    now q 1 i dun understand
    on this part
    Code:
    for(n=a;n!=0;n=n/10)
    
    {
    t=n%10;
    s=(s*10+t);
    }
    which is modulo 10 and then divide by ten.
    i think modulo ten is to get the last digit output first?or just to get it's remainder??
    but what's divide by ten purpose?is it for a loop to make it reverse order or what?
    but then
    s=(s*10+t);<<is this one to make it in reverse order ones???like change places from last to first and vice versa?
    now the loop...
    for(n=a;n!=0;n=n/10)
    first n = is equal to our inputed digit then n is not same or equal to zero and n is equal again to n divide by 10..<<can explain me clearly here please.
    So: pick a number for n. Go through the loop yourself, and see what t and s are each time through. Then you won't have to ask us what happens, because you'll have written it down.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    first n = is equal to our inputed digit then n is not same or equal to zero and n is equal again to n divide by 10..<<can explain me clearly here please.
    Do you know the for loop definition?
    before undestanding the code you should be familiar with the operators used in it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    well i get it now..
    but this piece
    Code:
    s=(s*10+t);
    what's s initial value ??
    zero??
    why must it multiply with 10>?
    isn't it only integers??

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what's s initial value ??
    look at the declaration of the var and see if there is some initialization there

    why must it multiply with 10
    Due to the definition of the decimal number
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Code comments
    By salvelinus in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-17-2002, 01:27 AM
  4. code and comments check
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-17-2002, 12:45 PM
  5. remove comments from source code
    By limbo100 in forum C Programming
    Replies: 2
    Last Post: 09-29-2001, 06:25 PM