linear search on array of object c++ - c++

i have a problem with a function ..
I want the function to have two arguments ..
one will be a array of objects and the second one will be the code given by the user
and into the function to run a linear search and check if the user's code exists and if the code exists will return his position in the array else returns -1...
this is what i have try already:
int passwdSearch(Eidos pinPro[], int pass)
{
int i=0;
bool found;
found=false;
while(i<N && found==false)
{
if(pinPro[i].getPasswd()==pass)
{
found=true;
return i;
}
else
return -1;
i++;
}
}
i want the function return the position if password exist else return symbolic number -1
The problem is that the code return position only for the first element of array and for the 4 others element function return -1

Try this:
int passwdSearch(Eidos pinPro[], int pass)
{
for(int i=0; i<N; i++)
{
if(pinPro[i].getPasswd()==pass)
return i;
}
return -1;
}

Try this code
int passwdSearch(Eidos pinPro[], int pass)
{
int i=0;
bool found;
found=false;
while(i<N && found==false)
{
if(pinPro[i].getPasswd()==pass)
{
found=true;
return i;
}
else
i++;
}
return -1;
}

The else will always be activated if the element is not the first one in the array. you should delete it, and return -1 after the while.
int passwdSearch(Eidos pinPro[], int pass)
{
int i=0;
bool found;
found=false;
while(i<N && found==false)
{
if(pinPro[i].getPasswd()==pass)
{
found=true;
return i;
}
}
return -1;
}
This way, if you pass through the whole array and you don't find the required element, then you would return -1.
Edit
And as NathanOliver mentioned, found is useless because you are returning anyways when you find an element. So the code becomes:
int passwdSearch(Eidos pinPro[], int pass)
{
int i=0;
while(i<N)
{
if(pinPro[i].getPasswd()==pass)
return i;
i++;
}
return -1;
}

Related

I have written a C++ program to solve sudoku puzzle ,it is showing "Exception has occurred. EXC_BAD_ACCESS"

Here is the code I have written:
#include<iostream>
using namespace std;
int puzzle[9][9]=
{
{0,5,0,0,6,2,7,0,0},
{0,0,0,0,0,0,1,0,2},
{7,0,9,3,0,0,0,0,0},
{3,0,0,0,8,0,0,0,0},
{0,8,0,7,0,9,0,2,0},
{0,0,0,0,5,0,0,0,7},
{0,0,0,0,0,6,2,0,8},
{2,0,6,0,0,0,0,0,0},
{0,0,3,4,2,0,0,9,0},
};//puzzle template
bool row_possible(int row,int number);//To find out whether a number is possible in the particular row
bool column_possible(int column,int number);//To find whether a number is possible in the particular column
bool square_possible(int row,int column,int number);//To find whether a number is possible in its square
bool possible(int row,int column,int number);//To find whether a number is possible in the given position
bool unassigned();//To check whether the puzzle has any unassigned spaces
void printSolution();//To print the final solution to the console
bool solve();//To solve the puzzle
int main()
{
if(solve())
printSolution();
else
cout<<"\nNo Solution";
return 0;
}
bool row_possible(int row,int number)
{
int m=0;
for(int column=0;column<9;column++)
{
if(puzzle[row-1][column]==number)
m++;
}
if(m!=0)
return false;
else
{
return true;
}
}
bool column_possible(int column,int number)
{
int m=0;
for(int row=0;row<9;row++)
{
if(puzzle[row][column-1]==number)
m++;
}
if (m!=0)
return false;
else
{
return true;
}
}
bool square_possible(int row,int column,int number)
{
int mod_x=(row-1)%3,mod_y=(column-1)%3;
int i=(row-1)-mod_x,j=(column-1)-mod_y;
int m=0;
int k=0;
int check_x=3,check_y=3;
for(k=0;check_x!=0;check_x--)
{
for(k=0;check_y!=0;check_y--)
{
if(puzzle[i][j]==number)
m++;
j++;
}
i++;
}
if(m!=0)
return false;
else
{
return true;
}
}
bool possible(int row,int column,int number)
{
if(row_possible(row,number)&&column_possible(column,number)&&square_possible(row,column,number))
return true;
else
{
return false;
}
}
bool unassigned()
{
int m=0;
for(int row=0;row<9;row++)
{
for(int column=0;column<9;column++)
{
if(puzzle[row][column]==0)
m++;
}
}
if(m>0)
return true;
else
{
return false;
}
}
void printSolution()
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
cout<<puzzle[i][j]<<" ";
}
cout<<"\n";
}
}
bool solve()
{
if(!unassigned())
return true;
for(int row=1;row<10;row++)
{
for(int column=1;column<10;column++)
{
for(int number=1;number<10;number++)
{
if(possible(row,column,number))
{
puzzle[row-1][column-1]=number;
if(solve())
return true;
puzzle[row-1][column-1]=0;
}
}
}
}
return false;
}
The debugger is throwing the error in:
int m=0;
int k=0;
int check_x=3,check_y=3;
It says Exception has occurred.
EXC_BAD_ACCESS.It also says that read memory is failed.
I dont understand what it says.Let me also know whether my program needs any improvements
Kindly help me.I am using Visual Studio Code on MacOS.Thank You
You can do a simple calculation on how long it would take for you to get an answer.
You have 55 open cells that you try 9 different numbers. This gives you 955 = 3 * 1052 tries.
If one try takes 1μs it will take ~ 9.6 * 1038 years to find a solution.
I have written a C++ program and that finds a solution of this sudoku in 30ms and my PC is not a very fast one (5 years old) and 10ms of this is used to print the solution to screen with printf

