It is a very simple code that should solve the Cut Rod Optimization Problem. Most of the time it outputs the correct output but sometimes it gives random answers. I don't know what might be causing this.
Code:
#include <iostream>
#include <limits>
int cutRod(int p[], int n);
int max(int a, int b);
int main()
{
int n = 10;
int p[n] = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
int numOfCuts = cutRod(p, n);
std::cout << "Cuts: " << numOfCuts << std::endl;
return 0;
}
int cutRod(int p[], int n)
{
if (n == 0)
{
return 0;
}
int q = INT32_MIN;
for (int i = 0; i <= n; ++i)
{
q = max(q, p[i] + cutRod(p, n-i-1));
}
return q;
}
int max(int a, int b){
if (a >= b)
{
return a;
}
else if (b > a)
{
return b;
}
}
How to regenerate error:
Just run the program a couple of times. Most of the times, it gives 30 as answer. Which is correct. But other times, it will give a random big number as an output.
for (int i = 0; i <= n; ++i)
{
q = max(q, p[i] + cutRod(p, n-i-1));
}
here you are calling p[n] which is out of index but in c++ array it still lets you access that memory block having a garbage value, so you are basically adding a garbage value with returned value of function.
in array you will have valid value upto only 0 to n - 1 for p,
I guess this is the thing that is causing the problem
Related
Implement a function which takes an array of numbers from 1 to 10 and returns the numbers from 1 to 10 which are missing. examples input: [5,2,6] output: [1,3,4,7,8,9,10]
C++ program for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to find the missing elements
void printMissingElements(int arr[], int N)
{
// Initialize diff
int diff = arr[0] - 0;
for (int i = 0; i < N; i++) {
// Check if diff and arr[i]-i
// both are equal or not
if (arr[i] - i != diff) {
// Loop for consecutive
// missing elements
while (diff < arr[i] - i) {
cout << i + diff << " ";
diff++;
}
}
}
}
Driver Code
int main()
{
// Given array arr[]
int arr[] = { 5,2,6 };
int N = sizeof(arr) / sizeof(int);
// Function Call
printMissingElements(arr, N);
return 0;
}
How to solve this question for the given input?
First of all "plzz" is not an English world. Second, the question is already there, no need to keep writing in comments "if anyone knows try to help me".
Then learn standard headers: Why should I not #include <bits/stdc++.h>?
Then learn Why is "using namespace std;" considered bad practice?
Then read the text of the problem: "Implement a function which takes an array of numbers from 1 to 10 and returns the numbers from 1 to 10 which are missing. examples input: [5,2,6] output: [1,3,4,7,8,9,10]"
You need to "return the numbers from 1 to 10 which are missing."
I suggest that you really use C++ and get std::vector into your toolbox. Then you can leverage algorithms and std::find is ready for you.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
std::vector<int> missingElements(const std::vector<int> v)
{
std::vector<int> missing;
for (int i = 1; i <= 10; ++i) {
if (find(v.begin(), v.end(), i) == v.end()) {
missing.push_back(i);
}
}
return missing;
}
int main()
{
std::vector<int> arr = { 5, 2, 6 };
std::vector<int> m = missingElements(arr);
copy(m.begin(), m.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
return 0;
}
If you want to do something with lower computational complexity you can have an already filled vector and then mark for removal the elements found. Then it's a good chance to learn the erase–remove idiom:
std::vector<int> missingElements(const std::vector<int> v)
{
std::vector<int> m = { -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (const auto& x: v) {
m[x] = -1;
}
m.erase(remove(m.begin(), m.end(), -1), m.end());
return m;
}
By this approach we are using space to reduce execution time. Here the time complexity is O(N) where N is the no of elements given in the array and space complexity is O(1) i.e 10' .
#include<iostream>
void printMissingElements(int arr[], int n){
// Using 1D dp to solve this
int dp[11] = {0};
for(int i = 0; i < n; i++){
dp[arr[i]] = 1;
}
// Traverse through dp list and check for
// non set indexes
for(int i = 1; i <= 10; i++){
if (dp[i] != 1) std::cout << i << " ";
}
}
int main() {
int arr[] = {5,2,6};
int n = sizeof(arr) / sizeof(int);
printMissingElements(arr, n);
}
void printMissingElements(int arr[], int n,int low, int high)
{
bool range[high - low + 1] = { false };
for (int i = 0; i < n; i++) {
if (low <= arr[i] && arr[i] <= high)
range[arr[i] - low] = true;
}
for (int x = 0; x <= high - low; x++) {
if (range[x] == false)
std:: cout << low + x << " ";
}
}
int main()
{
int arr[] = { 5,2,6,6,6,6,8,10 };
int n = sizeof(arr) / sizeof(arr[0]);
int low = 1, high = 10;
printMissingElements(arr, n, low, high);
return 0;
}
I think this will work:
vector<int> missingnumbers(vector<int> A, int N)
{ vector<int> v;
for(int i=1;i<=10;i++)
v.push_back(i);
sort(A.begin(),A.end());
int j=0;
while(j<v.size()) {
if(binary_search(A.begin(),A.end(),v[j]))
v.erase(v.begin()+j);
else
j++;
}
return v;
}
I am doing the coin problem, the problem says that,
Given a set of coin values coins = {c1, c2,..., ck} and a target sum
of money n, our task is to form the sum n using as few coins as
possible.
suppose you have 9 dollars and you have set of {6,5,1} so, the minimum no. of sum/change for 9 dollars would be ( 6+1+1+1=9) i.e. 4.
i tried doing it recursively using this formula :
solve(x) = min( solve(x−6)+1, solve(x−5)+1, solve(x−1)+1 )
,but I don't know why I'm getting Segmentation fault in my code.
There are plenty of codes available online, but I want to know what am I doing wrong here, I'm new to recursion please help me, The code goes here:
//my code
#include<bits/stdc++.h>
using namespace std;
int solve (int x, int a[], int n)
{
if (x < 0)
{
return INT_MAX;
}
if (x == 0)
{
return 0;
}
int best = INT_MAX;
for (int i = 0; i < n; i++)
{
best = min (best, solve (x - a[i], a, n) + 1);
}
return best;
}
int main ()
{
int a[] = { 6, 5, 1 };
int x = 9;
int n = 3;
cout << solve (x, a, n);
return 0;
}
The code which have been took from: https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/
#include <iostream>
using namespace std;
int minCoins(int coins[], int m, int amount) {
if (amount == 0) return 0;
int res = INT_MAX;
for (int i = 0; i < m; i++) {
if (coins[i] <= amount) {
int sub_res = minCoins(coins, m, amount - coins[i]);
if (sub_res != INT_MAX && sub_res + 1 < res) { // avoid overflow
res = sub_res + 1;
}
}
}
return res;
}
int main() {
int coins[] = { 6, 5, 1 };
int amount = 9;
cout << "Min coins is "
<< minCoins(coins, sizeof(coins) / sizeof(coins[0]), amount)
<< endl;
return 0;
}
About the problem:
Your Segmentation fault comes from the line:
best = min (best, solve (x - i, a, n) + 1);
The reason is: x-i will always gives you the same value so if you are run the program without debugging, your program crashing. So don't try to debug it because it will takes a lot of time to see this crashing.
For starters change to: best = min (best, solve (x - a[i], a, n) + 1);.
After fixing the section 1, the if case: if (x < 0) return INT_MAX; will causes problem and will return always the same value, which is: -INT_MAX. So you need to check the "if cases" again.
The algorithm you try to implement is not correct, see the pseudo-code of this algorithm:
minchange(M):
if M = 0:
return 0
v <- infinity
for c in denominations <= M:
v <- min { minchange(M - c) + 1, v }
return v
Better use: sizeof(a) / sizeof(a[0]) instead of int n = 3.
Im trying to get my factorial function to go through all the numbers in my array and give me the result but I keep getting an error message. Any idea what im doing wrong? Here is my code:
#include <iostream>
#include "Recursion.h"
int factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
int main()
{
int my_list[5] = {2, 4, 6, 8, 10};
for(int i = 0; i < 5; ++i)
{
int b = factorial(my_list[i]);
std::cout << b << std::endl;
}
return 0;
}
Your function is designed to take a single integer, and not an array. Iterate over the array, and call the method on each int within the array
for(int i = 0; i < 5; ++i)
{
int b = factorial(my_list[i]);
std::cout << b << std::endl;
}
Can someone point out the mistake in my code? Sorry for misleading names of function arguments - rptr should be rvalue or somethings etc, I was constantly changing something.
Most of it should be done using pointers.
I think the mistake might be in partArr, returning invalid variable, but i dont really know.
#include <iostream>
const int ArSize = 10;
void swap(int *lptr, int *rptr) {
int tempV = *lptr;
*lptr = *rptr;
*rptr = tempV;
}
int partArr(int *arr, int lptr, int rptr) {
int pivot = (lptr + rptr) / 2;
int * leftP = &lptr;
int * rightP = &rptr;
while (true) {
while (arr[*rightP] >= pivot) --(*rightP);
while (arr[*leftP] <= pivot) ++(*leftP);
if(*rightP > *leftP) {
swap(leftP,rightP);
--(*rightP);
++(*leftP);
}
else {
return rptr;
}
}
}
void quickSort(int *arr, int ptrL, int ptrR) {
if (ptrR > ptrL) {
int arr_piv = partArr(arr, ptrL, ptrR);
quickSort(arr, ptrL, arr_piv - 1);
quickSort(arr,arr_piv+1,ptrR);
}
}
int main() {
int tab[ArSize] = {10, 40, 30, 4, 3, 312, 3, 4, 1};
int ptrL = tab[0];
int ptrR = tab[ArSize - 1];
quickSort(tab, ptrL, ptrR);
for (int x : tab)
std::cout << x << " ";
return 0;
}
Here
int * leftP = &lptr;
int * rightP = &rptr;
you take the addresses of the function paramters. When you call
swap(leftP,rightP);
then you swap the values of lptr and rptr. When you write
--(*rightP)
you decrement the value of rptr. You never actually modify an element of the array.
I don't have a CS degree, hence when I want to sort an array I use std::sort:
#include <algorithm>
#include <iostream>
#include <iterator>
int main() {
int tab[] = {10, 40, 30, 4, 3, 312, 3, 4, 1};
std::sort( std::begin(tab), std::end(tab));
for (int& x : tab)
std::cout << x << " ";
return 0;
}
If you need to implement it yourself as an exercise you should to learn how to use a debugger, otherwise you will always run into problems like this. Becoming better in coding is not so much about not making mistakes, but more about knowing how to detect and fix them and a debugger is made for exactly that.
A quicksort using pointers does not need to pass the array name as a parameter. Example code:
void QuickSort(int *lo, int *hi)
{
int *i, *j;
int p, t;
if(lo >= hi)
return;
p = *(lo + (hi-lo)/2);
i = lo - 1;
j = hi + 1;
while (1){
while (*(++i) < p);
while (*(--j) > p);
if (i >= j)
break;
t = *i;
*i = *j;
*j = t;
}
QuickSort(lo, j);
QuickSort(j+1, hi);
}
The call to quick sort would be:
QuickSort(array, array+(sizeof(array)/sizeof(array[0]))-1);
I've written this code to sort an array using selection sort, but it doesn't sort the array correctly.
#include <cstdlib>
#include <iostream>
using namespace std;
void selectionsort(int *b, int size)
{
int i, k, menor, posmenor;
for (i = 0; i < size - 1; i++)
{
posmenor = i;
menor = b[i];
for (k = i + 1; k < size; k++)
{
if (b[k] < menor)
{
menor = b[k];
posmenor = k;
}
}
b[posmenor] = b[i];
b[i] = menor;
}
}
int main()
{
typedef int myarray[size];
myarray b;
for (int i = 1; i <= size; i++)
{
cout << "Ingrese numero " << i << ": ";
cin >> b[i];
}
selectionsort(b, size);
for (int l = 1; l <= size; l++)
{
cout << b[l] << endl;
}
system("Pause");
return 0;
}
I can't find the error. I'm new to C++.
Thanks for help.
The selectionSort() function is fine. Array init and output is not. See below.
int main()
{
int size = 10; // for example
typedef int myarray[size];
myarray b;
for (int i=0;i<size;i++)
//------------^^--^
{
cout<<"Ingrese numero "<<i<<": ";
cin>>b[i];
}
selectionsort(b,size);
for (int i=0;i<size;i++)
//------------^^--^
{
cout<<b[l]<<endl;
}
system("Pause");
return 0;
}
In C and C++, an array with n elements starts with the 0 index, and ends with the n-1 index. For your example, the starting index is 0 and ending index is 9. When you iterate like you do in your posted code, you check if the index variable is less than (or not equal to) the size of the array, i.e. size. Thus, on the last step of your iteration, you access b[size], accessing the location in memory next to the last element in the array, which is not guaranteed to contain anything meaningful (being uninitialized), hence the random numbers in your output.
You provided some sample input in the comments to your question.
I compiled and executed the following, which I believe accurately reproduces your shown code, and your sample input:
#include <iostream>
void selectionsort(int* b, int size)
{
int i, k, menor, posmenor;
for(i=0;i<size-1;i++)
{
posmenor=i;
menor=b[i];
for(k=i+1;k<size;k++)
{
if(b[k]<menor)
{
menor=b[k];
posmenor=k;
}
}
b[posmenor]=b[i];
b[i]=menor;
}
}
int main(int argc, char **argv)
{
int a[10] = {-3, 100, 200, 2, 3, 4, -4, -5, 6, 0};
selectionsort(a, 10);
for (auto v:a)
{
std::cout << v << ' ';
}
std::cout << std::endl;
}
The resulting output was as follows:
-5 -4 -3 0 2 3 4 6 100 200
These results look correct. I see nothing wrong with your code, and by using the sample input you posted, this confirms that.