Array sum

So I have to create text file from cpp. Everything worked fine before i added functions for array sum...

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
 int main () {
 int n = 100, sum = 0;

 int arr[] = {54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12,
                34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,
                29, 23, 83, 95, 25, 91, 33, 47, 24, 62, 13, 42, 73, 44, 78, 72, 7, 5,10, 48, 71, 18,
                39, 97, 64, 79, 51, 74, 31, 37, 57, 30, 94, 80, 28, 1, 56, 85, 46, 100, 82, 40, 26,
                21, 68, 43, 14, 3, 65, 99, 89, 52, 84, 36, 2, 6, 11, 50};

  int length = sizeof(arr)/sizeof(arr[0]);

  for (int i = 0; i < length; i++);

  for(int i = 0; i<n ; i++){
      sum+=arr[i];
  }

  ofstream myfile ("input.txt");
  if (myfile.is_open())
  {

    myfile << "Reversed array:\n";
    for (int i = length-1; i >= 0; i--) {
        myfile << arr[i] << " " ;
        myfile <<"The array sum is:\n "<<sum;
    }

    myfile.close();
  }

  return 0;
}
Line 12: The loop does nothing
Line 14: Use length instead of n. The best would be to remove n entirely.
Line 25: I guess you want the '\n' after sum not before.
 
 for (int i = 0; i < length; i++);


That code doesn't do anything useful!

You are writing the array sum to the file for every array element!

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

int main() {
	int sum {};

	const int arr[] {54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12,
				   34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,
				   29, 23, 83, 95, 25, 91, 33, 47, 24, 62, 13, 42, 73, 44, 78, 72, 7, 5,10, 48, 71, 18,
				   39, 97, 64, 79, 51, 74, 31, 37, 57, 30, 94, 80, 28, 1, 56, 85, 46, 100, 82, 40, 26,
				   21, 68, 43, 14, 3, 65, 99, 89, 52, 84, 36, 2, 6, 11, 50};

	const size_t length {sizeof(arr) / sizeof(arr[0])};

	for (size_t i = 0; i < length; ++i)
		sum += arr[i];

	std::ofstream myfile("input.txt");
	if (myfile.is_open()) {
		myfile << "Reversed array:\n";
		for (size_t i = length; i > 0; --i)
			myfile << arr[i - 1] << " ";

		myfile << "\nThe array sum is: " << sum << '\n';
	}
}

Thank you!
It can also be done as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>

int main() {
	const int arr[] {54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12,
				   34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,
				   29, 23, 83, 95, 25, 91, 33, 47, 24, 62, 13, 42, 73, 44, 78, 72, 7, 5,10, 48, 71, 18,
				   39, 97, 64, 79, 51, 74, 31, 37, 57, 30, 94, 80, 28, 1, 56, 85, 46, 100, 82, 40, 26,
				   21, 68, 43, 14, 3, 65, 99, 89, 52, 84, 36, 2, 6, 11, 50};

	std::ofstream myfile("input.txt");

	if (myfile) {
		myfile << "Reversed array:\n";
		std::copy(std::rbegin(arr), std::rend(arr), std::ostream_iterator<int>(myfile, " "));
		myfile << "\nThe array sum is: " << std::accumulate(std::begin(arr), std::end(arr), 0) << '\n';
	} else
		std::cout << "Cannot open file\n";
}

Topic archived. No new replies allowed.