Ok I have a little problem.I have a pointer in the struct which I don't know how to use as or move the information into an array on the bottom function. Need an explanation. Anyone please help...

// This is the minimum functionality for this exercise
// Notice that I did not perform any user input checking as the emphasis was
// on programming with pointers.
//============================================================================
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

const int MAX_NAME_SIZE = 25;

// Structure to hold StudentInfo
struct StudentInfo
{
       int    studentID;
       char   name[MAX_NAME_SIZE];       
       int    *progExercises;  // a pointer to an array of prog exercise scores   
       int    *quizzes;         // a pointer to an array of quiz score
       int    midTerm;         
       int    finalExam;
       int    attendanceParticipation;
       float  finalScore;      // Course grade calculated       
       char   finalGrade;      // Letter grade for the course
};

// function prototypes
void  getClassData (int &, int &, int &);
void  displayStudents(StudentInfo* , int, int, int); 
float calculateFinalScore(float, float, int, int, int);
char  assignLetterGrade(float);
double getLowest (StudentInfo* , int); 

int main(int argc, char *argv[])
{
    StudentInfo *ptrStudentInfoArray;    // a pointer to an array that 
                                         // will hold student structs
    
    int         numStudents;             // to hold # of students
    int         numQuizzes;              // to hold # of quizzes
    int         numProgExercises;        // to hold # of prog exercises
    double      lowestScore;

    //-
    // call getClassData function here to capture user input
    //
    getClassData(numStudents, numQuizzes, numProgExercises);
    
    // Dynamically allocate an array of StudentInfo for the number
    // of students the user specified. This is an array of structures
    ptrStudentInfoArray = new StudentInfo[numStudents];
    
    // if something goes wrong with the dynamic allocation, notify the user
    // and terminate the program
    if (!ptrStudentInfoArray)
    {
        cout << "Fatal Error! Could not allocate memory for the array." << endl;                     
        cout << "Terminating the program." << endl;
        system("PAUSE");
        exit(0);
    }
  
    // Everything went fine with dynamic memory allocation, proceed with the 
    // program. Since we did not learn in detail how to pass pointers into 
    // functions or have functions return pointers, I will implement the logic
    // here. Normally this would have been implemented in a function
     StudentInfo *ptrStudentInfo;  // pointer to the structure address 
     int progExSum;                // sum of programming exercises
     int quizSum;                  // sum of all quizzes
     float progExAvg, quizAvg;     // averages for programming exercises and
                                   // quizzes
     
     for (int i =0; i < numStudents; i++)
     {
         ptrStudentInfo = &ptrStudentInfoArray[i];
                 
         cout << "Enter student#" << i+1 << " name: ";
         cin  >> ptrStudentInfo->name;
         
         cout << "Enter student#" << i+1 << " ID: ";
         cin  >> ptrStudentInfo->studentID;
         
         progExSum = 0;   // initialize the sum of programming exercises for
                          // each student
         progExAvg = 0;   // initialize the average of programming exercises for
                          // each student
         
         // now we will allocate an array to hold programming exercise scores
         ptrStudentInfo->progExercises = new int[numProgExercises];
         for (int j=0; j<numProgExercises; j++)
         {
             cout << "Enter programming exercise score #" << j+1 << ": ";
             cin >> ptrStudentInfo->progExercises[j];
             
             // 
             // Add a line of code here to keep a running total (sum) of 
             // programming exercises
             //
             progExSum += ptrStudentInfo->progExercises[j];
         }
         
         // call a function to drop the lowest programming exercise
         // Note: you will need to implement this function, as there is no
         //       prototype for it in the source code. This is one of the
         //       "extra credit" functions for this assignment
         
         lowestScore = getLowest (ptrStudentInfoArray, numProgExercises);
         
         // add a line of code to compute a programming exercise average
         
         
         quizSum = 0;  // initialize the sum of quizzes for each student
         quizAvg = 0;  // initialize the averages for each student
         
         // now allocate an array to hold quiz scores
         ptrStudentInfo->quizzes = new int[numQuizzes];
         for (int j=0; j<numQuizzes; j++)
         {
             cout << "Enter quiz score #" << j+1 << ": ";
             cin >> ptrStudentInfo->quizzes[j];
         
             // 
             // Add a line of code to keep a running total (sum)
             // programming exercises
         }
         
         // Add a line of code to compute quizAverage
         
         cout << "Enter MidTerm Score: " ;
         cin  >> ptrStudentInfo->midTerm;
         
         cout << "Enter Final Exam Score: " ;
         cin  >> ptrStudentInfo->finalExam;
         
         cout << "Enter Attendance and Participation Score: " ;
         cin  >> ptrStudentInfo->attendanceParticipation;
         
         // Call a function here to calculate the final score
         
         // Call a function here to assign letter grade based on the final score
         

     }  // end of for
    
    cout << "=======================================================" << endl;                                 
    displayStudents(ptrStudentInfoArray, numStudents, numProgExercises, numQuizzes);
                                         
    // delete dynamically allocated memory
    // Notice that I grab array elements through their address and then delete
    // individual dynamically allocated arrays in each structure.
     for (int i =0; i < numStudents; i++)
     {
         ptrStudentInfo = &ptrStudentInfoArray[i];
         delete ptrStudentInfo->progExercises;
         delete ptrStudentInfo->quizzes;      
     }
     
     // once I am done with each structure, I delete the array
     delete []ptrStudentInfoArray;
     
    system("PAUSE");
    return EXIT_SUCCESS;
}

