i am trying to get the total amounts of numbers occurunces , my main problem is that i messed up some where and i cant read numbers that are higher than 12 , as in my count wont see it other than that works perfectly , doesnt matter if sorted on not array doesnt affect the program(for my random array example)
*** int Count(int r[], int n, int x) {
int res = 0;
for (int i = 0; i <n; i++)
if (x == r[i])
res++;
return res;
}
int main() {
int count = 0;
int r[12] = { 1, 1, 2, 3, 4, 5, 6, 6, 7, 8,13,13 };
int n = sizeof(r) / sizeof(r[0]);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) {
if (r[i] > r[j])
{
swap(r[i], r[j]);
}
}
}
for (int i = 0; i <= n; i++) {
if (Count(r, n, i) >= 2) {
count++;
cout << "number" << i << "-" << Count(r, n, i) << " Recurrences" << endl;
}
}
cout << "count is " << count;
return 0;
} ***
Try an easier and more optimized way using map:
int r[12] = { 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 13, 13 };
int n = sizeof(r) / sizeof(r[0]);
map<int, int> map;
for (int i = 0; i < n; i++)
{
if (map.find(r[i]) != map.end())
{
map[r[i]]++;
}
else
{
map[r[i]] = 1;
}
}
for (auto const& x: map)
{
cout << x.first << ':' << x.second << std::endl;
}
output
1:2
2:1
3:1
4:1
5:1
6:2
7:1
8:1
13:2
Related
i'm trying to make it so if i enter the number 3 , the matrix would change to {1,2,4,5,6,7,8,9,10,11,12,0}, i'm not sure if the method of switching the element between two arrays is the best way to do it and i'm trying to do it using pointers
using namespace std;
#include <iomanip>
int main() {
int arr[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, i, j,c;
int* ptr = &arr[0][0];
int zero[3][4] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
int* zptr = &zero [0][0];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout << setw(8) << arr[i][j] << ' ';
}
cout << endl;
cout << ' ' << endl;
}
for (i = 0; i < 3; i++) {
if (arr[i][0] = i)
{
break;
}
for (int j = 0; j < 4; j++)
cin >> i;
cout << ptr;
}
return 0;
}
you could use easier approach to achieve this using vector in c++ and by finding the index of the input element, consider this example :
int getIndex(vector<int> v, int K) {
auto it = find(v.begin(), v.end(), K);
// calculating the index of K
// note that here i'm supposed the input is valid
int index = it - v.begin();
return index;
}
and you can use the index you get to 1) append in A the element B[index], 2) assign B[index] to A[index] and 3) erase the element A[index].
hope this can be helpful.
I want to shift value in multi dimensional array, i try but its not working at all and am also confused within logic.
here what i want to do
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
and let i pass 1, 2 so it should delete 5 from array and shift all elements.
Expected Output:
1, 2, 3,
4, 6, 7,
8, 9, 0
Here is my code.
#include <iostream>
void display(int arr[][3], int size)
{
std::cout << "\nDisplay of array:\n";
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
std::cout << i << " " << j << " : " << arr[i][j] << std::endl;
}
}
return;
}
void insert(int arr[][3], int size)
{
std::cout << "\nInserting element in an array:\n";
int pos, num;
std::cout << "Enter new value to be inserted: ";
std::cin >> num;
std::cout << "Enter the position where you wanted to inserted it: ";
std::cin >> pos;
/*for (int i = size - 2; i >= pos; i--) {
arr[i+1] = arr[i];
}*/
//arr[pos] = num;
display(arr, size);
}
void delete_ele(int arr[][3], int size)
{
std::cout << "\nDeleting an array:\n";
int posX, posY;
display(arr, size);
std::cout << std::endl;
std::cout << "Enter the X pos where you wanted to delete element: ";
std::cin >> posX;
std::cout << "Enter the Y pos where you wanted to delete element: ";
std::cin >> posY;
const int s = 3;
int newArr[s][s];
int x = 0, y = 0;
bool t = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == posX && j == posY) {
t = true;
continue;
}
newArr[x][y] = arr[i][j];
y++;
}
if (t) {
continue;
}
x++;
}
display(newArr, size);
}
int main(void)
{
// size of array.
const int size = 3;
//Decleration of arrays
int arr[size][size] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int arr2[size][size] = {
{1, 2, 3},
{7, 8, 9}
};
// Delete an array basse on position.
delete_ele(arr, size);
// Insert an array by shifting..
insert(arr2, size);
return 0;
} // end of main.
Thanks
Hello I wanted to implement a bucket sort. I think it works for me:
#include <iostream>
using namespace std;
#include <vector>
#include <cmath>
#include <algorithm>
void bucket_sort(int[], const int);
int main(){
system("color 1f");
int array[] = {
5, 77, 99, 100, 77, 57, 23, 1, 2, 57,
81, 24, 21
};
cout << "before sorting: " << endl;
for(auto x : array)
cout << x << ", ";
cout << endl;
bucket_sort(array, 13);
cout << "after sorting: " << endl;
for( auto x : array)
cout << x << ", ";
cout << endl << endl;
return 0;
}
void bucket_sort(int array[], const int size){
int bucket = 10, divider;
int min = array[0], max = array[0], j;
for(auto i(0); i != size; ++i){
if(min > array[i])
min = array[i];
if(max < array[i])
max = array[i];
}
divider = (ceil( (double)(max + 1) / bucket) );
std::vector<int>* vi = new vector<int>[bucket];
for(i = 0; i < size; i++){
j = floor(array[i] / divider);
vi[j].push_back(array[i]);
}
for(i = 0; i < bucket; i++)
std::sort(vi[i].begin(), vi[i].end());
int k = 0;
for(i = 0; i < bucket; i++){
for(int j(0); j < vi[i].size(); j++){
array[k] = vi[i][j];
k++;
}
}
delete[]vi;
}
The code works fine. But is this the way I should do? If I have an array with values close to each other but one or two are too much big:
66, 42, 70, 10, 30, 32, 28, 1000, 50000
How to apply this bucket sort algorithm? Also how many buckets should I use?
I am not sure where you get the number 10. You can use the total number of items in the array.
You can declare vector of vector to create a 2-D vector (instead of new)
vector<vector<int>> vec;
Ideally, bucket_sort should not rely on other sort methods. Here is a version which uses recursion. It keeps iterating until there is zero or one element in each bucket (or several elements with the same value)
Example:
void bucket_sort(std::vector<int> &src)
{
if(src.size() <= 1)
return;
int min = *std::min_element(src.begin(), src.end());
int max = *std::max_element(src.begin(), src.end());
if(min == max)
return;
std::vector<std::vector<int>> vec;
vec.resize(src.size());
for(int i = 0; i < src.size(); i++)
{
//edit: double precision required
int v = int(double(src[i] - min) * src.size() / double(max - min + 1));
vec[v].push_back(src[i]);
}
for(size_t i = 0; i < vec.size(); i++)
//std::sort(vec[i].begin(), vec[i].end());
bucket_sort(vec[i]);
int index = 0;
for(size_t i = 0; i < vec.size(); i++)
for(size_t j = 0; j < vec[i].size(); j++)
src[index++] = vec[i][j];
}
int main()
{
std::vector<int> src = {66, 66, 42, 70, 10, 30, 32, 28, 1000, 50000 };
for(auto x : src) cout << x << ", ";
cout << endl;
bucket_sort(src);
for(auto x : src) cout << x << ", ";
cout << endl << endl;
return 0;
}
Testing:
int test()
{
srand((unsigned int)time(NULL));
//add 1 million random numbers for testing
std::vector<int> src;
for(int i = 0; i < 1000000; i++)
src.push_back(rand());
bucket_sort(src);
for(size_t i = 1; i < src.size(); i++)
if(src[i - 1] > src[i])
{
cout << "fail\n";
return 0;
}
cout << "success\n";
return 0;
}
Edit: changed division calculation to double precision, added test for 1 million
I'm tried to figure out how to do it for quite of time and its not working as intended; I'm writing a code where there is 1 to k numbers, I need to find all possible combination without repeats. e.g. for 3: 1, 2, 3, 12, 13.
Example for counting 4-digits numbers with 1, 2, 3, 4, 5.
int k = 5;
for (int p = 0; p < k; p++)
{
for (int i = p+1; i < k; i++)
{
for (int j = i + 1; j < k; j++)
{
for (int h = j + 1; h < k; h++)
{
cout << p + 1 << i + 1 << j + 1 << h + 1 << endl;
}
}
}
}
And there is example for 3-digits number with 1, 2, 3.
int k = 4
for (int p = 0; p < k; p++)
{
for (int i = p+1; i < k; i++)
{
for (int j = i + 1; j < k; j++)
{
cout << p + 1 << i + 1 << j + 1 << endl;
}
}
}
I think that to count n-digits possible position without repeat i need n for's.
And i don't know how to do it without recursion which don't work when i do it.
My goal to get recursion which will count and print possible positions for n-digits.
I did recursion to count possibility myself, but love you guys for all your help.
My recursion is
void col(int ilosc)
{
static int st;
for (int i = st++; i < k; i++)
{
if (ilosc > 1)
col(ilosc - 1);
else
sposob++;
}
}
where ilosc is digits number and sposob is count of possible positions numbers.
NOTE: sposob and k is global variables.
I am not sure whether recursion is the best choice here, but you could do it like this:
typedef std::vector<int> IV;
IV getFirst(int k){
IV res;
for (int i=0;i<k-1;i++){res.push_back(i+1);}
return res;
}
bool getNext(IV& numbers,int i){
if (i==-1){return false;} // end of recursion
if (numbers[i]>i+1){return getNext(numbers,i-1);}
numbers[i]++;
return true;
}
bool getNext(IV& numbers){ // start of recursion
return getNext(numbers,numbers.size()-1);
}
int main() {
IV numbers = getFirst(5);
for (int i=0;i<numbers.size();i++){std::cout << numbers[i];}
std::cout << std::endl;
while(getNext(numbers)){
for (int i=0;i<numbers.size();i++){std::cout << numbers[i];}
std::cout << std::endl;
}
}
I think this will get you pretty close. I have an occasional repeat here, but this should set you on the right path.
const int max_depth = 5; // How long your string is
const int max_digit = 3; // Max digit you are counting to
int *nums = new int [max_depth];
void recurse_count(int depth)
{
if (depth < max_depth)
{
for(int i = depth; i <= depth+1; i++)
{
nums[depth] = i;
recurse_count(i+1);
}
}
else
{
for (int j = 0; j < max_depth; j++)
cout<<nums[j]+1;
cout<<endl;
}
}
int main()
{
recurse_count(0);
return 0;
}
My approach (still too early in the evening probably, I had problems with it)
namespace detail
{
void recurse_hlp(int min, int max, std::vector<int> vals, std::function<void(const std::vector<int>&)> f, std::size_t ptr)
{
if (ptr == vals.size())
f(vals);
else
{
for (int i = min; i <= max; ++i)
{
vals[ptr] = i;
recurse_hlp(min, max, vals, f, ptr + 1);
}
}
}
}
void recurse(int min, int max, int count, std::function<void(const std::vector<int>&)> f)
{
std::vector<int> vals(count);
detail::recurse_hlp(min, max, vals, f, 0);
}
void print(const std::vector<int>& vals)
{
for (int v : vals)
std::cout << v << " ";
std::cout << std::endl;
}
int main()
{
recurse(0, 5, 3, &print);
}
recurse gets a function accepting std::vector<int>, which contains all numbers from min to max up to count places.
The function should check if there is any duplicated number but it's not working somehow.
anyone knows why?
#include <iostream>
using namespace std;
int main()
{
int const size = 10;
int arry[size] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};
int i = 0;
int k = 1;
for (i = 0; i < size; i++)
{
for (k = 1; k < size; k++)
{
if (arry[i] == arry[k])
{
cout << arry[i] << " " << arry[k] << endl;
cout << "duplicate" << endl;
return 0;
}
}
}
cout << "no duplicate" << endl;
return 0;
}
You should skip when i == k.
for (i = 0; i < size; i++)
{
for (k = i+1; k < size; k++) // <-- either change HERE
{
if (i != k && arry[i] == arry[k])
// ^^^^^^ or HERE
{
cout << arry[i] << " " << arry[k] << endl;
cout << "duplicate" << endl;
return 0;
}
}
}
You're checking every combination twice - for example, you're comparing element 2 with element 5, and then 5 with 2. Aside from the fact that you're also checking an element with itself (which is what timrau's answer is guarding against), you're doing the same work twice.
So try
for (i = 0; i < size; i++)
{
for (k = i + 1; k < size; k++)
instead.
Your approach still doesn't scale very well, it's essentially O(n^2). It would be faster to sort your array and then look for duplicate neighbors.
A more efficient approach should be to sort the array first:
std::vector<int> a = {4, 3, 2, 1, 2, 5, 6};
std::sort(std::begin(a), std::end(a));
for (std::size_t i = 1; i < a.size(); ++i) {
if (a[i - 1] == a[i]) {
std::cout << a[i] << " " << a[i] << std::endl;
std::cout << "duplicate" << std::endl;
return 0;
}
}
return 0;