I have the follow code below to generate a scrambled state array, however, it does not seem to be generating the properly randomized state array for the key (51323).
unsigned char* generateStateArray(unsigned long key) {
unsigned char s[256];
//Load the state array from 1-255
for (int i = 0; i < 256; i++) {
s[i] = i;
}
//Get the binary representation of the key
unsigned long n = key;
int binary[64];
for (int i = 0; i < 64; i++) {
binary[i] = 0;
std::cout << binary[i];
}
std::cout << std::endl;
for (int i = 0; n > 0; i++) {
binary[i] = n % 2;
n /= 2;
}
//Randomize the entries
int j = 0;
for (int i = 0; i < 256; i++) {
j = (j + binary[i % 64] + s[i]) & 255;
char c = s[i];
s[i] = s[j];
s[j] = c;
}
}
The output for S should look like:
But it gives me this output:
Any idea what I'm doing wrong and how I can fix it?
Related
I was trying to do a problem on HackerEarth, and I am getting Segmentation Faults for this for loop:
for (int index = 0; index < 18; index++){
cout << arr_list[arr_index][index];
}
Even though I assigned values to arr_list[arr_index][index] in the loop right before (so I'm guessing the values are somehow not being saved, but I don't know how the values aren't being saved).
When I remove this for loop, I don't get any segfaults, and the cout information prints what's expected (the numbers I've inputted, with each digit twice for each cout inside the loop).
#include <iostream>
#include <string>
using namespace std;
void step(int arr_list[1000000][18], int cs, int N){
/**
int freq[100000] = {0};
for (int i = 0; i < N; i++){
int cur_arr[18];
for (int index = 0; index < 18; index++){
cur_arr[index] = arr_list[i][index];
}
if (cs == 4){
freq[cur_arr[0]*100 + cur_arr[1] * 10 + cur_arr[2]] += 1;
} else{
freq[cur_arr[18 - cs*5] * 10000 + cur_arr[18 - cs*5 + 1] * 1000 + cur_arr[18 - cs*5 + 2]*100 + cur_arr[18 - cs*5 + 3] * 10 + cur_arr[18 - cs*5 + 4]] += 1;
}
}
for (int i = 1; i < 100000; i++){
freq[i] += freq[i-1];
}
int new_arr_list[1000000][18];
for (int i = N-1; i >= 0; i--){
int pos;
int cur_arr[18];
for (int index = 0; index < 18; index++){
cur_arr[index] = arr_list[i][index];
}
if (cs == 4){
pos = cur_arr[0]*100 + cur_arr[1] * 10 + cur_arr[2];
} else{
pos = cur_arr[18 - cs*5] * 10000 + cur_arr[18 - cs*5 + 1] * 1000 + cur_arr[18 - cs*5 + 2]*100 + cur_arr[18 - cs*5 + 3] * 10 + cur_arr[18 - cs*5 + 4];
}
for (int index = 0; index < 18; index++){
new_arr_list[freq[pos] - 1][index] = arr_list[i][index];
}
freq[pos] --;
}
for (int i = 0; i < N; i++){
for (int index = 0; index < 18; index++){
arr_list[i][index] = new_arr_list[i][index];
}
}
**/
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
int arr_index = 0;
cin >> T;
int arr_list[1000000][18];
int max_len = 0;
for (int testcase = 0; testcase < T; testcase ++){
string a;
cin >> a;
int a_len = a.length();
if (a_len > max_len){
max_len = a_len;
}
int arr_entry[18];
for (int i = 0; i < a_len ; i++){
arr_entry[18 - a_len + i] = a[i] - 48;
}
for (int i = 0; i < 18 - a_len; i++){
arr_entry[i] = 0;
}
for (int index = 0; index < 18; index++){
arr_list[arr_index][index] = arr_entry[index];
cout << arr_entry[index];
cout << arr_list[arr_index][index];
}
for (int index = 0; index < 18; index++){
cout << arr_list[arr_index][index];
}
arr_index ++;
}
/**
for (int c = 1; c < 5; c++){
if (max_len > (c-1)*5){
step(arr_list, c, T);
for (int i = 0; i < T; i++){
int is_leading_zero = 1;
for (int j = 0; j < 18; j++){
if (is_leading_zero == 0){
cout << arr_list[i][j];
}else{
if (arr_list[i][j] != 0){
is_leading_zero = 0;
cout << arr_list[i][j];
}
}
}
cout << " ";
}
cout << "\n";
}
}
**/
}
I'm assuming this is a common error, and that I'm missing something simple that gives me segfaults for values I already assigned data to.
Does anyone know why this is happening?
You are allocating 72 MB on the stack:
int main()
{
[...]
int arr_list[1000000][18];
[...]
}
This is probably causing a stack overflow.
On the Microsoft Windows platform, the maximum stack size is, by default, 1 MB. On Linux, it is typically 8 MB.
When allocating such large amounts of memory, I recommend that you instead either use
dynamic memory allocation, or
a global variable, or
a static local variable.
This ensures that the array is not stored on the stack.
I need to get bits of a character. I haven't used C++ bitwise operators before, and I can't figure out what I'm doing wrong.
int main()
{
bool bits[8];
char c = static_cast<char>(0b11101101);
for(int i = 0; i < 8; i++) {
bits[i] = (c >> i) & 1;
}
for(int i = 0; i < 8; i++) {
std::cout << bits[i];
}
}
//output: 10110111
Just change your first loop to:
for(int i = 0; i < 8; i++) {
bits[7-i] = (c >> i ) & 1;
}
I am trying to use radix sort to sort file contain social security and date of birth the format looks like this "###-##-####,#######.I have to apply radix sort on each fields according to command line switch. I have a radix sort that is work for int array and i am trying to modify the code for string type array but i am not sure how to accomplish this. I did a quick sort for string type by comparing strings and pivot and that is work fine however for radix sort I am not if I can do this with string type or I have to convert the string to integer. I have tried to use "atoi" to convert to integer but I am not sure how to correctly do this if I have to.
string getMax(string arr[], int n){
string max = arr[0];
for (int i = 1; i < n; i++){
if (arr[i]>max)
max = arr[i];
}
return max;
}
void countSort(string a[], int size, int k){
string *b = NULL; int *c = NULL;
b = new string[size];
c = new int[k];
for (int i = 0; i <k; i++){
c[i] = 0;
//cout << c[i] << "\n";
}
for (int j = 0; j <size; j++){
c[(a[j]/k)%10]++; //a[j] is a string
//cout << c[a[j]] << endl;
}
for (int f = 1; f <10; f++){
c[f] += c[f - 1];
}
for (int r = size - 1; r >= 0; r--){
b[c[(a[r] / k) % 10] - 1] = a[r];
c[(a[r] / k) % 10]--;
}
for (int l = 0; l < size; l++){
a[l] = b[l];
}
}
void radixSort(string b[], int r){
string max = getMax(b, r);
for (int digit = 1; max / digit > 0; digit *= 10){
countSort(b, r, digit);
}
};
I didn't try, but I think you can do radix sort for string.
Calculate the length of the longest string in the array to sort.
Do radix sort just like for integers. Do sorting using each characters in the string.
If a string is shorter than another and there is no character in the "digit", consider its value as -65536 (or a smaller value than any other characters).
UPDATE: I tested my idea and it seems working.
#include <cstdio>
#include <string>
using std::string;
size_t getMax(string arr[], int n){
size_t max = arr[0].size();
for (int i = 1; i < n; i++){
if (arr[i].size()>max)
max = arr[i].size();
}
return max;
}
void countSort(string a[], int size, size_t k){
string *b = NULL; int *c = NULL;
b = new string[size];
c = new int[257];
for (int i = 0; i <257; i++){
c[i] = 0;
//cout << c[i] << "\n";
}
for (int j = 0; j <size; j++){
c[k < a[j].size() ? (int)(unsigned char)a[j][k] + 1 : 0]++; //a[j] is a string
//cout << c[a[j]] << endl;
}
for (int f = 1; f <257; f++){
c[f] += c[f - 1];
}
for (int r = size - 1; r >= 0; r--){
b[c[k < a[r].size() ? (int)(unsigned char)a[r][k] + 1 : 0] - 1] = a[r];
c[k < a[r].size() ? (int)(unsigned char)a[r][k] + 1 : 0]--;
}
for (int l = 0; l < size; l++){
a[l] = b[l];
}
// avold memory leak
delete[] b;
delete[] c;
}
void radixSort(string b[], int r){
size_t max = getMax(b, r);
for (size_t digit = max; digit > 0; digit--){ // size_t is unsigned, so avoid using digit >= 0, which is always true
countSort(b, r, digit - 1);
}
}
int main(void) {
string data[] = {
"aaaba",
"dfjasdlifjai",
"jiifjeogiejogp",
"aabaaaa",
"gsgj",
"gerph",
"aaaaaaa",
"htjltjlrth",
"joasdjfisdjfdo",
"hthe",
"aaaaaba",
"jrykpjl",
"hkoptjltp",
"aaaaaa",
"lprrjt"
};
puts("before sorting:");
for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
printf(" %s\n", data[i].c_str());
}
radixSort(data, (int)(sizeof(data) / sizeof(data[0])));
puts("after sorting:");
for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
printf(" %s\n", data[i].c_str());
}
return 0;
}
i need help, the code below does not work as it returns nothing when i run it. I'm trying to add big number so large that the numbers won’t be able to be represented in the standard C++ integer data structures
mission.cpp
void Big2Add(const char * num1, const char * num2, char * result)
{
string a = num1;
string b = num2;
int min = (a.length() < b.length() ? a.length():b.length());
int max = (a.length() < b.length() ? b.length():a.length());
int *n1 = new int[max];
int *n2 = new int[max];
for (unsigned int i=0; i < a.length(); i++)
{
n1[i] = a.at(a.length() - 1 -i) - 48;
}
cout << a << endl;
for (unsigned int i=0; i < b.length(); i++)
{
n2[i] = b.at(b.length()-1 -i) - 48;
}
cout << b << endl;
int carry = 0;
int* sum = new int[max];
int k=0;
for (k = 0; k < max; k++)
{
sum[k] = (n1[k] + n2[k] + carry) % 10;
if ( (n1[k] + n2[k] + carry) >= 10)
carry = 1;
else carry = 0;
}
sum[max] = carry;
for (int j= max; j >= 0; j--)
{
*result = sum[j];
}
}
main.cpp
char result[10];
const char * num1 = "10";
const char * num2 = "10";
Big2Add(num1, num2, result);
cout << "Part 3" << endl;
cout << "The addition of " << num1 << " and " << num2 << " is " << result << endl;
cout << endl;
First obvious bug:
int* sum = new int[max];
...
sum[max] = carry;
You need to allocate max+1 in order to use position max.
Second and third obvious bugs:
*result = sum[j];
You forgot to advance result and you forgot to add '0'
Fourth, you forgot to null terminate the string.
Try:
for (int j= max; j >= 0; j--)
{
*(result++) = sum[j] + '0';
}
*result = 0;
ok it works now tks but my main problem now is that is that there is a extra zero in front of my answer if let say i do 10 plus 10 it will give me 010 or 100 with 10 it will give me 0110 but if i plus 99 with 99 it will give 198 y?
void Big2Add(const char * num1, const char * num2, char * result)
{
string a = num1;
string b = num2;
int max = (a.length() < b.length() ? b.length():a.length());
int *n1 = new int[max];
int *n2 = new int[max];
unsigned int i;
for (i=0; i < a.length(); i++)
{
n1[i] = a.at(a.length() - 1 -i) - 48;
}
for (int j = i; j < max; ++j)
{
n1[j] = 0;
}
for (i=0; i < b.length(); i++)
{
n2[i] = b.at(b.length()-1 -i) - 48;
}
for (int j = i; j < max; ++j)
{
n2[j] = 0;
}
int carry = 0;
int* sum = new int[max];
int k=0;
for (k = 0; k < max; k++)
{
sum[k] = (n1[k] + n2[k] + carry) % 10;
if ( (n1[k] + n2[k] + carry) >= 10)
carry = 1;
else carry = 0;
}
sum[max] = carry;
for (int j= max ; j >= 0; j--)
{
if(sum[0] == 0)
{
}
*(result++) = sum[j] + '0';
}
*result = 0;
}
I was researching counting sort and decided to try an algorithm i found online. Though, it doesn't seem to actually sort my array.
void countSort2(int arr[], int n, int exp)
{
int *output = new int[n]; // output array
int i, count[10] = {0};
// Store count of occurrences in count[]
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%10 ]++;
// Change count[i] so that count[i] now contains actual position of
// this digit in output[]
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build the output array
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
}
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to curent digit
for (i = 0; i < n; i++)
arr[i] = output[i];
}
int main()
{
int b[10] = {4,3,2,1,6,7,8,9,7,6};
countSort2(b,10,10);
int i = 0;
while(i<10)
{
cout<<b[i]<<endl;
i++;
}
When the array is printed out, I get: "4,3,2,1,6,7,8,9,7,6". Am I calling the function wrong?
This is how you call the method [1]..
10 is the number of elements...
int main()
{
int b[10] = {14,23,22,11,66,67,58,49,17,16};
countSort2(b,10,1);
countSort2(b,10,10);
int i = 0;
while(i<10)
{
cout<<b[i]<<endl;
i++;
}
return 0;
}
This is a radix sort that sorts an array by a decimal digit. The sort is done from least significant digit to most significant digit. This means a series of calls with exp = 1, 10, 100, 1000, 10000, ... .
Here is an example of a radix sort that sorts an array of 64 bit unsigned integers by the bytes in the integers, from least significant to most significant. In this example, the temporary array is passed as a parameter to RadixSort():
typedef unsigned __int64 UI64;
typedef unsigned __int64 * PUI64;
PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count)
{
size_t mIndex[8][256] = {0}; // index matrix
PUI64 pDst, pSrc, pTmp;
size_t i,j,m,n;
UI64 u;
for(i = 0; i < count; i++){ // generate histograms
u = pData[i];
for(j = 0; j < 8; j++){
mIndex[j][(size_t)(u & 0xff)]++;
u >>= 8;
}
}
for(j = 0; j < 8; j++){ // convert to indices
n = 0;
for(i = 0; i < 256; i++){
m = mIndex[j][i];
mIndex[j][i] = n;
n += m;
}
}
pDst = pTemp; // radix sort
pSrc = pData;
for(j = 0; j < 8; j++){
for(i = 0; i < count; i++){
u = pSrc[i];
m = (size_t)(u >> (j<<3)) & 0xff;
pDst[mIndex[j][m]++] = u;
}
pTmp = pSrc;
pSrc = pDst;
pDst = pTmp;
}
return(pSrc);
}