//*******************************************************************************
// Function   : assignLetterGrade
// Returns    : char - representing letter grade for a given score
// Parameters : finalScore (input) student's final score (numeric)
// Description: This function receives the final score as input and then 
//              assigns the appropriate letter grade to the grade.
//********************************************************************************
char assignLetterGrade(float finalScore)
{
     char letterGrade;
     
     // implement the logic here to assign the letterGrade based on the finalScore
     // that was passed in.
     
     return letterGrade;
}

//*******************************************************************************
// Function   : calculateFinalScore
// Returns    : float
// Parameters : progExercisesAvg (input) average score of programming exercises
//              quizAvg (input) average of quizz scores
//              midTerm (input) midTerm exam grade
//              finalExam (input) final exam grade
//              attendanceParticipation (input) - attendance and class 
//              participation grade 
//********************************************************************************
float calculateFinalScore(float progExercisesAvg, 
                            float quizAvg,
                            int    midTerm,
                            int    finalExam,
                            int    attendanceParticipation)
{
    float finalScore = 0.0;
    
    // implement the finalScore calculation here
    finalScore = (progExercisesAvg * 0.70) +(quizAvg * 0.10) + (midTerm * 0.05) 
    + (finalExam * 0.10) + (attendanceParticipation * 0.05);
                  
                 
    return finalScore;
}
    
//*******************************************************************************
// Function   : getClassData
// Returns    : void
// Parameters : numStuds (output) number of students
//              numPEs (output) number of programming exercises
//              numQs (output) number of quizzes
// Description: This function prompts the user for number of students, 
//              number of programming exercises, and number of quizzes and
//              returns these through reference parameters
//********************************************************************************
void getClassData(int &numStuds, int &numPEs, int &numQs)
{
    cout << "How many students are in your class?";
    cin >> numStuds;
    
    cout << "How many programming exercises did they have?";
    cin >> numPEs;
    
    cout << "How many quizzes did they have?";
    cin >> numQs;                                         
}                                         
    

//*******************************************************************************
// Function   : displayStudents
// Returns    : void
// Parameters : ptrStudentInfoArray - pointer to an array of student structures
//              numStuds (input) how many students in the array
//              numPEs (input) number of programming exercises
//              numQs (input) number of quizzes
// Description: This function displays the student array 
//********************************************************************************
void displayStudents(StudentInfo* ptrStudentInfoArray, 
                    int          numStuds, 
                    int          numPEs, 
                    int          numQs)
{
     StudentInfo *ptrStudentInfo;  // pointer to the structure address 
     
      cout << "===============================================================" << endl;
     cout << "SID \t\t Name\t\tFinal Score\tLetter Grade" << endl;
     cout << "===============================================================" << endl;
     for (int i =0; i < numStuds; i++)
     {
         ptrStudentInfo = &ptrStudentInfoArray[i];
      
        
         cout << setprecision(2) << fixed;
         cout << ptrStudentInfo->studentID << "\t\t"
              << ptrStudentInfo->name << "\t\t" 
              << ptrStudentInfo->finalScore << "\t\t"
              << ptrStudentInfo->finalGrade << endl;         
     }   
}

double getLowest (StudentInfo* ptrStudentInfoArray, int numProg)
{     
    StudentInfo *ptrStudentInfo;
    double lowest;
    int score[0]; 
    
    for (int i =0; i < numProg; i++)
    {
         ptrStudentInfo = &ptrStudentInfoArray[i];
         score = ptrStudentInfo->progExercises;
    }
    
    lowest = score[0];
    
    for (int count = 1; count < numProg; count++)
    {
        if (score[count] < lowest)
            lowest = score[count];
    }
    return lowest;
}

Well, the code is reasonably well formatted, but it's sort-of all crammed together in a single file so I'm having a little trouble following it. I can't address your problem specifically so perhaps an example will help:

#include <iostream>
#include <cstdlib>

struct Sample {
  int *pIntArray;
  int ArraySize;
  int UsedElements;
  Sample();                 //default constructor
  ~Sample();                //destructor
  void SetArraySize(int);   //initialize the array
  void AddElement();        //add a new value to the array
};

Sample::Sample()            //default constructor
  : pIntArray(0)
  , ArraySize(0)
  , UsedElements(0) {}

Sample::~Sample() {         //destructor
  if (pIntArray != NULL) {
    delete[] pIntArray;
    pIntArray = 0;
  } //end if
} //end destructor

void Sample::SetArraySize(int newSize) {
  this->pIntArray = new int[newSize];     //allocate the array
  this->ArraySize = newSize;              //set the limiting value
  this->UsedElements = 0;                 //set the counter value
  for (int i = 0; i < ArraySize; ++i) {   //initialize the array elements
    pIntArray[i] = 0;
  } //end for
} //end function

