firstly I tried the problem using vector but I got the segmentation fault.
This is my code using vector...
int main()
vector<int>v;
int n,x;
cin>>n;
int size=v.size();
int max_c=v[0];
int count_max=0;
for(int i=0;i<n;i ){
cin>>x;
v.push_back(x);
}
for(int i=0;i<v.size();i ){
if(max_c<v[i]){
max_c=v[i];
}
}
for(int i=0;i<n;i ){
if(v[i]==max_c){
count_max =1;
}
}
cout<<count_max;
return 0;
}
Then I try the same problem using array then my program successfully compiled This is my code using array..
int main()
{
int n;
cin>>n;
long v[n];
long max_c=v[0];
int count_max=0;
for(int i=0;i<n;i ){
cin>>v[i];
}
for(int i=0;i<n;i ){
if(max_c<v[i]){
max_c=v[i];
}
}
for(int i=0;i<n;i ){
if(v[i]==max_c){
count_max =1;
}
}
cout<<count_max;
return 0;
}
Please tell me what is the problem in my first code(using vector)?
CodePudding user response:
Read the comments below for the first code that you provided:
int main() // missing opening brace {
vector<int>v;
int n,x;
cin>>n;
int size=v.size(); // what's the point here, vector is empty at this point
int max_c=v[0]; // vector is empty at this point, undefined behavior
int count_max=0;
for(int i=0;i<n;i ){
cin>>x;
v.push_back(x);
}
for(int i=0;i<v.size();i ){
if(max_c<v[i]){
max_c=v[i];
}
}
for(int i=0;i<n;i ){
if(v[i]==max_c){
count_max =1;
}
}
cout<<count_max;
return 0;
}
For second code that you provided, read the comments:
int main()
{
int n;
cin>>n;
long v[n]; // not standard C , GCC may support this though - not sure
long max_c=v[0]; // vector is empty at this point, undefined behavior
int count_max=0;
for(int i=0;i<n;i ){
cin>>v[i];
}
for(int i=0;i<n;i ){
if(max_c<v[i]){
max_c=v[i];
}
}
for(int i=0;i<n;i ){
if(v[i]==max_c){
count_max =1;
}
}
cout<<count_max;
return 0;
}
Solution:
You can resize the vector during declaration by passing n to the constructor after taking n as input.
Also, you don't need a separate loop to find max element. Just use the same loop you use for input into vector.
#include <vector>
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> v(n);
int max_c = INT_MIN;
for (int i = 0; i < n; i ) {
cin >> v[i];
max_c = max(max_c, v[i]);
}
int count_max = 0;
for (int i = 0; i < n; i ) {
if (v[i] == max_c) {
count_max ;
}
}
cout << count_max;
return 0;
}
CodePudding user response:
The problem is that you're trying to access the 1st element of the empty vector v when you wrote:
int max_c=v[0]; //since v is empty vector, this is undefined behavior
The above statement leads to undefined behavior.
Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.
The program may also produce segmentation fault which is what happens in your case.
So the first step to make the program correct would be to remove undefined behavior.
Solution
To solve this problem you can create a vector of size n after taking the value of n from user, as shown below:
#include <iostream>
#include<vector>
int main()
{
int n;
std::cin>>n;
//create vector of size n
std::vector<int> v(n);
//take input from user
for(int& element: v){
std::cin>>element;
}
int max_c=v.at(0);
int count_max=0;
//find max
for(int i=1;i<v.size();i ){ //note i starts from 1 instead of 0 now
if(max_c<v[i]){
max_c=v[i];
}
}
std::cout<<"maximum is: "<<max_c<<std::endl;
//count max occurence
for(int i=0;i<n;i ){
if(v[i] == max_c){
count_max =1;
}
}
std::cout<<"occurence is: "<<count_max;
return 0;
}
Note you can also get rid of one of the for loops in the above code by merging the 1st and 2nd for loop, as shown below:
#include <iostream>
#include<vector>
#include <climits> //for INT_MIN
int main()
{
int n;
std::cin>>n;
//create vector of size n
std::vector<int> v(n);
int max_c = INT_MIN;
//find max
for(int i=0;i<v.size();i ){
std::cin >> v[i];
if(max_c<v[i]){//you can also use max_c = std::max(max_c,v[i]) instead of this if block
max_c=v[i];
}
}
int count_max=0;
std::cout<<"maximum is: "<<max_c<<std::endl;
//count max occurence
for(int i=0;i<n;i ){
if(v[i] == max_c){
count_max =1;
}
}
std::cout<<"occurence is: "<<count_max;
return 0;
}
1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.
