how to use a function with pointer parameter to print array
Jul 20, 2013 at 6:36pm UTC
The printArray function should take in the dynamically created array and the size of the array as parameters. It should print out the contents of the array.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <string>
using namespace std;
void printArray (int *, int );
int main()
{
int numStudents;
int *movies;
cout << "How many students were surveyed?" << endl;
cin >> numStudents;
while (numStudents <= -1)
{
cout << "Enter a positive number." << endl;
cin >> numStudents;
}
movies = new int [numStudents];
//cout << "Enter the number of movies watched by each student: ";
for (int i = 1; i <= numStudents; i++)
{
cout << "Enter number of movies watched by student " << i << " : " ;
cin >> movies[i-1];
while ( movies[i-1] < 0)
{
cout << "Enter a positive number: " ;
cin >> movies[i-1];
}
}
printArray(movies, numStudents);
return 0;
}
void printArray (int *, int )
{
}
My problem is that I have no idea how to write the code to print the array using pointers. I've been stuck for awhile trying to figure it out.
Jul 20, 2013 at 8:16pm UTC
Well, that's only a skeleton, it doesn't even have names for the parameters. What code have you actually tried so far inside that function?
Topic archived. No new replies allowed.