-
April 21st, 2013, 08:06 PM
#1
HArd time with structures, functions and pointers.
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;
}
-
April 22nd, 2013, 01:09 AM
#2
Re: HArd time with structures, functions and pointers.
Originally Posted by LauraBond
Hello, sorry but I am superlost with this, any help will be very welcome.
What exactly are you "superlost with"?
What kind of help will be "very welcome"?
Victor Nijegorodov
-
April 22nd, 2013, 01:21 AM
#3
Re: HArd time with structures, functions and pointers.
I figured out Victor (or actually my friend did), thank you very much!
-
April 22nd, 2013, 04:29 AM
#4
Re: HArd time with structures, functions and pointers.
Originally Posted by LauraBond
(or actually my friend did)
I wish next time it was you. Like any other skill issue resolving needs practice.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|