Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer problem between functions

Status
Not open for further replies.

Matteo

Programmer
Mar 7, 2002
43
US
Hello,

I simply need to allocate an array with a value given to me from command line. So from main there is a function to call the method:

allocate_array(grades, numgrades);

The declaration of these variables are:

double *grades;
size_t numgrades;

Inside of my allocate_array function, I ask for the size of array and save it in the variable numgrades. Then create grades of size numgrades. When I try and access these variables in another function I cannot. The next function call is:

read_input(grades, numgrades);

I can read input and output it fine inside of allocate_array, but not outside of it. any ideas of where the problem is?

allocate_array looks like this:

void allocate_array(double *grades, size_t numgrades) {

cout << endl << "Enter the size of the array to be allocated: ";
cin >> numgrades;
grades = new double(numgrades);

}// allocate_array...
 
Use a reference
Code:
void allocate_array(double *&grades, size_t numgrades) {

--
 
Can I also pass by reference the value of numgrades even though it is of type size_t? because that is defaulted I think to MAX_INT and is a huge number outside of allocate_array.

Thanks,
Matt
 
Show your code.
Passing numgrades by reference adds nothing since you're not modifying it in the function.


--
 
#include <iostream.h>
#include <stdlib.h>
#include <assert.h>

const size_t GRADETYPES = 5;

void allocate_array(double *&grades, size_t numgrades);
void read_input(double *&grades, size_t numgrades);
void find_distribution(double *&grades, size_t numgrades, size_t gradecount);

int main()
{
double *grades;
size_t numgrades;
double mean_value, med_value;
size_t gradecount[GRADETYPES] = {0};

cout << "This program will read in some numbers (grades), which will\n";
cout << "be stored in an array of doubles (allocated dynamically). The\n";
cout << "distribution of the grades (# of As, Bs, Cs, Ds and Fs), the\n";
cout << "average, and the median of the grades will then be printed out.\n";


allocate_array(grades, numgrades);

read_input(grades, numgrades);

find_distribution(grades, numgrades, gradecount);

// Print the distribution
// print_distribution(gradecount);

// Calculate the average
// mean_value = average(grades, numgrades);

// Calculate the median
// Print the average and the median of the grades

cout << endl << endl;
cout << "The average is: " << mean_value << endl;
cout << "The median is: " << med_value << endl << endl;

return EXIT_SUCCESS;
}


void allocate_array(double *&grades, size_t numgrades) {

cout << endl << "Enter the size of the array to be allocated: ";
cin >> numgrades;

grades = new double(numgrades);

}// allocate_array...

void read_input(double *&grades, size_t numgrades) {

cout << "Enter the array elements: ";
for(int x = 0 ; x < numgrades-1 ; x++)
cin >> grades[x];

}// read_input

void find_distribution(double *&grades, size_t numgrades, size_t gradecount) {

for(int x=0;x!=numgrades-1;x++) {
if(grades[x]>90.0) gradecount[0]++;
else if(grades[x]<90.0 && grades[x]>80.0) gradecount[1]++;
else if(grades[x]<80.0 && grades[x]>70.0) gradecount[2]++;
else if(grades[x]<70.0 && grades[x]>60.0) gradecount[3]++;
else if(grades[x]<60.0 && grades[x]>0.0) gradecount[4]++;
else cout << "There was a wrong input entered.";
}

}// find_distribution
 
1. Please use [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

2. #include <iostream.h>
How old is your compiler?
The new standard basically is this
Code:
#include <iostream>   // drop the .h
#include <cstdlib>    // for 'C' headers, drop the .h
#include <cassert>    // and prefix with a letter c
using namespace std;

> grades = new double(numgrades);
This only allocates ONE double, and initialises its value.
You need
Code:
grades = new double[numgrades];

> for(int x=0;x!=numgrades-1;x++) {
Code:
for(int x=0;x<numgrades;x++) {
This is the normal way to loop through all the elements of an array.

Also, there is no need to pass the array by reference to your other two functions, since you've no intention of modifying the array itself (only its contents)

--
 
Ok, I will include the tags in the future.

The variable numgrades loses its value from allocate_array (where the user inputs it) to read_input. numgrades is defined as a size_t and I cant reference an int. And I dont know any other way besides return and reference. What I have done is add an extra element to my array, and set it to numgrades so that I can access it outside allocate array. And then I will set my loops to loop from 1 to numgrades.

Code:
	grades = new double[numgrades+1];
	grades[0] = numgrades; // Stores the value of numgrades
 
Heh - so you do :)

So I guess something like this
Code:
void allocate_array(double *&grades, size_t &numgrades) {

You may have to read into a local integer variable and then assign that result to numgrades, if cin doesn't have a size_t variation.


--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top