Functions,Arrays question

I want to write a code that takes n numbers,then it finds the biggest digits of each number.And then, find sum of the biggest digits.For example:
n=3
145 625 802 (5+6+8=19) I wrote this code by using arrays and functions,but it doesn't take automatically the values from the getData function..How can I develop it in order to work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>

using namespace std;

void getData (int list[], int &n)
{
    ifstream fin ("input.txt");
    fin >> n;
    for (int i=0;i<n;i++)
        fin >> list[i];
}

int sumofBiggestdigits (int list[],int n)
{
    int sum=0;
    int max;
    for (int i=0;i<n;i++){
     max = list[i] % 10;
     list[i]=list[i]/10;
     if(list[i] % 10 > max) max = list[i] % 10;
     list[i]= list[i]/10;
    } return sum+max;
}

int main ()
{
    int list[1000],n,result;
    getData(list,n);
    result=sumofBiggestdigits(list,n);
    cout << result;
    return 0;
}
Last edited on
Your sumofBiggestdigits() needs some thought. How about writing a function
int biggestDigit( int number );
that returns the largest digit of number?

Then have the sumofBiggestdigits() call that with each element in the list and sum up the results.


PS. proper indexing of arrays is 0..n-1, not 1..n.
Last edited on
When you divide number by powers of ten, you get its numbers.
In C++, when you divide int by int and you get a fraction, everything past dot is thrown out. So, 15/10 = 1.5 , but we ignore whatever is after dot, so 15/10 = 1 . Same goes for 19 - 19/10 = 1
And 15/100 = 0.

It looks like you did something like it, but tell me, why do you start checking array from 1 to n, when arrays have indexes form 0 to n-1?
I changed some things in the code but it says error "name look up of i changed for ISO for scoping " in the funcion biggestDigit..How can I manage to fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

void getData (int list[], int &n)
{
    ifstream fin ("input.txt");
    fin >> n;
    for (int i=0;i<n;i++)
        fin >> list[i];
}

int biggestDigit (int list[], int n)
{
    int max;
    for (int i=0;i<n;i++)
        max=list[i]%10;
        list[i]=list[i]/10;
        if (list[i]>max) max=list[i]%10;
        list[i]=list[i]/10;
        return max;
}

int sumofbiggestDigits (int list[], int n)
{
    int sum=0;
    for (int i=0;i<n;i++)
        return sum+max;
}

int main ()
{
    int list[1000],n;
    getData(list,n);
    biggestDigit(list,n);
    cout << sumofbiggestDigits(list,n);
    return 0;
}
First, I suggested a int biggestDigit( int number );
but you created a int biggestDigit (int list[], int n);
Do you spot the difference?

Your problem comes from the fact that:
1
2
3
4
5
6
7
8
9
for (int i=0;i<n;i++)
  max=list[i]%10;
  list[i]=list[i]/10;
// means same as
for (int i=0;i<n;i++)
  {
    max=list[i]%10;
  }
list[i]=list[i]/10; // 'i' delcared in for does not exist any more 


As for the sumofbiggestDigits:
sum=0
for each number in list
  add biggest digit of number to sum

return sum
Topic archived. No new replies allowed.