I have looked extensively for the problem in this code, but I can't seem to figure out what tragic error I made and why it is triggering a breakpoint.
(After 3 or 4 inputs, it triggers and I don't know why it doesn't trigger at the start or what is causing it)
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout", for example.
string convertDecToBin(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 2;
r = r / 2;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
string convertDecToOct(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 8;
r = r / 8;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
int main()
{
int input = 0;
while (input != -1)
{
cout << "\nEnter a decimal number (-1 to exit loop): ";
cin >> input;
if (input != -1)
{
cout << "Your decimal number in binary expansion: " << convertDecToBin(input);
cout << "\nYour decimal number in octal ecpression: " << convertDecToOct(input);
}
}
cout << "\n\nPress any key to exit. . .";
_getch();
return 0;
}
arrayHex = new int[] is your problem - C\C++ does not support dynamic sizing arrays. You need to specify a size for the array to allocation, otherwise you'll get memory block overruns.
Related
I am still learning about threads and I was trying to solve this problem in my code, when I am putting the pthread_join(thread[i],NULL) outside the loop that is creating the threads it always gives me wrong output and Thread with ID = 0 will not work(call the median func) and the last thread will work two times, for better understanding see the output below:
ThreadID= 0, startRow= 0, endRow= 0 // first thread doesn't call the median func
ThreadID= 1, startRow= 1, endRow= 1
ThreadID 1 numOfBright 0 numOfDark 1 numOfNormal 4
ThreadID= 2, startRow= 2, endRow= 2
ThreadID 2 numOfBright 0 numOfDark 1 numOfNormal 4
ThreadID= 3, startRow= 3, endRow= 3
ThreadID 3 numOfBright 0 numOfDark 0 numOfNormal 5
ThreadID= 4, startRow= 4, endRow= 4
ThreadID 4 numOfBright 0 numOfDark 5 numOfNormal 0
ThreadID 4 numOfBright 0 numOfDark 5 numOfNormal 0 // last thread is calling the median func two times.
This is the part of the code that prints the start and end row of each thread.
pthread_t* threads = new pthread_t[num_threads];
struct Th_Range* RANGE = (struct Th_Range*)malloc(sizeof(struct Th_Range*));
int thread_status;
RANGE->SizeOfImage = r; // 2d array with size (n*n) so rows(r) = columns(c)
if (n == num_threads) { //rows = num of threads then every thread will work in a single row
for (int i = 0; i < num_threads; i++) {
RANGE->ThreadId = i;
RANGE->StartRow = RANGE->EndRow = i;
cout << "ThreadID= " << i << ", startRow= " << RANGE->StartRow << ", endRow= " << RANGE->EndRow << endl;
thread_status = pthread_create(&threads[i], NULL, Median, RANGE);
if (thread_status)
exit(-1);
} //for loop ends here
for (int i = 0; i < num_threads; i++)
pthread_join(threads[i],NULL);
} //end of if statement
Here is the part of the code if needed with the median function and the above if statement.
#include <iostream>
#include <bits/stdc++.h>
#include <pthread.h>
pthread_mutex_t Lock;
pthread_mutex_t Pixels;
pthread_mutex_t Pixels2;
using namespace std;
int numOfBright, numOfDark, numOfNormal;
int** Oimage, ** Fimage; //original and filtered image
struct Th_Range {
int SizeOfImage;
int StartRow;
int EndRow;
int ThreadId;
};
void* Median(void* par)
{
struct Th_Range* Num = (struct Th_Range*)par;
int StartRow = Num->StartRow;
int EndRow = Num->EndRow;
int Size = Num->SizeOfImage;
int Neighbour[9] = { 0 };
int dark = 0, bright = 0, normal = 0;
if (EndRow == StartRow)
EndRow += 2;
else
EndRow++;
for (int i = StartRow +1; i < EndRow ; i++)
{
for (int j = 1; j < Size - 1; j++)
{
Neighbour[0] = Oimage[i - 1][j - 1];
Neighbour[1] = Oimage[i - 1][j];
Neighbour[2] = Oimage[i - 1][j + 1];
Neighbour[3] = Oimage[i][j - 1];
Neighbour[4] = Oimage[i][j];
Neighbour[5] = Oimage[i][j + 1];
Neighbour[6] = Oimage[i + 1][j - 1];
Neighbour[7] = Oimage[i + 1][j];
Neighbour[8] = Oimage[i + 1][j + 1];
pthread_mutex_lock(&Pixels); //it can be moved only to lock the Fimage and the numOfBright or any other global variables
sort(Neighbour, Neighbour + 9);
Fimage[i][j] = Neighbour[4];
if (Neighbour[4] > 200) {
bright++;
numOfBright++;
}
else if (Neighbour[4] < 50) {
dark++;
numOfDark++;
}
else {
normal++;
numOfNormal++;
}
pthread_mutex_unlock(&Pixels);
}
}
pthread_mutex_lock(&Pixels2); //when I try to remove this lock the output gets interrupted
cout << "ThreadID " << Num->ThreadId << " numOfBright " << bright << " numOfDark " << dark << " numOfNormal " << normal<<endl;
pthread_mutex_unlock(&Pixels2);
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
int num_threads, n, r, c; // n is the size of the matrix r and c are rows and columns
numOfNormal = numOfDark = numOfBright = 0;
if (argc >= 2)
num_threads = atoi(argv[1]);
else
exit(-1);
ifstream cin("input.txt");
cin >> n;
r = c = n + 2;
Oimage = new int* [r]();
Fimage = new int* [r]();
for (int i = 0; i < c; i++)
{
Oimage[i] = new int[c]();
Fimage[i] = new int[c]();
}
for (int i = 1; i < r - 1; i++)
for (int j = 1; j < c - 1; j++)
cin >> Oimage[i][j];
pthread_t* threads = new pthread_t[num_threads];
struct Th_Range* RANGE = (struct Th_Range*)malloc(sizeof(struct Th_Range*));
RANGE->SizeOfImage = r;
if (n == num_threads) { //rows = num of threads then every thread will work in a single row
//n+2
int thread_status;
for (int i = 0; i < num_threads; i++) {
RANGE->ThreadId = i;
RANGE->StartRow = RANGE->EndRow = i;
// pthread_mutex_lock(&Lock);
cout << "ThreadID= " << i << ", startRow= " << RANGE->StartRow << ", endRow= " << RANGE->EndRow << endl;
thread_status = pthread_create(&threads[i], NULL, Median, RANGE);
if (thread_status)
exit(-1);
}
}
I tried to move pthread_join inside the loop of pthread_create it gives a correct output but of course it is a wrong solution. I have no idea what to do next. Thanks in advance
Maybe you should use #include
or (using namespace sff) it must work
Code wars Question:(Isogram)
An Isogram is a word that has no repeating letters,consecutive or non-consecutive.Implement a function that determines whether a string that contains only letters is an Isogram. Assume the empty string is an Isogram.Ignore letter case.
Test Cases:
"Dermatoglyphics"-->true
"aba" --> false
"balLoon"--> false
My Code:
#include <bits/stdc++.h>
using namespace std;
bool isIsogram(string s)
{
for (int i = 0; i < s.length(); i++)
{
for (int j = i + 1; j < s.length(); j++)
{
if (s[i] == s[j] || s[i] == s[j] + 32 || s[i] == s[j] - 32)
{
return false;
}
return true;
}
}
}
int main()
{
cout << isIsogram("abd") << endl;
return 0;
}
If there is an optimized solution for this problem?
Can we reduce time complexity of this problem?
Your code is of O(N^2) Time Complexity.
Yes, there is an Optimized Way of doing it.
Here's the optimized solution: O(N)
#include <bits/stdc++.h>
using namespace std;
bool Isogram(string s)
{
int N = 125;
bool check[N];
for (int i = 0; i < 125; i++)
{
check[i] = false;
}
for (int i = 0; i < s.size(); i++)
{
if (check[s[i]] != false)
{
return false;
}
check[s[i]] = true;
if (s[i] + 32 < N)
check[s[i] + 32] = true;
if (s[i] - 32 >= 0)
check[s[i] - 32] = true;
}
return true;
}
int main()
{
string s;
cin >> s;
if (Isogram(s) == true)
cout << "The word " << s << " is an Isogram" << endl;
else
cout << "The word " << s << " is not an Isogram" << endl;
return 0;
}
We made an array of 125 elements because the ASCII value of lowercase particles lies in the range from 65 to 90, and for uppercase, it is 97 to 122.
And we are adding 32 and subtracting 32 to cover the uppercase alphabet (if the element is in lowercase) and vice versa.
Hope you get it...
here is my code, I can not figure out why it won't work as a function when the exact code in main() produces the correct answer. The assignment is to convert binary number to decimal.
#include <iostream>
#include <cstdlib>
#include "std_lib_facilities.h"
using namespace std;
int binaryCon(int biNum);
int main()
{
int num, bin, Bnum;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
Bnum = binaryCon(num);
cout << "The decimal equivalent of " << bin << " : " << Bnum << endl;
}
int binaryCon(int biNum)
{
long dec = 0, rem = 0, base = 1;
enter code here`while (biNum > 0)
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
return biNum;
}
corrected code:
#include <iostream>
#include <cstdlib>
using namespace std;
int binaryCon(int biNum);
int main()
{
int num, bin, Bnum;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
Bnum = binaryCon(num);
cout << "The decimal equivalent of " << bin << " : " << Bnum << endl;
getchar();
return 0;
}
int binaryCon(int biNum)
{
long dec = 0, rem = 0, base = 1;
while (biNum > 0){
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
return dec;
}
As you are not using { and } in your while loop may be its going in infinite loop. As its working for this line only
while (biNum > 0)
rem = biNum % 10; // running this line infinite as `biNum > 0`
Use
while (biNum > 0){
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
maybe this?
while (biNum > 0)
{
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
int main() or int main(int argc, char** argv) must return a value. If you return 0 then it means that there is no problem with code. Another numbers 1,2 etc means there is an error.(Returned numbers are error numbers)
Using Bottom to up DP approach, I am able to solve the problem How to solve http://www.spoj.com/problems/MST1/ upto 10^8.
If input is very large n upto 10^9. I will not be able to create lookup table for upto 10^9. So what will be better approach to solve the problem ?
Is there any heuristic solution ?
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int main()
{
const int N_MAX = 20000001;
int *DP = new int[N_MAX];
DP[1] = 0;
for (int i = 2; i < N_MAX; i++) {
int minimum = DP[i - 1];
if (i % 3 == 0) minimum = min(minimum, DP[i/3]);
if (i % 2 == 0) minimum = min(minimum, DP[i/2]);
DP[i] = minimum + 1;
}
int T, N; cin >> T;
int c = 1;
while (T--) {
cin >> N;
cout << "Case " << c++ << ": " << DP[N] << endl;
}
delete[] DP;
}
i want to convert decimal values to 16 bit binary values. i used this code in another one.
#include <iostream>
#include <bitset>
int main() {
int x = 5;
std::bitset<8> bin_x(x);
std::cout << bin_x;
return 0;
}
this is a code posted by a member. i want to use it in a loop and store the value of bin_x in a 16 two dimensional character array. how can it be done?
here is what iam doing
#include<iostream>
using namespace std;
#include <bitset>
int main(){
int DecimalArray[] = {1,2,3,4,5,22,555,85,18,741}; //Create an array of decimal numbers.
const int ArrayLen = sizeof(DecimalArray)/sizeof(int); //Store the size of the Decimal Array in a constant
//strcpy(BinaryArray[i], "0000000000000000");
char BinaryArray[ArrayLen][16]; //Create an array of the same length for binary nos.
for(int i = 0; i<ArrayLen; i++)
{
int CurrentDec = DecimalArray[i]; //Store current Decimal number in CurrentDec variable
strcpy(BinaryArray[i], "0000000000000000");
std::bitset<16> bin_x(CurrentDec);
cout<< "bin"<<bin_x<< endl;
for (int j = 0; j<15; j++)
{
bin_x=BinaryArray[i][j];
cout<< "b1"<< BinaryArray[i] << endl;
}
cout<<"The Decimal numbers and their Binary Equivalents are:\n\n";
cout<<"Decimal Binary \n\n";
}
//Output both arrays
for( i = 0; i<ArrayLen; i++){
cout<<DecimalArray[i]<<"\t "<<BinaryArray[i]<<endl;
}
cin.get();
return 0;
}
but i do not get the value in BinaryArray. kindly help me with it, its very urgent. Thanks!
#include<iostream>
using namespace std;
#include <bitset>
int main(){
int DecimalArray[] = {1,2,3,4,5,22,555,85,18,741}; //Create an array of decimal numbers.
const int ArrayLen = sizeof(DecimalArray)/sizeof(int); //Store the size of the Decimal Array in a constant
//strcpy(BinaryArray[i], "0000000000000000");
char BinaryArray[ArrayLen][17]; //Create an array of the same length for binary nos.
int i;
for(i = 0; i<ArrayLen; i++)
{
int CurrentDec = DecimalArray[i]; //Store current Decimal number in CurrentDec variable
int index = 1, CurrentBin = 0;
strcpy(BinaryArray[i], "0000000000000000");
std::bitset<16> bin_x(CurrentDec);
cout<< "bin"<<bin_x<< endl;
for (int j = 0; j<16; j++)
{
if (bin_x[15-j])
{
BinaryArray[i][j] = '1';
}
cout<< "b1"<< BinaryArray[i][j]<<endl ;
}
}
cout<<"The Decimal numbers and their Binary Equivalents are:\n\n";
cout<<"Decimal Binary \n\n";
//Output both arrays
for( i = 0; i<ArrayLen; i++){
cout<<DecimalArray[i]<<"\t "<<BinaryArray[i]<<endl;
}
cin.get();
return 0;
}