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.
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.
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.
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
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.
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];
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.
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.
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. .
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