C++ PROBLEM

How do I add a counter for the comparisons so that it increments every time the two array value are compared?

// Session5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

const int NMAX =10;

int main()
{
double Numbers[NMAX] = {31.2, 29.7, 53.5, 69.0, 23.7, 71.8, 49.3, 52.9, 51.3, 57.1};

// note this top counter, i, counts down
for (int i=NMAX-1; i>0; i--)
{
//and this counter, j, only goes as far as the current i value
// it means it doesn't go over the elements that have already 'bubbled up' to the end of the array
for (int j=0; j<i; j++)
{
double temp;
// Compare 2 values - increment a counter here
if (Numbers[j]>Numbers[j+1])
{
temp = Numbers[j];
Numbers[j]= Numbers[j+1];
Numbers[j+1]= temp;
//increment a swap counter here
}
}
}

// display the sorted array:
cout<<"ARRAY CONTENTS SORTED, IMPLEMENTATION 1 "<<endl;
for (int i=0; i<NMAX; i++)
{
cout<<Numbers[i]<<endl;
}
//Display the values of the counter after the whole sort
return 0;
}
Topic archived. No new replies allowed.