Hello!

I need help with this question. My main problem is that I dont know how to sum values in a vector!

En vector with integer stored in :
int Mat_data_Vektor [10 ] ;

a) Write a for loop that sums the ten integer values ​​in the array above .

b ) Print The largest value in the array. Use System.out.println ( ) to print until
Serial Monitor.

Thanks!

My main problem is that I dont know how to sum values in a vector

That's OK. The Arduino does not do vectors.

a) Write a for loop that sums the ten integer values ​​in the array above .

Are you serious? This is a day-one exercise in any C class. Look at the documentation for the for statement and the array page. No, I'm not going to tell you where. You should KNOW.

b ) Print The largest value in the array. Use System.out.println ( ) to print until
Serial Monitor.

What jackass told you to do that?

As Paul say, this is really easy-peasy stuff
Which part of it are you stuck on ?

The addiation part! I know it is really easy but i can't find it! Like; (1+2+3+4+5+6+7+8)?

Set a variable, let's name it total, to zero, then use a for loop to go through each of the array elements and add the current one to total.

melker:
The addiation part! I know it is really easy but i can't find it! Like; (1+2+3+4+5+6+7+8)?

Arrays in the C/C++ programming language are zero based. So the last index in your vector/array with 10 elements is 9.

``````Mat_data_Vektor[0] + Mat_data_Vektor[1] + Mat_data_Vektor[2] + ... + Mat_data_Vektor[9];
``````

melker:
b ) Print The largest value in the array. Use System.out.println ( ) to print until
Serial Monitor.

System.out.println?
Really?

Thanks! something like this?:

Int Mat_data_vektor[10];
Int total=0;
Int i;

Void setup () {
serial.begin (9600)
}

Void loop() {

for(i=0; i<= 10; i++); {
int value =total+X
}
serial.println (value)
}
}
Something like this? Thanks!

i meant Int value = total + X

something like this?

Did you compile that first? No, of course not.

i meant Int value = total + X

Why? Personally, I'd have used total = total + Mat_data_vektor[ i ];.

Thats right paul, I wrote wrong, im used to name my array to X*! Thanks alot*

melker:
Thanks! something like this?:

Yes, something similar.

But you will have to avoid numeric overflows as well as buffer overflows and the for-loop has to be done correctly.

If the single elements of your array are 'int' and you don't know about the values you are adding, it would be more safe to use a bigger variable for the result, such like 'long' when adding several 'int' values:

``````long total=0;
``````

And this would create a buffer overflow if you'd really access your array:

``````for(i=0; i<= 10; i++);
``````

There is no element with index 10 in your array, so the for-loop compasison has to be:

``````for(i=0; i<10; i++) ...
``````

In that case the index is increasing from 0 to 9, which is 10 elements.

Besides of that you should not define a do-nothing loop. The syntax of a for-loop is either:

``````for(...) singleStatement;
``````

So after the round brackets there follows a single statement and a semicolon that finishes the for-loop.

Or the syntax is:

``````for(...)
{ // curly bracket to start a code block
statement1;
statement2;
...
} // curly bracket to end a code block
``````

And finally the name of the vector elements is Mat_data_vektor and not X. So you better add:

``````total =total+Mat_data_vektor[i];
``````

or shorter:

``````total += Mat_data_vektor[i];
``````

melker:
En vector with integer stored in :
int Mat_data_Vektor [10 ] ;

a) Write a for loop that sums the ten integer values ​​in the array above .

b ) Print The largest value in the array. Use System.out.println ( ) to print until Serial Monitor.

dude, is this a homework question? Sure smells like one. Can you write a for(; loop at all? Do you know how to print each value in the array?

No, not something like that, value goes out of scope when your for loop ends. It is also being redeclared all the time. You also need to watch what you use upper case characters for, C++ is case sensitive. I put it all in setup() so it only executes once.

``````// Something more like this:

int Mat_data_vektor[10] = {0,1,2,3,4,5,6,7,8,9}; // have something to add up

void setup ()
{
Serial.begin (9600);

int value = 0;
for(int i=0; i<= 10; i++)
{
value += Mat_data_vektor[i]; // equivalent of value = value + Mat_data_vektor[i];
}
Serial.println (value);
}

void loop() {
}
``````

You really need to pay attention to syntax and in particular what this ';' guy does.