MIPS. Count an amount of each ascii symbol in a string. - string

I am trying to complete a task in MIPS.
It is needed to compare 2 strings in the next way: to count a number of each ascii symbol from 1 to 255 in both strings and and compare values for each symbol.
For a moment i have wrote piece of pseudo code in java just to visualize a problem. The part that i can't understand is how can i make a "search" of each ascii character in a string.
// array allAscii that suppose to contain all ascii symbols (i have made it
containing all zeros bacause in this java code it is not that much important)
int[] allAscii = new int[255]
String s1 = "ascii."; // first string
String s2 = "asciiz,"; // second string
int difference = 0;
for(int i = 0; i< allAscii.length; i++){
int count1 = 0; //counter for the character of the string1
int count2 = 0; //counter for the character of the string1
for(int k = 0; k<s1.length(); k++){
if( s1.charAt(k) == allAscii[i]) count1++;
}
for(int l = 0; l<s2.length(); l++){
if( s2.charAt(l) == allAscii[i]) count2++;
}
difference += Math.abs(count1- count2);
}
so as the result for strings "ascii." and "asciiz,"
symbols from 0 to 43: count1 = 0; count2 = 0; difference = 0;
',': count1 = 0; count2 = 1; difference = 1;
'.': count1 = 1; count2 = 0; difference = 1;
symbols from 47 to 96: count1 = 0; count2 = 0; difference = 0;
'a': count1 = 1; count2 = 1; difference = 0;
'c': count1 = 1; count2 = 1; difference = 0;
'i': count1 = 2; count2 = 2; difference = 0;
's': count1 = 1; count2 = 1; difference = 0;
'z': count1 = 0; count2 = 1; difference = 1;
and so on....
it is non needed to store amount of each letter. i need just to accumulate variable difference but i should run through ALL ascii symbols

Related

Remove the first group of consecutive negative array elements from the array

Delete the first group of consecutive negative
elements in the array.
void delNegative(int* arr, int& size) {
if (!check_prop(arr, size, neg)) return;
int ind_l, ind_f;//
if (arr[0] < 0) ind_f = 0;
if (arr[size - 1] < 0) ind_l = size - 1;
for (int i = 1; i < size; i++){
if (arr[i - 1] > 0 && arr[i] < 0) ind_f = i;
if (arr[i - 1] < 0 && arr[i] > 0) ind_l = i - 1;
}
if (ind_l < ind_f) ind_l = size - 1;
int ii = 0;
for (int i = 0; i < size; i++)
if (i > ind_l || i < ind_f) {
arr[ii] = arr[i];
ii++;
}
size -= ind_l - ind_f + 1;
}
There is this function, but it removes the last group of negative elements.
It needs to be redone for dynamic arrays

"Program has triggered a breakpoint" while free memory malloced

When I free memory, the error "Program has triggered a breakpoint" occur. Check the code below, I wonder where is wrong ?
int SSavep(char *visited, int t, int n, int m)
{
int* map = (int*)malloc(m*n * sizeof(int));
int* q = (int*)malloc(m*n * sizeof(int));
int count = 0, cur = 0;
int begin = 0, end = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
//set value for map
}
}
..........
if (t >= map[end]) {
free(map);
free(q);
return 0;
}
else{
free(map);
free(q);
return -1;
}
}
the entire code is as below:
static int dir[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
int SSavep(char *visited, int t, int n, int m)
{
int* map = (int*)malloc(m*n * sizeof(int));
int* q = (int*)malloc(m*n * sizeof(int));
int count = 0, cur = 0;
int begin = 0, end = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (visited[i*n + j] == '.')
map[i*n + j] = 0;
else if (visited[i*n + j] == '*')
map[i*n + j] = -1;
else if (visited[i*n + j] == 'p') {
map[i*n + j] = -12;
end = i*n + j;
}
else {
map[i*n + j] = -9;
begin = i*n + j;
}
}
}
q[count++] = begin;
while (cur < count && q[cur] != end) {
int i = q[cur] / n;
int j = q[cur] % n;
for (int k = 0; k < 4; k++) {
int ni = i + dir[k][0];
int nj = j + dir[k][1];
if (ni < 0 || ni >= m || nj < 0 || nj >= n || map[ni*n + nj]>0 || map[ni*n + nj] == -1)
continue;
map[ni*n + nj] = map[i*n + j] + 1;
q[count++] = ni*n + nj;
}
cur++;
}
if (map[end] > 0 && t >= map[end]) {
free(map);
free(q);
return 0;
}
else{
free(map);
free(q);
return -1;
}
}
You are getting error on >> free(q);
for m=n=4
int* q = (int*)malloc(m*n * sizeof(int));
q == [m*n*sizeof(int)] == 4*4*4 == 64 bytes == int[16];
Because you wrote beyond the address space reserved for pointer variable 'q'.
check 'count' variable before free(q). I've got 1208, called with:
char* visited = new char[100 * 100];
memset(visited, 0, 10000);
int res = SSavep(visited, 0, 4, 4);
Btw this algo looks alot like some path finding examining neighboring cells on map and assigning weights, right? If so there are many opensource solutions, why not using them instead of reinventing the wheel? There are links to opensource solutions on wiki Path Finding page:
https://en.wikipedia.org/wiki/Pathfinding
check the bottom of the page for links.

Getting segmentaition fault with subset dp problem

