I'm working on a program assignment and the instructor wants us to use void functions. I'm not understanding the reason these are used anywhere. Wouldn't it just be easier for me to declare variables and cout those? I'm working with arrays. Does using void with arrays have any benefits to not using it?
Void function as in functions that take nothing or return nothing?
Functions are generally created for one of three purposes:
1. When there's a bit of code that shows up often, making a function out of that code will reduce the room for error and file sizes.
2. To split up a program into pieces that improve readability and can be split over multiple files if necessary.
3. When creating a library.
As for your arrays question, could you please elaborate?
The brackets on void functions are no different from other functions, meaning that your change you made messed it up further.
Weren't you supposed to read into levelsArray on lines 20 and 35?
You're still missing a closing bracket on line 43.
You need to get rid of the s on line 42.
You still haven't switched lines 40 and 42.
Line 25 is still wrong. i should start at zero and go up with each iteration of the loop. Similarly, on line 25, you need to check that i is less than ARSIZE or you'll have to face out-of-bounds issues. Finally, I think you were meant to return i instead of ARSIZE.
On line 48, you need to iterate over each element of the loop. I'm guessing you already learned about for loops? This is where they come in handy.
1 2 3
for(int i = 0; i < array_size; ++i)
Thanks for being so patient with me. Programming is kicking my butt this semester. I used for(i=0; i<ARSIZE; i++) to start my for loop. for line 50, I want to iterate over all three arrays so my chart has three columns.
The function article really helps but for arrays, how do I code them if I don't know how many levels the user is going to enter? I know I'm using ARSIZE for that variable but I don't understand how the computer will process that and form the end chart.
The start of the for loop bit sounds about right. If you kept the break condition at the end of what used to be your while loop, then if not for line 35 (what's different on this line compared to the previous two cin statements?), the loop should be alright.
Well, as for what you want to do regarding iterating over all three arrays at the same time, what's stopping ya from using i to sequentially access elements of those arrays too?
When you use ARSIZE, you're making an assumption that the user will want to enter no more than 400 values. For now, this is a fair assumption. When, in the future, you want the size of your array to be customizable at runtime, then you can use either the std::vector template or dynamic memory using new and delete. BUT! One thing at a time. Walk before you can run. :)