Array

Create a function call sumArray which will return the int sum of an array that is passed in. Note that a length of the array should also be specified.


input: arr = {1, 2, 3, 4}, length = 3, 
returns: 6 (because the 4 was ignored because it was past the length)
-----------------------------------------
Write a function printAccending, that will take an int array and the length as its input and output each integer on a line that is greater than the last. If one is smaller it will drop to a new line and continue from there:


input {2, 5, 8, 3, 9, 9, 7}


output: 

2, 5, 8

3, 9, 9

7

closed account (E0p9LyTq)
Where is the code you have written?
Okay I have this and I'm sure I code it correctly however, its not outputting is there an error for it?

int sum = 0;
int arr[] = { 1, 2, 3, 4 };
for (int i = 0; i < 3; i++)
sum += arr[i];
cout << "The sum of the array is: " << sum << endl;

As for the second part I am confused where to start:
is this what I do?

void increaseArray(int arr[], int length, int amount=1)
{
for (int i=0;i<length;i++)
arr[i] += amount;
}
closed account (E0p9LyTq)
PLEASE learn to use code tags, it makes reading your code MUCH easier.
http://www.cplusplus.com/articles/jEywvCM9/

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
#include <iostream>
#include <vector>

void PrintVector(const std::vector<int>&);
int Sum(const std::vector<int>&);

int main()
{
   std::vector<int> vec1 { 1, 2, 3, 4 };
   std::vector<int> vec2 { 2, 5, 8, 3, 9, 9, 7 };

   std::cout << "vec1:\n";
   PrintVector(vec1);

   std::cout << "\nvec2:\n";
   PrintVector(vec2);

   std::cout << "\nThe sum of vec1:\n" << Sum(vec1) << '\n';
   std::cout << "\nThe sum of vec2:\n" << Sum(vec2) << '\n';
}

void PrintVector(const std::vector<int>& vec)
{
   for (unsigned int loop = 0; loop < vec.size(); loop++)
   {
      if ((loop > 0) && (vec[loop - 1] > vec[loop]))
      {
         std::cout << '\n';
      }
      std::cout << vec[loop] << ' ';
   }
   std::cout << '\n';
}

int Sum(const std::vector<int>& vec)
{
   int sum = 0;

   for (const auto itr : vec)
   {
      sum += itr;
   }
   
   return sum;
}

vec1:
1 2 3 4

vec2:
2 5 8
3 9 9
7

The sum of vec1:
10

The sum of vec2:
43


Using regular arrays is more difficult, and can be prone to errors.
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
#include <iostream>

void PrintArray(const int*, const int&);
int Sum(const int*, const int&);

int main()
{
   int arr1[] { 1, 2, 3, 4 };
   int arr2[] { 2, 5, 8, 3, 9, 9, 7 };

   std::cout << "arr1:\n";
   PrintArray(arr1, 4);

   std::cout << "\nvec2:\n";
   PrintArray(arr2, 7);

   std::cout << "\nThe sum of vec1:\n" << Sum(arr1, 4) << '\n';
   std::cout << "\nThe sum of vec2:\n" << Sum(arr2, 7) << '\n';
}

void PrintArray(const int* arr, const int& size)
{
   for (int loop = 0; loop < size; loop++)
   {
      if ((loop > 0) && (arr[loop - 1] > arr[loop]))
      {
         std::cout << '\n';
      }
      std::cout << arr[loop] << ' ';
   }
   std::cout << '\n';
}

int Sum(const int* arr, const int& size)
{
   int sum = 0;

   for (int loop = 0; loop < size; loop++)
   {
      sum += arr[loop];
   }
   
   return sum;
}


C++ containers are MUCH easier to work with.
Topic archived. No new replies allowed.