print out even numbers between two integers - c++

I need to create a function to print out even numbers between two integers
#include <iostream>
using namespace std;
int evens_between(int m, int n)
{
for(int i = m; i<= n; i++)
{
if(i % 2 == 0)
cout<<i<<" ";
}
}
int main()
{
int m;
int n;
cin>>m>>n;
cout<<evens_between(m,n)<<endl;
return 0;
}
I'm getting error messages not sure if this is right though. Would appreciate some help understanding my error a little better

You're trying to print the function's result here:
cout<<evens_between(m,n)<<endl;
But that's wrong. Just call the function:
evens_between(m,n);
However, the error is because you're making the function return an int. At least you tell it to do so in its signature, it's not actually returning any value. Change it to void instead:
void evens_between(int m, int n)

Related

Executing a procedure with conversion error

#include<iostream>
using namespace std;
void MinMax(int tab[],int* min, int& max)
{
int size=0;
for(int i=0; i<4095; i++)
{
if(tab[i]==-1) break;
size++;
}
size=(int)size;
for(int passes=0; passes<size; passes++)
{
if(tab[passes]<tab[passes+1])
{
swap(tab[passes],tab[passes+1]);
};
}
min=tab[size];
cout<<min<<" ";
for(int passes=0; passes<size; passes++)
{
if(tab[passes]>tab[passes+1])
{
swap(tab[passes],tab[passes+1]);
};
}
max=tab[size];
cout<<max<<" ";
}
int main()
{
int z1[]= {10,2,5,7,4,-1};
int min, max;
MinMax(z1,min,max);
for(int i=0; i<7; i++)
{
cout<<z1[i]<<" ";
}
}
I need to write some values in z1 in main function, then execute MinMax. It should find smallest and biggest value from an array. The first problem is when i try to compile it, there is en error about converting int to int* and i can't assign tab[size] to min. I should also use 2 intigers when executing MinMax, but it doesn't work.
void MinMax(int tab[],int* min, int& max)
// ^
This appears to be a typo.
Given the way you're passing the argument, and given the way you're using it inside MinMax, I suspect you meant int&, like you have for max.
The error you are getting is because you assign an integer to a pointer variable by doing min=tab[size]; where min is int * type and tab[size] is int
In order to correct this behavior, you should assign tab[size] to the value min is pointing to with the deference operator: *min = tab[size]
This should correct your error.

Need to devise a recursive algorithm for the following problem in c++

A recursive algorithm that gets an integer n and integer array A as input and check if the given array
A contains two integers, such that n= A[i]+A[j] where A[i] & A[j] can be at any position in array.
This is what I have come up with
#include <iostream>
using namespace std;
void func(int arr[],int size,int n)
{
if (size==1)
return;
else
{
for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++)
if (arr[i]+arr[j]==n)
{
cout<<"yes";
return;
}
func(arr,size-1,n);
}
}
int main()
{
int A[] = {1,2,3,4,5};
int n = 8;
func(A,5,n);
return 0;
}
I know it's using recursion but I want a recursive function that avoids all unnecessary iterations. Any help will be greatly appreciated!!
#include <iostream>
using namespace std;
void func(int arr[],int index,int size,int n)
{
if(index==size-1)
return;
int flag=0;
for (int j=index+1;j<size;j++)
if (arr[index]+arr[j]==n)
{
flag=1;
cout<<"yes";
}
if(flag==0)
func(arr,index+1,size,n);
}
int main()
{
int A[] = {1,2,3,4,5};
int n = 6;
func(A,0,5,n);
return 0;
}
If you really want a recursive code, this may be one kind of implementation. All i have done is start from 0th index and sum it with every other element and compare it with with 'n'. If it matches fine otherwise make a recursive call with next index(prev index + 1).

Recursive function, which writes integer digits in an array(vector) in C++?