how to return string from function in c++

I wrote a c++ code to convert infix expression to postfix expression using stacks but whenever I try to return the popped value from the stack,it's not returning the string.The returned string is null instead of the original content of the stack.
Do I need to convert it into char first?strong text
input: A+B
output: AB
correct output: AB+
How to return string from member function in c++?
#include<bits/stdc++.h>
using namespace std;
#define MAX 1000
int top=-1;
string stck[MAX];
void push(char data)
{
top++;
*(stck+top)=data;
}
string pop()
{
if(top<0)
{
return "";
}
else
{
top--;
return (*(stck+top));
}
}
bool isstckempty()
{
if(top==-1){
return true;
}
else
return false;
}
int main()
{
string s;
cin>>s;
string ss="";
int len=s.length();
int j=0;
for(int i=0;i<len;i++)
{
if(isalpha(s[i]))
{
ss=ss+s[i];
}
else
{
if(s[i]=='(')
{
push(s[i]);
}
else if(s[i]==')')
{
j=i-1;
while((s[j]!='(')&&(j>0))
{
ss=ss+pop();
j--;
}
ss=ss+pop();
}
else if(s[i]=='+'||s[i]=='-')
{
j=i-1;
while((isstckempty()||s[j]!='(')&&(j>0))
{
ss=ss+pop();
j--;
}
push(s[i]);
}
else if(s[i]=='*')
{
j=i-1;
while((isstckempty()||s[j]!='(')&&(j>0))
{
ss=ss+pop();
j--;
}
push(s[i]);
}
else if(s[i]=='*')
{
j=i-1;
while((isstckempty()||s[j]!='(')&&(j>0))
{
ss=ss+pop();
j--;
}
push(s[i]);
}
}
}
while(!isstckempty){
ss=ss+pop();
}
cout<<ss<<endl;
return 0;
}
Your function pop() returns invalid data when top==0, because you decrement top before indexing the stack, and any array access with a negative index will be undefined. As others have said, don't implement your own stack, use std::stack and more obvious api's.

Wrong Answer http://www.spoj.com/problems/WORDS1/

