C++ Adding Sums With Void Functions HELP

Sup Guys, having trouble with the program. I have part 1 complete already but im having trouble with the 2nd part. I made an attempt at it (you can see in the code below) Can Someone tell me what im doing wrong?

PART 1 - 1) This first function initializes an array of 30 components so that the first 15 components are equal to the square of the index value and the last 15 components are equal to the index value multiplied by 3.



PART 2 - 2) The second function processes the array by finding the sum of the first 15 components and the sum of the last 15 components to determine which sum is bigger. The output to the screen should do the following:
a) State “The sum of the first 15 components is:” and then show the sum.
b) State “The sum of the last 15 components is:” and then show the sum.
c) State which of the two resulted in the greater sum or if the two sums were equal.


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Name:
Date:
Program Name: Practice with Arrays
Function: The program uses two subroutines.  One to initialize an array and the other
to process the array and print to screen results
*/

#include <iostream>
#include <iomanip>
// Include any other header files you may need.

const int ARRAY_SIZE = 30;
void initialize ( double list[], int index );
void square ( double list[], int index );
void threeTimes ( double list[], int index );
void output ( const double list[], int index );
void FirstSum(double list[], int index);
void SecondSum(double list[], int index);

// Include here the function prototypes remembering that the formal parameter list
// should not include names of variables declared in the main function.

using namespace std;

int main()
{
	const int size = 50;
	double alpha[ARRAY_SIZE];
	int counter;
	int firstSum;
	int secondSum;

	cout << "This first function initializes an array of 30 components so that" << endl
		<< "the first 15 components are equal to the square of the index value and the" << endl
		<< "last 15 components are equal to the index value multiplied by 3." << endl << endl;

	initialize ( alpha, ARRAY_SIZE );

	cout<<"The values in the array after initialization are: "<<endl;
	for ( counter = 0; counter < 30; counter++ )
		cout<<alpha[ counter ]<<" ";
	
	cout<<endl<<endl;

	square ( alpha, ARRAY_SIZE );
	cout<<"The values in the array after using the square function are: "<<endl;
	for ( counter = 0; counter < 30; counter++ )
		cout<<alpha[ counter ]<<" ";

	cout<<endl<<endl;

	threeTimes( alpha, ARRAY_SIZE );
	cout<<"The values in the array after using the function threeTimes is: "<<endl;
	 for ( counter = 0; counter < 30; counter++ )
		cout<<alpha[ counter ]<<" ";
	
	cout<<endl<<endl;

    output( alpha,ARRAY_SIZE ); //OUTPUT FUNCTION
	cout<<endl;
	// Insert the call to the function that initializes the array

	cout << "The second function calculates which is bigger - the sum of the first 15 elements" << endl
		<< "or the sum of the last 15 elements.  The output shows the sum of the first 15 elements on" << endl
		<< "one line and the sum of the last 15 elements on the second line.  The third line states" << endl
		<< "which sum is larger or if the sums are equal. The numbers are formatted so that each number is provided" << endl
		<< "in a set width of 10 spaces with two digits displayed to the right of the decimal point." << endl << endl;

	FirstSum( alpha,ARRAY_SIZE );
	cout << endl;
	cout << endl;
	SecondSum( alpha,ARRAY_SIZE );
	cout << endl;
	cout << endl;
	// Insert the call to the function that prints out the array as specified

	cout << "ALL DONE!" << endl;

	return 0;
}

void initialize ( double list[], int index )
{
	int counter;

	for( counter = 0; counter < index; counter++ )
		list[counter] = counter + 1;
}

void square ( double list[], int index )
{
	int counter;
	for ( counter = 1; counter <= (index / 2); counter++ )
		list[counter-1] = counter * counter;
}

void threeTimes ( double list[], int index )
{
	int counter;

	for(counter = (index / 2); counter < 30; counter++)
		list[counter] = (counter * counter)*counter;

}

void output (const double list[], int index )
{
	//VARIABLES TO CONTROL THE LOOPS
	int counter;
	int counter2;

	counter = 0;
	counter2 = 10;
	//SETS OUTPUT FORMAT
	cout<<fixed<<showpoint;
	cout<<setprecision(1);
	//OUTPUTS EVERY VALUE IN THE ARRAY
	for ( counter = 0; counter < index; counter++ )
	{
		cout<<list[counter]<<" ";
		//AFTER THE ARRAY OUTPUTS 10 VALUES THE SCREEN WILL BEGIN A NEW LINE
	while ( counter2 == (counter+1) && counter2 < index )
	{
		cout<<endl;
		counter2 += 10;
	}
	}
}

void FirstSum(double list[], int index)
{
	double sum = 0;

	for(int index; index <= 15; index++)
		sum = sum + list[index];
}

void SecondSum(double list[], int index)
{
	double sum = 0;

	for(int index; ( index >= 15 && index <= 30 ); index++)
		sum = sum + list[index];
}


Last edited on
My void FirstSum & SecondSum arent working right...
Please count the number of functions in your code and in your assignment

1) This first function i....
2) The second function processes ...
I have the functions broken up
but would still be nice to get help with Part 2...
I haven't read your code accurately, but I think one of the problems lies in the fact that you pass that array by value, and not by reference. Because of that, the changes made to the array (in initialize() for example) are only made to a local copy of that array, and not to the one declared in main().

You can pass an array by reference like this:
 
void initialize( double ( &list ) [ ARRAY_SIZE ], int index );
Topic archived. No new replies allowed.