Thread: Some Beginner/array Help.

1. Some Beginner/array Help.

Hey guys, I've been a long time fan of programming and have dabbled into it abit here and there in some java/html etc. I've decided to have a go at C++ and I'm having a few problems. I understand the basics of variables/functions and how things work, but im having a few slight problems.

I wanted to create a small program that asked the user to input a series of numbers, and then display the sum of these numbers. Ive been reading some tutorials and example code and to my understanding the best way to do this would be to create an array to store the data in, and create a function to enter the data, and a function to calculate the sum. I believe in theory I have the right Idea but im having a few small problems.

When it comes to functions, I understand how they work for the most part, but I am a bit confused as to where the arguments if any come from in the () when declaring the function. If somebody could help me out with this it would be great.

Anyways here is the code that I have came up with, but when compiling I get some errors.

Code:
```#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

// declare some variables
int number;
int arraylength;
int array1[arraylength];

//funtction to get the numbers entered

int numbers(){

for (i = 0;i <= arraylength;i++) {

cout << "Please enter number " << i << " of " << arraylength;

cin >> number;

array1[i] = number;

}

}

//function to get the sum of the numbers entered in the array

int sumofarray(array1[i];arraylength) {

for (i = 0; i <= arraylength;i++){

acumulate = acumulate + array1[i];

}
return acumulate;

}

//main function

int main(int nNumberofArgs, char* pszArgs[]){

int sum;

cout << " This program lets you enter a series\n of numbers and gives you the sum.\n Please enter how many numbers you want to add";

cin >> arraylength;

cout << " you have chosen " << arraylength << " numbers. /';

//call the function to enter numbers into the array

numbers();

//get the sum of the numbers entered in the array

sum = sumofarray();

cout << " You have entered all the numbers. The sum of these numbers is " << sum;

system("PAUSE");
return 0;

}```

And here is the error log:
\arrays.cpp(12) : error C2057: expected constant expression
\arrays.cpp(12) : error C2466: cannot allocate an array of constant size 0
\arrays.cpp(18) : error C2065: 'i' : undeclared identifier
\arrays.cpp(18) : error C2065: 'i' : undeclared identifier
\arrays.cpp(18) : error C2065: 'i' : undeclared identifier
\arrays.cpp(20) : error C2065: 'i' : undeclared identifier
\arrays.cpp(24) : error C2065: 'i' : undeclared identifier
\arrays.cpp(31) : error C2065: 'i' : undeclared identifier
\arrays.cpp(31) : error C2143: syntax error : missing ')' before ';'
\arrays.cpp(31) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>arrayfun - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

If someone here more experienced could lend me a hand and point me in the right direction as to what im doing wrong, it would be great. Thanks.

2. There's so much wrong here, I don't know where to begin:

Code:
`int sumofarray(array1[i];arraylength) {`
You need to pick up a C++ programming book, or read through some online tutorials.

3. Originally Posted by Jimdubbs
Hey guys, I've been a long time fan of programming and have dabbled into it abit here and there in some java/html etc. I've decided to have a go at C++ and I'm having a few problems. I understand the basics of variables/functions and how things work, but im having a few slight problems.

I wanted to create a small program that asked the user to input a series of numbers, and then display the sum of these numbers. Ive been reading some tutorials and example code and to my understanding the best way to do this would be to create an array to store the data in, and create a function to enter the data, and a function to calculate the sum. I believe in theory I have the right Idea but im having a few small problems.
That's a really really really really really really really bad understanding. Arrays are neither necessary nor desirable for this problem (of adding up a series of numbers).

That said: you must know, right now, how many numbers you are going to have, if you want to have an array. If you intend to ask somebody later on how many numbers there are, you can't use an array because you must know, right now, how many numbers you are going to have. C++ has some "containers" (such as vector) that will allow you to store some unspecified number of numbers.

4. All that needs to be done is to allocate the array then delete when finished with, and no need for globals.
Code:
```void numbers(int *array1, int arraylength){
for (int i = 0;i < arraylength;i++) {
cin >> number;
array1[i] = number;
}
}

int sumofarray(int *array1, int arraylength) {
int acumulate =0;

for (int i = 0; i < arraylength;i++){
acumulate = acumulate + array1[i];
}
return acumulate;
}

int main(int nNumberofArgs, char* pszArgs[]){
int sum;
int arraylength;
int *array1;

cin >> arraylength;

array1 = new int[arraylength];  // allocate the array.

numbers(array1, arraylength);
sum = sumofarray(array1, arraylength);

delete [] array1;   // delete the array.

return 0;
}```

5. Don't hand out solutions!
Furthermore, your solution is not desirable either, specially since it uses new/delete when not needed and because you can do this without arrays!
(And who wants to ENTER the array length? That is an artifact of C, if anything.)
Plus the code is riddled with input buffer problems.

6. Hi, Yes I known my solution is not desirable, but, I was answering the question that was being asked i.e. using a variable length array and passing arrays as function parameters, by using the example of the code given.

That's why the person had come to the forum is the first place was to get an answer to a question !

7. Originally Posted by Scarlet7
Hi, Yes I known my solution is not desirable, but, I was answering the question that was being asked i.e. using a variable length array and passing arrays as function parameters, by using the example of the code given.
Which can easily be done by a vector, without new/delete.
And why use the poor solution when there's a better one? You should strive to teach people better solutions instead of using older solutions, if you can.

That's why the person had come to the forum is the first place was to get an answer to a question !
This does not mean you should hand out solutions, because most likely the OP will not learn properly.
A good programmer can search out resources him-/herself to solve problems, among other things. We strive to make newbies into good programmers.
And it's obvious that the OP does not fully understand the language itself, and that is a very bad thing™.
If the OP took some time properly studying a book, he/she would perhaps not be in so much trouble and keep asking questions about every little thing.

Popular pages Recent additions