I am new to programming and have written the code for the following program.
PROGRAM: Input 2 arrays => arrays 1 and 2 from the user each containing 5 elements. Sum is another array which is sum of elements of array 1 and array 2. Convert each of the elements of the sum array into binary. Count number of 1's in each binary element and output it to another array "arr".
Example: arr1 = {1,2,3,4,5}
arr2 = {6,7,8,9,10}
sum = {7,9,11,13,15}
binary of 7 [111], 9[1001], 11[1011], 13[1101], 15[1111]
No of 1's in 7 [3], 9 [2], 11 [3], 13 [3], 15 [4]
arr array will be {3, 2, 3, 3, 4}
I am not getting the desired output.
My code is:-
include <iostream>
using namespace std;
int main() {
int array1[5];
int array2[5];
int sum[5], arr[5];
int i, rem ;
cout<<"enter array 1 elements\n"; //array1
for(i=0;i<5;i++)
cin>>array1[i];
cout<<"enter array 2 elements\n"; //array2
for(i=0;i<5;i++)
cin>>array2[i];
Well with the data you've shown your while() loop will never execute. I also don't recommend modifying sum[] inside that loop, use a temporary variable instead.
And please start using code tags when you post code.
Thanks for the reply. I have change while(sum[i]==1) to while(sum[i]!=1) but am still not getting the correct answer. I am getting arr[] array as {2,1,2,2,3} but correct output is {3,2,3,3,4} for the example mentioned above. I have also tried initializing flag to 1.