Word Search using Multidimensional arrays - multidimensional-array

This program is supposed to allow the user to enter a word to be searched for in a multi-dimensional array filled with characters. The word can be in any non-diagonal direction.
As it is written it will only find the correct location for a one character long word. I understand that it will only find the first instance of any word entered but that is okay. I cannot figure out why it will not find words longer than one character.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#define GRIDSIZE 10
#define MAXSIZE 15 //Max size for word to search
#define SENTINEL -1
void print_grid(int[][GRIDSIZE], int);
int search_grid(int[][GRIDSIZE], int, int[], int, int[], int[]);
int main(void){
int grid[GRIDSIZE][GRIDSIZE];
int grid_size;
char word[MAXSIZE];
int word_int[MAXSIZE];
int a,b,i,j,h;
int word_size;
int word_found;
int go_again;
int location_row[word_size];
int location_column[word_size];
srand(time(NULL));
//fills grid with random letters
for(i=0; i<10; i++){
for(j=0; j<10; j++){
do{grid[i][j] = rand() % 123;
}while(grid[i][j]<97);
}}
//gets word and searches for it, returns 1 or 0 for found or not
//exits of sentinel is entered
do{
print_grid(grid, grid_size);
printf("Please enter a word to search followed by 'ENTER':\n");
scanf(" %s", word);
word_size = strlen(word);
for(b=0; b < word_size; b++){
word_int[b] = word [b];
}
word_found = search_grid(grid, grid_size, word_int, word_size, location_row, location_column);
("%d", word_found);
if(word_found == 0){
printf("\nThe word you entered was not found\n");
printf("Enter '1' to search for another word, or '0' to exit, followed by 'ENTER'\n");
scanf(" %d", &go_again);
}
}while(word_found != 1 && go_again == 1);
if(word_found == 1){
printf("The word you entered was found at:\n");
for(h=0; h < word_size; h++){
printf("%c%d: ", toupper(location_row[h] + 97), location_column[h]+1);
}
print_grid(grid, grid_size);
}
return 0;
}
//prints N*N grid with labels on each axis
void print_grid(int grid[][GRIDSIZE], int grid_size){
int i,j,k;
char row[26] ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("\n");
printf(" ");
for(k=1; k < GRIDSIZE +1; k++){
printf("%d ", k);}
printf("\n");
for(i=0; i<10; i++){
printf("%-c ", row[i]);
for(j=0; j<10; j++){
printf("%-c ", grid[i][j]);
}
printf("\n");
}
printf("\n");
return;
}
//searches grid for word
int search_grid(int grid[][GRIDSIZE], int gride_size, int word_int[], int word_size, int location_row[], int location_column[]){
//printf("SEARCH GRID RAN//////////////////////");
int r, c, q;
int num = 0;
int found[word_size];
int found_total = 0;
for(q=0; q < word_size; q++){
found[q] = 0;}
//search loops
for(r=0; r < GRIDSIZE; r++){
for(c=0; c < GRIDSIZE; c++){
if(grid[r][c] == word_int[num]){
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
for(num=1; num < word_size; num ++){
if(r=0){
if(c=0){
if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK DOWN AND RIGHT
}
if(c = GRIDSIZE - 1){
if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK DOWN AND LEFT
}
else{
if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK LEFT RIGHT DOWN
}
}
if(r = GRIDSIZE - 1){
if(c=0){
if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK up AND RIGHT
}
if(c = GRIDSIZE - 1){
if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK UP AND LEFT
}
else{
if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK LEFT RIGHT UP
}
}
else{
if(c=0){
if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK UP DOWN AND RIGHT
}
if(c = GRIDSIZE - 1){
if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK UP DOWN AND LEFT
}
else{
if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
found[num] = 1;
location_row[num] = r;
location_column[num] = c;
//CHECK LEFT RIGHT UP DOWN
}
}
}
}
}
}
for(q=0; q < word_size; q++){
found_total += found[q];
}
//printf("FOUND TOTAL %d", found_total);
if(found_total == word_size){
return 1;
}
if(found_total != word_size){
return 0;
}
}

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] << " ";
}

Rotate Image by pressing button

