a function that computes the alternating sum of all elements in an array

Pages: 12
I'm working on a homework require me to write a function that print out alternating sum of all elements enter in an array. Followin is what I got so far.

#include <iostream>

using namespace std;

double alternating_sum ( int numbers[] )

{

double sum;

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

{
sum = sum + numbers[i-1] - numbers[i+1];

}


return sum;

}


int main()

{
double input_numbers[];

cout << " Enter a group of numbers to find the sum: " << endl;

cin >> input_numbers;

double result = alternating_sum(input_numbers[]);

system("pause");

return 0;

}

Question:

1. When I do double result = alternating_sum(input_numbers[]);
it shows error.

2. how do I use main program to calls this function using an array of your choice and prints out the resulting sum.

3. Did I mess up makin the function?


example if alternatingSum is called with an array containing

1 4 9 16 9 7 4 9 11

then it computes

1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11 = - 2
You may not define an array with unknown size

double input_numbers[];

Ypu shall specify its size in the declaration.

Also you cna not enter elements of the array such a way as you are doing

cin >> input_numbers;

You shall eneter each element of the array separately in a loop.

Your function alternating_sum could look like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double alternating_sum ( double numbers[], int size )
 
{
 
   double sum = 0.0;

   for ( int i = 0; i < size; i++ )
   {
      sum += ( i % 2 == 0 ) ? numbers[i] : -numbers[i];
   }

 
   return sum;
}
Last edited on
Vlad from moscow's point about the array with unknown size seems off, because you CAN declare dynamic arrays like that, but I didn't actually read the code too carefully. Also, his comment about

cin >> input_numbers;

is logical and possible, but an easier way to replace this code (which won't work) is to use cin.get, if you know how long you want the input to be. i.e.

1
2
3
cin.ignore(80, '\n') //basically a buffer flush
cin.get(input_numbers, NUM_OF_DIGITS);
cin.ignore(80, '\n'); //another flush 


where NUM_OF_DIGITS is replaced with the number of characters you want to get.
Also, I do not think this:

1
2
3
4
5
6
for (int i = 0; i < 100; i++)

{
sum = sum + numbers[i-1] - numbers[i+1];

}


will accomplish your goal. Instead, try:

1
2
3
4
5
6
7
8
9
10
11
12
13

sum=numbers[0]; //start with initial value

for(int i=1; i<100; i++) //initialize i to 1 because the value at numbers[0] is already inside the sum
{

if(((i+2)%2)==0) //if POSITION IN ARRAY is even, then...
{sum=sum+numbers[i];}

else //if POSITION IN ARRAY is odd, then...
sum=sum-numbers[i];

}


that should work. The reason I added 2 is because I don't know what 0%2 would return, but it may not be necessary. It was just a precaution.

NerdTastic (39)

Mar 14, 2012 at 8:23pm



Vlad from moscow's point about the array with unknown size seems off, because you CAN declare dynamic arrays like that,


There is no such type as "dynamic arrays" in C++. So you are wrong and one more wrong. In C++ it is impossible to declare arrays of unknown size. When you are using, for example, code as the following

double * p = new double[x];

then you are declaring a pointer not an array.
In c++ you can declare

char x[];

This is perfectly legal. It will take the size of the first use given it. For example:

char x[];

cin.get(x, 10);
cin.ignore(80, '\n');

after the input, x[] will be somewhere between 1 and 10, depending on how long the input is.
I believe there are dynamic arrays in c++, not that I understand them, I've seen people talk about it here before, something like "int **x", that may be way off though.
NerdTastic wrote:
]n c++ you can declare

char x[];

This is perfectly legal. It will take the size of the first use given it. For example:


This is completely false.
Last edited on
This is completely false.


I literally just did it in a program, that runs fine.
Either you're trolling, or you're using one screwed up compiler. Either way, that isn't valid C++.
It's an ancient, pre-standard (and thus not C++) compiler (comes with iostream.h)
Im new in C++ i hope this work.




#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int z;
int S_a = 0 ;
cout <<"Enter the number of array element you to sum" <<endl;
cin >> z;
cout <<"Enter the numbers " <<endl;
int x[z];


for (int i = 0; i<z; i++)
{
cin >> x[i];
};

for (int i=0; i<z; i++)
{
S_a = S_a + x[i];
}




cout << S_a <<endl;


system("PAUSE");
}
1
2
3
4
5
6
int z;
int S_a = 0 ;
cout <<"Enter the number of array element you to sum" <<endl;
cin >> z;
cout <<"Enter the numbers " <<endl;
int x[z];


This is not valid C++. Variable length arrays are a part of C which was not included in C++. A compiler allowing it does not make it valid. It makes the compiler non-conforming. If you're using a gcc variant compile with -pedantic.

Last edited on

naqib (4)

Mar 16, 2012 at 9:09pm

Im new in C++ i hope this work.

int main(int argc, char *argv[])
{
int z;
int S_a = 0 ;
cout <<"Enter the number of array element you to sum" <<endl;
cin >> z;
cout <<"Enter the numbers " <<endl;
int x[z];



No, thsi code does not suttisfy C++ standard. This can be done only in a C-compiler that supports the C99 Standard. Nevertheless thsi is incorrect code according to the C++ Standard of any release.
why is it not in the standard? seems like a pretty useful thing to be able to do...

then again, a lot of useful things tend to be non-standard.
Last edited on
There are much useful things that are present in C and are absent in C++ and vice versa there are much useful things in C++ that are absent in C.
In fact C and C++ are two different languages. .
why is it not in the standard?


int x[z]; is forbidden in C++, unless z is a constant known at compile-time. Why? Here's a good chat about it: http://groups.google.com/group/comp.std.c++/browse_thread/thread/2bfe25800d4961e8/9545494bbb336dfa
Last edited on
I told u all im new in C++ but i did this According to the request
by the way i Just start C++ one week ago without any course just by self
i will try do thing standard

Im using Dev C++.

how do i know if this is a C# language;



tnx from all of u
Last edited on
dev c++ is out of date, I have no idea why so many beginners seem to pick it, use Code::Blocks
Pages: 12