C++'s min_element() not working for array [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to find the minimal element of a subset of an array in c++ as follows:
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::min_element;
void load_values(int n);
unsigned *w;
int main() {
int n, a, b;
cin >> n >> a >> b;
w = new unsigned[n];
load_values(n); // sets values to w[i]
int min = min_element(w+a, w+b);
}
void load_values(int n) {
while(n--)
w[n] = 1;
}
I get the error invalid conversion from 'unsigned int*' to 'unsigned int' [-fpermissive]. What am I missing here?

Note, that std::min_element() returns an iterator to a minimal value, not the value itself. I doubt that you get this error based on the above code alone: assuming proper include directives and using directives the code compiles OK although it would print the address of the minimum element rather than its value. Most likely your actual code looks more like
unsigned min = std::min_element(w + a, w + b);
Note, that you should also check the result of reading from std::cin:
if (std::cin >> n >> a >> b) {
// process the values
}
... and, of course, it seems you're leaking the memory allocated for w.

Related

Is it okay if we assign const integer a value like this? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I wanted to assign a value to a constant but as it is constant, we cant assign value to it through cin. So I came up with this idea. Is it okay if we use it like this?
{
int data = 0;
cout << "enter number of students"<<endl;
cin>>data;
const int number = data;
cout << number<<endl;
}
This is fine. const values do not need to be compile time constants. They just can't be changed after they are defined.
Similarly you could do:
int getNumberFromStdin() {
int data;
cin >> data;
return data;
}
int main() {
const int number = getNumberFromStdin();
}
Yes, because it is not an assignment; it is a declaration.
Despite looking quite similar, the two constructs are different: assignment changes something that is already declared, while the declaration sets up something new, and optionally gives it a value. The value does not need to be hard-coded, though: it can be a result of some processing, as is the case in your example.
Naturally, assignment is not allowed for constants, because they already have a value.

Exercise about pointer to function in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an exercise like that. The requirement is to write code in the section
#include <iostream>
int minus (int a, int b) {
return a - b;
}
void calculate(int a, int b)
{
int (*myfunc)(int, int);
// STUDENT ANSWER BEGIN
// STUDENT ANSWER END
int ans = (*myfunc)(a, b);
printf("Output of calculation is %d.\n", ans);
}
int main() {
calculate(1,2);
return 0;
}
When I write
int (*myfunc) (int, int) = add;
int temp = (*myfunc)(a, b);
cout << temp;
the value of temp = -1 as expected (just for test) but I can't get the value of ans. So, I figure that two *(myfunc) is different. How can I fix that?
Thank you very much!
When you write:
int (*myfunc) (int, int) = add;
you are creating a new variable named myfunc. If you do it inside the same scope as the original myfunc, it is a compilation error, but if you write it inside a new scope (which the indentation of the code you pasted suggest, even if you haven't showed the brackets), you are shadowing the original variable with the new one. By using the name myfunc inside that scope, you are referring to the new variable.
The solution is simple. Instead of declaring a new variable, simply assign to the existing one:
myfunc = add; // or minus
EDIT: I misread the question and interpreted "but I can't get the value of ans" as "but I can't get the value into ans". But I'll leave this answer here in case that is what OP meant...

What did I do wrong? C++ Newbie here [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I thought the output would be 70? (20+20+20+10=70) Why is it so large?
#include <iostream>
using namespace std;
int main()
{
int a,b,c=20;
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
The issue is that you are not initializing the variables a and b. That means when you attempt to run your program, the computer is looking in memory for a value to use for each, and that number could be very big or very small. Try this:
#include <iostream>
using namespace std;
int main()
{
int a = 20,b = 20,c=20; //here, a and b are defined
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
C is the only one variable that you initialize to 20, the other 2 variables
(a and b) are holding garbage..
so your math calculation is undefined behaviour.

In my C++ code If I print sum outside loop it gives correct answer, but not in any loop for/while, why not? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
int main()
{
long long int first=0,second=1,T,N; //here T is Number of Cases
cin>>T;
long long int fab=first+second;
long long int sum[T];
for(long long int i=0;i<T;i++)
{
cin>>N;
while(fab<N)
{
first=second;
second=fab;
if(fab%2==0)
{
sum[i]+=fab;
}
fab=first+second;
}
}
for(int i=0;i<T;i++)
{
cout<<sum[i]<<endl;
}
return 0;
}
In the above for loop sum is not providing the correct answer but if sum is used outside for loop then it gives appropriate answer.
This:
cin >> T;
long long int sum[T];
is not valid C++. An array in C++ must be created using a compile-time expression to denote the number of items, not a variable such as T.
The proper construct to use that is standard C++ is std::vector<long long>:
#include <vector>
//...
cin >> T;
std::vector<long long> sum(T);
The code is now standard C++.
The other aspect that this code does is solve the issue brought up in the answer by DietrichEpp, in which you failed to initialize the VLA to 0. The vector above would have automatically initialized the items to 0 for you.
So the moral of the story is that if you used standard C++, you probably would not have had a problem with your code.
The sum[T] variable is not initialized. You must initialize it:
long long sum[T] = {};
The code
long long int sum[T];
isn't valid C++.
Google for variable length array or dynamic array. You'll likely see an answer like
long long int *sum = new long long int[T].

program to view addresses of elements in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
help plz its showing invalid indirection
i used it to find the location or memory addresses of elements in the array b
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr;
int b[]={1,0,2,3,4,5,6,7,8,9};
ptr=b;
for(int i=0;i<10;i++)
cout<<ptr[i]<<" "<<*b[i];
}
In order to print the address of the ith element in an array b, use
std::cout << b + i;
This will work in all cases except when b is an array of char, in which case you need to cast to void*
std::cout << static_cast<const void*>(b + i);
in place of iostream.h it should be iostream.
void main() ; it should be int main().
cout<<ptr[i]<<" "<<(b+i)<<endl;
Formatting by using endl in above line of code will make the result clear.
Your function should return an integer value.
return 0;