I have written the following code for the above problem.
I have checked if Graph is connected by choosing the start node and doing DFS from it. And checking the conditions for Euler path and tour, in code that would be count= 2 or 0. It's working on all given test cases.But getting wrong answer on submission
#include<iostream>
#include<cmath>
#include<list>
#include<string>
#include<stack>
using namespace std;
class Graph
{
int V;
list<int> *adj;
int *in;
public:
Graph(int v)
{
this->V=v;
this->adj=new list<int>[V];
this->in=new int[V];
for(int i=0;i<V;i++)
{
in[i]=0;
}
}
void addEdge(int src,int des)
{
adj[src].push_back(des);
in[des]=in[des]+1;
}
void DFSUtil(bool visited[],int v)
{
stack<int> s;
s.push(v);
visited[v]=true; //mark v as visited;
while (!s.empty())
{
int top=s.top();
s.pop();
list<int> :: iterator it;
for(it=adj[top].begin();it!=adj[top].end();it++)
{
if(!visited[*it])
{
visited[*it]=true;
s.push(*it);
}
}
}
}
/*void DFSUtil(bool visited[],int v)
{
visited[v]=true;
list<int> :: iterator it;
for(it=adj[v].begin();it!=adj[v].end();it++)
{
if(!visited[*it])
{
DFSUtil(visited,*it);
}
}
}*/
// Graph reverseGraph()
// {
// Graph g(V);
// for(int i=0;i<V;i++)
// {
// list<int> :: iterator it;
// for(it=adj[i].begin();it!=adj[i].end();it++)
// {
// g.addEdge(*it,i);
// }
// }
// return g;
// }
bool isConnected()
{
//bool visited[V];
bool* visited=new bool[V];
for(int i=0;i<V;i++)
visited[i]=false;
int i=0;
int flag=0;
int n;
for(i=0;i<V;i++)
{
if(adj[i].size()>0)
{
n=i;
flag=1;
}
if(((int)adj[i].size()-in[i])==1 && in[i]==0) //selecting the start vertex i.e vertex with no incoming edges
{
n=i;
break;
}
}
if(i==V&&flag==0)
return 0;
DFSUtil(visited,n); //dfs to check if every node is reachable fro start vertex
for(int i=0;i<V;i++)
{
if(visited[i]==false && adj[i].size()>0)
return 0;
}
// Graph gr=reverseGraph();
// for(int i=0;i<V;i++)
// visited[i]=false;
// gr.DFSUtil(visited,n);
// for(int i=0;i<V;i++)
// {
// if(visited[i]==false && adj[i].size()>0)
// return 0;
// }
return 1;
}
bool isEuler()
{
int count=0;
int magnitude;
for(int i=0;i<V;i++) //check conditions on in and out edges, out edges=adj[].size
{
magnitude=in[i]-(int)adj[i].size();
if(magnitude<0)
magnitude=magnitude*-1;
if((magnitude)==1)
{
count=count+1;
}
else if(in[i]!=adj[i].size())
{
return 0;
}
}
if(count==1 || count>2)
return 0;
if(isConnected()==0) //check if graph is connected
return 0;
return 1;
}
};
int main()
{
int t;
//scanf("%d",&t);
cin>>t;
while(t--)
{
int n;
//scanf("%d",&n);
cin>>n;
string str;
if(n==1) //only one string entered
{
cin>>str;
cout<<"Ordering is possible.\n";
continue;
}
Graph g(26);
int src,des;
for(int i=0;i<n;i++)
{
cin>>str;
src=str[0]-'a';
des=str[str.length()-1]-'a';
g.addEdge(src,des);
}
if(g.isEuler())
{
cout<<"Ordering is possible.\n";
}
else
{
cout<<"The door cannot be opened.\n";
}
}
}
In your function isConnected, you attempt to find the start vertex with no incoming edges by breaking out of the for loop upon a certain condition. If this condition is not encountered, the loop variable i will contain the value 26, rather than an expected index in the range 0..25. When the value 26 is passed to the DFSUtil function it will caused an out of bounds array access. I get a crash on the line visited[v] = true;
A condition that will cause this to arise, is the test case from the linked site with the repeated word:
2
ok
ok
Your check to find the start vertex does not handle this case well.