I have a C++ program, in which I have to create a recursive function that writes all the digits of a given positive integer in an array - in this case a vector.
However, when I compile the program and enter a number, it stops working. I want to ask why this happens?
#include <iostream>
#include <vector>
using namespace std;
vector <int> arr;
int temp;
int fill_Array(int num)
{
if(num>=1 && num<=9)
{
arr.push_back(num);
}
temp = fill_Array(num)%10;
arr.push_back(temp);
num/=10;
}
int main()
{
int n;
cin>>n;
fill_Array(n);
for(int i=0; i<arr.size(); i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
In the given code, recursion function does not returning any value, so return type for that function have no use.
calling the function for recursion is in the wrong place. Correct code given below:
#include <iostream>
#include <vector>
using namespace std;
vector <int> arr;
int temp;
void fill_Array(int num)
{
if(num>=1 && num<=9)
{
arr.push_back(num);
}
else{
temp = num%10;
arr.push_back(temp);
fill_Array(num/=10);
}
}
int main()
{
int n;
cin>>n;
fill_Array(n);
for(int i=0; i<arr.size(); i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
A couple of reasons I can see:
There is no conditional to stop the recusion so it will keep going until it runs out of stack or memory. I presume you want to stop when num is zero
fill_Array has no return value so will assign temp with some random value which will be pushed into the array
Also why use recursion for this when iterative would be easier and more obvious what it is doing

Whats wrong with this one? C++ Array Pointer

It says:
[Error] invalid conversion from 'int*' to 'int' [-fpermissive] on line 9 col 5.
What was asked of me to do:
Make a program that would accept array of 10 integers and determine the highest and the lowest integers from the set of integers. Use pointer variables for the highest and lowest integer.
what i did:
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *kre_p;
for(int k = 0; k<=10; k++)
{
kre_p[k] = &kre_arr[k];
}
int j,temp;
cout<<"Enter 10 Integers: ";
for (*kre_p=0; *kre_p < 10; *kre_p++)
{
cin>>kre_arr[*kre_p];
}
for(*kre_p=0;*kre_p<=10;*kre_p++)
{
for(j=*kre_p+1;j<=10;j++)
{
if(kre_arr[*kre_p] > kre_arr[j])
{
temp = kre_arr[*kre_p];
kre_arr[*kre_p] = kre_arr[j];
kre_arr[j] = temp;
}
}
}
for(*kre_p=0;*kre_p<=9;*kre_p++)
{
cout<<endl<<kre_arr[*kre_p];
}
}
code i did before adding pointer i dont seem to understand pointer that much.
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *kre_p;
int i,j,temp;
cout<<"Enter 10 Integers: ";
for (int i=0; i < 10; i++)
{
cin>>kre_arr[i];
}
for(i=0;i<=10;i++)
{
for(j=i+1;j<=10;j++)
{
if(kre_arr[i] > kre_arr[j])
{
temp = kre_arr[i];
kre_arr[i] = kre_arr[j];
kre_arr[j] = temp;
}
}
}
for(i=0;i<=9;i++)
{
cout<<endl<<kre_arr[i];
}
}
Looking at what you are asked to do I think you just have to determine the highest and lowest int in the array and point to. You sort the array thats slower.
I think it should look like that:
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *low;
int *high;
cout<<"Enter 10 Integers: ";
for (int i=0; i < 10; i++)
{
cin>>kre_arr[i];
}
//determine the lowest
low=&kre_arr[0];
for(int i=1;i<10;i++)
{
if(kre_arr[i] < *low)
{
low=&kre_arr[i];
}
}
//determine the highest
high=&kre_arr[0];
for(int i=1;i<10;i++)
{
if(kre_arr[i] > *high)
{
high=&kre_arr[i];
}
}
cout<<"lowest: "<<*low<<"\nhighest: "<<*high;
}
kre_p[k] = &kre_arr[k];
kre_arr is array.
kre_arr[k] is integer.
&kre_arr[k] is integer address ( similar int*)
kre_p is pointer.
kre_p[k] is integer.
So, as a result, you cannot pass directly int* to int.
I guess you want kre_p+k = &kre_arr[k]
Given the state of your code, I fear for your life... So, for your overal survival, and of course in the hopes that you will learn something:
Never use 'using namespace std'. It's bad form.
You are not allocating memory for your array of pointers (kre_p). That will cause your program to crash for sure.
You don't actually need an array of pointers. Your array elements can be conveniently referred to by their offset in the array.
You are doing what appears to be a bubblesort to find the lowest and highest value. That's incredibly inefficient, and completely unnecessary.
C++ can be such a nice language. It bothers me when teachers seem to think they should be teaching it in a form that's as ugly as possible. Consider:
#include <algorithm>
#include <array>
#include <iostream>
int main () {
std::cout << "Enter 10 Integers: ";
std::array<int, 10> kre_arr;
for (auto &Val : kre_arr)
std::cin >> Val;
const int Low = *std::min_element (kre_arr.begin (), kre_arr.end ());
const int High = *std::max_element (kre_arr.begin (), kre_arr.end ());
// The assignment calls for pointers, so let's not disappoint.
const int *LowPtr = &Low;
const int *HighPtr = &High;
}

implementation counting sort

here is code for counting sorting
#include <iostream>
using namespace std;
int main(){
int a[]={2,3,1,2,3};
int n=sizeof(int)/sizeof(int);
int max=a[0];
for (int i=1;i<n;i++) {
if (a[i]>max) {
max=a[i];
}
}
int *output=new int[n];
for (int i=0;i<n;i++) {
output[i]=0;
}
int *temp=new int[max+1];
for (int i=0;i<max+1;i++) {
temp[i]=0;
}
for (int i=0;i<n;i++){
temp[a[i]]=temp[a[i]]+1;
}
for (int i=1;i<max+1;i++) {
temp[i]=temp[i]+temp[i-1];
}
for (int i=n-1;i>=0;i--) {
output[temp[a[i]]-1]=a[i];
temp[a[i]]=temp[a[i]]-1;
}
for (int i=0;i<n;i++) {
cout<<output[i]<<" ";
}
return 0;
}
but output is just 2,only one number. what is wrong i can't understand please guys help me
int n=sizeof(int)/sizeof(int);
is wrong. That just assigns 1 to n.
You mean
int n=sizeof(a)/sizeof(int);
I've not looked beyond this. No doubt there are more problems, but this is the most significant.
This is the kind of thing you can work out very easily with a debugger.
Look at this expression:
int n=sizeof(int)/sizeof(int);
What do you think the value of n is after this? (1)
Is that the appropriate value? (no, the value should be 5)
Does that explain the output you are seeing? (yes, that explains why only one number is shown)
My advice would be that if you're going to do this in C++, you actually try to use what's available in C++ to do it. I'd look up std::max_element to find the largest element in the input, and use an std::vector instead of messing with dynamic allocation directly.
When you want the number of elements in an array in C++, you might consider a function template something like this:
template <class T, size_t N>
size_t num_elements(T const (&x)[N]) {
return N;
}
Instead of dumping everything into main, I'd also write the counting sort as a separate function (or, better, a function template, but I'll leave that alone for now).
// warning: Untested code:
void counting_sort(int *input, size_t num_elements) {
size_t max = *std::max_element(input, input+num_elements);
// allocate space for the counts.
// automatically initializes contents to 0
std::vector<size_t> counts(max+1);
// do the counting sort itself.
for (int i=0; i<num_elements; i++)
++counts[input[i]];
// write out the result.
for (int i=0; i<counts.size(); i++)
// also consider `std::fill_n` here.
for (int j=0; j<counts[i]; j++)
std::cout << i << "\t";
}
int main() {
int a[]={2,3,1,2,3};
counting_sort(a, num_elements(a));
return 0;
}