CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2013
    Location
    USA
    Posts
    2

    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;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: HArd time with structures, functions and pointers.

    Quote Originally Posted by LauraBond View Post
    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

  3. #3
    Join Date
    Apr 2013
    Location
    USA
    Posts
    2

    Re: HArd time with structures, functions and pointers.

    I figured out Victor (or actually my friend did), thank you very much!

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: HArd time with structures, functions and pointers.

    Quote Originally Posted by LauraBond View Post
    (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
  •  





Click Here to Expand Forum to Full Width

Featured