2021年1月22日星期五

Need to add elements of two array together

Adding two integer array elements

if array1 = {0,0,0,0,9,9,9,9}—————> 00009999  and   array2 = {0,0,0,0,0,0,0,1}————————> 00000001     adding the two arrays together should result in 10000 being in array1, since 9999 + 1 = 10000    therefore, the result should be   array1 = {0,0,0,1,0,0,0,0}  

Does anyone know how to write a code for this? I was trying to use a while loop which didn't work effectively within another for loop. Fairly new to coding and stack overflow, any help will be appreciated!

CODE THAT I TRIED Note: both arrays will have the same number of elements, they were initialized with the same size

int length = sizeof(array1)/sizeof(array1[0]);  for(int i = length; i>0; i--){      if(array1[i-1] + array2[i-1] < 10){          array1[i-1] += array2[i-1];      }else{          array1[i-1] = array1[i-1] + array2[i-1] - 10;          if(array1[i-2]!=9){              array1[i-2]++;          } else{              int j = i-2;              while(j == 9){                  array1[j] = 0;                  j--;              }              array1[j-1]++;          }      }  }  
https://stackoverflow.com/questions/65853263/need-to-add-elements-of-two-array-together January 23, 2021 at 05:54AM

没有评论:

发表评论