void Sample::AddElement() {
  if (UsedElements < ArraySize) {         //verify the array isn't full
    int inputValue = 0;                   //a variable to accept input
    cout << "Please enter a value: ";     //input prompt
    cin >> inputValue;
    cin.ignore();
    pIntArray[UsedElements] = inputValue; //store the value in the array
    ++UsedElements;                       //increment the counter
  } //end if
} //end function

This should give you the basic idea about how to dynamically-allocate an array inside a struct/class. Here, I've done it with an allocation function (called "SetArraySize"), but you can also do it in the constructor:

Sample::Sample()            //default constructor
  : UsedElements(0)
  , ArraySize(DEFAULT_ARRAY_SIZE) {
  pIntArray = new int[DEFAULT_ARRAY_SIZE];
  for (int i = 0; i < ArraySize; ++i) {
    pIntArray[i] = 0;
  }
}

You could also specify "AddElement()" with an argument/parameter and make it act more like vector::push_back() if you like:

void Sample::AddElement(int newValue) {
  if (UsedElements < ArraySize) {         //verify the array isn't full
    pIntArray[UsedElements] = inputValue; //store the value in the array
    ++UsedElements;                       //increment the counter
  } //end if
} //end function

I apologize but I don't know how to read your style of writing.

Can you clarify? What part of it don't you understand?

This is a struct/class definition:

struct Sample {
  int *pIntArray;
  int ArraySize;
  int UsedElements;
  Sample();                 //default constructor
  ~Sample();                //destructor
  void SetArraySize(int);   //initialize the array
  void AddElement();        //add a new value to the array
};

These are the implementations of the class' methods:

Constructor, using initializers:

Sample::Sample()            //default constructor
  : pIntArray(0)
  , ArraySize(0)
  , UsedElements(0) {}

Did the initializers confuse you? Let me write differently, without initializers:

Sample::Sample() {
  pIntArray = NULL;
  ArraySize = 0;
  UsedElements = 0;
}

Destructor, nothing real special here:

Sample::~Sample() {         //destructor
  if (pIntArray != NULL) {
    delete[] pIntArray;
    pIntArray = NULL;
  } //end if
} //end destructor

Remaining member methods:

void Sample::SetArraySize(int newSize) {
  this->pIntArray = new int[newSize];     //allocate the array
  this->ArraySize = newSize;              //set the limiting value
  this->UsedElements = 0;                 //set the counter value
  for (int i = 0; i < ArraySize; ++i) {   //initialize the array elements
    pIntArray[i] = 0;
  } //end for
} //end function

void Sample::AddElement() {
  if (UsedElements < ArraySize) {         //verify the array isn't full
    int inputValue = 0;                   //a variable to accept input
    cout << "Please enter a value: ";     //input prompt
    cin >> inputValue;
    cin.ignore();
    pIntArray[UsedElements] = inputValue; //store the value in the array
    ++UsedElements;                       //increment the counter
  } //end if
} //end function

This struct would be used in your program like this:

int main() {
  const int ARRAY_SIZE = 10;
  Sample mySampleObj;
  mySampleObj.SetArraySize(ARRAY_SIZE);
  for (int i = 0; i < ARRAY_SIZE; ++i) {
    mySampleObj.AddElement(i);
  }
  return 0;
}

I am just a beginner and don't understand the logic behind someone who actually knows how to write. So I am not like others who just want you to do and show them code. Can I get less code and more explanation please. I passed the structure into my getlowest function, but don't know how to pass the information in the *progExercises out into the array I set for them.

double getLowest (StudentInfo* ptrStudentInfoArray, int numProg)
{     
    StudentInfo *ptrStudentInfo;
    double lowest;
    int score[0]; 
    
    for (int i =0; i < numProg; i++)
    {
         // I know the following is wrong, how would I get it to work?
         ptrStudentInfo = &ptrStudentInfoArray[i];
         score[i] = ptrStudentInfo->progExercises;
    }
    
    lowest = score[0];
    
    for (int count = 1; count < numProg; count++)
    {
        if (score[count] < lowest)
            lowest = score[count];
    }
    return lowest;
}

As I said in my first post, I'm not sure what your question is. The code that I have posted is intended simply to demonstrate how to dynamically allocate an array within a class/struct.

If this is not your issue, please elaborate on your question. Once you do, I can probably provide you with some information that is more useful.

Thanks for your time. I finally found what I wanted to do.

double getLowest (StudentInfo* ptrStudentInfoArray, int numProg)
{     
    StudentInfo *ptrStudentInfo;
    int *score;
    double lowest; 
    
    for (int i =0; i < 1; i++)
    {
         ptrStudentInfo = &ptrStudentInfoArray[i];
         score = ptrStudentInfo->progExercises;
    }
    lowest = *score;
    
    for (int count = 1; count < numProg; count++)
    {
        if (*(score+count) < lowest)
            lowest = *(score+count);
    }
    
    
    return lowest;
}

So you were trying to use an array as an argument to a function? Why not just say that? The thread title was completely misleading...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.