Recursion with test case - recursion

I am trying to code following recursion problem. Every time I run the test case it gives me out of bound error on the else if line. How do I fix the out of bound error. under the code is the test case.
static boolean allEqual(int[] a, int start, int end) {
if( a[start + 1]>=a.length){return false;}
if (start == end && a[start] == a[end]) {
return true;
}
///recursive
else if (a[start] == a[start + 1]) // Error over here
{
return allEqual(a, start + 1, end);
}
// else if (start == end && a[start] == a[end]) {
// return true;
else {
return false;
}
}
// Following is the test case
Random rng = new Random(SEED);
for(int i = 0; i < RUNS; i++) {
int[] a = new int[i];
int v = rng.nextInt();
for(int j = 0; j < i; j++) {
a[j] = v;
}
assertTrue(rp.allEqual(a, 0, i-1));
for(int j = 1; j < i; j++) {
assertTrue(rp.allEqual(a, j, i-1));
assertTrue(rp.allEqual(a, 0, j));
a[j] = a[j] - 1;
assertTrue(rp.allEqual(a, 0, j-1));
assertTrue(rp.allEqual(a, j + 1, i-1));
assertFalse(rp.allEqual(a, 0, i-1));
a[j] = a[j] + 2;
assertTrue(rp.allEqual(a, 0, j-1));
assertTrue(rp.allEqual(a, j + 1, i-1));
assertFalse(rp.allEqual(a, 0, i-1));
a[j] = a[j] - 1;
}
}

Related

recursive merge sort repetitive element print

