comparing array

The homework assignment is to create a program that stores ten integers in an array. Then the array is used as an argument for a function that outputs the sum of all the negative numbers, the sum of all the positive numbers and the sum of all the numbers. I'm completely lost please help. The professor also wants us to us a header file.

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
  //Carlos Vargas
//homework 2 v2

#include <iostream>
#include <cstdlib>
#include <cstddef>

using namespace std;

int sumfunction(int arrayname[], int size_ofarray);
int sumfunction_neg(int negarray[], int negsizearray)

int main ()
{
	int ten_numberarray[10], i;
	cout << "Please enter ten numbers \n";
	
		
		
		for(i=0; i<10; i++)
		{
			cin >> ten_numberarray[i];
		}
		
			if ten_numberarray[i]>0
			{
				sumfunction(ten_numberarray, size_1)
			else
			
			sumfunction_neg(ten_numberarray,size_2)
			
			}

			cout<< "The sum of the positive numbers is "<<sumfunction_pos endl;
				
		    cout<< "The sum of the negative numbers is "<<sumfunction_neg endl;
			int result;
				result=sumfunction_pos+sumfunction_neg;
			cout<< "The sum of all the numbers is"<<result;
			
			system("pause");
				return 0;
}

int sumfunction_pos(int arrayname[], int size_ofarray)
{
int pos_array[];
int resultpos;

return resultpos;
	
}

int sumfunction_neg(int negarray[], int negsizearray) 
{
	int neg_array[];
	int resultneg;

return resultneg;

}
What's the problem? What about the assignment or program is confusing you?
I know what I need to do...I just don't know how to put it in code. Like for my if statement is that going to work? Will it test each individual element or do I need to use another for loop?
Last edited on
I think i need to use a pointer too. My professor yelled at me last time I asked him for help so I haven't really had the opportunity to ask anyone for help ANY input is appreciated.
My professor yelled at me last time I asked him for help so I haven't really had the opportunity to ask anyone for help ANY input is appreciated.

Ha ha! Looking at your code I can see why.
(Of course, I'm on your side. A good professor will have an almost infinite amount of patience.)

Back to your code, it is pretty bad. Is this below what you call an if() structure?

You forget to put the parentheses around the condition.
Then you mess up the curly braces.
Then you forget about the semicolon, mandatory at the end of an instruction.

25
26
27
28
29
30
31
32
			if ten_numberarray[i]>0
			{
				sumfunction(ten_numberarray, size_1)
			else
			
			sumfunction_neg(ten_numberarray,size_2)
			
			}


1
2
3
4
5
6
7
8
    if (ten_numberarray[i] > 0)
    {
        sumfunction(ten_numberarray, size_1);
    }
    else
    {
        sumfunction_neg(ten_numberarray,size_2);
    }


Later on, you do this:

result=sumfunction_pos+sumfunction_neg;

Since the two "sumfunctions" are functions, you must call them as such. After their name you must append parentheses. And if the functions need to be passed arguments, you give those arguments in the parentheses.

result = sumfunction_pos(arg1, arg2) + sumfunction_neg(arg3, arg4);

There are more things wrong here and there, but honestly I feel I'd be wasting both my time and yours by pointing them out.

So my suggestion now is this: read this site's C++ tutorial, and rewrite your program from scratch. This will take a fair amount of time and effort, so hopefully you're not pressured by some deadline that's near.

http://www.cplusplus.com/doc/tutorial/
Last edited on
I'll give some more help, as the OP sent me a PM which I could not completely understand. So I rather bump the thread than to reply.

The homework assignment is to create a program that stores ten integers in an array. Then the array is used as an argument for a function that outputs the sum of all the negative numbers, the sum of all the positive numbers and the sum of all the numbers.


OK. So first you store ten integers in an array. Then you pass this array to... a single function that does three different things at once? (At least that's how I read the above.)

So how should main() look?

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int ia[10];

    for (int i=0; i < 10; ++i)
    {
        cout << "ia[" << i << "]: ";
        cin >> ia[i];
    }

    super_function(ia, 10);
}


Then super_function() should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void super_function(int ia[], int size)
{
    int min_sum = 0;
    int max_sum = 0;
    int total_sum = 0;

    for (/* figure this out yourself */)
        if (ia[i] < 0)
            min_sum += ia[i];

    // write two more for() loops similar to the above

    cout << "Sum of negative numbers: " << min_sum << '\n';
    cout << "Sum of positive numbers: " << max_sum << '\n';
    cout << "Sum of all numbers: " << total_sum << '\n' << endl;
}


If you need to write different functions for each sum, it's still easy. Just move each of the for() loops into its own function.
Last edited on
Thanks a lot man you were really helpful.
Topic archived. No new replies allowed.