Incrementally adding and displaying array elements

Im trying to add the elements I entered into an array in increments until the total but I just get the exact same erroneous value when I enter it reaches the function call for the function designed to do it.

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
 #include<iostream>
#include<conio.h>
#include<math.h>

using namespace std;
int multiplyarray(int[]);

int main(){

int results[11];
int max=12;
int num[12];
int count=0;
int modarr[11];
int summarray[11];

    for(int count=0;count<max;count++)
{

cout<<"Please input a number";
cin>>num[count];

}

cout<< "The series is"<<endl;

    for(count=0;count<max;count++)
{

results[count] = num[count];
cout<<results[count]<<" ";
}

cout<<"\nModified array:"<<endl;

    for(count=0;count<max;count++){

modarr[count]= multiplyarray(summarray);
cout<<modarr[count]<<" ";
}

getch();
}

int multiplyarray(int num[]){

int sumarray[11];
int countm;
int total=0;


    for(countm=0;countm<12;countm++)
{
total=(num[countm]+num[countm+1]);

sumarray[countm]=total;

return sumarray[countm];
}



}
what is you intention on line 38? summarray is uninitialzed. I'd guess that you want to pass num? And yes you will get in modarr[count] always the same value because you do always the same.

In multiplyarray:

sumarray appears useless. Do you want to return total of the array? Why did you name the function multiplyarray? If you want the total do it like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
int multiplyarray(int num[]){

int countm;
int total=0;


    for(countm=0;countm<12;countm++)
{
total+=num[countm]; // Note: countm+1 is out of bounds for the last field
}

return total; // Note: return outside the loop
}
I want the total in increments line 38 was a function call
Topic archived. No new replies allowed.