I am facing problem in the below code...
This code is of recursive merge sort but the array which gets printed has repetitive elements from the array.
help me in identifying the problem.
void merge(int arr[], int l, int mid, int h) {
int i = l;
int j = mid + 1;
int k = l;
int b[h + 1];
while (i <= mid && j <= h) {
if (arr[i] < arr[j])
b[k++] = arr[i++];
else
b[k++] = arr[j++];
}
while (i <= mid)
b[k++] = arr[i++];
while (j <= h)
b[k++] = arr[j++];
for (int i = 0; i <= h; i++)
arr[i] = b[i];
}
void Rmerge_sort(int arr[], int l, int h)
{
if (l < h) {
int mid = (h + l) / 2;
Rmerge_sort(arr, l, mid);
Rmerge_sort(arr, mid + 1, h);
merge(arr, l, mid, h);
}
}
int main() {
int arr[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 10 }, n = 10;
Rmerge_sort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
You define array b with size h + 1 instead of h - l + 1. The merge loops copy the elements to the index values l to h, but the final copy loop takes elements from 0 to h, copying elements from an uninitialized part.
Here is a corrected version:
void merge(int arr[], int l, int mid, int h) {
int len = h - l + 1;
int i = l;
int j = mid + 1;
int k = 0;
int b[len];
while (i <= mid && j <= h) {
if (arr[i] < arr[j])
b[k++] = arr[i++];
else
b[k++] = arr[j++];
}
while (i <= mid)
b[k++] = arr[i++];
while (j <= h)
b[k++] = arr[j++];
for (int i = 0; i < len; i++)
arr[l + i] = b[i];
}
Note however that a better approach to this problem is to save only the left half of the slice to merge and to consider h as the index of the first element after the end of the slice. This avoid confusing and error prone +1/-1 adjustments and reduces the number of copies:
void merge(int arr[], int low, int mid, int hi) {
int len1 = mid - lo;
int b[len1];
int i, j, k;
for (i = 0; i < len1; i++)
b[i] = a[low + i];
for (i = 0, j = mid, k = lo; i < len;) {
if (j >= hi || arr[i] <= arr[j])
arr[k++] = b[i++];
else
arr[k++] = arr[j++];
}
}
void Rmerge_sort(int arr[], int low, int hi) {
if (hi - low > 1) {
int mid = low + (hi - low) / 2; // avoid arithmetic overflow
Rmerge_sort(arr, low, mid);
Rmerge_sort(arr, mid, hi);
merge(arr, low, mid, hi);
}
}
int main() {
int arr[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
Rmerge_sort(arr, 0, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

Semantic analyzer for checking type incompatibilities - Symbol table Implementation?

I got this code for Semanitic Analysis. Can't figure out the code for Symbol Table to make it run? Making a 2D array like this :
String[,] Symboltable = new String[20, 6];
Gives error of wrong number of indices inside, which I resolved but still wouldn't work. What is the other way I could do this?
Region Semantic Analyzer
void Semantic_Analysis(int k)
{
Regex variable_Reg = new Regex(#"^[A-Za-z|_][A-Za-z|0-9]*$");
if (finalArray[k].Equals("+"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String type = finalArray[k - 4] + "";
String left_side = finalArray[k - 3] + "";
int left_side_i = 0;
int left_side_j = 0;
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(left_side))
{ left_side_i = i; left_side_j = j; }
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (type.Equals(Symboltable[before_i][2]) && type.Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.Add(Ans);
}
if (Symboltable[left_side_i][2].Equals(Symboltable[before_i][2]) && Symboltable[left_side_i][2].Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.RemoveAt(Constants.Count - 1);
Constants.Add(Ans);
Symboltable[left_side_i][3] = Ans + "";
}
}
}
if (finalArray[k].Equals("-"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String type = finalArray[k - 4] + "";
String left_side = finalArray[k - 3] + "";
int left_side_i = 0;
int left_side_j = 0;
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(left_side))
{ left_side_i = i; left_side_j = j; }
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (type.Equals(Symboltable[before_i][2]) && type.Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) - Convert.ToInt32(Symboltable[after_i][3]);
Constants.Add(Ans);
}
if (Symboltable[left_side_i][2].Equals(Symboltable[before_i][2]) && Symboltable[left_side_i][2].Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.RemoveAt(Constants.Count - 1);
Constants.Add(Ans);
Symboltable[left_side_i][3] = Ans + "";
}
}
}
if (finalArray[k].Equals(">"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (Convert.ToInt32(Symboltable[before_i][3]) > Convert.ToInt32(Symboltable[after_i][3]))
{
int start_of_else = finalArray.IndexOf("else");
int end_of_else = finalArray.Count - 1;
for (int i = end_of_else; i >= start_of_else; i--)
{
if (finalArray[i].Equals("}"))
{
if (i < finalArray.Count - 2)
{ end_of_else = i; }
}
}
for (int i = start_of_else; i <= end_of_else; i++)
{ finalArray.RemoveAt(start_of_else); }
}
else
{
int start_of_if = finalArray.IndexOf("if");
int end_of_if = finalArray.IndexOf("}");
for (int i = start_of_if; i <= end_of_if; i++)
{ finalArray.RemoveAt(start_of_if); }
if_deleted = true;
}
}
}
}
#endregion

msp430f2618 fading LED using pwm

I'm trying to write code in C to fade an off-board LED using PWM and the MSP430f2618. I can get the LED to turn on but it stays at full intensity. I am trying to read in an array of frequency values and fade the LED based on the frequency values.
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int array_size = 0, i = 0, delay = 0;
double frequency[50] = {0.0};
array_size = sizeof(frequency);
frequency [0] = 60.0;
for (i = 1; i < array_size; i++)
{
if (frequency[i - 1] < 61)
{
frequency[i] = frequency[i-1] + 0.1;
}
else
{
frequency[i] = 60.0;
}
}
P4OUT &= 0;
P4DIR |= (BIT1 + BIT2); //P4.1 and P4.2 output
P4SEL &= ~(BIT1 + BIT2); //P4.1 and P4.2 TBx options, timer select
TBCCR0 = 512-1;
TBCCTL1 = OUTMOD_7;
TBCCTL2 = OUTMOD_7;
for (i = 0; i < array_size; i++)
{
P4OUT &= 0;
if ((frequency[i] < 60.2) && (frequency[i] >=60.0))
{
//TBCCR1 = 3200;
TBCCR1 = 384;
}
else if ((frequency[i] < 60.4) && (frequency[i] >=60.2))
{
//TBCCR1 = 2560;
TBCCR1 = 256;
}
else if ((frequency[i] < 60.6) && (frequency[i] >=60.4))
{
//TBCCR1 = 1920;
TBCCR1 = 128;
}
else if ((frequency[i] < 60.8) && (frequency[i] >=60.6))
{
//TBCCR1 = 1280;
TBCCR1 = 64;
}
else if ((frequency[i] < 61) && (frequency[i] >=60.8))
{
//TBCCR1 = 640;
TBCCR1 = 32;
}
else
{
TBCCR2 = 512;
}
P4OUT ^= BIT1;
for (delay = 0; delay < 32000; delay++);
}
TBCTL = TBSSEL_2 + MC_1; // ACLK, up mode
__bis_SR_register(LPM0_bits); // Enter LPM3
return 0;
}
The timer is not running until you start it by setting the MC field. That initialization must be done at the beginning.

C++ Multidimensional Array help delete a row and column

I need someone to tell me how to write a code easy and understandable for this matrix
In this exercise is search to delete a row and a column:
const int n = 4, m = 4;
int A[n][m] = { { 2, -8, -7, 5 },{ 4, -7, -8, 2 },{ 1, 10, 3, 6 },{ 4, 7, 9, -3 } };
int i, j, r, k, B[n][m];
variable r stands for row which need to be deleted
variable k stands for column which needs to be deleted
B[n][m] stands for new matrix that will be showed on console
Waiting for answer
there's my code :
const int n = 4, m = 4;
int A[n][m] = { { 2, -8, -7, 5 },
{ 4, -7, -8, 2 },
{ 1, 10, 3, 6 },
{ 4, 7, 9, -3 } };
int i, j, r, k, B[n][m];
cout << "give a value r and k:";
cin >> r >> k;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if ((i < r) && (j < k))
B[i][j] = A[i][j];
if ((i >= r) && (j < k))
B[i][j] = A[i + 1][j];
if ((i < r) && (j >= k))
B[i][j] = A[i][j + 1];
if ((i >= r) && (j >= k))
B[i][j] = A[i + 1][j + 1];
}
}
cout << "Matrix B ={" << endl;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < m - 1; j++)
{
cout.width(3);
cout << B[i][j];
}
cout << endl;
but here is what mix up things to me :
if ((i < r) && (j < k))
B[i][j] = A[i][j];
if ((i >= r) && (j < k))
B[i][j] = A[i + 1][j];
if ((i < r) && (j >= k))
B[i][j] = A[i][j + 1];
if ((i >= r) && (j >= k))
B[i][j] = A[i + 1][j + 1];
}
this is the part where i mess up more often
Can somebody tell me more easier way ?

