creating dynamic array in getData function

Please don't laugh & I AM DOING THIS FOR SCHOOL SO RATHER THAN POSTING ANY CODE, I'D MUCH RATHER IF SOMEONE CAN MAYBE HELP TO JUST EXPLAIN how if i'm creating a pointer and dynamic array in a fuction, how do i get that pointer or pointer to first element of array out of the function? I'm a newbie with c++, & though i enjoy it very much, i obviously don't have that knack for it as you'll see from my code. My question is if i'm creating a dynamic array in my getData function how do i get the contents back out of the function? If i declare & pass a pointer in, it says it can't convert from int* to int when I use new to declare my array?
My pathetic code is below; Once again, i appreciate help but please dont respond with any code.

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

int getData (int *,int&);
void processData (int *, int *, int, int&);
void printHistogram (int *, int);

int main()
{
int numbOfStudents;
int numbOfQuestions;
int* scoresPtr;
int* frequencyPtr;

numbOfQuestions=getData(scoresPtr,numbOfStudents);
processData (scoresPtr, frequencyPtr, numbOfStudents, numbOfQuestions);
printHistogram (frequencyPtr, numbOfQuestions);

return 0;
}

int getData(int* scoresPtr,int &numbOfStudents)
{
int questions;
string fileName;
ifstream in;
cout<<"Please enter the full name of input file: ";
cin>>fileName;
cout<<endl;
in.open("fileName");
in>>numbOfStudents>>questions;
scoresPtr=new int[numbOfStudents];
for (int index=0;index<numbOfStudents;index++)
in>>scoresPtr[index];
in.close();
return questions;
}

void processData (int* scoresPtr, int* frequencyPtr, int numbOfStudents, int &numbOfQuestions)
{
numbOfQuestions=numbOfQuestions+1;
frequencyPtr=new int[numbOfQuestions];
int temp=0;
for (int index=0; index<numbOfStudents;index++)
{
temp=scoresPtr[index];
frequencyPtr[temp]++;
}
return;
}

void printHistogram (int *frequencyPtr, int numbOfQuestions)
{
int count=0;
while (count<numbOfQuestions)
cout<<frequencyPtr[count]<<endl;
}

While your insistence against code examples is commendable (unlike many, you aren't trying to get others to do your homework), it would be very difficult to help you without at least demonstrating the concepts. To this end, I have used a few very small snippets of code.

If you want to get a pointer from a function, simply return it from the function.
1
2
3
4
5
//returns a pointer to an int
int *pointer(){
  int *ptr;
  return ptr;
}


Or, you can pass a pointer to a pointer, like you attempted to do in your getData function.
1
2
3
4
5
//ptrarg is a pointer to a pointer to an int. 
void pointerarg(int **ptrarg){
  int *ptr;
  *ptrarg = ptr;
}

That function would be called like so:
1
2
3
  int *localptr;
  pointerarg(&localptr);
  //localptr has now been modified. 


Hope this helps.
Your help is much appreciated & working on a function that would return the pointer to my newly created dynamic array was exactly what i started to work on right after i posted my question & following is the small bit that i was able to get out in hopes of just trying to get the input file open & some values read into the array correctly. What i'm getting confused about is when you use the new operator in a statement such as: int*ptr=new int[x];, i realize that *ptr is a pointer to the first element of this new array, but where is it pointing to?

int * getScores(int&, int&);//This function will open the input file, read in the # of students that took the quiz followed by the # of questions
//on the quiz. Then after updating the reference variable passed as a parameter with the # of questions, it will
//create the dynamic scores array based on the # of students & read in the remaining scores returning to main the
//pointer to the newly created scores array & closing the input file.
void printHistogram(int, int);

int main()
{
int numbOfStudents, numbOfQuestions;
int scoresArray=*getScores(numbOfStudents, numbOfQuestions);
printHistogram(scoresArray, numbOfQuestions);

return 0;
}

int * getScores(int &noStudents, int &noQuestions)
{
string fileName;
ifstream in;
cout<<"Please enter the name of the file: ";
cin>>fileName;
in.open(fileName);
cout<<endl;
in>>noStudents>>noQuestions;
int* ptr=new int[noStudents];
for (int index=0;index<noStudents;index++)
{
in>>ptr[index];
}
in.close();
return ptr;
}

void printHistogram(int scores, int questions)
{
for (int index=0;index<questions;index++)
{
cout<<scores;
}
}
The new operator returns a pointer to a contiguous chunk of memory of the proper size. When you use
 
int *ptr = new int[x]

new allocates a chunk of memory somewhere on the heap. The chunk of memory is sizeof(int) * x chars (bytes) long. The pointer that is returned by new points to the first int of the array.
Last edited on
Topic archived. No new replies allowed.