• 01-29-2009
MyRedz
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.
• 01-29-2009
laserlight
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.
• 01-29-2009
rags_to_riches
• 01-29-2009
MyRedz
yes..well we have just finish the intro to oop which has these assignment from c++.
sorry for that..
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.
• 01-29-2009
vart
Quote:

i dun understand it's formula.
I do not understand your question at all
• 01-29-2009
legit
Sounds alot like somebody hasn't paid attention in class?
• 01-29-2009
MyRedz
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.
• 01-29-2009
tabstop
Quote:

Originally Posted by MyRedz
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.
• 01-29-2009
vart
Quote:

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...
• 01-29-2009
MyRedz
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??
• 01-29-2009
vart
Quote:

what's s initial value ??
look at the declaration of the var and see if there is some initialization there

Quote:

why must it multiply with 10
Due to the definition of the decimal number