Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Write a program that dynamically allocates an array large enough to hold a u

ID: 3838635 • Letter: C

Question

C++

Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all of the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. the program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible.

DO NOT ACCEPT NEGATIVE NUMBERS FOR TEST SCORES

Modify the program 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.

Explanation / Answer

#include<iostream.h>
#include<string>
struct student {
   string name;
   int score;
};
void calAverage1(int *data, int n){
   int sum;
   int *p;
   sum = 0;
   p = data;
   for (int i = 0; i<n; i++){
        sum = sum + *(p + i);
              
   }
   cout << "Average:" << setprecision(2) << sum/n;
}
void calAverage2(student *data, int n){
   int sum;
   student *p;
   sum = 0;
   p = data;
   for (int i = 0; i<n; i++){
        sum = sum + *(p + i).value;
              
   }
   cout << "Average:" << setprecision(2) << sum/n;
}

void Sort1(int *data, int n){
   int sum;
   int *p;
   int temp;
  
   p = data;
   for (int i = 0; i<n; ++i){
      for (int j=0; j<n-i-1; ++j){
          if (*(p + j) > *(p + j + 1)){
             temp = *(p + j);
             *(p + j) = *(p + j + 1);
             *(p + j +1) = temp;
          }
      }
            
   }
  
}
void Sort2(student *data[], int n){
   int sum;

#include<iostream.h>
#include<string>
struct student {
   string name;
   int score;
};
void calAverage1(int *data, int n){
   int sum;
   int *p;
   sum = 0;
   p = data;
   for (int i = 0; i<n; i++){
        sum = sum + *(p + i);
              
   }
   cout << "Average:" << setprecision(2) << sum/n;
}
void calAverage2(student *data, int n){
   int sum;
   student *p;
   sum = 0;
   p = data;
   for (int i = 0; i<n; i++){
        sum = sum + *(p + i).value;
              
   }
   cout << "Average:" << setprecision(2) << sum/n;
}

void Sort1(int *data, int n){
   int sum;
   int *p;
   int temp;
  
   p = data;
   for (int i = 0; i<n; ++i){
      for (int j=0; j<n-i-1; ++j){
          if (*(p + j) > *(p + j + 1)){
             temp = *(p + j);
             *(p + j) = *(p + j + 1);
             *(p + j +1) = temp;
          }
      }
            
   }
  
}
void Sort2(student *data, int n){
   int sum;
   student *p;
   student temp;
  
   p = data;
   for (int i = 0; i<n; ++i){
      for (int j=0; j<n-i-1; ++j){
          if (*(p + j).value > *(p + j + 1).value){
             temp = *(p + j);
             *(p + j) = *(p + j + 1);
             *(p + j +1) = temp;
          }
      }
            
   }
  
}
void main(){
     int n;
     int *scores;
     student *scores1;
     
     cout << "Enter number of scores" << endl;
     cin >> n;
     scores = new int[n];
     cout << "Enter" << " " << n << "scores"
     for (int i =0; i<n; i++){
         cin >> scores[i];
     }
     calAverage1(scores, n);
     Sor1(scores, n);
     cout << "Enter number of scores(student,value) pair" << endl;
     cin >> n;
     scores1 = new student[n];
     cout << "Enter (name, value)" << " " << n << "scores"
     for (int i =0; i<n; i++){
         cout << "Enter name" << endl;
         cin >> scores1[i].name;
         cout << "Enter score" << endl;
         cin >> scores1[i].value;
     }
     calAverage2(scores1, n);
     Sort2(scores1, n);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote