integar array palindrome checker - c++

i made this program to make a function to check if integar array is palindrome or not.it is always giving output that it is not a palidrome on every input. can you plz help me out?
code:
#include<iostream>
using namespace std;
void palindrome(int[],int[],int);
int main()
{
int size=5;
int array1[size];
int array2[size];
palindrome(array1,array2,size);
}
void palindrome(int array1[],int array2[],int size)
{
size=5;
int l=0;
cout<<"enter your array=";
for(int i=0;i<size;i++)
{
cin>>array1[i];
}
for(int i=0;i<size;i++)
{
array2[i]=array1[size-i];
}
if(array1==array2)
{
cout<<"given array is palindrome.";
}
else{
cout<<"given array is not palindrome.";
}
}

You can't compare two arrays with ==, it will only compare if the array's address are equals, so different arrays never equals, we can switch it to std::equal
There is one memory issue with array2[i]=array1[size-i];, you will get a buffer overflow.
This is a slighly modified version of your code:
#include <iostream>
using namespace std;
void palindrome(int[], int[], int);
int main() {
int size = 5;
int array1[size];
int array2[size];
palindrome(array1, array2, size);
}
void palindrome(int array1[], int array2[], int size) {
size = 5;
int l = 0;
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> array1[i];
}
for (int i = 0; i < size; i++) {
array2[i] = array1[size - i - 1];
}
if (std::equal(array1, array1 + size, array2, array2 + size)) {
cout << "given array is palindrome.";
} else {
cout << "given array is not palindrome.";
}
}
It's recommended to use std::vector instead of the C style array:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void palindrome(int);
int main() {
int size = 5;
palindrome(size);
}
void palindrome(int size) {
std::vector<int> vec(size);
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> vec[i];
}
std::vector<int> rev_rec{vec.rbegin(), vec.rend()};
if (vec == rev_rec) {
cout << "given array is palindrome." << std::endl;
} else {
cout << "given array is not palindrome." << std::endl;
}
}
And we can also avoid the copy of vector, with reverse iterator:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void palindrome(int);
int main() {
int size = 5;
palindrome(size);
}
void palindrome(int size) {
std::vector<int> vec(size);
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> vec[i];
}
if (std::equal(vec.begin(), vec.end(), vec.rbegin(), vec.rend())) {
cout << "given array is palindrome." << std::endl;
} else {
cout << "given array is not palindrome." << std::endl;
}
}

Related

C++ I cant get my function to run in my program

The Function won't initiate can someone help? When I run it in the debugger program skips over function and I don't know why?
#include <iostream>
using namespace std;
int size_array= 0;
int *data_array;
void sorting(int *[], int);
int main()
{
cout<<"enter in array size \n";
cin>>size_array;
int *data_array=new int(size_array);
for(int i=0;i<size_array;i++)
{
cout<<"enter number "<<i+1<<endl;
cin>>data_array[i];
}
**int sorting(int data_array, int size_array);**
for (int i=0; i<size_array;i++)
{
cout<<data_array[i]<<endl;
}
return 0;
}
The marked code is simply declaring the function, not calling it.
Also, your data_array is a pointer to a single int whose value is initialized as size_array. But you want an array of size_array number of ints instead.
Try this:
#include <iostream>
using namespace std;
void sorting(int[], int);
int main()
{
int size_array = 0;
cout << "enter in array size \n";
cin >> size_array;
int *data_array = new int[size_array];
for(int i = 0; i < size_array; i++)
{
cout << "enter number " << i+1 << endl;
cin >> data_array[i];
}
sorting(data_array, size_array);
for (int i = 0; i < size_array; i++)
{
cout << data_array[i] << endl;
}
delete[] data_array;
return 0;
}
void sorting(int data_array[], int size_array)
{
// sort data_array as needed...
}

C++ returning a vector and displaying it properly