Given a set of numbers, check whether it can be partitioned into two subsets such that the sum of elements in both subsets is same or not
I am getting segmentation fault in C++(g++ 5.4) with a this problem.
This is where i submitted my solution in C++
https://practice.geeksforgeeks.org/problems/subset-sum-problem/0
I am checking if the array can be divided into two parts with equal sum. So I am just checking if there exists a subset with sum equal to half the sum of the array
I have implemented the below logic with dynamic programming
Let dp[i][j] denote yes or no whether a subset with sum j is possible to form with elements in the range [0, i](both inclusive) where i is 0-based index. I have done nothing new with this traditional problem. But I am getting segmentation fault. The program is giving correct output for small test cases. What mistake have I made
I haven't used any comments because I have done nothing new. Hope it is understandable.
#include <iostream>
#include <bits/stdc++.h>
#include<cstdio>
#define ll long long int
using namespace std;
bool isVowel(char c){
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
bool isLower(char c){
return 97 <= c && c <= 122;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(10);
ll t, n;
cin >> t;
while (t--) {
cin >> n;
ll a[n];
ll sum = 0;
for (ll i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum % 2) {
cout << "NO" << '\n';
continue;
}
sum /= 2;
ll dp[n][sum + 1];
for (ll i = 0; i < n; i++) {
for(ll j = 0; j < sum + 1; j++) {
dp[i][j] = 0;
}
}
for (ll i = 0; i < n; i++) {
dp[i][a[i]] = 1;
dp[i][0] = 1;
}
for (ll i = 1; i < n; i++) {
for (ll j = 1; j < sum + 1; j++){
if (j - a[i] > 0) {
dp[i][j] = dp[i - 1][j - a[i]];
}
dp[i][j] |= dp[i - 1][j];
}
}
cout << (dp[n - 1][sum] ? "YES" : "NO") << '\n';
}
}
The segmentation fault is due to
ll dp[n][sum + 1];
Even though the constraints say 1 <= N<= 100, 0 <= arr[i]<= 1000, the test cases used are probably much larger, so ll dp[n][sum + 1] will end up taking some serious stack memory, use
bool dp[n][sum + 1];
It should work fine.
On a side note, avoid using ll randomly, use them according to the constraints.

In Verilog, how do I use a variable in logic

Say I have the following code:
genvar i,j;
generate
for(i = 0; i < MAX; i = i + 1) begin: gen_blah
for(j = 0; j < MAX; j = j + 1) begin: gen_foo
assign match[i] = entry[j] = i;
end
end
endgenerate
Is this a synthesizable expression? It seems like it should be since this will just unroll out into a bunch of compare-to-constant assignments. If not, how would I write this to accomplish that?
No. Imagine unrolling this and typing it out by hand. The fist two lines would cause a multiply driven net:
assign match[0] = (entry[0] == 0);
assign match[0] = (entry[1] == 0);
However, if you get rid of the generates and do this with loops inside an always block then it will work:
always_comb begin
for(int i = 0; i < MAX; i = i + 1) begin: gen_blah
for(int j = 0; j < MAX; j = j + 1) begin: gen_foo
match[i] = (entry[j] == i);
end
end
end
Inside this block you get multiple assignments to the same value but the last one "wins". I'm not sure what you are trying to accomplish with your code, so this might not actually perform the function you are looking for. If you are trying to see if any of the entries equal i then you should change it to:
always_comb begin
for(int i = 0; i < MAX; i = i + 1) begin: gen_blah
match[i]=0;
for(int j = 0; j < MAX; j = j + 1) begin: gen_foo
match[i] = match[i] || (entry[j] == i);
end
end
end

Conditional increment in generate block

I want to create 256 instances of foo. Therefore, I have two nested generate loops. However, I need a separate index variable l to for a proper selection of the signal.
genvar j, i, l;
generate
l = 0;
for(j = 0; j < 16; j++)
begin
for(i = 0; i < 16; i++) begin
foo bar
(
.a_i(a_i[(l+1)*8-1:l*8]),
.b_i(b_i[(j+1)*8-1:j*8]),
.c_o(c_i[i][j])
);
if(i < 15)
l = (l + 1) % 16;
end
end
endgenerate
Unfortunately, this construction does not work. How to add l to this generate to get the correct selection of the input signals?
A genvar can only be assigned as the index in a for-loop. So Leave it as an expression:
genvar j, i;
generate
for (j = 0; j < 16; j++) begin
for (i = 0; i < 16; i++) begin
foo bar (
// prefix 1'b0 for correct sign extinction, 4'() does casting
.a_i(a_i[{1'b0,4'(i-j)}*8 +: 8]),
.b_i(b_i[j*8 +: 8]),
.c_o(c_i[i][j])
);
end
end
endgenerate
+: is for array slicing, allowing variable index and constant offset. It is more concise and easier to maintain then specifying the msb:lsb, and it is synthesizable. Refer to 'Indexing vectors and arrays with +:' and 'What is `+:` and `-:`?
You can also combined the this method with dave_59's parameter approach:
genvar j, i;
generate
for (j = 0; j < 16; j++) begin
for (i = 0; i < 16; i++) begin
// defining the parameter with a data type to insure it is a unsigned 4 bit value
parameter bit [3:0] l = (i-j);
foo bar (
.a_i(a_i[l*8 +: 8]),
.b_i(b_i[j*8 +: 8]),
.c_o(c_i[i][j])
);
end
end
endgenerate
Use the conditional operator ?:, and make l a parameter
genvar j, i;
generate
l = 0;
for(j = 0; j < 16; j++)
begin
for(i = 0; i < 16; i++) begin
parameter l = (i < 15) ? ((l + 1) % 16) : 0;
foo bar
(
.a_i(a_i[(l+1)*8-1:l*8]),
.b_i(b_i[(j+1)*8-1:j*8]),
.c_o(c_i[i][j])
);
end
end
endgenerate

Resources