*pointers with arrays

Hello everyone,

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.
¿anybody care enough to translate it?
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);
}
Thanks everyone.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iterator> // std::size, C++17

void printArray(int[], int);

int main()
{
   const size_t size1 { 5 };
   int arr1[size1] { 1, 2, 3, 4, 5 };

   printArray(arr1, size1);

   int arr2[] { 5, 10, 15, 20, 25, 30, 35 };

   // https://en.cppreference.com/w/cpp/iterator/size
   printArray(arr2, std::size(arr2));
}

void printArray(int arr[], int arrSize)
{
   for (size_t index { }; index < arrSize; index++)
   {
      std::cout << arr[index] << ' ';
   }
   std::cout << '\n';
}


std::vector makes passing a container to a function easier, no need to include the number of elements in the object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>

void printVector(std::vector<int>&);

int main()
{
   std::vector<int> vec1 { 1, 2, 3, 4, 5 };

   printVector(vec1);

   std::vector<int> vec2 { 5, 10, 15, 20, 25, 30, 35 };

   printVector(vec2);
}

// the vector is passed by reference so no copying is done
void printVector(std::vector<int>& vec)
{
   for (size_t index { }; index < vec.size(); index++)
   {
      std::cout << vec[index] << ' ';
   }
   std::cout << '\n';
}
Wow, you guys are way above my skill level. I may need a tutor or something. I just don't know if I can afford it lol.
There is an online tutorial here at CPlusPlus, a bit outdated.
http://www.cplusplus.com/doc/tutorial/

Another online tutorial that is more up to date.
https://www.learncpp.com/

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.
Thanks @Furry Guy. I taking a class right now. Just hard to get help without just getting the answer lol.
https://www.learncpp.com/

^ 100% Likely to be better than any class you're taking.
Learn C++ has the advantage of being able to "skip ahead" if/when the classroom instructions seems to lag behind one's ability to grasp the material.

https://en.cppreference.com/w/ is good at being a reference site working at a level above "Talk to me like I'm a 3 year old!"

Frustrating at times when you look up one concept and the example(s) show 3-8 other language features. Most you've never really understood.

What you were hoping was going to be a 30 second refresher turns into a several hour sojourn into other areas.
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.
Last edited on
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 



And so on..
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.

Like why can't I cout like this:

1
2

cout<<array[i];


Assuming you have a for loop. like um

1
2
3
4
for(int i = 0; i < 7; i++)
{
      //some stuff lol
}
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.
Topic archived. No new replies allowed.