convert to array

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;
    }
}
out of what?
@TheJJJunk this code shown here. how can i make it into array? :-)
You have the number, the base, the new result, which do you want to make into an array?
Last edited on
@TheJJunk that's what im asking for help. i dont know how to make an array out of that. please do help me.
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?
@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.
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
Topic archived. No new replies allowed.