Hey everyone,
can anyone help me in understanding this question
Write a C++ program that takes an array of integers A and its length N as
parameters and return back the number of pairs of elements that are multiple of each other.
i'm not asking anyone to solve or give me a code, i just don't understand it
I would assume that "multiple of each other" should actually mean "one of them is a multiple of the other". Any resident pedagogues here will hopefully correct me if I'm wrong.
If that is the case, then you will iterate over the array (think nested loop) and look for any pair where one number is a multiple of the other. Use the modulo operator to look for this relationship.
i followed your instructions, but i feel there is something wrong because in this code i'm detecting only the even numbers
can you please test it and tell me what is missing?
also is it mandatory to use functions because the question have the key words (parameters and return)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
usingnamespace std;
int main()
{
int i,j=0, n[10][2];
for(i=0;i<10;i++)
{
cout<<" Enter element "<<i+1<<" ";
cin>>n[i][1];
}
for(i=0;i<10;i++)
{
if(n[i][1]%2==0)
cout<<" element"<<i+1<<" "<<n[i][1]<<" is a multiple of another number "<<endl;
}
return 0;
}
Line 5; Why are you using a two dimensional array? Instructions imply a single dimensional array.
Line 13: You're testing if the number is even. You're not testing if the number is a multiple of another number in the array.
As booradley60 pointed out you need to think about nested loops. The outer loop iterates over the array to get the number to be tested, while the inner loop iterates over the loop to find any factors.
i used a nested loop but the point is that the condition in the if statement to test if any number in array n is divisible by the tested number seems to be wrong
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<iostream>
usingnamespace std;
int main()
{
int i,j, num, element[10], n[10]={1,2,3,4,5,6,7,8,9,10};
for(i=0;i<10;i++)
{
num=n[i];
for(j=0;j<10;j++)
if(n[j]%num)
cout<<element[i]<<n[i]<<" is a factor of a number in array n "<<endl;
}
return 0;
}
Think about line 10 for a minute. You're testing if the number from the inner loop has a remainder when divided by the number from the outter loop. Two problems.
1) You want the test the other way around.
2) You want to test if the result of the modulo operation is zero. i.e. if there is NO remainder. If there is a remainder, then the number on the right side of the % operator is NOT a factor.
it worked when i put modulo operation = 0
& what do u mean by testing the other way ?
i'm still confused about " array of integers A and its length N as
parameters and return back the number of pairs "
do i have to declare one or two arrays ?
" as parameters " should i define the arrays in separate function and return the number of element instead of returning 0 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
usingnamespace std;
int main()
{
int i,j, num, n[10]={1,2,3,4,5,6,7,8,9,10};
for(i=0;i<10;i++)
{
num=n[i];
for(j=0;j<10;j++)
if(n[j]%num==0)
cout<<n[j]<<" is a multiple of number "<<num<<" in array n "<<endl;
}
return 0;
}