how to get the last value of a vector [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 want to say if the value of the last element in the vector = 4 cout<<"Yes" how can i write the code write if(v.size()==y) where y is a number but it dosen't work i begin recently at writing codes
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main()
{
int n,x,y;
cin >> n >> x >> y;
vector<int> v(n);
for(int i = 0; i < n; i++)
{
cin >> v[i];
}
for(int i = 0; i < n; i++)
{
if(v[0] == x)
{cout<<"EASY";
return 0;
}
if(v[0] == x && v.back() == y)
{
cout << "BOTH";
return 0;
}
if(v.back()==y)
{
cout << "HARD";
return 0;
}
if(v[0] != x && v.back() != y)
{
cout << "OKAY";
return 0;
}
}}

v.size() will returns the number of the elements in v, not the last element in the vector.
To get the last value of a vector, you can simply call std::vector::back().
Of course, you have to make sure the vector is not empty before that by checking std::vector::empty.
if (!v.empty() && v.back()==y)
{
...
}

Related

how to compare unknown elements of an array 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 26 days ago.
Improve this question
Sorry for bad English .
I was trying to write a program that gets a number and see if the digits of an entered number are repeated or not . I did try to if(analyse[0]==analyse[1]==analyse[2]==...) but since I don't know exactly how many elements will array have, it didn't work
#include<iostream>
int main(){
int number,number_help;
const int count{10};
std::cin>>number;
number_help = number ;
int digitcount{0};
while(number_help>0){
number_help/=10;
digitcount+=1;
}
int analyse[count]{};
for(size_t i {0}; i<digitcount ; i++){
analyse[i] = number%10;
number/=10;
}
//I don't know what to code here
return 0;
}
Change your approach: count how many there are of each digit instead of comparing them to each other.
This is much simpler.
Example:
#include<iostream>
int main(){
int number;
std::cin >> number;
const int count{10};
int frequency[count]{};
do {
frequency[number % 10] += 1;
number /= 10;
} while (number != 0);
for (int i{0}; i < count; i++) {
if (frequency[i] > 1) {
std::cout << i << " was repeated " << frequency[i] << " times.\n";
}
}
}

How can I fix my C++ code ? (no match for operators) [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
How can I fix my C++ code ? (no match for operators)
I got an error : no match for "operators-". What can be the problem and how can I solve it?
Can anybody help me to fix it ?
error: no match for 'operator-' (operand types are 'std::set::iterator' {aka 'std::_Rb_tree_const_iterator'} and 'std::set::iterator' {aka 'std::_Rb_tree_const_iterator'})|
#include <iostream>
#include <set>
using namespace std;
int main()
{
int O;
int N;
int M;
cin >> O >> N >> M;
int tanarsorszama[O];
int tantargysorszama[O];
int nap [O];
int ora [O];
for(int i=0; i<O; i++)
{
cin >> tanarsorszama[i] >> tantargysorszama[i] >> nap[i] >> ora[i];
}
int dblyukas[N]={0};
set<int>orai;
for(int i=0; i<N; i++)
{
for(int k=1; k<6; k++)
{
orai.clear();
for(int j=0; j<9; j++)
{
if(tanarsorszama[i]!=0 && ora!=0 && nap[k]!=0)
{
orai.insert(ora[j]);
}
}
if (orai.size() > 1)
{
dblyukas[i] += orai.end() - orai.begin() +1 - orai.size(); // There is the error
}
}
}
return 0;
}
std::set doesn't have random-access iterators. That is why you are getting the error.
To access the first and last elements of a set, use orai.begin() and std::prev(orai.end()). These return iterators, which will have to be de-referenced with operator*. So, correcting what I think you are intending to do, leads to the following:
if (orai.size() > 1)
{
dblyukas[i] += *std::prev(orai.end()) - *orai.begin() +1 - orai.size(); // There is the error
}

How does change in Passing parameters changes the output of the Code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When I pass only Sum as Parameter in My coin Exchange Problem to find no of ways to find the amount Sum..
Example
if the coins are {2,3,5} and the desired sum is 9 i get correct output as 8.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum) {
int S = 0;
if(sum == 0) return 1;
if(sum < 0) return 0;
for(int i=1;i<=n;i++) {
S += fun(sum - a[i]);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum);
return 0;
}
But when I also give Current Index as a parameter it gives me output as 3 which is wrong.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum, int idx) {
int S = 0;
if(sum == 0) return 1;
if(idx > n) return 0;
if(sum < 0) return 0;
for(int i=idx;i<=n;i++) {
S += fun(sum - a[i], i);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum,1);
return 0;
}
But WHY??
Is my parameter passing in this case is wrong?
You don't have the same result between your 2 solutions because in the first, at each "function call", you will iterate from 1 to N and in your second solution, you will iterate from Index to N. So the number of loop will be different.

Skipped condition and I don't know why [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
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.

How can I copy the values from a vector to an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I get the dValues[] in the line double dValues[] = {what should i input here?}?Because I'm using an array. The goal is to get the mode.
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
double GetMode(double daArray[], int iSize) {
// Allocate an int array of the same size to hold the
// repetition count
int* ipRepetition = new int[iSize];
for (int i = 0; i < iSize; ++i) {
ipRepetition[i] = 0;
int j = 0;
bool bFound = false;
while ((j < i) && (daArray[i] != daArray[j])) {
if (daArray[i] != daArray[j]) {
++j;
}
}
++(ipRepetition[j]);
}
int iMaxRepeat = 0;
for (int i = 1; i < iSize; ++i) {
if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
iMaxRepeat = i;
}
}
delete [] ipRepetition;
return daArray[iMaxRepeat];
}
int main()
{
int count, minusElements;
float newcount, twocount;
cout << "Enter Elements:";
std::cin >> count;
std::vector<float> number(count);
cout << "Enter " << count << " number:\n";
for(int i=0; i< count ;i++)
{
std::cin >> number[i];
}
double dValues[] = {};
int iArraySize = count;
std::cout << "Mode = "
<< GetMode(dValues, iArraySize) << std::endl;
You already have all the values in your number vector, but if you wanted to copy those values into a new array called dValues, you have to allocate it on the heap (since you don't know the size at compile-time), copy the elements from the vector, and later free up that memory:
double *dValues = new double[number.size()];
for (size_t i = 0; i < number.size(); i++)
{
dValues[i] = number[i];
}
// whatever you need to do with dValues
delete [] dValues;
You're also not checking that you're within the bounds of your vector in the for loop. A safer implementation would use the push_back() method on the vector rather than assigning the values by index.
If I understood you correctly, you wish to copy the elements from the vector to array. If yes -
float *dValues = new float[count] ; // Need to delete[] when done
std::copy( number.begin(), number.end(), dValues );
std::copy is in algorithms header. But why do you want to use/create raw array for this task. You already have the vector number and just pass it to GetMode(..)