Sudoku Solver in C++ partially solving

I recently made a sudoku solver in c++. I used a backtracking algorithm to solve it, but there is a problem : for some cases it only solves up to the 5th row.
Working case : [6][6] = 2, [4][5] = 1
Case failing after row 5 : [1][1] = 1
I don't know what might be the reason it is partially solving the sudoko for some cases and there exist soloution for that cases
using namespace std;
#include<iostream>
int a[9][9],b[9][9];
bool searchrow(int i,int w,int p){
int q=0;
for(int j=0;j<9;j++){
if(j==w){
continue;
}
if(a[i][j]==p){
q=1;break;
}
}
if(q==1){
return false;
}
else
return true;
}
bool searchcoloumn(int i,int w,int p){
int q=0;
for(int j=0;j<9;j++){
if(j==w){
continue;
}
if(a[j][i]==p){
q=1;break;
}
}
if(q==1){
return false;
}
else
return true;
}
bool searchmatrix(int i,int j,int p){
int m,n,x,y,l,k,q;
m=(i/3)*3;
n=(j/3)*3;
x=m+2;
y=n+2;
q=0;
for(l=m;l<=x;l++){
for(k=n;k<=y;k++){
if(l==i&&k==j){ //skip the current location
continue;
}
if(a[l][k]==p){
q=1;
break;
}
}
}
if(q==0){
return true;
}
else
return false;
}
bool place(int i,int j,int p){
if(searchrow(i,j,p)&&searchcoloumn(j,i,p)&&searchmatrix(i,j,p)){
return true;
}
else{
return false;
}
}
bool sudoko(int i,int j){
int w,x;
for(int p=1;p<10;p++){
x=0;
if(place(i,j,p)){
if(b[i][j]==0){
a[i][j]=p;
}
if((i==8)&&(j==8)){
return true;
}
else if(j==8){
sudoko(i+1,0);
}
else{
sudoko(i,j+1);//move to next coloumn
}
}
}
}
int main(){
int i,j,t,data;
cout<<"\nEnter how many no. to add to sudoko\n";
cin>>t;//t is the no of element which are initially present in sudoko and user should give as input
cout<<"\nEnter row , coloumn and then data at the respective location\n";
for(int m=0;m<9;m++){
for(int n=0;n<9;n++){
a[m][n]=0;
b[m][n]=0;
}
}
while(t--){
cout<<"Enter row";
cin>>i;
cout<<"Enter coloumn";
cin>>j;
cout<<"Enter data";
cin>>data;
a[i][j]=data;
b[i][j]=data;
}
if(sudoko(0,0));//used a semicolon here so that to display result
for(int p=0;p<9;p++){
cout<<"\n"
for(int q=0;q<9;q++){
cout<<a[p][q]<<"\t";
}
}
}
In sudoko(int, int) you perform two recursive calls, then drop out of the function without returning a value, which is undefined behaviour. You probably want to replace each call by return sudoko(...), and crank up your compiler warnings.

Remove only one element to make a string palindrome

