its been 6 hours since I have been writing the code but to no avail, I don't no where I am making the mistake but I am making some. Its a frequency output program and output should be like this:
array[8] = {6,1,7,8,6,6,1,9}
Output:
6:3
1:2
7:1
8:1
9:1
But its repeating the same numbers in my code. Any help would be much appreciable.
int array[8] = {6,1,7,8,6,6,1,9};
int store[8];
int a =0;
int b =0;
int c=0;
int d = 0;
store[d] = array[b];
for (d = 0; d < 8; d++){
int count=0;
c = d;
b = d;
for (int e = 0; e < d; e++){
if (array[b] == store[e]){
store[d] = array[b];
b++;
e = 0;
}
else
{
store[d] = array[b];
break;
}
}
for ( int z = 0; z < 7; z++){
if (store[d] == array[z])
{
count++;
}
}
cout << store[d] << ":" << count << endl;
}
You may use a map first to store num->frequency and then a multimap to store freqeuncy => num.
Here is the working solution.
#include <map>
#include <algorithm>
#include <iostream>
int main()
{
int array[8] = {6,1,7,8,6,6,1,9};
// A map to store num => freq
std::map <int, int> freq;
// A map to store freq(can be duplicate) => num
std::multimap <int, int> freqCounts;
// Store num => frequency
for (int i = 0 ; i < 8; i++)
{
freq[array[i]] += 1;
}
// Now Store freq => num
for(auto const & iter : freq)
{
freqCounts.insert (std::pair<int,int>(iter.second, iter.first));
}
// Print in reverse order i.e. highest frequency first
for (std::multimap<int,int>::reverse_iterator rit=freqCounts.rbegin(); rit!=freqCounts.rend(); ++rit)
{
std::cout << rit->second << " : " << rit->first << '\n';
}
return 0;
}
You never seem to update the counters. Try this:
int array[8] = {6,1,7,8,6,6,1,9};
unsigned int store[10] = {}; // large enough to hold the largest array value,
// initialized to zero
for (int n : array) ++store[n]; // update counts
for (int i = 0; i != 10; ++i)
{
std::cout << "Frequency of int " << i << " is " << store[i] << "\n";
}
If the set of values that occur is sparse, or includes negatives, or simply does not fit into a dense range of integers nicely, you can replace unsigned int[10] with an associative container, e.g.:
std::map<int, unsigned int> store;
// algorithm as before
for (auto const & p : store)
{
std::cout << "Frequency of " << p.first << " is " << p.second << "\n";
}
I'm not sure what you are trying to do with the arrays. I have tried to follow the logic, but it's hard to see it with all the anonymous variable names. It seems like you are trying to look for duplicates earlier in the array, but the variable e never gets any other value than 0, so you will only be comparing with the first item in the array.
You can just look in the array itself for previous occurances, and once you know that the number is the first occurance, you only need to look for more occurances after it in the array:
int array[8] = {6,1,7,8,6,6,1,9};
for (int i = 0; i < 8; i++) {
// look to the left in the array if the number was used before
int found = 0;
for (int j = 0; j < i; j++) {
if (array[i] == array[j]) found++;
}
// go on if it's the first occurance
if (found == 0) {
// we know of one occurance
int count = 1;
// look to the right in the array for other occurances
for (int j = i + 1; j < 8; j++) {
if (array[i] == array[j]) count++;
}
cout << array[i] << ":" << count << endl;
}
}
I wanted to submit my solution which I think it´s a more easy one:
#include <iostream>
using namespace std;
int main() {
int n; //number of Elements in the vector
cin>>n;
int vec[n]; //vector with all elements
int v[n]; //vector with Elements without repetition
int c[n]; // vector which stores the frequency of each element
for(int i=0; i<n; i++)
cin>>vec[i];
int k=0; // number of Elements of the vector without Repetition, in the begining, it has 0 Elements
int j=0; //logic Counter, could be replaced with bool
for(int i=0; i<n; i++) {
for(int h=0; h<=k; h++) {
if(vec[i]==v[h]) {
c[h]++;
j=1;
break;
}
}
//if element i of the original vector is equal to element h of the second vector, then increment the frequency of this element
if(j==0) { //else if the element is not equal to any of the second vector, the Position of the 2nd vector is filled with the element, which in this case is the first of ist Kind.
v[k]=vec[i];
c[k]=1;
k++;
} //the number of Elements is increased by one to store another element;
else {
j=0;
}
}
cout<<endl<<endl;
for(int i=0; i<k; i++)
cout<<v[i]<<":"<<c[i]<<endl;
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
main()
{ int count[10],key[10],n=10,m;
int i,j,k,temp;
cout<<"Enter The Size Of Array:-\n";
cin>>n;
int a[n];
cout<<"Enter The Elements in Array:-\n";
for(i=0; i<n; i++)
cin>>a[i];
for(i=0; i<n; i++)
for(j=0; j<n-1; j++)
{ if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
for(i=0; i<n; i++)
cout<<a[i]<<"\t";
for(i=0,k=0; i<n; k++)
{ count[k]=0;
key[k]=a[i];
for(j=i; j<n; j++)
{ if(a[i]==a[j])
count[k]++;
}
i=i+count[k];
}
for(i=0; i<k; i++)
cout<<endl<<key[i]<<" Occurred "<<count[i]<<" Times\n";
getch();
}
/**
* The methods counts the frequency of each element in an array.
*
* Approach: The method checks if the element is already present in the <strong>Map of frequency</strong>.
* If it is not present, add it to the map with the frequency 1 else put it in the map with
* an increment by one of it's existing frequency.
*
* #param arr list of elements
* #return frequency of each elements
*/
public static Map<Integer, Integer> countFrequency(int[] arr) {
Map<Integer, Integer> frequency= new HashMap<Integer, Integer>();
for(int i = 0; i < arr.length; i++) {
if(frequency.get(arr[i])==null) {
frequency.put(arr[i], 1);
}
else {
frequency.put(arr[i],frequency.get(arr[i])+1);
}
}
System.out.println("\nMap: "+frequency);
return frequency;
}
Related
Assume that I have a array_dist, and I sorted this array in ascending order successfully. But I need also to store the indices of the original array in new array called index_arr.
Example: array [5,4,3,2,1] has indices [0,1,2,3,4]. what I need after sorting the array [1,2,3,4,5]. is these indices [4,3,2,1,0]
I try the following code which correctly sorted the array_dist but not correctly store the indices in index_arr
int index_arr[4344]{};
for (int i = 0; i < 4344; ++i) {
index_arr[i] = i;
}
float temp; int x;
for (int i = 0; i < 4344 ; i++)
{
for (int j = i + 1; j < 4344; j++)
{
if (array_dist[i] > array_dist[j])
{
temp = array_dist[i];
array_dist[i] = array_dist[j];
array_dist[j] = temp;
x = index_arr[i];
index_arr[i] = index_arr[j];
index_arr[j] = x;
}
}
}
cout << "print out distance colum after ascending sorting : \n";
for (int i = 0; i < 4344; ++i)
{
cout << index_arr[i] << " : "
<< array_dist[i] << endl;
}
The correctly working code that I compare my results with:
auto sortRuleLambda = [](pair<int, float> const& s1, pair<int, float> const& s2) -> bool
{
return s1.second < s2.second;
};
sort(array_dist.begin(), array_dist.end(), sortRuleLambda);
cout << "print out distance colum after ascending sorting : \n";
for (int i = 0; i < array_X_train.size(); ++i)
{
cout << array_dist[i].first << ": "
<< array_dist[i].second << endl;
}
map<int, int> smallest;
int arr[4344];
//do some initialize
for (int i=0;i<4344;i++)
{
smallest.insert(pair(arr[i], i));
}
Now smallest will contain the sorted array by value and contain its corresponding index.
Edit (same but without using map container):
class sth //sorry I don't creative with the name
{
public:
int value, index;
explicit sth()
{
value = 0;
index = 0;
}
sth (const int v, const int i)
{
value = v;
index = i;
}
};
int main()
{
constexpr int n = 10; //make a variable like this will make life easier
sth* smallest = new sth[n];
int arr[n] = {1,2,3,-4,12,5,124,-12,22,123};
//do some initialize
for (int i = 0; i < n; i++)
{
smallest[i]=sth(arr[i], i);
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (smallest[i].value > smallest[j].value)
{
sth temp = smallest[i];
smallest[i] = smallest[j];
smallest[j] = temp;
}
}
}
}
Now the smallest array should contain the value and the index of the first array.
The issue is that you are sorting the original array_dist when you shouldn't be sorting this at all.
You should only sort the index_array based on the comparison of the array_dist values :
// Compare values at `array_dist`, using the index array
if (array_dist[index_array[i]] > array_dist[index_array[j]])
{
// the indices need to be swapped
x = index_arr[i];
index_arr[i] = index_arr[j];
index_arr[j] = x;
}
Once you have this, then to print out the array_dist in sorted order:
for (int i = 0; i < 4344; ++i)
{
cout << index_arr[i] << " : "
<< array_dist[index_arr[i]] << endl;
}
You use the index_array as a subscript within the original array.
How can I change my code to get a count for every element?
With my code everything is okay. And it works, but how can I change only that part?
#include <iostream>
#include <vector>
void countFreq(int arr[], int n)
{
// Mark all array elements as not visited
std::vector<bool> visited(n, false);
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Skip this element if already processed
if (visited[i] == true)
continue;
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
visited[j] = true;
count++;
}
}
std::cout<<count<<" ";
}
}
int main()
{
int n;
std::cin>>n;
int arr[n];
for(int i = 0; i < n; i++){
std::cin>>arr[i];
}
countFreq(arr, n);
return 0;
}
And about the result`
input 10
1 1 2 2 3 3 4 4 5 5
output 2 2 2 2 2
but I want to get
output 2 2 2 2 2 2 2 2 2 2
(for every element)
Your function contains extra code that ends up confusing you. The visited variable is essentially unnecessary. Start the count at 0 and make no special case for the "current" cell and you'll find that some very simple code will do what you need:
void countFreq(int arr[], int n)
{
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Count frequency
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
std::cout << count << " ";
}
}
You need to save the result to an array for each number. Then when you find any processed number then print counter from the saved array.
#include <iostream>
#include <vector>
#include <unordered_map>
void countFreq(int arr[], int n)
{
// Mark all array elements as not visited
std::vector<bool> visited(n, false);
std::unordered_map<int, int> counter;
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++)
{
// Skip this element if already processed
if (visited[i] == true)
{
std::cout << counter[arr[i]] << " ";
continue;
}
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
visited[j] = true;
count++;
}
}
counter[arr[i]] = count;
std::cout<<count<<" ";
}
}
int main()
{
int n;
std::cin>>n;
int arr[n];
for(int i = 0; i < n; i++)
{
std::cin>>arr[i];
}
countFreq(arr, n);
return 0;
}
The issue is that you discard the values already visited.
One possibility is instead to memorize the count when the value is visited the first time,
and to memorize the index value of the first value appearance, when a value is visited the 2nd, 3rd ... time.
#include <iostream>
#include <vector>
void countFreq(const std::vector<int>& arr) {
int n = arr.size();
// Mark all array elements as not visited
std::vector<int> mem_count(n, n);
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
// Skip this element if already processed
if (mem_count[i] != n) {
std::cout << mem_count[mem_count[i]] << " ";
continue;
}
// Count frequency
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
mem_count[j] = i;
count++;
}
}
mem_count[i] = count;
std::cout << count << " ";
}
}
int main() {
int n;
std::cin>>n;
std::vector<int> arr(n);
for(int i = 0; i < n; i++){
std::cin >> arr[i];
}
countFreq(arr);
return 0;
}
You can find the frequencies of numbers this way if you know the what is your maximum element in the input array. lets say m is maximum number in your array.
so you have to create a new array of size m. you can simply co-relate them as m buckets. from 0 to m. And each bucket will hold the count of each element in the input array. The index of each bucket will refer to element in the input array. This has time complexity O(1) if we know what is the max element the array.
You can do this way:
std::vector<int> frequencey(std::vector<int>& nums){
auto max = *(std::max_element(nums.begin(), nums.end()));
std::vector<int> frequencies(max + 1, 0);
for(int i = 0; i < nums.size(); ++i){
frequencies[nums[i]] +=1;
}
return frequencies;
}
This is very simple
#include <vector>
#include <map>
#include <iostream>
void main()
{
std::vector<int> v { 1,1,2,2,3,3,4,4,5,5 }; // Your input vector
// Count "frequencies"
std::map<int, int> m;
for (auto i : v)
m[i]++;
// Print output
for (auto i : v)
std::cout << m[i] << " ";
}
So I am attempting to do a bubble-like sort. Not a bubble sort because I don't want to exchange every single value that i run into. I simply want to find the smallest value of each index and place it in order. Such as in the arrayVal[3, 5, 2].
Instead of replacing the value 3 with 2 and then replacing 5 with 3. I want to find the smallest number of the entire array and place it at arrayVal[0] and then move to arrayVal[1].
I can't quite figure out how to do this and am kind of stuck.(I took out the <> on the libraries so you could see which libraries I am using)
#include iostream
#include cmath
#include ctime
using namespace std;
int main()
{
const int STARTLOOP = 0;
const int MAXLOOP = 5;
const int MINRANGE = 1;
const int MAXRANGE = 10;
//int temp = 0;
int smallestVal = 0;
int arrayVal[MAXLOOP];
srand(time(0));
for (int i = STARTLOOP; i < MAXLOOP; i++)
{
arrayVal[i] = (rand() % MAXRANGE) + MINRANGE;
}
for (int i = STARTLOOP; i < MAXLOOP; i++)
{
cout << arrayVal[i] << endl;
}
cout << "Before the sort" << endl;
for (int i = STARTLOOP; i < MAXLOOP; i++)
{
for (int j = i; j < MAXLOOP; j++)
{
if (arrayVal[j] < arrayVal[i])
{
arrayVal[i] = smallestVal;
}
}
}
for (int i = STARTLOOP; i < MAXLOOP; i++)
{
cout << arrayVal[i] << endl;
}
cout << "After the sort" << endl;
return 0;
I also recognize I'm not using functions, i just wrote up the code because I was trying to figure this out. Thank you in advance.
You're never setting smallestVal to anything from the array. You don't need nested loops. Just go through the array once, comparing each value to smallestVal. If it's smaller, you set smallestVal to that value. You should also have a variable that holds the index of the smallest value, which you update at the same time.
At the end, you swap the first element with the smallest one.
int smallestVal = arrayVal[STARTLOOP];
int smallestIndex = STARTLOOP;
for (int i = STARTLOOP + 1; i < MAXLOOP; i++) {
if (arrayVal[i] < smallestVal) {
smallestVal = arrayVal[i];
smallestIndex = i;
}
}
if (smallestIndex != STARTLOOP) {
// swap it with the first element
int temp = arrayVal[STARTLOOP];
arrayVal[STARTLOOP] = smallestVal;
arrayVal[smallestIndex] = temp;
}
You can then increment STARTLOOP and repeat this.
What you want to achieve is some sort of a modified insertion sort. Check out this implementation :
void sort(int values[], int n)
{
for (int i = 0; i < n; ++i)
{
int min = i;
for ( int j = i ; j < n ; j++)
{
if (values[j]<values[min]) min =j;
}
while(values[min-1]>values[min])
{
if(min==0)
break;
if(values[min-1]!=values[min])
swap (values+min-1,values+min);
min--;
}
}
return;
}
What this function does is that it searches the array every time for the smallest element starting from index i and when it finds this element it keeps on swapping it will all of the previous elements in the array until it reaches a smaller element.
I'm having problems figuring out where my bubble sort code went wrong. I'm almost positive it's in the sorting algorithm. Here is what I need my code to accomplish:
-Initialize an array a fill it with random numbers (I use getNumbers() to accomplish this)
-Compare the first element with all later elements. If the first element is larger, swap them.
-Compare the second element with all later elements one by one. If the second element is larger, swap them.
-Continue comparing and swap operations until the second to last element.
-Print out the sorted array
And here's my code:
#include <iostream>
#include <cstdlib>
using namespace std;
void swap(int *, int *);
int *getNumbers(int);
int main()
{
//Get the size of the array from keyboard
int arraySize;
cout << "How many integers would you like to declare: ";
cin >> arraySize;
//Initialize array
int *array;
array = getNumbers(arraySize); //getNumbers should return a pointer
//Print out original array
cout << "Original array" << endl;
for(int count = 0; count < arraySize; count++)
{
cout << *(array + count) << " ";
}
//Sort array using the swap function
//Have a for loop to swap numbers one by one from min to max
//Compare values using a second for loop
//Swap values if former value is larger
//swap(&array[i],&array[j]);
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < (arraySize - 1); j++)
{
if(array[j] > array[j + 1])
{
swap(&array[i], &array[j]);
}
}
}
//Print out sorted array
cout << "\nSorted Array" << endl;
for(int count = 0; count < arraySize; count++)
{
cout << *(array + count) << " ";
}
return 0;
}
void swap(int *num1, int *num2)
{
//Keep record of original value of num1
int tempNum;
tempNum = *num1;
*num1 = *num2; //num1 value has been changed
*num2 = tempNum; //Fetch the original value of num1 and assign it to num2
}
int *getNumbers(int size)
{
int *array;
array = new int[size];
srand(time(0));
for(int i = 0; i < size; i++)
{
array[i] = rand() % 100;
}
return array;
}
Here is the correct code.
#include <iostream>
#include <cstdlib>
using namespace std;
void swap(int *, int *);
int *getNumbers(int);
int main() {
//Get the size of the array from keyboard
int arraySize;
cout << "How many integers would you like to declare: ";
cin >> arraySize;
//Initialize array
int *array;
array = getNumbers(arraySize); //getNumbers should return a pointer
//Print out original array
cout << "Original array" << endl;
for (int count = 0; count < arraySize; count++) {
cout << *(array + count) << " ";
}
//Sort array using the swap function
//Have a for loop to swap numbers one by one from min to max
//Compare values using a second for loop
//Swap values if former value is larger
//swap(&array[i],&array[j]);
for (int i = 0; i < arraySize; i++) {
for (int j = 0; j < (arraySize - 1); j++) {
if (array[j] > array[j + 1]) {
/*********** This line was changed ***********/
swap(&array[j+1], &array[j]); // You were earlier swapping ith and jth entries.
/*********************************************/
}
}
}
//Print out sorted array
cout << "\nSorted Array" << endl;
for (int count = 0; count < arraySize; count++) {
cout << *(array + count) << " ";
}
return 0;
}
void swap(int *num1, int *num2) {
//Keep record of original value of num1
int tempNum;
tempNum = *num1;
*num1 = *num2; //num1 value has been changed
*num2 = tempNum; //Fetch the original value of num1 and assign it to num2
}
int *getNumbers(int size) {
int *array;
array = new int[size];
srand(time(0));
for (int i = 0; i < size; i++) {
array[i] = rand() % 100;
}
return array;
}
You were swapping array[i] with array[j] in line 32. array[j] and array[j+1] should be swapped. Also, as pointed out by dd2, your loop bounds are not strict. The code would work correctly nonetheless but would take more steps. You can change the bound to j < (arraySize - i - 1)
Your loop bounds are not correct and swapping was wrong as well.
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < (arraySize - i - 1); j++)
{
if(array[j] > array[j + 1])
{
swap(&array[j], &array[j+1]);
}
}
}
I've managed to find the minimum value of every row of my 2D array with this
void findLowest(int A[][Cm], int n, int m)
{
int min = A[0][0];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (A[i][j] < min)
{
min = A[i][j];
}
}
out << i << " row's lowest value " << min << endl;
}
}
I'am trying to find the maximum value of every row using the same way,but it only shows me first maximum value
void findHighest(int A[][Cm], int n, int m)
{
int max = A[0][0];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (A[i][j] > max)
{
max = A[i][j];
}
}
out << i << " row's highest value " << max << endl;
}
}
I can't find what's wrong with the second function and why is it only showing me the first maximum value it finds. Any help ?
Both functions return the result (maximum or minimum) for the whole array rather than each row, because you set max once rather than once per row. You can get the result for each row as follows:
void findHighest(int A[][Cm], int n, int m)
{
for (int i = 0; i < n; i++)
{
int max = A[i][0];
for (int j = 1; j < m; j++)
{
if (A[i][j] > max)
{
max = A[i][j];
}
}
// do something with max
}
}
or, even better, use the standard library function max_element:
void findHighest(int A[][Cm], int n, int m)
{
if (m <= 0) return;
for (int i = 0; i < n; i++)
{
int max = *std::max_element(A[i], A[i] + m);
// do something with max
}
}
This should give you all values which is easy to check:
#include <algorithm>
#include <iostream>
enum { Cm = 2 };
void findHighest(int A[][Cm], int n, int m) {
if (m <= 0) return;
for (int i = 0; i < n; i++) {
int max = *std::max_element(A[i], A[i] + m);
std::cout << max << " ";
}
}
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
findHighest(A, 2, 2);
}
prints 2 4.
If your compiler supports C++11, for concrete arrays you could use the following alternative, that's based on std::minmax_element:
template<typename T, std::size_t N, std::size_t M>
void
minmax_row(T const (&arr)[N][M], T (&mincol)[N], T (&maxcol)[N]) {
for(int i(0); i < N; ++i) {
auto mnmx = std::minmax_element(std::begin(arr[i]), std::end(arr[i]));
if(mnmx.first != std::end(arr[i])) mincol[i] = *(mnmx.first);
if(mnmx.second != std::end(arr[i])) maxcol[i] = *(mnmx.second);
}
}
Live Demo
Your test data is guilty for not clearly showing you the defect.
The row minima occur in decreasing values, so that they get updated on every row.
And the row maxima also occur in decreasing values, so that the first one keeps winning.
As others pointed, your function finds the global minimum/maximum, no the per-row extrema.
Move the initialization of the min/max variable inside the outer loop.
As mentioned your code only shows the maximum element in the whole array.
Here is the code which will help you.
void findHighest(int A[][Cm], int n, int m)
{
int max[n];
max[0]=A[0][0];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (A[i][j] > max[i])
{
max[i] = A[i][j];
}
}
cout << i << " row's highest value " << max[i] << endl;
}
}
{
int i,j;
int arr[4][2]={(1,2),(3,4),(5,6),(7,8)};
int max;
max=arr[0][0];
for( int i=0; i<4; i++)
{
for(int j=0; j<2; j++)
{
if(max<arr[i][j])
{
max=arr[i][j];
}
}
}
int min;
min=arr[0][0];
for(int i=0; i<4; i++)
{
for(int j=0; j<2; j++)
{
if(min>arr[i][j])
{
min=arr[i][j];
}
}
}
cout<<"maximum number is:"<<max;
cout<<endl;
cout<<"Minimum Number is:"<<min;
}