I want to die

How do I add in a vector, the sum of consecutive numbers, between these numbers. It's not HOMEWORK. I'm trying to learn. This is what i got so far. (I get
>"System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'"
for some reason. I've tried everything to fix it in the past 2 hours or more)

// suppose we set 5 values for vector : {3,4,5,6,7}. // It'll go through the vector till index [4]. It doesnt even get to the part where it should do the sum and replace the numbers and etc. I dont understand why it still goes through the IF condition if it doesn't respect it.

j = 4
j < vec.length is true
vec[j + 1] is out of bounds

i know that... but how should i fix it? I dont know any other way around... and i gotta use basic stuff to finish this, no special functions or anything (Array.Sort, count, etc... whatever)

your code looks fucking ugly

thank you im dying.

please help....

>How do I add in a vector, the sum of consecutive numbers, between these numbers.
can you explain this? what should [3, 4, 5, 6, 7] return? [3, 7, 4, 9, 5, 11, 6, 13, 7]?

vec[j+1] throws the exception
you have to check if j+1 is < vec.length

precisely.

1. loop array
2. print number at index i
3. check if index i + 1 < array length
4, print number + next number


or instad of print just add it to a new array

Stop using single letter variable names and abbreviations.

now it doesnt give me any error but it won't go throughout everything... fucking hell... i can't even think straight just from a basic exercise such as this... I hardly understand what i wrote...

have you guys been through this kind of shit when you started? Does it get easier or im just plain stupid and i shouldnt perform in this domain ? I mean, i like it, but i dont think i fit this. I mean, I havent done any math in highschool and now im in my 2nd college year and im studying c# and .net

You need to make a new array with a length of 2 times the original - 1. So if vec has a length of 4, the new one should have a length of 7.

are you a poo in loo? your "code" smells like curry...
also: FUCK OFF, THIS IS NOT A TECH SUPPORT BOARD

here's my solution in javascript, see if you can translate it to c#. the comments show the output array through the first loop.
const input_array = [3, 4, 5, 6, 7];
const output_array = [];

// [] -> [3]
output_array.push(input_array[0]);
for (let i = 0; i < input_array.length - 1; ++i) {
// 3 + 1 === 4
if (input_array[i] + 1 == input_array[i + 1]) {
// sum = 3 + 4 === 7
let sum = input_array[i] + input_array[i + 1];
// [3] -> [3, 7]
output_array.push(sum);
}
// [3, 7] -> [3, 7, 4]
output_array.push(input_array[i + 1]);
}

console.log(output_array);

>nice trips
The exercise says i should only modify the current vector, not create other vector.
I managed thanks to the help provided to get through the error. The problem now is that somehow it skips from {3,4,5,6,7} the "4" with "5" and "5" with "6"

the thing is that i cannot create a new vector. that would've made my problem much easier. But thanks for trying :(.

what are you, 8 or something ?

your solution is shit
>expression in loop condition evaluation
>non-explicit ==
>many unnecessary array lookups

> I mean, I havent done any math in highschool and now im in my 2nd college year and im studying c# and .net
How is that possible?
Before you can even take CS classes here, you need Calculus with One variable.

You wanna have two vectors you feed the first in then the sum then the second then the sum and so on. You have your old vector of size n wich means the next vector is 2*n-1 long.
std::vector input;
//fill vector input
std::vector output;
output.reserve(2*input.size()-1);
for(int cntr = 1; cntr < input.size(); cntr++){
output.push_back(input[cntr-1]);
output.push_back(input[cntr-1] + input[cntr]);
output.push_back(input[cntr]);
}
[\code]
This is c++. reserve means how much there is going to be in the array. push_back is adding an element at the end of the array. Oh and vector is array ofc.

If you need to add more elements to the vector, then you either need to make it bigger when you create it or make an ArrayList. You can't start with an array of size 4 and expand it to a size of 7.

Most important thing you need to learn when learning to code is to debug.
If you can't read your code and try to compute the execution in your mind to see where it goes wrong, just put debug everywhere (monitor each value on each iteration of the loop) until you spot where thing don't go the way you expect them to go. Then you can correct it, and actually learn something from your tribulations.

I cant go through "ArrayList"... the only thing i can use is Array.Resize, which i did... i somehow succeeded... except it doesnt include the sum of 4 with 5, and 5 with 6... because it replaces them when it runs and thus making them gone... And i cant even use 2 vectors... i'll just leave it the way it is and come back some other time later... it's been too many hours ....
i cant use 2 vectors... that's the problem. :(

is that some other language or .... really now, i have no idea what are those.
I've done a math class in my first year, passed it with 5/10... will have maths again next semester... oh well....

const arr = [3, 4, 5, 6, 7];

for (let i = 0; i < arr.length - 1; ++i) {
if (arr[i] + 1 === arr[i + 1]) {
let sum = arr[i] + arr[i + 1];
arr.splice(i + 1, 0, sum);
// skip the element just added
++i;
}
}

console.log(arr);

that's what im trying to do... but sometimes, the problem is that i dont know how to fix what i've done wrong in the code i've written. From what I've seen, they barely thought us the basics of c#... In Jan i got an exam where i should access an MS.Access database using a project with windows forms buttons and do some basic shit. Next year they'll teach us Java... Wish i had time to study on my own cause they dont teach us shit... I also have the economics part which I cant understand nor i want to... I have so many regrets...although, i wouldn't have survived in an IT college... Wish i made better choices when i was in 8th grade... They literally put us to choose between "Physics, Chemistry, Math" and "Literature and History"... guess what i picked.

i'll try to do it in c# later

i cant use "advanced" functions... that's the problem. The only "Advanced" thing i can use is "Array.Resize"...and no other vectors. Everything has to be done only 1 vector.
Will wait for the c# solution if this thread stays alive.

I don't really know what you're allowed to do, and what you aren't, so I'll keep it to your current array which should now be
3 4 5 6 7 _ _ _ _
Your main problem is that you overwrite your data as you try to go through them while applying changes. You have one solution that is a bit shitty in my opinion that would be FIRST, to move every number in their final position
3 _ 4 _ 5 _ 6 _ 7
Then make your sums. It's quite heavy.

A more elegant way (well, I'm pretty sure there is something better anyway), is to begin by the end.
3 4 5 6 [7] __ __ __ __
3 4 5 6 [7] __ __ __ [7]
3 4 5 [6] 7 __ __ __ [7]
3 4 5 [6] 7 __ __ 13 [7]
3 4 5 [6] 7 __ [6] 13 7

Well, you see the trick.

3 4 5 6 7 __ 6 13 7
3 4 5 6 7 11 6 13 7
3 4 5 6 5 11 6 13 7
3 4 5 9 5 11 6 13 7
3 4 4 9 5 11 6 13 7
3 7 4 9 5 11 6 13 7

This way, the data that you overwrite were already used, so it's not a big deal.

Hope it can help you.

I'm not sure what this Array.Resize() is. Granted, I have very little experience with Java, but I can't find a reference to it online. I do know that you cannot resize an array though.

Since you know how long it should be before you create it, then just make it the larger size initially. So if the input is a length of 5, make it 9 instead. Then just take an input of 5 integers and do the rest from there.

Also, since you keep mentioning vectors, Java has an actual vector class that's more flexible.

This is C#, not Java... I have no experience with Java, will have next year though. The values I've mentioned are just examples i gave...
Array.Resize(ref vector, #)

vector - the vector's name
# - the size of the vector resized

Mhmm... I'll make it from scratch and try it again. For some reason I can hardly believe this exercise is supposed to be this hard... comparing to the others I had...God knows what the hell...Thank you very much for explaining me in such detail and thank you everyone who helped as well.
If you have any tips on how should i proceed with programming so I can actually learn to code one day, it'd be nice.

can you make an insert function? maybe something like this (i'm just guessing what Array.resize does)
function insert(index, element) {
Array.resize(array, array.length + 1)
for (let i = index + 1; i < array.length; ++i) {
array[i] = array[i - 1];
}
array[index] = element;
}

nah... they never even thought us something like this in class...
I'll try this

Test regularly your program while you develop it so you're not overwhelmed if everything goes wrong and you don't know where it come from.
Sometime, don't be afraid to just throw part of your algorithm and redo it. You know what you want to do, try a different way and you might fix your code.
They are asking you to do stuff with things you didn't master, so don't be afraid to do really small steps and test every time.
Cut your problem into small tasks. You can resolve each small tasks so you can resolve the problem as a whole.
And it help to not feel completely dumb to successfully resolve small tasks one after the other. You will feel like you're progressing. Mindset is also important. If you feel like Hackerman, you'll enjoy programmation more and therefore, learn faster.

Since you sound like you're an uni students, it might help to have some paper, and to actually write down what you want to do, to better visualize how you should interact with your data structures, what sequence of instruction you need, etc.
A bit like what I've done here
Don't discourage yourself, programmation is all about making big mistakes, and remembering them because you lost your time and never want to lost it again.

I only wish I didn't have other unrelated classes to attend and study for... I'd have more time for programming... Getting into doing code only 2 days/week it's pretty hard... I tend to forget all the algorithms used and how i got to the end of a problem.
Again, thank you so much for everything. Merry Christmas :)

Hi I didn't read the thread but try using a while loop instead of a for loop.

What's happening is that the conditional break happens after it's out of index.

yah, i managed to get through that matter. The last value from the vector didn't even have anything to be compared with, neither it needed to do so as it was the last one. Thank you nonetheless :)

I meant, try a while loop or a for loop instead of a "do while"
Also you could try another conditional for breaking the cycle before its out of range