array

How to display elements of the array is equal to the sum of 15? Example 69

1
2
3
4
5
  const int n = 10;
    int a[n] = { 19, 32, 69, -44, 5, 63, -7, 8, -29, 906 };
 
for(int i=0;i<n;++i)
?
Use a variable to hold the sum, then iterate again and print the array if the sum is 15

1
2
3
4
5
6
7
8
9

int sum=0;

for(int i=0; i<n;++i)
sum+=a[i];

if(sum==15)
 for(int i=0; i<n;++i)
   std::cout<<a[i];
closed account (E0p9LyTq)
If the OP is trying to get the sum of the digits of each element (sloppy, but shows the idea of how to sum a number's individual digits):

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>

const unsigned num_to_compare = 15;

bool IsEqual(int num, unsigned num_to_compare = num_to_compare);

int main()
{
   const int array_size = 10;
   int array[array_size] = { 19, 32, 69, -44, 5, 63, -7, 8, -29, 906 };

   for (unsigned loop = 0; loop < array_size; loop++)
   {
      if (IsEqual(array[loop]))
      {
         std::cout << array[loop] << " sums to " << num_to_compare << "\n\n";
      }
      else
      {
         std::cout << array[loop] << " does NOT sum to " << num_to_compare << "\n\n";
      }
   }

}


bool IsEqual(int num, unsigned num_compare)
{
   // get the absolute (positive) value of the passed number
   unsigned abs_num = abs(num);

   // turn it into a string
   std::string num_str = std::to_string(abs_num);

   unsigned sum = 0;

   // use a C++11 for loop to walk through the string
   for (auto loop: num_str)
   {
      // obtain each individual stringized digit
      std::string temp_str = { loop };

      // covert each digit to an integer and add to the sum
      sum += std::stoi(temp_str);

      // printout the ongoing sum for testing purposes
      // you can remove this if you want
      std::cout << "\t" << temp_str << "\t" << sum << "\n";
   }

   // compare the complete sum with the number to compare
   if (sum == num_compare)
   {
      return true;
   }
   else
   {
      return false;
   }
}


        1       1
        9       10
19 does NOT sum to 15

        3       3
        2       5
32 does NOT sum to 15

        6       6
        9       15
69 sums to 15

        4       4
        4       8
-44 does NOT sum to 15

        5       5
5 does NOT sum to 15

        6       6
        3       9
63 does NOT sum to 15

        7       7
-7 does NOT sum to 15

        8       8
8 does NOT sum to 15

        2       2
        9       11
-29 does NOT sum to 15

        9       9
        0       9
        6       15
906 sums to 15


It might be better to rewrite the array to being a vector, it maintains its own count of elements so no need to have a separate array size variable:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <vector>

const unsigned num_to_compare = 15;

bool IsEqual(int num, unsigned num_to_compare = num_to_compare);

int main()
{
   std::vector<int> array = { 19, 32, 69, -44, 5, 63, -7, 8, -29, 906 };

   for (unsigned loop = 0; loop < array.size(); loop++)
   {
      if (IsEqual(array[loop]))
      {
         std::cout << array[loop] << " sums to " << num_to_compare << "\n\n";
      }
      else
      {
         std::cout << array[loop] << " does NOT sum to " << num_to_compare << "\n\n";
      }
   }
}


bool IsEqual(int num, unsigned num_compare)
{
   // get the absolute (positive) value of the passed number
   unsigned abs_num = abs(num);

   // turn it into a string
   std::string num_str = std::to_string(abs_num);

   unsigned sum = 0;

   // use a C++11 for loop to walk through the string
   for (auto loop: num_str)
   {
      // obtain each individual stringized digit
      std::string temp_str = { loop };

      // covert each digit to an integer and add to the sum
      sum += std::stoi(temp_str);

      // printout the ongoing sum for testing purposes
      // you can remove this if you want
      std::cout << "\t" << temp_str << "\t" << sum << "\n";
   }

   // compare the complete sum with the number to compare
   if (sum == num_compare)
   {
      return true;
   }
   else
   {
      return false;
   }
}


If you don't want to do the string manipulation there is ways to parse out the individual digits using the modulo operator:

http://stackoverflow.com/questions/4261589/howto-split-a-int-into-its-digits
Topic archived. No new replies allowed.