Executing a procedure with conversion error - c++

#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.

Related

Why am I getting the wrong output for this C++ code? (one of the problem of hackerrank)

This is the program for printing out sum of array elements. It is showing run time error. The output is coming out to be 0 instead of printing out the sum of the elements.
#include<iostream.h>
using namespace std;
void simpleArraySum()
{
int ar[100],n,i,sum=0;
for(i=0;i<n;i++)
{
sum=sum + ar[i];
}
cout<<sum;
}
int main()
{
int ar[100],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
simpleArraySum();
return 0;
}
On this line in your main:
int ar[100], n;
You create an array of 100 elements. You later fill that array using cin
for(int i = 0 ; i < n ; i++)
{
cin >> ar[i];
}
Then you do nothing with that array. You are not calculating any sum. You let that array go, forgotten.
Then, you call a simpleArraySum function. That function is creating an entirely new, distinct array.
// v-----v------There
int ar[100],n,i,sum=0;
That array has no value assigned to it. In fact, reading from it is undefined behavior.
What you want is to receive that array in the arguments of your function:
void simpleArraySum(int* ar, int n) {
// ...
}
And call it like that in your main:
simpleArraySum(ar, 100);
You can avoid the issues of arrays and functions by not using them:
int main()
{
int quantity = 0;
std::cin >> quantity;
int sum = 0;
int value;
while (std::cin >> value)
{
sum += value;
}
std::cout << sum << "\n";
return EXIT_SUCCESS;
}
In simpleArraySum, the variable n is uninitialized. So this loop:
for(i=0;i<n;i++)
invokes undefined behavior when reading from n.
Also, you are summing a different array in the function, than the one you read in mian. It seems that you need to pass in the array from main to this function:
void simpleArraySum(int *ar, int n) {
and call it like this:
simpleArraySum(ar, n);
Finally, you don't even need a function for this, since there is an existing algorithm std::accumulate that you can use:
cout << std::accumulate(ar, ar + n, 0);
In the function, you're adding the elements of ar which is local to the function simpleArraySum() and is not of the array ar that is local to main().
So, pass the array and its length to the function and return its sum. Here is your corrected code:
#include<iostream>
using namespace std;
void simpleArraySum(int ar[], int n)
{
int i, sum = 0;
for(i=0;i<n;i++)
{
sum=sum + ar[i];
}
cout<<sum;
}
int main()
{
int ar[100],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
simpleArraySum(ar, n);
return 0;
}

Is there any solution of the error of sorting user input array using STL?

My code is:
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
struct Interval
{
int init,last;
};
bool compare(Interval a,Interval b)
{
return (a.init < b.init);
}
int main()
{
int t,i,j;
int a[1000];
cin >> t;
for( j=0;j<t;++j){
for(i=0;i<t;i++)
{
cin >> a[i];
}
}
sort(a,a+t,compare);
for( j=0;j<t;++j)
for(i=0;i<t;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
return 0;
}
What is the solution of the below line?
sort(a,a+t,compare);
The problem is here
bool compare(Interval a,Interval b)
{
return (a.init < b.init);
}
compare compares Interval objects
But
int a[1000];
sort(a,a+t,compare);
you are trying to sort an int array.
Either sort an int array or an Interval array, but be consistent. The compare function must match the array that you are sorting.
You are attempting to sort int a[1000]; which is an int array, not an Interval array. If this is really your intention, then you do not need the predicate (compare function) for sort. You can simply use the default operator< that is provided for int. That means you code could just be:
std::sort(std::begin(a), std::begin(a) + t);

print out even numbers between two integers

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)

Array Manipulation C++

I am trying to create a code that reads in an array, finds the smallest element in that array, and then subtracts that smallest element from all the other elements in the array and prints a new array with these elements.
The code I have written so far will not even compile.
Here is what I have:
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE(25);
void read_list(int a[], int&num_ele);
void print_array(const int a[], const int num_ele);
int find_min(const int a[], const int num_ele);
void array_subtract(int x, int&a[], int num_ele) ;
int main()
{
int num_ele(0);
int array[SIZE] ;
read_list(array, num_ele);
cout<<"Before list: ("<<num_ele<< " numbers): " <<endl;
print_array(array, num_ele) ;
min = find_min(array, num_ele) ;
cout<<"The minimum value = "<<min<<endl;
array_subtract(min ,array ,num_ele) ;
cout<<"After list: ("<<num_ele<< " numbers): " <<endl;
print_array(array, num_ele) ;
return 0;
}
void read_list(int a[], int&num_ele)
{
int x(0);
cout<<"Enter positive numbers (ints) terminated 0: "<<endl;
cin>>x;
while(x!=0 && num_ele <= SIZE)
{
a[num_ele] = x ;
num_ele++;
cin >> x;
}
}
void print_array(const int a[], const int num_ele)
{
for(int i=0; i<num_ele; i++)
{
if(i<num_ele-1)
{cout<<a[i]<<", "; }
else
{cout<<a[i]<<"."}
}
}
int find_min(const int a[], const int num_ele)
{
int x= a[0];
for(int k=0; k<num_ele; k++)
{
if(a[k] < x)
{ x=a[k] ;}
}
return x ;
}
void array_subtract(int x, int&a[], int num_ele)
{
for(int j=0; j<num_ele; j++)
{
a[j] = a[j]-x ;
}
}
When I go to compile, at the line
cout<<"The minimum value = "<<min<<endl;
I get about 100 lines of errors.
Is there some huge error in my code I am missing? Why would this happen? I would like to keep the code in the same format it is in (calling functions to get the final output).
Thanks
You have 3 problems, after correcting these problems, at least you have not compile errors.
i.
You're passing arrays wrong:
void array_subtract(int x, int&a[], int num_ele);
^^^^^^^
Pass it like below (remove &)
void array_subtract(int x, int a[], int num_ele);
ii.
You're using variable min without declaring it. Declare it like below:
int min;
iii.
You've missed a semicolon ; in function print_array:
cout<<a[i]<<".";
^
You did not define variable min so the compiler reports errors when encounters statement
min = find_min(array, num_ele) ;
Aside from the compilation errors addressed by others...
Instead of using an array, use a vector (since logically you array is variably sized from 0-SIZE, and a vector is a variable sized array)...
vector<int> v;
Accept integer input until 0 is typed...
for(int n; cin >> n && n != 0;)
v.push_back(n);
Find the min element...
auto min = min_element(begin(v), end(v));
Printing can be done a variety of ways...
cout << "Vector: [";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << "]\n";
Subtract the min from the elements...
transform(v.begin(), v.end(), v.begin(), [min](int n){ return n-min; });
Try to use the C++ standard library when possible instead of hand rolling loops. It reduces the potential for bugs, and is often clearer (but not always... for example that print I posted above doesn't exactly read intuitively IMO)

First time using arrays, getting an error that 'x was not declared in this scope' however I did declare it?

My compiler keeps saying that 'small' and 'x' were not declared in this scope, how do I fix my array so that they are accurately displayed? overall the code is supposed to find the smallest positive nonzero value stored in the array.
#include <iostream>
#include <string>
using namespace std;
int findthesmall( int small[x], int y)
{
for(int i=0; i< y; i++){
for(int j=0; j< y; j++){
int temp = small[i];
if( small[i] > small[j] )
small[i] = small[j];
small[j] = temp;
}
}
return small[0];
}
int main(){
return 0;
}
I think you need:
int findthesmall( int* small, int y) {
Try this:
int findthesmall( int small[], int y) {
for(int i=0; i< y; i++){
for(int j=0; j< y; j++){
int temp = small[i];
if( small[i] > small[j] )
small[i] = small[j];
small[j] = temp;
}
}
return small[0];
}
int main(){
return 0;
}
int small[x]
This is illegal for 2 reasons.
Like your compiler says, X is undefined
Size of the array cannot be set to the value of a non compile time constant.
To fix this you can do what #ajon suggested( pass array as pointer + length), it is historically the way to pass arrays.
There are other better ways in C++ though.
You can consider using std::array or std::vector. Both of them can be passed as you would any other variable, know their own size, and can be accessed like a normal array
Or you could use template code to capture the size of the array automatically.
template<int len>
int findthesmall(int (&small)[len]){
The 2nd option maybe a little convoluted and more complex than other options, especially now that you have got your answer, I'm just including it here for completeness.
Apart from other answers, there is also a bug in the logic. If your function is just to find the smallest element as function name indicated, one for loop should be enough.
Sample code presented below:
int findthesmall( int small[], int y)
{
int temp = small[0];
for(int i=1; i< y; i++)
{
if( temp > small[i] )
temp = small[i];
}
return temp;
}
Or you could use std::min_element algorithm as well
std::cout << *std::min_element(small, small+y) << std::endl;