Hey guys. I ma new to c++ and need a little help.
My teacher gave us an assignment in which we have to take input from the user as to how many integers are to be entered. the program then compares the first integer with the rest,
then it compares the 2nd with the rest excluding the first one. Until it reaches the end. it then displays the num of integers which were smaller than the 1st one, then displays the num of integers smaller than the second one and so on....
for example the num of integers to be entered is 4. and the integers entered are 5,4,2,3.
it will compare 5 to 4,5 to 2 and 5 to 3 and display that 3 numbers were smaller than 5. Next it will compare 4 with 2 and 4 with 3 and display that 2 num were smaller than 4. Next it will compare 2 with 3 and display that 0 numbers were smaller than 2. Finally it will sum these comparison and display the final answer which over here would be 5.

Here is what I have come up with so far.

#include <iostream>
using namespace std;
int main()
{
    int n=0;
    int x=0;
     int count=0;
    cout<<"enter size of permutation: ";
    cin>>n;
    int size[n];
    for(int j=1;j<=n;j++)
            {
            cout<<"Enter no."<<j<<" ";
            cin>>size[j];
            }
            for (x=1;x<=n;x++)
            {
     if(size[1]>size[x])
    count++;}
     cout<<count<<" inversions for "<<size[1]<<endl;              
            system("pause");
            return 0;
            }

Based on your explanation, I take it you have more to do on the program, but since you didn't ask any question nor explain any problems, all we can assume is you're on track.

First of all, you cannot create an array whose size is not fixed at compile-time. So, your array "size" cannot be a static array of size "n", you need a dynamic array. In C++, the preferred way to create a dynamic array is to use a standard class called "vector". So, you would do the following:

#include <iostream>
#include <vector>   // include the "vector" library.
using namespace std;

int main() {
  // the code to get the value of 'n'
  // then, create the vector of size n:
  vector<int> size(n);
  //.. the rest of your code..
  return 0;
};

Then, you must know that indices for arrays in C/C++ are zero-based, meaning that the first element in the array has index 0, and the last element in the array has index n-1. So, you should not do a for-loop as for(int j=1;j<=n;++j) , but you should do the for-loops as for(int j = 0; j < n; ++j) . This is a general rule for all array-like things in C/C++, indices are always starting from 0.

Finally, you are somewhat on the right track when it comes to your last loop. However, you will need a nested loop, i.e., one loop inside another. This is because you are iterating through all elements, and for each element you need to iterate through all remaining elements. Typically, this would look something like this:

for(int i = 0; i < n; ++i) {
  for(int j = i; j < n; ++j) {
    //..
  };
};

I'll let you figure out how to make this work.

thanks.
i changed the code a bit so it looks like this now

#include <iostream>
using namespace std;
int main()
{
    int n=0;
    int x=0;
     int count=0;
     int a=0;
     int z=0;
     int v=0;    
    cout<<"enter size of permutation: ";
    cin>>n;
    int size[n];
    for(int j=0;j<n;j++)
            {
            cout<<"Enter no."<<j+1<<" ";
            cin>>size[j];
            }
            {
            for (z=0;z<n-1;z++)
            for (x=z+1;x<n;x++)
     if(size[a]>size[x])
     count++;}
     cout<<"Total no. of inversions are: "<<count<<endl;
            system("pause");
            return 0;
            }

but i need it to display the number of inversions for each integer in the array. e.g. in an array of 3 2 1 it should say "3 has 2 inversions" "2 has 1 inversions" "1 has 0 inversions". Anny suggestions? Thanks in advance.

You need to have a variable like that "count" variable within the outer for-loop to count the number of inversions in the inner loop. At the end of the inner loop, you display the result, like cout << size[z] << " has " << inner_count << " inversions." << endl; , and then you add that to the global count, as in count += inner_count; .

Btw, there is an error in your code, at line 22, it should be if(size[z] > size[x]) (notice the "z" instead of "a").

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.