Arrays | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок


Guys, I need help: I have task to print an element of array with largest sum of digits. Here is my code, I tried to create subarray which shows sum of digits and it works, but when I run this program it prints "Largest sum of digits has element 0", (in my native: "Najveći zbir cifara ima element 0").I think it is the matter with last condition, variable is lost in memory or somewhat. Could someone help me with that I would greatly appreciate.Here is code:#include <stdio.h> int main() { int x[20]; int i,j,n,k,maks; int sum[20]; printf("Unesite broj elemenata niza:"); scanf("%d",&n); i=0; while(i<n) { printf("Unesite %d-ti element niza:",i); scanf("%d",&x[i]); i++; } i=0; k=0; while(i<n) { sum[k]=0; while(x[i]) { sum[k]=sum[k]+x[i]-(x[i]/10)*10; x[i]=x[i]/10; } k++; i++; } maks=sum[0]; i=0; while(i<k) { if(maks<sum[i]) maks=x[i]; i++; } printf("Najveci zbir cifara ima element %d",maks); return 0; }

28th Aug 2020, 7:16 PM
ivan koz
ivan koz - avatar
1 ответ
+ 1
In your code I think x[i] - x[i] /10*10 => x[i] - x[i] => 0 So sum[k] = sum[k] + x[i] - (x[I] /10)*10; IS equal to sum[k] =sum[k] which is 0 And maks=sum[0] is 0 again... You may here only x[i] %10 need.....?
28th Aug 2020, 7:55 PM
Jayakrishna 🇮🇳