Cannot find class type name "Normalized"- Processing

I'm having a dilemma with this code and have no clue what to do. I'm pretty new to processing. This is a project from this link...
http://blog.makezine.com/2012/08/10/build-a-touchless-3d-tracking-interface-with-everyday-materials/
any help is massively appreciated... Thanks in advance
import processing.serial.*;
import processing.opengl.*;
Serial serial;
int serialPort = 1;
int sen = 3; // sensors
int div = 3; // board sub divisions
Normalize n[] = new Normalize[sen];
MomentumAverage cama[] = new MomentumAverage[sen];
MomentumAverage axyz[] = new MomentumAverage[sen];
float[] nxyz = new float[sen];
int[] ixyz = new int[sen];
float w = 256; // board size
boolean[] flip = {
false, true, false};
int player = 0;
boolean moves[][][][];
PFont font;
void setup() {
size(800, 600, P3D);
frameRate(25);
font = loadFont("TrebuchetMS-Italic-20.vlw");
textFont(font);
textMode(SCREEN);
println(Serial.list());
serial = new Serial(this, Serial.list()[serialPort], 115200);
for(int i = 0; i < sen; i++) {
n[i] = new Normalize();
cama[i] = new MomentumAverage(.01);
axyz[i] = new MomentumAverage(.15);
}
reset();
}
void draw() {
updateSerial();
drawBoard();
}
void updateSerial() {
String cur = serial.readStringUntil('\n');
if(cur != null) {
String[] parts = split(cur, " ");
if(parts.length == sensors) {
float[] xyz = new float[sen];
for(int i = 0; i < sen; i++)
xyz[i] = float(parts[i]);
if(mousePressed && mouseButton == LEFT)
for(int i = 0; i < sen; i++)
n[i].note(xyz[i]);
nxyz = new float[sen];
for(int i = 0; i < sen; i++) {
float raw = n[i].choose(xyz[i]);
nxyz[i] = flip[i] ? 1 - raw : raw;
cama[i].note(nxyz[i]);
axyz[i].note(nxyz[i]);
ixyz[i] = getPosition(axyz[i].avg);
}
}
}
}
float cutoff = .2;
int getPosition(float x) {
if(div == 3) {
if(x < cutoff)
return 0;
if(x < 1 - cutoff)
return 1;
else
return 2;
}
else {
return x == 1 ? div - 1 : (int) x * div;
}
}
void drawBoard() {
background(255);
float h = w / 2;
camera(
h + (cama[0].avg - cama[2].avg) * h,
h + (cama[1].avg - 1) * height / 2,
w * 2,
h, h, h,
0, 1, 0);
pushMatrix();
noStroke();
fill(0, 40);
translate(w/2, w/2, w/2);
rotateY(-HALF_PI/2);
box(w);
popMatrix();
float sw = w / div;
translate(h, sw / 2, 0);
rotateY(-HALF_PI/2);
pushMatrix();
float sd = sw * (div - 1);
translate(
axyz[0].avg * sd,
axyz[1].avg * sd,
axyz[2].avg * sd);
fill(255, 160, 0);
noStroke();
sphere(18);
popMatrix();
for(int z = 0; z < div; z++) {
for(int y = 0; y < div; y++) {
for(int x = 0; x < div; x++) {
pushMatrix();
translate(x * sw, y * sw, z * sw);
noStroke();
if(moves[0][x][y][z])
fill(255, 0, 0, 200);
else if(moves[1][x][y][z])
fill(0, 0, 255, 200);
else if(
x == ixyz[0] &&
y == ixyz[1] &&
z == ixyz[2])
if(player == 0)
fill(255, 0, 0, 200);
else
fill(0, 0, 255, 200);
else
fill(0, 100);
box(sw / 3);
popMatrix();
}
}
}
fill(0);
if(mousePressed && mouseButton == LEFT)
msg("defining boundaries");
}
void keyPressed() {
if(key == TAB) {
moves[player][ixyz[0]][ixyz[1]][ixyz[2]] = true;
player = player == 0 ? 1 : 0;
}
}
void mousePressed() {
if(mouseButton == RIGHT)
reset();
}
void reset() {
moves = new boolean[2][div][div][div];
for(int i = 0; i < sen; i++) {
n[i].reset();
cama[i].reset();
axyz[i].reset();
}
}
void msg(String msg) {
text(msg, 10, height - 10);
}
You are missing a class, in fact, more than one. Go back to the github and download, or copy and paste, all three codes, placing each one in a new tab named same name of the class (well this is not required, but is a good practice). The TicTacToe3D.pde is the main code. To make a new tab choose "new tab" from the arrow menu in Processing IDE (just below the standard button at the right). The code should run. WIll need an Arduino though to really get it working.

Resources