Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

HArd time with structures, functions and pointers.

$
0
0
Hello, sorry but I am superlost with this, any help will be very welcome.

Thank you.

Code:

/* Modify the program of Programming Challenge 1 to allow the user to enter name-score
pairs. For each student taking a test, the user types a string representing the name of the
student, followed by an integer representing the student’s score. Modify both the sorting
and average-calculating functions so they take arrays of structures, with each structure
containing the name and score of a single student. In traversing the arrays, use pointers
rather than array indices.*/


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Score { string name; int score;};

void bubbleSort(double scores[], int numScores);
void average (double scores[], int numScores);

int main (){
        Score *pscores;        int numScores, count;

        cout << "How many scores are you entering? ";
        cin >> numScores; 

        while (numScores <=0)
        { cout << "PLEASE ENTER A LARGER NUMBER THAN 0\n";
        cin >> numScores; }

        scores = new double [numScores]
        Score *pscores[numScores] = new Score ;
       
        cout << "Enter the scores:\n";

        for (count = 0; count < numScores; count++)
{ cout << "Enter name:" << (count + 1) << ": \t"; 
        cin >> pscores->name;
while (pscores[count] <=0)
{cout << "ENTER MORE THAN 0\n"; cout << "Test Score #" << (count + 1) << ": "; cin >>pscores->score;}
        }

        bubbleSort(&pscores,numScores);
        average(&pscores,numScores);

        delete [] pscores;       
        pscores = 0;       
        return 0; }

void bubbleSort(Score pscores[], int numScores)
{       
        bool swap;
do
{ swap = false;
for(int index = 0; index < numScores - 1; index++)
{
        if(pscores[index] > pscores[index + 1])
{ double temp = pscores[index]; pscores[index] = pscores[index+1]; pscores[index+1] = temp; swap = true; }       
}
}while (swap);
cout<<"\nIN ASCENDING ORDER:\n"<<endl;
for(int count = 0; count < numScores; count++)
        cout<<" "<<pscores->score[count];
}

void average (Score pscores[], int numScores)
{        double total= 0, average = 0;
for (int count = 0; count < numScores; count++)
{ total += pscores[count]; }
        average = total / numScores;
        cout << fixed << showpoint << setprecision(2);
        cout << "\n\nAverage score is: " << average << endl<< endl;
}


Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images