-4

suppose the array is a[]={123,34533,21123}.How do i calculate the length of element at index 1?I know i am doing it all wrong but i dont know how to approach this

#include<iostream>
using namespace std;
int main()
{
    int a[]={123,231,23},j=0,num,last_digit,sum;
    int size_of_array=sizeof(a)/4;
    int pos;
    cin>>pos;
    num=a[pos];

    for(int i=0;i<size_of_array;i++)
    {
       last_digit=num%10;
       sum=sum+last_digit;
       num=num/10;
    }
    cout<<sum;
}
Aayush peace
  • 55
  • 1
  • 8
  • 2
    Do you know how to compute the length of a regular integer variable? Just copy the array element into another `int` and do the same thing to it. – NathanOliver Oct 29 '18 at 17:35
  • related/dupe: https://stackoverflow.com/questions/1489830/efficient-way-to-determine-number-of-digits-in-an-integer – NathanOliver Oct 29 '18 at 17:36
  • actually i want to calculate sum of the digits of the element for which i need its length – Aayush peace Oct 29 '18 at 17:46
  • It is unclear what you mean by "length" of an element. Do you mean the number of digits in the decimal representation of an `int`? Please clarify and [edit](https://stackoverflow.com/posts/53050894/edit) your question to include useful information and improve the chances of getting a good answer – alter_igel Oct 29 '18 at 17:46
  • Do you to calculate the sum of digits of _every_ element in the array? Right now, your `for` loop looks promising but is doing the same thing many times in a row, and the body does not depend at all on `i`. Perhaps you want `num=a[i];` at the beginning of the loop? – alter_igel Oct 29 '18 at 17:51

3 Answers3

0

"i want to calculate sum of the digits of the element for which i need its length "

Confusing. If you want to convert the element to a string and then take its length, you would do this:

    std::cout << "Length: " << std::to_string(a[i]).size() << std::endl;

Where i is the index into array a[].

However, if you want to calculate the sum of the digits, then I suppose you are trying to do this:

    int sumCell = 0;
    int cellValue = a[i];
    while(cellValue > 0) {
        sumCell = cellValue % 10;
        cellValue /= 10;
    }

Sum is in the variable sumCell.

Are you looking for one of these two solutions, or another?

Gardener
  • 2,591
  • 1
  • 13
  • 22
0
#include<iostream>
using namespace std;
int main()
{
    int a[] = { 123, 231, 23 }, j = 0, num, last_digit, sum = 0, count = 0;
    size_t size_of_array = sizeof(a) / 4;
    size_t index;
    cin >> index;
    num = a[index];

    while (num != 0) {
        last_digit = num % 10;
        sum = sum + last_digit;
        num = num / 10;
        count = count + 1;
    }
    cout << sum << endl;        //sum
    cout << count;              //number of digits
}
OznOg
  • 4,440
  • 2
  • 26
  • 35
Aayush peace
  • 55
  • 1
  • 8
0

Just like an ordinary int

#include<iostream>

int main() {

    int a[]={123,231,23};
    size_t pos; // use size_t for index and size
    std::cin >> pos;
    int elm = a[pos];
    int cnt = 1;
    while ( elm /= 10) {
        ++cnt;
    }

    std::cout << cnt << std::endl;

    return 0;
}

Enter the index and get the length.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93