I am attempting to create a function that returns a vector as an answer, ie [1,3]. I am confused about the process of manipulating that information once the function has been called. Should I set it equal to a new vector? How would I then display the contents of this new vector? Here is my code for reference. When I attempt to set the function call to a new vector and display it, I get an out of bounds error.
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
vector<int> twoNumberSum(vector<int> array, int targetSum);
int main()
{
int tSum = 10;
vector<int> test{3,5,-4,8,11,1,-1,6};
twoNumberSum(test,tSum);
}
//O^2 complexity
//Two number Sum
vector<int> twoNumberSum(vector<int> array, int targetSum)
{
for (int i=0; i<array.size() -1; i++)
{
int firstNum = array[i];
for(int j=i+1;i<array.size();i++)
{
int secondNum = array[j];
if(firstNum + secondNum == targetSum)
{
return vector<int>{firstNum,secondNum};
}
}
}
return {};
}
To print vector, you could use something like this:
void printVector(vector<int> v) {
cout << "{";
for(int item : v) {
cout << item << ", ";
}
cout << "}";
}
And use it as in a main function: print_vector(twoNumberSum(test, tSum));
Now, bug.
for(int j=i+1;i<array.size();i++)
You increase and check the value of variable i, but you need to use j there.
That will be correct: for(int j=i+1;j<array.size();j++)
The program works correctly and prints the result of function to the console:
void print_vector(vector<int> v);
vector<int> twoNumberSum(vector<int> array, int targetSum);
int main()
{
int tSum = 10;
vector<int> test{3,5,-4,8,11,1,-1,6};
print_vector(twoNumberSum(test, tSum));
}
vector<int> twoNumberSum(vector<int> array, int targetSum)
{
for (int i=0; i<array.size() -1; i++)
{
int firstNum = array[i];
for(int j=i+1;j<array.size();j++)
{
int secondNum = array[j];
if(firstNum + secondNum == targetSum)
{
return vector<int>{firstNum,secondNum};
}
}
}
return {};
}
void print_vector(vector<int> v) {
cout << "{";
for(int item : v) {
cout << item << ", ";
}
cout << "}";
}

I need to display "Monday" with left justified and the highest columns appears in first row in descending order

