If you have a pointer that points to an array and you are making a function. You have your for loop and counter, etc. How does one use the pointer to calculate an average? I'm having trouble with that. I mean I know basic average, but for me its hard to wrap my head around. The pointer has to pass through each element in the array and then you get your total. Then to my knowledge that total is used for the average. For example, average = sum / ?. Earlier I did *pointer = prices; This is for an example. However if you have a better way of showing that would be great. I love learning C++ but it doesn't always click. If I'm being honest, most of the time it doesn't but I'm really eager to learn. For the example if you have one just pretend that I have no clue lol.
When passing an array into a function, you're really passing a memory address pointing to the first element of the array. So passing in the array is no different than passing a pointer to the array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int avg(int arr[])
{
int a = 0;
for (int i = 0; i < 5; i++)
a += arr[i];
return a / 5;
}
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *pnt = arr;
std::cout << avg(pnt);
}
When passing a regular array to a function it is recommended you also pass the array's size. The number of elements. Hard-coding that information limits the reusability of your function.
Keep writing code, keep challenging yourself. I am still very much learning the C++ language, on my own from books and online. I've been trying to self-teach even before there was a standard, C++98.
I agree. Like I'm not stupid but it makes you feel kinda stupid. Especially when there are multiple ways to get the answer. Plus my brain works really different. I overthink a lot. Also, in this class we have to make a flowchart. I can't make a flowchart first, I have to do the code first and work backwards. I don't know if any has that problem. Then, after doing all this code, I'm like oh s*** I have no clue how to turn this into a flow chart.
A flowchart huh.. Probably a technique that professors liked using when learning or found that a few students liked to do, so they decided to force it on everyone. The point of something like a flowchart is to help make things click, if you have to go out of your way to make the flowchart, it kind of defeats the purpose.
I also had to make a flow chart in my first CS class, never actually did it correctly though. I'd always forget when to use a circle or triangle, ended up making a key at the bottom to show what I was doing.
However, it's pretty simple - just follow the logic of your program start to finish. You hit an if statement, this will create a new branch on the chart. One branch if the statement is true, and another if false. Once that's over, you'll likely reconnect the two branches on the next step, since whether true or false, they'll both likely go on to do the same thing later:
1 2 3 4 5
if(x == y)
//Do Something
//Do something else
//^Whether or not the if statement was true or false, both pathways will lead to the execution of this next part
Thanks @zapshe. I feel stupid asking so many questions. I work every day for hours on my assignments. Like some stuff I wish I could figure out, but I can't.
Hopefully, your array is not called "array", since this is a keyword. The only reason an array can't be outputted that way is if it's an array of a user-defined type, like this:
1 2 3 4 5 6 7 8 9 10 11
struct data
{
int a = 10;
int b = 20;
};
int main()
{
data var[5];
std::cout << var[1]; //What would this output??
}
In this example, it can't read your mind and know what you want to output, so it'll give you an error.
You should start a new topic and post the code to your question.