My program for class right now is asking me to create an array using pointers? I this is the first time we're learning pointers, and the only available material to study is through a few online links given by the professor. I'm confused about the set up of the program that we're given and asked to finish.
#include <iostream>
#include <string>
usingnamespace std;
// Prototypes
int create_array(int);
void enter_data();
double find_average();
double show_array(int, int);
// main begins
int main()
{
int *dyn_array;
int students;
float avrg;
//int dyn_array;
do
{
cout << "How many students will you enter? ";
cin >> students;
}while ( students <= 0 );
dyn_array = create_array( students );
//this function creates a dynamic array
//and returns its pointer
/* enter_data( dyn_array, students );
//use 'pointer' notation in this function to
//access array elements
//accept only numbers 0-100 for movie seen
avrg = find_average( dyn_array, students );
//use 'pointer' notation in this function to
//access array elements
*/
cout << "The array is:" << endl << endl;
// show_array( dyn_array, students);
cout << endl;
// cout << "The average is " << avrg << ".\n";
// delete [] dyn_array;
return 0;
}
/////////////////////////////////////////////
// create_array function
int create_array(int f_students) {
int num_movies[f_students];
for (int i = 1; i <= f_students; i++) {
cout << "Enter number of movie(s) for student #" << i << ": " << endl;
cin >> num_movies[i];
}
}
/////////////////////////////////////////////
// enter_data function
void enter_data() {
}
/////////////////////////////////////////////
// find_average function
double find_average() {
}
/////////////////////////////////////////////
// show_array function
double show_array() {
}
I'm trying to figure out how to create this pointer array, since the declared pointer variable at the top of the main is *dyn_array, and then a little bit down the program is the call for the function create_array(students), this is being assigned to dyn_array again, but without the * being placed there. Is this a mistake?
I talked to some classmates, I think what I put inside the create_array() should go in enter_data(). I'm not sure still how to create the dynamic array and retrieve a pointer.
#include <iostream>
#include <string>
usingnamespace std;
// Prototypes
int *create_array(int);
void enter_data(int, int);
double find_average();
double show_array(int, int);
// main begins
int main()
{
int *dyn_array;
int students;
float avrg;
do
{
cout << "How many students will you enter? ";
cin >> students;
}while ( students <= 0 );
dyn_array = create_array( students );
//this function creates a dynamic array
//and returns its pointer
enter_data( dyn_array, students );
/* //use 'pointer' notation in this function to
//access array elements
//accept only numbers 0-100 for movie seen
avrg = find_average( dyn_array, students );
//use 'pointer' notation in this function to
//access array elements
*/
cout << "The array is:" << endl << endl;
// show_array( dyn_array, students);
cout << endl;
// cout << "The average is " << avrg << ".\n";
// delete [] dyn_array;
return 0;
}
/////////////////////////////////////////////
// create_array function that creates a dynamic array
// and returns its pointer
int create_array(int f_students) {
int *ptr = newint[f_students];
return ptr;
}
/////////////////////////////////////////////
// enter_data function uses 'pointer' notation to access
// array elements access only number 0-100 for movies seen
void *enter_data(int *f_dyn_array, int f_students) {
for (int i = 1; i <= f_students; i++) {
cout << "Enter number of movie(s) for student #" << i
<< ": " << endl;
//cin >> *f_dyn_array[i];
}
}
/////////////////////////////////////////////
// find_average function
double find_average() {
}
/////////////////////////////////////////////
// show_array function
double show_array() {
}
Right now I'm unsure about how to create a new data entry function. Should it be a pointer function too?