should be like this output:
Monday
onday
nday
day
ay
y
what I have so far:
#include <iostream>
using namespace std;
int main () {
char *weekDays[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
for (int i=0;i<7;i++){
cout << weekDays[0][i] << endl;
}
return 0;
}
output:
M
o
n
d
a
y
Im on mobile and tired but sth. like this should work:
int main(){recPrint(0);}
void recPrint(int level){
char mon[] = "Monday";
for(int i=level; i<strlen(mon); i++){
std::cout << mon[i];
}
std::cout << std::endl;
recPrint(++level);
}
add another parameter char dayOfTheWeek[] and you can call it with whatever you want.
Try this
#include <iostream>
using namespace std;
void display(char s[])
{
int n = strlen(s);
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
cout << s[j];
}
cout << endl;
}
}
int main () {
char *weekDays[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
display(weekDays[0]);
}

C++ unable to find multiple modes

My assignment asks me to write a function that takes an array and the size of that array as a parameter, and to find the mode. If there are multiple modes, I am to find them all, and place them in a vector and print said vector in an ascending order.
For example, if I input the following integers:
3, 4, 2, 1, 2, 3
Then the output should display
2, 3
If I input the following integers:
1, 2, 3, 4
Then the output should display:
1, 2, 3, 4.
However, my program somehow only finds the first mode and displays it in a really awkward manner.
Here was my input:
3, 4, 2, 3, 2, 1
And this was the output:
3
3
3
3
3
Here is my code. Any help would be greatly appreciated. Thank you all for your time!
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int size; //array size
int* array; //array of ints
int arraycount; //counter for array loop
void findMode(int array[], int size); //function prototype
//intialize array
cout << "Enter number of integers ";
cout << "you wish to input." << endl;
cin >> size;
cout << "Enter the integers." << endl;
array = new int[size];
for (arraycount = 0; arraycount < size;
arraycount++)
cin >> array[arraycount];
//call function
findMode(array, size);
return 0;
}
void findMode(int array[], int size) {
int counter = 1;
int max = 0;
int mode = array[0];
int count;
vector <int> results;
//find modes
for(int pass = 0; pass < size - 1; pass++) {
if(array[pass] == array[pass+1]) {
counter++;
if(counter > max) {
max = counter;
mode = array[pass];
}
}
else {
counter = 1;
}
}
//push results to vector
for (count=0; count < size - 1; count++) {
if(counter == max) {
std::cin >> mode;
results.push_back(mode);
}
}
//sort vector and print
std::sort(results.begin(), results.end());
for (count=0; count < size - 1; count++) {
cout << mode << endl;
}
}
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main() {
int size; //array size
int* array; //array of ints
std::vector<int> vecInput;
int arraycount; //counter for array loop
void findMode(std::vector<int> vec); //function prototype
//intialize array
cout << "Enter number of integers ";
cout << "you wish to input." << endl;
cin >> size;
cout << "Enter the integers." << endl;
array = new int[size];
for (arraycount = 0; arraycount < size; arraycount++)
{
int num;
cin >> num;
vecInput.push_back(num);
}
//call function
findMode(vecInput);
return 0;
}
void findMode(std::vector<int> vec)
{
std::sort(vec.begin(), vec.end());
std::map<int, int> modMap;
std::vector<int>::iterator iter = vec.begin();
std::vector<int> results;
int prev = *iter;
int maxCount = 1;
modMap.insert(std::pair <int,int>(*iter, 1));
iter++;
for (; iter!= vec.end(); iter++)
{
if (prev == *iter)
{
std::map<int, int>::iterator mapiter = modMap.find(*iter);
if ( mapiter == modMap.end())
{
modMap.insert(std::pair <int,int>(*iter, 1));
}
else
{
mapiter->second++;
if (mapiter->second > maxCount)
{
maxCount = mapiter->second;
}
}
}
else
{
modMap.insert(std::pair <int,int>(*iter, 1));
}
prev = *iter;
}
std::map<int, int>::iterator mapIter = modMap.begin();
for (; mapIter != modMap.end(); mapIter++)
{
if (mapIter->second == maxCount)
{
results.push_back(mapIter->first);
}
}
cout << "mod values are " <<endl;
std::vector<int>::iterator vecIter = results.begin();
for (; vecIter != results.end(); vecIter++)
cout<<*vecIter<<endl;
}
Thank you all for the help, I was able to get the code to work. Turns out the problem was the cin in my vector portion of the function. Here is my revised code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdlib.h>
#include <vector>
using namespace std;
#define N 100
void findMode(int x[], int size); //function prototype
vector<int> results; //vector
int main(void) {
int* x;
int size=0;
int arraycount;
//intialize array
cout << "Enter number of integers ";
cout << "you wish to input." << endl;
cin >> size;
cout << "Enter the integers." << endl;
x = new int[size];
for (arraycount = 0; arraycount < size;
arraycount++)
cin >> x[arraycount];
//send array and size to function
findMode(x, size);
return 0;
}
//findMode function
void findMode(int x[], int size) {
int y[N]={0};
int i, j, k, m, cnt, count, max=0;
int mode_cnt=0;
int num;
int v;
vector<int> results;
vector<int>::iterator pos;
//loop to count an array from left to right
for(k=0; k<size; k++) {
cnt=0;
num=x[k]; //num will equal the value of x[k]
for(i=k; i<size; i++) {
if(num==x[i])
cnt++;
}
y[k]=cnt; //
}
//find highest number in array
for(j=0; j<size; j++) {
if(y[j]>max)
max=y[j];
}
//find how many modes there are
for(m=0; m<size; m++) {
if(max==y[m])
mode_cnt++;
}
//push results to vector
for (m=0; m < size; m++) {
if(max == y[m]) {
//after taking out this line the code works properly
// std::cin >> x[m];
results.push_back(x[m]);
}
}
//sort vector and print
std::sort(results.begin(), results.end());
cout << "The mode(s) is/are: ";
for (pos=results.begin(); pos!=results.end(); ++pos) {
cout << *pos << " ";
}
}

How to erase integers in a vector from standard input

I'm trying to make a program that will erase an integer from a vector, containing a random sequence of integers only if it is found in the vector. I wrote code already, but I get an error when I call the erase() function to actually delete the same integers in the vector. Thoughts?
#include <iostream>
#include <vector>
using namespace std;
void removeX(vector<int>& wl, int x);
int main()
{
vector<int> m_list;
int num;
for(int i = 0; i <= 25; i++) //made the vector have values from 1 to 25.
{
m_list.push_back(i);
}
cout << "Enter a number you wish to find and remove: " << endl;
cin >> num;
removeX(m_list, num);
for (size_t i = 0; i < m_list.size(); i++)
{
cout << "Content at index: " << i << ": " << m_list[i] << endl;
}
return 0;
}
void removeX(vector<int>& wl, int x)
{
for(size_t i = 0; i < wl.size(); i++)
{
if (x == wl[i])
{
wl.erase(w[i]);
}
}
}
you show know that the parameter of function erase is iterator type, not size_t type.
void removeX(vector<int>& wl, int x)
{
vector<int>::iteratot iter = wl.begin();
while(iter != wl.end())
{
if (*iter == wl[i])
{
iter = wl.erase(iter);
}
else
iter++;
}
}