How to Remove duplicate entries while insertion in c++ array [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am creating a c++ array program in which I am trying to get input from the user but during insertion I want to prompt the user for duplicate values. I have used a while loop and for loop inside, but it doesn't work if the user enters a duplicate value. He will be asked to enter the value again at the particular index.
int size=0;
int k;
int index=0;
int temp=0;
int aray1[2];
char ch='y';
while(ch='y') {
for(k=0; k<=2; k++)
{
if(aray1[k]==temp) {
cout<<"please do not enter duplicates";
ch='y';
index--;
} else {
aray1[index]=temp;
index++
ch='n';
}
}
system("pause");
}

I would use an std::vector instead.
#include<iostream>
#include<vector>
#include<algorithm>
int main(){
using namespace std;
vector<int> v;
int size=2;
while(v.size()<size){
int i;
cin >> i;
vector<int>::iterator it = find(v.begin(), v.end(), i);
if(it==v.end()) // i is not in v so insert it to the end of the vector.
v.push_back(i);
else
cout << "Duplicate entered." << endl;
}
}
http://www.cplusplus.com/reference/algorithm/find/

Related

what will bw the output of this code? 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 last year.
Improve this question
#include <iostream>
using namespace std;
int f(int i){
int k=0;
if(i>0)
{
int k=i*10;
}
else {
int k= i++;
}
cout <<k;
return i;
}
int main()
{
cout << f(1);
cout << ".";
cout << f(0);
return 0;
}
This is the code, compiler shows "01.01" which i quite don't understand, any help will be very much welcomed!
int k = i * 10; and int k = i++; are declarations of k that shadow the outer k. The statement std::cout << k; in the outer scope therefore always outputs zero.
The only effect of the if body is to increase i by 1. And it only does that if i is zero (or less). That value of i is returned printed.
Thus the output is 01.01. Armed with a line by line debugger, the shadowing effect will be obvious.

in the c++ hacker rank preperation cause, the last index returns 0 when i reverse it. but when i try the code out in visual studio, it works perfectly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
HERE IS THE QUESTION I FACED ON HACKERRANK.
the hackerrank question
HERE IS THE CODE I PRINTED
#include <iostream>
using namespace std;
int main() {
int n;
int array[n];
int c,x;
cin>>n; //inputting the array size
n=n+1;
int m=n;
if(n>=1 && n<=1000 && m>=1 && m<=1000 )
{
for(c=1;c<=n;c++)//inutting the numbers
{ if(c<=10000)
{
cin>>array[c];
}
}
for(x=m-1;x>=1;x--)//outputing the numbers reversed
{
cout<<array[x]<<" ";
}
}
return 0;
}
WHEN I GIVE INPUT THE ARRAY SIZE AS 4 AND THE DATA AS 1 4 3 2,
ITS REVERSED AS 2,3,4,0
BUT IT PERFECTLY GETS REVERSED WHEN COMPILED WITH VISUAL STUDIO CODE. WHYS THAT?? AND ALSO WHEN I SUBMIT CODE SEVERAL TESTS PASS, BUT SOME GET A SEGMENTATION ERROR
As mentioned in the comments, array should be initialized with a proper capacity. You should first read the size and then create the array.
#include <iostream>
using namespace std;
int main() {
int n;
// First read the array size and then create the array.
cin>>n; //inputting the array size
int array[n];
//inputting the numbers
// Remember indices go from 0 to N-1
for(int c=0;c<n;c++)
{
cin>>array[c];
}
//outputing the numbers reversed
for(int x=n-1;x>=0;x--)
{
cout<<array[x]<<" ";
}
return 0;
}

I have to calculate difference of sum of diagonal elements of a square matrix [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 4 years ago.
Improve this question
In the following program:
#include <iostream>
#include <cmath>
using namespace std;
int diagonalDifference(int x[][],int n)
{
int sum1=0,sum2=0,y;
for(int i=0;i<n;i++)
{
sum1+=x[i][i];
sum2+=x[i][n-1-i];
}
y=abs(sum1-sum2);
return y;
}
int main()
{
int n,**z;
cin>>n;
int arr[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
z=diagonalDifference(arr,n);
cout<<x;
return 0;
}
I get a compilation error I don't understand.
error:declaration of 'x' as multidimensional array must have bounds for all dimensions except the first
Could you help me fix it?
int[][] is not a valid type:
int diagonalDifference(int x[][],int n)
You declare z as an int**:
int n,**z;
But you assign it an int:
int diagonalDifference(int x[][],int n);
z=diagonalDifference(arr,n);
And finally you print x which does not exist:
cout<<x;
As rules of thumb:
declare only one variable per line, and give it a meaningful name;
declare what possibly can as const;
Don't use C-style arrays unless you have to; prefer std::vector for instance;
don't use using namespace std;
much more you need to learn.
.
int diagonalDifference(int x**,int n) { /* .... */ }
int matrix_size = 0;
std::cin >> matrix_size;
std::vector<std::vector<int>> matrix{matrix_size, std::vector<int>{matrix_size}};
/* fill the matrix */
const int diag_diff = diagonalDifference(matrix, matrix_size);
std::cout << diag_diff << '\n';

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.

why is the size of vector is out of range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <int> v;
vector<int> :: iterator ip;
v.push_back(2);
for(int i=3;i<=32000;i+=2)
{
int top;
top = sqrt(i)+1;
int flag=0;
if(*ip>top) break;
if(i%*ip==0)
{
flag=1;
break;
}
if(flag==0)
{
v.push_back(i);
}
}
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
cout<<v.at(n)<<endl;
}
return 0;
}
can you please explain me why my code is getting runtime error.
is size of the vector is getting out of range ??
I dont know why i am getting wrong answer in this code although my code is perfectly fine.
You're dereferencing an uninitialized iterator, you want something more like:
vector<int>::iterator ip = v.begin();