How to eliminate certain numbers in a series by C++? - c++

I'm trying to eliminate some number from range (170-2500) and then calculate the remaining numbers after the elimination. The numbers composed of 3 digits from number's list (2,5,6,7). I have tried using Cartesian Product to generate the numbers, but I confused how to eliminate the numbers from the range. The cartesian product code is obtained from geeksforgeeks. I know how to calculate the remaining numbers but I confused with the numbers that will be eliminated.
void findCart(int arr1[], int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
printf("{%d%d%d}, ", arr1[i], arr1[j], arr1[k]);
}
int main()
{
min=170;
max=2500;
int arr1[] = {2,5,6,7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
findCart(arr1, n1, number);
int count=0;
if (number>=min && number<=max){
count++;
}
int total=max-min+1;
int result=total-count;
cout<<result;
return 0;
}

Not sure that I understand your question, but from your code, i think you are trying to do this: create an integer number from list of digits then check if it is from min to max, then print number of remaining in range.
try this code, hope you understand my idea:
// return total number which are meet conditions
int findCart(int arr1[], int n, int min, int max)
{
int totalNumbers = 0;
int number = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++){
number = arr1[i]*100 + arr1[j]*10 + arr1[k];
if ( number >= min && number <= max )
++totalNumbers;
}
return totalNumbers;
}
int main()
{
int min=170;
int max=2500;
int arr1[] = {2,5,6,7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int totalNumberFound = findCart(arr1, n1, min, max);
int total=max-min+1;
int result=total- totalNumberFound;
cout<<result;
return 0;
}

Related

Why isn't my C++ maximizing pairwise product code working?

So the problem asked me to find the greatest product from a given sequence of non-negative integers. What I did was I tried to find the greatest two integers from the sequence (for which I used a vector, by taking an input of n numbers) and multiplied them, since there are no negative integers. I used the long long type as well, since just an int type would not be enough for huge numbers. I kept getting a random huge number as the output whenever I tried to run the program :
#include <iostream>
#include <vector>
using namespace std;
long long max_prod(const vector<int>& numbers) {
int max1 = -1;
int max2 = -1;
int n = numbers.size();
for (int i = 0; i<n; i++){
if (numbers[i] > numbers[max1])
max1 = i;
}
for (int j = 0; j < n; j++) {
if (numbers[j] > numbers[max2] && j!=max1)
max2 = j;
}
return ((long long)(numbers[max1])) * ((long long)(numbers[max2]));
}
int main(){
int n;
cin >> n;
vector<int> numbers(n);
for (int i = 0; i<n; i++){
cin >> numbers[i];
}
long long result = max_prod(numbers);
cout << result << "\n";
return 0;
}
the last line is the output given by the program
You haver undefined behavior right here
long long max_prod(const vector<int>& numbers) {
int max1 = -1; <<<<<====
int max2 = -1;
int n = numbers.size();
for (int i = 0; i < n; i++) {
if (numbers[i] > numbers[max1]) <<<<<==
max1 = i;
}
for (int j = 0; j < n; j++) {
if (numbers[j] > numbers[max2] && j != max1)
max2 = j;
}
return ((long long)(numbers[max1])) * ((long long)(numbers[max2]));
}
You try to access numbers[-1] (twice once in the j loop and once in the i loop).
Set maxi and maxj to 0

Bucket sort with huge random numbers

I know Bucket sort is has a lot of examples everywhere, so I tried to implement this so it can take huge random numbers with no luck
void Bucket_sort(int arr[], int max){
const int maxsize = max;
int bucket_list = new int [maxsize+1];
int length = sozeof(bucket_list) / sizeof(bucket[0]);
for(int i = 0; i <max;i++){
bucket_list[i] = 0; //fill with zeros
}
for (unsigned int i = 0; i <length; i++){
bucket_list[arr[i]]++;
}
int position = 0;
for (unsigned int i = 0 i < length; i++){
for(int k = 0; k<bucket_list[i];k++){
arr[position++] = i;
}
}
}
int main() {
int max = 50000
int arr[max];
for (int i = 0; i < max; i++){
arr[i] = rand() % 50000;
}
cout<<"Here are the numbers before Bucker Sort"<<endl;
for (int j = 0; j < max; j++){
cout<<arr[j];
}
Bucket_sort(arr,max);
for (int k = 0; k<max; k++){
cout<<arr[k];
}
}
some how I can't get it working, it will just out put the same order (unsorted)
I did find some same questions as mine, but none of them helped, here is one
https://stackoverflow.com/questions/20037176/c-bucket-sort-putting-integers-into-buckets
This line:
bucket_list = 0; //fill with zeros
this is changing your pointer, not filling with zeros. You should use
bucket_list[i] = 0; //fill with zeros
Edit: There are a lot more compiler issues with your code. Once you have those sorted out, the calculation of length is still wrong. You can't use the sizeof dividing trick, because bucket_list isn't an array. Replace
int length = sozeof(bucket_list) / sizeof(bucket[0]);
with
int length = maxsize
or just don't use length at all (you already have maxsize).
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void Bucket_sort(int arr[], int max){
int maxsize = max;
int *bucket_list = new int[maxsize+1];
// int length = sozeof(bucket_list) / sizeof(bucket[0]);
int length = maxsize;
for(int i = 0; i <max;i++){
bucket_list[i] = 0; //fill with zeros
}
for (unsigned int i = 0; i <length; i++){
bucket_list[arr[i]]++;
}
int position = 0;
for (unsigned int i = 0 ; i < length ; i++){
for(int k = 0; k<bucket_list[i];k++){
arr[position++] = i;
}
}
}
int main() {
int max = 50;
int arr[max];
for (int i = 0; i < max; i++){
arr[i] = rand()%50;
}
cout<<"Here are the numbers before Bucker Sort"<<endl;
for (int j = 0; j < max; j++){
cout<<arr[j];
}
Bucket_sort(arr,max);
for (int k = 0; k<max; k++){
cout<<arr[k];
}
getch();
return 0;
}

Why this radixSort doesn't work

It look's like it's OK. But result is telling something different. Really, I don't know whats wrong with it. Maybe it's because I wrote it at 2 o'clock at night ...
EDIT:
template <class T>
void radixSort(T & arr, msize numberBase)
{
long maxValue = findMax(arr);
dynarray<long> tarr(arr,arr.size());
dynarray<long> presenceTable(numberBase+1);
for (register long i=0, max=presenceTable.size(); i<max; ++i)
{
presenceTable[i] = 0;
}
for (register long exp=1; maxValue/exp>0; exp*=numberBase)
{
for (register long i=0, max=tarr.size(); i<max; ++i)
{
++(presenceTable[(tarr[i]/exp)%numberBase]);
}
for (register long i=1, max=presenceTable.size(); i<max; ++i)
{
presenceTable[i] += presenceTable[i-1];
}
for (register long i=0, max=tarr.size(); i<max; ++i)
{
arr[ (--(presenceTable[(tarr[i]/exp)%numberBase])) ] = tarr[i];
}
tarr = arr;
for (register long i=0, max=presenceTable.size(); i<max; ++i)
{
presenceTable[i] = 0;
}
}
}
Try algorithm from this code:
#include<bits/stdc++.h>
using namespace std;
// Function to get maximum value in array a[].
int getmax(int a[], int n)
{
int max = a[0];
for (int x=1; x<n; x++)
if (a[x] > max)
max = a[x];
return max;
}
// Function to do counting sort according to significant digits repesented by
// exp (where exp is 10^i).
void CountSort(int a[], int n, int exp)
{
int result[n], i, count[10] = {0};
// Counting occurence of digits
for (i =0; i <n; i++)
count[(a[i] / exp) % 10]++;
// Changing the position of count so that it appears at actual position in result.
for (i =1; i<10; i++)
count[i] += count[i-1];
// Resultant output array
for (i =n-1; i>= 0; i--)
{
result[count[(a[i] / exp) % 10] - 1] = a[i];
count[(a[i] / exp) % 10]--;
}
for (i =0; i <n; i++)
a[i] = result[i];
}
// Radix Sort to sort a[] of given size.
void radixsort(int a[], int n)
{
int exp, i;
i = getmax(a, n);
for (exp = 1; i/exp > 0; exp *= 10)
CountSort(a, n, exp);
}
// Driver Program
int main()
{
int n;
cout<<" Enter the number of elements to be sorted: ";
cin>>n;
int a[n];
cout<<"\n Enter the elements: ";
for(int i =0; i <n; i++)
{
cin>>a[i];
}
radixsort(a, n);
// Printing the sorted list.
cout<<"\nSorted List: ";
for (int i = 0; i < n; i++)
cout<<a[i]<<", ";
return 0;
}
I changed it a bit and it worked for me

How can I properly add and access items to a multidimensional vector using loops?

I have this program that is trying to determine how many unique items are within some intersecting sets. The amount of input entirely depends on the the first value n, and then the amount of sets entered afterward. For example, if I start with entering n = 2, I am expected to enter 2 integers. The program then determines how many intersections there are between n items (this is like choosing 2 items from n items). This goes on as k increments. But that's kind of beyond the point. Just some background info.
My program adapts correctly and accepts the proper amount of input, but it stops working properly before the first for loop that is outside of the while loop. What I have tried to do is make a vector of integer vectors and then add every other row (when index starts at 0 AND index starts at 1). But I am guessing I have constructed my vectors incorrectly. Does anybody see an error in my vector logic?
#include <iostream>
#include <vector>
using namespace std;
int fact (int m) {
if (m <= 1)
return 1;
return m * fact(m - 1);
}
int comb (int n, int k) {
return fact(n)/(fact(n-k)*fact(k));
}
int main() {
int n = 0;
int k = 2;
int sum = 0;
int diff = 0;
int final = 0;
vector <vector <int> > arr;
cin >> n;
while (n > 0) {
vector <int> row;
int u;
for (int i = 0; i < n ; ++i) {
cin >> u;
row.push_back(u);
}
arr.push_back(row);
n = comb(row.size(), k);
k++;
}
for (int i = 0; i < arr.size(); i+2)
for (int j = 0; j < arr[i].size(); ++j)
sum += arr[i][j];
for (int i = 1; i < arr.size(); i+2)
for (int j = 0; j < arr[i].size(); ++j)
diff += arr[i][j];
final = sum - diff;
cout << final;
return 0;
}
for (int i = 0; i < arr.size(); i+=2)
^
You want to do i+=2 or i=i+2, else the value of i is never changed, leading to an infinite loop.

Selection sort ascending

That is my function:
int main() {
double data[100];
int num;
cout<<"num= ";
cin>>num;
for(int i = 1; i <= num; i++) {
cout<<i<<" element = ";
cin>>data[i];
}
Sort(data, num);
for (int i = 1; i <= num; i++) {
cout<<data[i]<<endl;
}
return 0;
}
void Sort(double data[], int n) {
int i,j,k;
double min;
for(i = 0; i < n-1; i++) {
k = i;
min = data[k];
for(j = i+1; j < n; j++)
if(data[j] < min) {
k = j;
min = data[k];
}
data[k] = data[i];
data[i] = min;
}
}
if I write for exp. three elements: 8,9,1 again cout 8,9,1?
for(int i = 1; i <= num; i++) { // WRONG
I think you mean:
for(int i = 0; i < num; i++) { // RIGHT
Arrays in C are 0-indexed remember.
Your sorting function is fine. The only problem is that you enter elements at positions 1 through n, inclusive, while you should use 0 through n-1, inclusive, in both loops of the main() function.
If you need to print numbers 1 through n, use
cout<<(i+1)<<" element = ";
You should get used of the 0 index begin in the for loop
for(int i = 0; i < N; ++i)
so fixing these two index errors will make your code run properly.
the reason is:
if you write data to data[] using 1 as the begining, your data array's first item will be a random number:
if you insert 3 elements, the array will be like this:
data[0] = ??? // maybe a very very big number
data[1] = 8
data[2] = 9
data[3] = 1
and in your Sort function, your index begins at 0 and ends before num, that means your code would only sort data[0], data[1], data[2].
if you use: num = 3, 3 2 1 as your input data for the origin code you could see that 3 and 2 is sorted
I guess your Sort code is googled from somewhere, please try to understand it.
Good online algorithm course: https://www.coursera.org/course/algs4partI
a very good algorithm online book: http://algs4.cs.princeton.edu/home/
btw, for(j = i+1; j < n; j++) in the Sort function would be better if it has { } braces.