convert to array

Oct 7, 2012 at 5:58pm
hey guys, can anyone help me how to make an array out of this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 #include <iostream>
using namespace std;

void decToBin(int num, int base);

int main()
{
    int decimal;

    cout<<"Enter the number you want to convert? : ";
    cin>>decimal;

    cout<<"\nIn binary is : ";

    decToBin(decimal, 2);

}
void decToBin(int num, int base)
{
    if(num > 0)
    {
        decToBin(num/base , base);

        cout<<num%base;
    }
}
Oct 7, 2012 at 6:59pm
out of what?
Oct 7, 2012 at 8:42pm
@TheJJJunk this code shown here. how can i make it into array? :-)
Oct 7, 2012 at 10:25pm
You have the number, the base, the new result, which do you want to make into an array?
Last edited on Oct 7, 2012 at 10:25pm
Oct 8, 2012 at 1:02am
@TheJJunk that's what im asking for help. i dont know how to make an array out of that. please do help me.
Oct 8, 2012 at 1:05am
Why do you need an array? You need to state what you want to turn into an array. Do you want an array to enter different values to convert? Do you want an array to store the binary results?
Oct 8, 2012 at 2:06am
@TheJJunk our instructor asked us to make an array out of the problem that we solved. but i was having a hard time making an array out of it. i was so unfamiliar with array.
Oct 8, 2012 at 1:22pm
You could do this, which is assigning each number to an integer array index and then calling the function on each index. Or you could store all the values first, and then use another loop to call the function which displays them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

void decToBin(int num, int base);

int main()
{
    int decimal;
    int number;
   
    cout << "How many conversions do you want to do?: ";
    cin  >> number;

    int array[number];

    for (int i=0; i < number; ++i)
    {
       cout<<"Enter the number you want to convert? : ";
       cin >> array[i];

       cout<<"\nIn binary is : ";

       decToBin(array[i], 2);
       cout << endl;
    }
}

void decToBin(int num, int base)
{
    if(num > 0)
    {
        decToBin(num/base , base);

        cout<<num%base;
    }
}
Last edited on Oct 8, 2012 at 1:24pm
Topic archived. No new replies allowed.