I need help with adding a structure that allows user to enter name-score pairs. I also need help in modifing my sorting and average calculating functions so they take array of structures with each structure containing the name and score of a single student. In traversing the arrays, use pointers rather than array indices. I really have trouble understanding structures so can someone please help me get started in doing this. Here is
my program

void sort(int* a, const int size)

{
  for (int i = 0; i < size; ++i)
    for (int j = i + 1; j < size; ++j)
      if (a[i] > a[j])
      {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
      }
}

double average(int* a, const int size)
{
  double avg = 0;

  for (int i = 0; i < size; ++i)
    avg += a[i];
  avg /= size;

  return avg;
}

void display(int* a, const int size)
{
  cout << "Sorted scores list: ";
  for (int i = 0; i < size; ++i)
    cout << a[i] << " ";
  cout << endl;
  cout << "Average score: " << average(a, size) << endl;
}

int main()
{
  const int MAX_SIZE = 100;
  int* a = new int[MAX_SIZE];

  int n;
  cout << "Scores amount: ";
  cin >> n;

  cout << "Enter scores:" << endl;
  for (int i = 0; i < n; ++i)
  {
    cout << i + 1 << ": ";
    while (true)
    {
      cin >> a[i];
      if (a[i] < 0)
        cout << "Must be nonnegative\n";
      else
        break;
    }
  }

  sort(a, n);
  display(a, n);

  delete[] a;

  return 0;
}

do you know how to create a structure ? Not much to them

struct <struct name>
{
   string name;
   int score;
}

Now just create an array of those things just like you did with the int array on line 37.

This article has been dead for over six months. Start a new discussion instead.