How do i Pass values from one array to another?

hi there, i was making a program that segregates numbers that are divisible by 2 3 and 5. everything seems to be working out alright but the numbers that appear when segregated are random numbers. Can you guys help me out? thank you

Here's my code:


#include <iostream>
using namespace std;

int main(){

int a;
int intarray[a];


cout<<"how many numbers do you want to enter?\n";
cin>>a;

int divby2[a];
int divby3[a];
int divby5[a];

cout<<"please enter a number:\n";
for(int i=0;i<a;i++){
cin>> intarray[i];
}



for(int i=0;i<a;i++){

if(intarray[a]%2==0){

intarray[a]=divby2[a];

}else if(intarray[a]%3==0){

intarray[a]=divby3[a];

}else if(intarray[a]%5==0){

intarray[a]=divby5[a];
}

}


cout<<"int input: ";

for(int i=0;i<a;i++){

cout<<*(intarray+i)<<",";
}
cout<<"\n \n";

cout<<"divisible by 2: "<<endl;
for(int i=0;i<a;i++){
cout<<divby2[i]<<",";
}
cout<<"\n \n";

cout<<"divisible by 3: "<<endl;
for(int i=0;i<a;i++){
cout<<divby3[i]<<",";
}
cout<<"\n \n";

cout<<"divisible by 5: "<<endl;
for(int i=0;i<a;i++){
cout<<divby5[i]<<",";
}
cout<<"\n \n";




return 0;
}[/code]
Last edited on
a number can be divisible by 2 && 3 && 5 (e.g. 90), so if, else-if checks are not suitable in this case. Also suggest using std::vector<int> rather than C-style arrays:
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>
#include <vector>

int main()
{
    std::cout << "How many numbers \n";
    int num{};
    std::cin >> num;
    int i{1};
    std::vector<int> divTwo{}, divThree{}, divFive{};
    while (i <= num)
    {
       std::cout << "Enter number " << i << " of " << num << ": ";
       int temp{};
       std::cin >> temp;
       if(temp % 2 == 0)divTwo.push_back(temp);
       if(temp % 3 == 0)divThree.push_back(temp);
       if(temp % 5 == 0)divFive.push_back(temp);
       ++i;
    }
    if(divTwo.size())std::cout << "Div2 \n"; for(const auto& elem : divTwo)std::cout << elem << " ";
    std::cout << "\n";
    if(divThree.size())std::cout << "Div3 \n"; for(const auto& elem : divThree)std::cout << elem << " ";
    std::cout << "\n";
    if(divFive.size())std::cout << "Div5 \n"; for(const auto& elem : divFive)std::cout << elem << " ";
    std::cout << "\n";
}
Hello redhood,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

How do i Pass values from one array to another?

You do not. You can only assign from one array to another.

You have some problems with your program. To start with line 6 "a" is not initialized it contains whatever was left in the memory location when the program runs and chooses memory for that variable. Most important with line 7 is that the size of the array must be defined with a constant value so when the program runs it knows how much memory to set aside. Otherwise you will have to crate the array dynamically with the "new" key word when the program is running. You will have the same problem with lines 13- 15.

The for loop at line 24 is set up all wrong. The use of "a" in the subscripts should be "i" and your assignments are backwards.

Hope that helps,

Andy

P.S. Hint assignment "=" is from right to left.
Last edited on
Topic archived. No new replies allowed.