I will be given string. I can remove only 1 element from it. After removing it if the new string becomes palindrome I have to print "Yes" otherwise "No".
For example, I am given a string "abdbca". Here I can remove 5th index 'c' and make it palindrome and i have to print "Yes". On the other hand if the string is something like "abcd" I can not make it palindrome by removing only one character. Hence I have to print "No".
I tried to do it but my code is not efficient enough. Can anybody please suggest me a efficient way to do it? I have to check strings of 10^5 length in less than 2.5 seconds.
the way I tried to do it is shown bellow :
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
#define MAX 100010
using namespace std;
bool isPalindrome(char abc[]){
int len = strlen(abc), lem = len/2;
for(int i=0,n=len-1;i<=lem;i++,n--) if(abc[i]!=abc[n]) return false;
return true;
}
int main()
{
int tc;
char str[MAX];
scanf("%d",&tc);
while(tc--){
scanf("%s", str);
int length = strlen(str), len = length - 1, z = length % 2, res = 0, ans = 0, b=0,lem = length / 2;
for(int i = 0;i<length;i++){
int n=0, m=1;
for(int x = 0, y = len;x<i && y!=i;x++,y--){
n++;
if(str[x]!=str[y]){
m=0; ++res;
break;
}
}
if(i>lem) for(int x=n,y=len-n-1;x<y;x++,y--){
if(str[x]!=str[y]){
m=0; ++res;
break;
}
}
else for(int x=n+1,y=len-n;x<y;x++,y--){
if(str[x]!=str[y]){
m=0; ++res;
break;
}
}
if(m==1) {printf("YES\n");b++;break;}
}
//if(length <= res) printf("NO\n");
if(b==0) printf("NO\n");
}
return 0;
}
Since you you only need to remove one character, you can do so in linear time by modifying palindrome checking. The idea is that you compare characters from the beginning to characters from the end and stop at the first mismatch. If you remove one character from the mismatching pair and get a palindrome, then return true, otherwise return false. I implemented the idea in C++ below.
#include<iostream>
#include<string>
using namespace std;
bool palindromeExists(string s)
{
int i = 0;
int j = s.length()-1;
while(i < j)
{
if(s[i] != s[j]) //first mismatch
break;
i++;
j--;
}
int tempj = j-1; //remove s[j]
int tempi = i;
while(tempi < tempj)
{
if(s[tempi] != s[tempj])
break;
tempi++;
tempj--;
}
if(tempi >= tempj) //palindrome found?
return true;
tempi = i+1; //remove s[i]
tempj = j;
while(tempi < tempj)
{
if(s[tempi] != s[tempj])
return false;
tempi++;
tempj--;
}
return true;
}
int main()
{
string s = "abca";
if(palindromeExists(s))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
This should return true if the string is already a palindrome, or if it can be a palindrome after the removal of one character. I hope I didn't miss any corner cases.
You can refer complete program in c++ here. Input the string to get the index of character to be removed. String reversal is performed in palim() function. It returns -1 if string is already palindrome.
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool palim(string s)
{
string s2;
s2=string(s.rbegin(),s.rend());
if(s2==s)
{
return true;
}
else
{
return false;
}
}
int check(string s)
{
int x;
if(s.length()%2==0)
{
for(int i=0,j=s.length()-1;i<s.length()/2,j>=s.length()/2;i++,j--)
{
if(s[i]!=s[j])
{
string s1=s;
s1.erase(j,1);
if(palim(s1))
{
x=j;
break;
}
else
{
string s1=s;
s1.erase(i,1);
if(palim(s1))
{
x=i;
break;
}
}
}
}
}
else
{
for(int i=0,j=s.length()-1;i<s.length()/2,j>s.length()/2;i++,j--)
{
if(s[i]!=s[j])
{
string s1=s;
s1.erase(j,1);
if(palim(s1))
{
x=j;
break;
}
else
{
string s1=s;
s1.erase(i,1);
if(palim(s1))
{
x=i;
break;
}
}
}
}
}
return x;
}
int main()
{
string s;
cin>>s;
if(palim(s))
{
cout<<"-1"<<endl;
}
else
{
cout<<check(s)<<endl;
}
return 0;
}
Similar to turingcomplete, but with sub functions:
bool isPalindrome(std::string::const_iterator& start, std::string::const_iterator& end)
{
while (start < end) {
--end;
if (*start != *end) {
return false;
}
++start;
}
return true;
}
bool test(const std::string& s)
{
auto start = s.begin();
auto end = s.end();
if (isPalindrome(start, end)) {
// If we remove the middle character of a palindrome,
// We still have a palindrome.
return true;
}
// Now test if there is a palindrome
// if we skip the mismatch char from the start or from the end.
auto start2 = start;
auto end2 = end;
++start2;
--end;
return isPalindrome(start, end) || isPalindrome(start2, end2);
}
Live example