#include <iostream>
#include <string.h>
using namespace std;
int palindrome(char str1[], int n){
int result = 1;
for(int i = 0; i<=(n/2)-1 ;i++){
if(str1[i]!=str1[n-1-i]){
result = 0;
break;
}
}
return result;
}
void copyArrayChar(char *src, char *dest,int startSrc, int size){
for(int i=0; i<=size-1; i++){
src[startSrc + i] = dest[i];
}
}
int patternj(char str1[], int n){
int result = 0;
int check;
check = palindrome(str1, n);
if(n==1||n==0){
result = 1;
} else if(check == 1){
char strL[n/2], strR[n/2];
copyArrayChar(str1, strL, 0, n/2);
copyArrayChar(str1, strR, (n/2)+1, n/2);
result+= patternj(strL,n/2) + patternj(strR, n/2);
}
return result;
}
int main(int argc, const char * argv[]) {
string str[100000];
int strLength[100000], result[100000];
char str1[100000][100000];
int ni;
cin >> ni;
for(int i=0;i<=ni-1;i++){
getline(cin,str[i]);
strLength[i] = (int) str[i].length();
str[i].copy(str1[i],strLength[i],0);
result[i]= patternj(str1[i], strLength[i]);
}
return 0;
}
Hi, so I've written this code to check for patternj strings but for some reason it gives me an error with this name "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8)". I don't quite understand why this is happening.
Related
I'm working with c++ classes and I got to deal with some data. I had to use 2 classes with composition relation. I had a problem at printing. The function prints the directory of the value, instead of the value.
#include <iostream>
#include <string.h>
class Examen;
class Comisie{
private:
char *descriere;
int numarMembri;
char **numeMembri;
public:
Comisie(char *, int);
~Comisie(){}
friend void addMembru(char *);
friend void afisare(Examen **, int);
};
Comisie::Comisie(char *numeExamen, int nrMembri){
this->descriere =new char[strlen(numeExamen)];
strcpy(this->descriere, numeExamen);
this->numarMembri = nrMembri;
this->numeMembri =new char*[nrMembri];
char nume[50];
for(int i = 0; i<nrMembri; i++){
std::cin.ignore();
std::cin.get(nume, 50);
this->numeMembri[i] = new char[strlen(nume)];
strcpy(this->numeMembri[i], nume);
}
}
class Examen{
private:
int oraInceput;
char *numeExamen;
Comisie *comisie;
public:
Examen(int, int, char *);
Examen(Examen &);
~Examen(){}
friend void afisare(Examen **, int);
friend Comisie;
};
Examen::Examen(int oraInceput, int nrMembri, char *numeExamen){
this->oraInceput = oraInceput;
this->numeExamen = new char[strlen(numeExamen)];
strcpy(this->numeExamen, numeExamen);
Comisie temp2(numeExamen, nrMembri);
this->comisie = &temp2;
}
int powInt(int x, int y)
{
for (int i = 0; i < y; i++)
{
x *= 10;
}
return x;
}
int parseInt(char* chars)
{
int sum = 0;
int len = strlen(chars);
for (int x = 0; x < len; x++)
{
int n = chars[len - (x + 1)] - '0';
sum = sum + powInt(n, x);
}
return sum;
}
int char_ora_to_min(char ora[]){
int i = 0;
while(ora[i] != ':') i++;
char c_ora[2];
char c_min[5];
strncpy(c_ora, ora, i);
strcpy(c_min, ora+i+1);
int i_ora, i_minut;
i_ora = parseInt(c_ora);
i_minut = parseInt(c_min);
i_minut += i_ora*60;
return i_minut;
}
Examen ** citire(int nrExamene){
Examen **lista = (Examen**)malloc(sizeof(Examen)*nrExamene);
for(int i = 0; i<nrExamene; i++){
char c_ora[10];
std::cin.ignore();
std::cin.get(c_ora, 10);
int ora;
ora = char_ora_to_min(c_ora);
char numeEx[100];
std::cin.ignore();
std::cin.get(numeEx, 100);
int nrM;
std::cin>>nrM;
Examen temp(ora, nrM, numeEx);
lista[i] = &temp;
}
return lista;
}
//print function
void afisare(Examen **lista, int nrExamene){
for(int i = 0; i<nrExamene; i++){
std::cout<<lista[i]->numeExamen<<std::endl<<"Ora inceperii: "<<lista[i]->oraInceput<<":"<<lista[i]->oraInceput<<std::endl;
std::cout<<"Participanti:"<<std::endl;
for(int j = 0; j < lista[i]->comisie->numarMembri; j++){
std::cout<<"- "<<lista[i]->comisie->numeMembri[j]<<std::endl;
}
}
}
//int numarare(Examen**, int nrExamene, char *numeMembru){ return 0;}
int main(){
int nrExamene;
std::cin>>nrExamene;
Examen **lista_examene = citire(nrExamene);
afisare(lista_examene, nrExamene);
return 0;
}
Result:
Output
The input is:
1
8:00
Comisia a
1
Ana
The output should be:
Comisia a
Ora inceperii: xx:xx
Participanti:
- Ana
So the function (void afisare) can acces lista[i]->numeExamen, but can't acces lista[i]->comisie->numeMembri[j] and lista[i]->comisie->numarMembri
i want to create a link between two programs throughout the execs functions .
my idea is to create function then point on it by a function pointer then send it to the other program to test it . this is my first programenter code here
1- is this possible ?
2- how ?
i get this idea because i find each time to change the function name in the main function but the remainning still as it was but if i send a pointer function as a character pointer then my programm still as it without changing
#include <iostream>
#include <cstdlib>
#include <unistd.h>
using namespace std;
void
Random(int* ,const int );
int*
selection_sort(int *arr ,const int length)
{
int i = 0,minIndex{0},tmp{0},k{0};
while(i < length-1) // T(n-1) * C1
{
minIndex = i; // Tn * C2
for(int j = i+1 ; j < length ; j++ ) // som(Ti) from i = 0 to i = length-1 )*C3.
{
if((arr)[j] < (arr)[minIndex])
minIndex = j;
}
if(minIndex != i) // Tn * C4
{
tmp = (arr)[i];
(arr)[i] = (arr)[minIndex];
(arr)[minIndex] = tmp;
}
i++;
}
return arr;
}
void
Random(int* array,const int length)
{
srand(time(nullptr));
int i{-1};
while(i++ < length)
{
array[i] = rand()%100;
sleep(0.2);
}
}
int main(int argc,char* argv[])
{
int* (*ptr)(int*,const int ) = selection_sort;
execl("/home/hellios/Documents/Algorithms/sort_Algorithms/main",(char*)ptr,0); // complete the call
return EXIT_SUCCESS;
}
sort_Algorithms/main.c
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <ctime>
using namespace std;
void
Random(int* array,const int length);
int
main(int argc,char* argv[])
{
int* (*ptr)(int *,const int ) =(int* (*) (int*,const int)) argv[1];
int arr1[100],k{0},*arr;
Random(arr1,100);
arr = (*ptr)(arr1,100);
//selection_sort(arr,100);
cout<<"out of selection_sort"<<endl;
for(int j = 0 ; j < 100 ; j++ )
{
printf("[%d]\t", arr[j]);
if(!(++k %10))
cout<<endl;
}
printf("\n");
return EXIT_SUCCESS ;
}
void
Random(int* array,const int length)
{
srand(time(nullptr));
int i {-1};
while(i++ < length)
{
array[i] = rand()%100;
sleep(0.2);
}
}
I am trying to solve the yodaness problem, but my code is not outputting anything when i run my program. I am not sure where my mistake is, because my code is compiling and running just with no output. I am trying to count the number of inversions in a string of characters, but I am not sure that I am going about it the correct way. Any help would be appreciated, thanks
#include <iostream>
#include <sstream>
#include <limits>
#include <map>
#include <string>
using namespace std;
const int MAX = 30000, LEN = 33;
int total = 0;
map<string, int> index_words(const string &line)
{
map<string, int> result;
istringstream in(line);
string word;
int index = 0;
while (in >> word)
{
if (result.find(word) == result.end())
result[word] = index++;
}
return result;
}
void merge(int *a, int p, int q, int r) {
int i, j, k, n1 = (q-p+1), n2 = (r-q);
int L[n1], R[n2];
for(i=0;i<n1;i++) L[i] = a[p+i];
for(j=0;j<n2;j++) R[j] = a[q+j+q];
for(k=p, i=j=0; k<=r; k++) {
if(j>=n2 || (i<n1 && L[i] <=R[j])) a[k] = L[i++];
else {
total += n1-i;
a[i] = R[j++];
}
}
}
void mergesort(int *a, int p, int r) {
if(p<r) {
int q = (p+r)/2;
mergesort(a,p,q);
mergesort(a,q+1,r);
merge(a,p,q,r);
total++;
}
}
char word[MAX][LEN];
int a[MAX];
int main()
{
cin >> instances;
char temp[LEN];
for (int i = 0; i < instances; ++i)
{
int numwords;
cin >> numwords;
cin.ignore(numeric_limits<streamsize>::max(), '\n';
string yoda_line, english_line;
getline(std::cin, yoda_line);
getline(std::cin, english_line);
auto yoda_idx = index_words(yoda_line);
auto english_idx = index_words(english_line);
for(int i = 0; i < instances; i++) a[i] = yoda_idx[word[i]];
mergesort(a,0, instances-1);
cout << total;
}
}
I'm writing a program for my algorithmic math class at university and I'm using Win 7 (x64), Eclipse Oxygen.1a Release (4.7.1a) with MinGW 6.3.0.
Whenever I build and run the program it crashes with windows claiming 'Abgabe3.exe stopped working' but when trying to find the problem using the debugger and breakpoints I step trough the whole program and it finishes without errors...
I stripped everything not used by the problematic function and copied everything into a seperate file and the exact problem occurs.
Maybe somebody has a clue what happened at my side. ^^
#include <math.h> /* pow, sqrt */
#include <iostream> /* cin, cout */
#include <new> /* new */
#include <string> /* string */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
void NORM(double* res, double* x, int n){
res[0] = 0.0;
for(int i = 0; i < n; i++){
res[0] += pow(x[i], 2);
}
res[0] = sqrt(res[0]);
}
void initRand(double* x, int n){
srand (time(NULL) * rand());
for(int i = 0; i < n; i++){
x[i] = (((double) rand()) / ((double) RAND_MAX));
}
}
void createArray(double* &x, int n){
if (n > 0){
x = new double[n];
initRand(x, n);
}
}
void printArray(double* x, int n){
if (x != NULL){
cout<<"(\n";
for(int i = 0; i < n; i++){
if(i+1 == n) cout<<x[i];
else if ((i % 5) == 0) cout<<x[i];
else if ( ((i+1) % 5) == 0 ){
cout<<", "<<x[i]<<"\n";
}
else {
cout<<", "<<x[i];
}
}
cout<<"\n)\n";
}
else cout<<"\nError: pointer = NULL\n";
}
unsigned long long int bin(unsigned int n, unsigned int k){
unsigned long long res = 1;
if(k == 0) return 1;
else if( n >= k){
for(unsigned long long int i = 1; i <= k; i++){
res *= (n + 1 - i) / i;
}
}
else return 0;
return res;
}
void newArray(double** x, unsigned int v, unsigned int n){
for(unsigned int i = 0; i < v; i++){
double* ptr = x[i];
createArray(ptr,n);
x[i] = ptr;
}
}
void experiment(double** vektorArray){
unsigned int n = 10, v = 20;
cout<<"Dimension n = "<<n<<"\nAnzahl Versuche v = "<<v<<endl;
//Erstellen der Vektoren
cout<<"Erstellen - starte\n";
vektorArray = new double*[n];
newArray(vektorArray, v, n);
cout<<"Erstellen - fertig\n";
for(unsigned int i = 0; i < v; i++){
if(i%10 == 0) printArray(vektorArray[i], n);
}
}
int main(int argc, char** argv){
double** vektorArray = NULL;
experiment(vektorArray);
return 0;
}
vektorArray = new double*[n];
created an array of size n, but
void newArray(double** x, unsigned int v, unsigned int n)
{
for (unsigned int i = 0; i < v; i++)
{
double* ptr = x[i];
createArray(ptr, n);
x[i] = ptr;
}
}
and
for (unsigned int i = 0; i < v; i++)
{
if (i % 10 == 0)
printArray(vektorArray[i], n);
}
index that array with v. Looks like you got your variables crossed. Strongly recommend giving variables better, more descriptive names to help make this more obvious.
I have written a binary search like following. When I try to find 10, it's not showing me the result. What am I missing??
// BinarySearch.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void BinarySearch(int arr[],int value);
int * insertionshot(int arr[]);
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,10,5,9,6,8,7,4};
int value;
cin >> value ;
static int *ptr;// = new int[10];
ptr = insertionshot(arr);
BinarySearch(ptr,value);
return 0;
}
int * insertionshot(int arr[])
{
int ar[10];
for(int i =0;i < 10; i++)
{
ar[i] = arr[i];
}
int arrlength = sizeof(ar)/sizeof(ar[0]);
for(int a = 1; a <= arrlength -1 ;a++)
{
int b = a;
while(b > 0 && ar[b] < ar[b-1])
{
int temp;
temp = ar[b-1];
ar[b-1] = ar[b];
ar[b] = temp;
b--;
}
}
return ar;
}
void BinarySearch( int a[],int value)
{
int min,max,middle;
min = 0;
int ar[10];
for(int i =0;i < 10; i++)
{
ar[i] = a[i];
}
//printf("size of array = %d",sizeof(arr));
max = (sizeof(ar)/sizeof(ar[0]) -1);
middle = (min+max)/2;
while(min <= max)
{
if(ar[middle] == value)
{
cout << "The value found" << ar[middle];
break;
}
else if(ar[middle] < value)
{
min = middle +1;
}
else if(ar[middle] > value)
{
max = middle-1;
}
middle = (min+max)/2;
}
}
Finally i made it work,I think this code does not have any problem.This could help any one
// BinarySearch.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void BinarySearch(int arr[],int value);
int * insertionshot(int arr[],int);
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,10,5,9,6,8,7,4};
int * arr1 = new int[10];
for(int i = 0;i< sizeof(arr)/sizeof(arr[0]);i++)
{
arr1[i] = arr[i];
}
int value;
cin >> value ;
int *ptr = new int[10];
ptr = insertionshot(arr1,10); // address of sorted array will be returned.
BinarySearch(ptr,value);
arr1 = 0;
ptr =0;
delete arr1;
delete ptr;
return 0;
}
int * insertionshot(int arr1[],int n)
{
for(int a = 1; a <= n -1 ;a++)
{
int b = a;
while(b > 0 && arr1[b] < arr1[b-1])
{
int temp;
temp = arr1[b-1];
arr1[b-1] = arr1[b];
arr1[b] = temp;
b--;
}
}
return arr1;
}
void BinarySearch( int a[],int value)
{
int min,max,middle;
min = 0;
int ar[10];
for(int i =0;i < 10; i++)
{
ar[i] = a[i];
}
max = (sizeof(ar)/sizeof(ar[0]) -1);
middle = (min+max)/2;
while(min <= max)
{
if(ar[middle] == value)
{
cout << "The value found" << ar[middle];
break;
}
else if(ar[middle] < value)
{
min = middle +1;
}
else if(ar[middle] > value)
{
max = middle-1;
}
middle = (min+max)/2;
}
}
You're missing the most important part of a binary search: The collection you search in must be sorted.
For binary search, the array should be arranged in ascending or descending order.