So I am programming scrips with Arduino and I am supposed to make an arrow that turns 90 degrees when the up button is pressed. I don't really understand how I am supposed to make it turn when the Up button is pressed. I provided the whole code if you want to test it. I'm doing it in german so "unten" is bottom and "oben" is top
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
const int IO_BUTTON_UP = 4;
void setup() {
Serial.begin(115200);
Serial.println("Matrix8x8");
Serial.println("<Autor>");
matrix.begin(0x70);
matrix.setBrightness(1);
pinMode(IO_BUTTON_UP, INPUT);
}
int loops = 0;
void loop() {
Serial.println("loop");
int ON = 1;
int OFF = 0;
int unten = 0;
int oben = 7;
int rotation = 0;
matrix.setRotation(rotation);
if (digitalRead(IO_BUTTON_UP) == 1){
rotation ++;
}
if (loops == 0){
unten = 3;
oben = 4;
} else if (loops == 1){
unten = 3;
oben = 4;
} else if (loops == 2){
unten = 3;
oben = 4;
} else if (loops == 3){
unten = 3;
oben = 4;
} else if (loops == 4){
unten = 0;
oben = 7;
} else if (loops == 5){
unten = 1;
oben = 6;
} else if (loops == 6){
unten = 2;
oben = 5;
} else if (loops == 7){
unten = 3;
oben = 4;
}
for (int i = unten; i <= oben; i++){
matrix.drawPixel(loops, i, ON);
}
matrix.writeDisplay();
delay(200);
if (loops == 7){
delay (200);
matrix.clear();
matrix.writeDisplay();
delay (200);
}
loops = (loops = loops + 1) % 8;
Serial.print ("loops: ");
Serial.println (loops);
}
I got an answer. I just had to define "rotation" outside of the "void loop"
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
int IO_BUTTON_UP = 4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Matrix8x8");
Serial.println("<Autor>");
matrix.begin(0x70);
matrix.setBrightness(1);
pinMode(IO_BUTTON_UP, INPUT);
}
int loops = 0;
int rotation = 0;
void loop() {
// put your main code here, to run repeatedly:
Serial.println("loop");
int ON = 1;
int OFF = 0;
int unten = 0;
int oben = 7;
Serial.println(IO_BUTTON_UP);
if (digitalRead(IO_BUTTON_UP) == 1){
rotation++;
}
matrix.setRotation(rotation);
if (loops == 0){
unten = 3;
oben = 4;
} else if (loops == 1){
unten = 3;
oben = 4;
} else if (loops == 2){
unten = 3;
oben = 4;
} else if (loops == 3){
unten = 3;
oben = 4;
} else if (loops == 4){
unten = 0;
oben = 7;
} else if (loops == 5){
unten = 1;
oben = 6;
} else if (loops == 6){
unten = 2;
oben = 5;
} else if (loops == 7){
unten = 3;
oben = 4;
}
for (int i = unten; i <= oben; i++){
matrix.drawPixel(loops, i, ON);
}
matrix.writeDisplay();
delay(200);
if (loops == 7){
delay (200);
matrix.clear();
matrix.writeDisplay();
delay (200);
}
loops = (loops = loops + 1) % 8;
Serial.print ("loops: ");
Serial.println (loops);
}

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.

Partial sorting of small array in OpenCL private memory

The task is to find N largest elements in array. The array is quite small (~40 items). I am using this algorithm:
float max1 = -inf;
int max1I = -1;
float max2 = -inf;
int max2I = -1;
float max3 = -inf;
int max3I = -1;
float max4 = -inf;
int max4I = -1;
float max5 = -inf;
int max5I = -1;
float performances[MAX_NUMBER_OF_SELECTIONS];
for (int i = 0; i < numberOfSelections; ++i) {
float performance = /*some calculations*/;
performances[i] = performance;
if (performance > max1) {
max5 = max4; max5I = max4I;
max4 = max3; max4I = max3I;
max3 = max2; max3I = max2I;
max2 = max1; max2I = max1I;
max1 = performance; max1I = i;
} else if (performance > max2) {
max5 = max4; max5I = max4I;
max4 = max3; max4I = max3I;
max3 = max2; max3I = max2I;
max2 = performance; max2I = i;
} else if (performance > max3) {
max5 = max4; max5I = max4I;
max4 = max3; max4I = max3I;
max3 = performance; max3I = i;
} else if (performance > max4) {
max5 = max4; max5I = max4I;
max4 = performance; max4I = i;
} else if (performance > max5) {
max5 = performance; max5I = i;
}
}
The approach was good enough but now I need to make it top10 instead of top5. Should I copy-paste this pattern? Or maybe there is something better?
If you want to do it on a large array this code is NOT valid.
I am supposing that you have lots of small arrays and each work item works on one of them.
I would do something like:
//Init
float maxs[10+1];
for(int i=0; i<10+1; i++){
maxs[i] = -inf;
}
for(int i=0; i<size; i++){
//Is it higher than the element 0?
if(data[i] > maxs[0]){
maxs[0] = data[i];
for(int j=0; j<10; j++){
if(maxs[j] > maxs[j+1])
swap(maxs[j], maxs[j+1]);
else break;
}
}
}
Now you have an array of 11 elements ordered from small to high, just take the last 10 elements.
The code can be further optimized, but it is very simple.

Resources