2

I misstook arrays for vectors, Sorry (array is vektor in swedish)

I would need some help with a program I'm making. It is a assignment so I really need to understand how I do this and not just get the code :P

I need to make a array containing 10 "numbers" (I would like to make them editable when the program is running). After I'v done this I need to make the program calculate the "average value" of all the numbers "/

Would be pretty neat if you could pick how many numbers you wanted the average value of as well, if anyone could share some knowledge in how I should to that :P Anyways, I'v tried some code to make the vector that didn't work, I might as well add it here:

int vector[10];

and vector[0] "number 1: "; and so on for the input of numbers in the vector.

int sum = vector[0] + vector[1] + ...
cout << "average value is: " << sum/5;

should work for getting the average value though (right?)

I should allso add:

float average(int v[], int n)

to this thing as well, can't really se how though.

Any help/knowledge at all would be awesome! Cheers.

5
  • start with an implementation of average() which returns the average of an array with length 1 (n=1). Commented Feb 15, 2014 at 18:09
  • I would not name any variable vector since it is a name in namespace std, especially since you seem to be using using namespace std; (which often is a bad idea). Commented Feb 15, 2014 at 18:14
  • Are you trying to do this using the std::vector or an array? Commented Feb 15, 2014 at 18:17
  • I pretty much have no idéa how to do this "/ according to the book int temp[5]; should create a vector with 5 values. I will name it v then "/ could do without namespace std; then? I have not even learnt what a array is and std::vector is unknown to me as well :c Commented Feb 15, 2014 at 18:22
  • It doesn't create a vector with five values. It creates a raw array with five values. A "vector" is a different type of container. What book are you using, by the way? Commented Feb 15, 2014 at 18:36

4 Answers 4

2

To pick how many numbers you wanted to average:

Native: (G++/Clang) only, not "legal" C++

cin >> num;
int vector[num];

"Correct" native (pointers):

int *vector = new int [num];

"Proper" C++:

#include <vector>
std::vector<int> v(num);
Sign up to request clarification or add additional context in comments.

5 Comments

So for visual studio 2013 I should use: #include <iostream> #include <vector> // To add the ability to even use vectors at all: std::vector<int> v(10); // What is std for here? would float work as well?:
2nd or 3rd, they are all fine, though I would recommend the third one
Using float or int depends on whether the user should be able to enter numbers with decimals (then use float or double) or not (then int is completly fine - at least for the input).
aha, yeah. so I could use double? it would work as int with numbers like 6,7,3?
Yes. When your vector stores int then the user just can enter ...-1, 0,1,2,... But when you use float or double the user can enter ...,-1,0,1,2,... but additionally also numbers like 1.2 or -4.2.
1

A function like following would work for computing average of an array containing n elements.

float average(int v[], int n)
{
    float sum = 0;
    for(int i = 0 ; i < n ; i++)
    {
       sum += v[i]; //sum all the numbers in the vector v
    }

    return sum / n;
}

You can declare your array as you have done however i do recommend you to name it something else then vector to avoid confusion. About tour issue with changing the numbers in the array you can do this by for example maning a loop going from one to 10 and then make the user enter values for all the fields.

Vektor på svenska = array på engelska (vector är något annat :))

Comments

1

If you want exactly 10 numbers, you can eliminate a lot of overhead by simply using an array. However, assuming you want to use a vector, you can easily find the average taking advantage of its "size" member, as such:

float average(std::vector<int> nums)
{
    int sum = 0;
    for (unsigned int i = 0; i < nums.size(); i++)
        sum += nums[i];
    return sum / nums.size();
}

Note that this does assume the sum won't be higher than 2^31-1, IE the highest number a signed integer can represent. To be safer you could use an unsigned and/or 64 bit int for sum, or some arbitrary precision library like gmp, but I'd assume that is all outside the scope of your assignment.

1 Comment

I misstook vector for arrays :c sorry, but now I learnt what a vector is so I guess that is something! Thanks
1

You must declare and array of size 10, which you have done.

Use a loop to get ten inputs from the user. (for or while loops would do)

Use another loop to calculate the sum of all ten numbers and store it in a variable.

Divide the variable by ten.

This is what you need to do essentially. But, to make your driver program prettier, you can define the following functions:

void GetInput(int *A); //put the input loop here

You can also write any one of the given two functions:

long Sum(int * A)   //put the summing loop here

double Average(int * A)  //put the summing loop here AND divide the sum by ten

Since you are a beginner I feel obliged to tell you that you don't need to return an array since it isalways passed as a reference parameter. I did not bother to pass the array size as a parameter to any functions because that is fixed and known to be 10 but it will be good practice to do that.

3 Comments

Sorry but I can sadly not understand much of that "/ in the worthless book I should get this knowledge is tells me that int n[10]; should work to make a array.. But it doesent
As for book, go for Deitel and Deitel. You can also consult, learncpp.com. The slashes represent comments, comments are not executed as program statements, they are ignored by the compiler
int n[10] does make an array. But you will need to initialize it with numbers. If you are new then I should also tell you that this statement makes an array of size ten with indeces ranging from 0 to 9 inclusive. You need to store integers in the array for it to give you a reasonable output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.