Jump to content

How to show the times a number is shown in an array [C++]

Hello LTT,

I want to solve a problem that has been getting on my nerves for a week now. So, I have this code:

#include <iostream>
#include <fstream>

using namespace std;

int main () {
	ifstream in ("sch.in");
	int x = 0;
	in >> x;

	int temp;

	int array[x];

	for(int i=0; i<x ; i++)
    {
    	in >> array[i];
    }

	for(int i=0; i<x ; i++)
    {
      for(int j=0; j<x-1; j++)
      {
        //Swapping element in if statement
        if(array[j]>array[j+1])
        {
          temp=array[j];
          array[j]=array[j+1];
          array[j+1]=temp;
        }
      }
    }

    for(int i=0; i<x ; i++)
    {
    	cout << array[i] << " ";
    }

    

    cout << endl;

    return 0;
}

This, combined with an array inserted or a sch.in file with an array inside of it of this type:

11
1 2 1 3 3 3 3 1 3 3 3 

should output an array with the same numbers in order numerically from 1 to whatever there is:

1 1 1 2 3 3 3 3 3 3 3 

Is there a way where it outputs the numbers in order of how many times they appear?

Project "Transcend": i7 7700k-R9 380 4GB by MSi, Z170-HD3P

 

 

My Steam Profile (from SteamDB)

 

  • Worth: $453 ($123 with sales)
  • Games owned: 76
  • Games not played: 40 (53%)
  • Hours on record: 667.0h

 

Link to comment
Share on other sites

Link to post
Share on other sites

you could store each number in a map.  Maps let you choose your own index as opposed to the standard 0,1,2,.. so what you can do is use the numbers in the array as the key(index) and let the value be number of times it appears in the array.  Once you create the map ( which should look something like (1,3),(2,1),(3,7)), then you can sort by value and output.  So you would output 3,1,2.  If you wanted, you could even make a for loop that outputs the key a value amount of times (3,3,3,3,3,3,3,1,1,1,2).  Maps can be a little annoying if you've never used one but they are practically built for finding frequencies.  

Link to comment
Share on other sites

Link to post
Share on other sites

The word you are looking for is sort. You want to sort your array. Look up how to sort an array.

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, WaxyMaxy said:

The word you are looking for is sort. You want to sort your array. Look up how to sort an array.

Are you familiar with programming? can I ask a question? This is very newbie stuff i know xD

Project "Transcend": i7 7700k-R9 380 4GB by MSi, Z170-HD3P

 

 

My Steam Profile (from SteamDB)

 

  • Worth: $453 ($123 with sales)
  • Games owned: 76
  • Games not played: 40 (53%)
  • Hours on record: 667.0h

 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, slanensis said:

Are you familiar with programming? can I ask a question? This is very newbie stuff i know xD

I'm not a c++ programmer but ask away

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, WaxyMaxy said:

I'm not a c++ programmer but ask away

What could be the syntax of a sort command? I tried what i found on C++.com, sort(array,array + SIZE); but it didn't work. What could be the syntax?

Project "Transcend": i7 7700k-R9 380 4GB by MSi, Z170-HD3P

 

 

My Steam Profile (from SteamDB)

 

  • Worth: $453 ($123 with sales)
  • Games owned: 76
  • Games not played: 40 (53%)
  • Hours on record: 667.0h

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just a sidenote: Variable length arrays are not supported by the C++ standard, so the code:

int array[x];

For a non-constexpr int x is invalid code that should not even compile. If it compiles for you that means your compiler supports VLA trough some non-standard extention, which is fine, as long as you keep in mind this is non-standard and non-portable.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, slanensis said:

Is there a way where it outputs the numbers in order of how many times they appear?

std::sort(std::begin(array), std::end(array));

Include <algorithm> for std::sort and enable c++11 if required for std::begin and std::end.

But you should really be using a std::vector in stead of a raw array.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×