Multiple value array - arrays

I am new to jScript and have written this code [which works perfectly]. Its purpose is to test that the term for the amount of loan is not exceeded. Can the process be consolidated into one array where you pass the loan amount which returns the term based on the range i.e. 6000 to 7000 = 96
function TestMaxTerm()
{
var LnAmt = 14000 //Testing Purposes
var Term = 0 //Testing Purposes
if (LnAmt > 0 && LnAmt <= 1000){Term = 0;}
if (LnAmt > 1000 && LnAmt <= 2000){Term = 1;}
if (LnAmt > 2000 && LnAmt <= 3000){Term = 2;}
if (LnAmt > 3000 && LnAmt <= 4000){Term = 3;}
if (LnAmt > 4000 && LnAmt <= 5000){Term = 4;}
if (LnAmt > 5000 && LnAmt <= 6000){Term = 5;}
if (LnAmt > 6000 && LnAmt <= 7000){Term = 6;}
if (LnAmt > 7000 && LnAmt <= 8000){Term = 7;}
if (LnAmt > 8000 && LnAmt <= 9000){Term = 8;}
if (LnAmt > 9000 && LnAmt <= 10000){Term = 9;}
if (LnAmt > 10000 && LnAmt <= 11000){Term = 10;}
if (LnAmt > 11000 && LnAmt <= 12000){Term = 11;}
if (LnAmt > 11000){Term = 12;}
//Obtain Maximum Term for Loan Amount
var MaxTerm = new Array();
MaxTerm[0] = 24; MaxTerm[1]=36; MaxTerm[2] = 48; MaxTerm[3] = 60;
MaxTerm[5] = 72; MaxTerm[5]=84; MaxTerm[6] = 96; MaxTerm[7] = 108;
MaxTerm[8] = 120; MaxTerm[9]=132; MaxTerm[10] = 164; MaxTerm[11] = 176;
MaxTerm[12] = 420;
var text = MaxTerm[Term];
alert(text);
}

You could put all of the data into a 2D array and loop through the array to find your answer.
Here's some sample code which you can tweak for your JScript implementation. Your last entry could go from 12000 to your max loan amount or Number.MAX_VALUE
var loanAmt = 2200;
var answer = -1;
var myData =
[
[0, 1000, 24],
[1000, 2000, 36],
[2000, 3000, 48],
[3000, 4000, 60]
];
for (var x=0; x < myData.length; x++) {
if ( (loanAmt > myData[x][0]) && (loanAmt <= myData[x][1]) ) {
answer = myData[x][2];
break;
}
}
alert(answer);

As the terms are multiples of 12 and the grouping is based on 1000, the term can be computed:
% function f(amt) {return 12 * (2 + Math.floor(amt / 1000));}
undefined
% f(6500)
96
% f(3999)
60
% f(4000)
72
%
On second thought:
% function g(amt) {return 24 + 12 * Math.floor((amt - 1) / 1000);}
undefined
% g(1)
24
% g(999)
24
% g(1000)
24
% g(1001)
36
% g(6000)
84
% g(6001)
96
% g(6999)
96
% g(7000)
96
% g(7001)
108
%

Related

Reduce size of c program without optimizing compiler

Im trying to make a minesweeper game in c which is compact enough to fit into a qr code, like some other people have done with snake. As it stands, my program needs to be around 2.98 KB and is currently 58KB.
Before I move on to trying to make some compiler magic happen, I wanted to know how could refine my code pre-compiler. Is my approach workable, would it have to be completely different, or is fitting the minesweeper program into that small of a size impossible without using assembly? My code can be seen here:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 100
#define HEIGHT 100
#define BOMBS 799
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
HANDLE wHnd;
HANDLE rHnd;
void SetGrid(int grid[WIDTH][HEIGHT])
{
int bomb[2] = { abs(rand() % WIDTH-1) + 1,
abs(rand() % HEIGHT-1) + 1 };
for (int i = 0; i < BOMBS; i++)
{
while (grid[bomb[0]][bomb[1]] < -1 || bomb[0] == 0 || bomb[1] == 0 || bomb[0] >= WIDTH-1 || bomb[1] >= HEIGHT-1)
{
bomb[0] = abs(rand() % WIDTH-1) + 1;
bomb[1] = abs(rand() % HEIGHT-1) + 1;
}
grid[bomb[0]][bomb[1]] = -9;
grid[bomb[0] + 1][bomb[1] + 1]++;
grid[bomb[0] + 1][bomb[1]]++;
grid[bomb[0]][bomb[1] + 1]++;
grid[bomb[0] - 1][bomb[1] + 1]++;
grid[bomb[0]][bomb[1] - 1]++;
grid[bomb[0] + 1][bomb[1] - 1]++;
grid[bomb[0] - 1][bomb[1] - 1]++;
grid[bomb[0] - 1][bomb[1]]++;
}
}
void ExpandGrid(int fullGrid[WIDTH][HEIGHT], int knownGrid[WIDTH][HEIGHT], int blankPos[2])
{
int neighbors[8][2] = {{0,1}, {1,0}, {1,1},
{0,-1}, {-1,0},
{-1,-1},{-1,1},{1,-1}};
int curTile[2];
knownGrid[blankPos[0]][blankPos[1]] = 1;
if(fullGrid[blankPos[0]][blankPos[1]] != 0) return;
for(int blck = 0; blck < 8; ++blck)
{
curTile[0] = abs(blankPos[0]+neighbors[blck][0]);
curTile[1] = abs(blankPos[1]+neighbors[blck][1]);
if(curTile[0] > WIDTH-1 || curTile[1] > HEIGHT-1) continue;
if(fullGrid[curTile[0]][curTile[1]] == 0 && knownGrid[curTile[0]][curTile[1]] == 0)
{
knownGrid[curTile[0]][curTile[1]] = 1;
ExpandGrid(fullGrid, knownGrid, curTile);
}
else if(fullGrid[curTile[0]][curTile[1]] > 0) knownGrid[curTile[0]][curTile[1]] = 1;
}
}
int main(void)
{
SMALL_RECT windowSize = { 0, 0, WIDTH - 1, HEIGHT - 1 };
COORD characterBufferSize = { WIDTH, HEIGHT };
COORD characterPosition = { 0, 0 };
SMALL_RECT consoleWriteArea = { 0, 0, WIDTH - 1, HEIGHT - 1 };
CHAR_INFO consoleBuffer[WIDTH][HEIGHT];
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
rHnd = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleTitle("Minesweeper!");
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
srand((unsigned int)time(NULL));
int startGrid[WIDTH][HEIGHT] = { 0 };
int knownGrid[WIDTH][HEIGHT] = { 0 };
SetGrid(startGrid);
int startCoord[2] = {0, 0};
int arrowPos[2] = {0, 0};
ExpandGrid(startGrid, knownGrid, startCoord);
while(1)
{
if (arrowPos[0] > WIDTH-1) arrowPos[0] = WIDTH-1;
if (arrowPos[0] < 0) arrowPos[0] = 0;
if (arrowPos[1] > HEIGHT-1) arrowPos[1] = HEIGHT-1;
if (arrowPos[1] < 0) arrowPos[1] = 0;
for (int x = 0; x < WIDTH; ++x)
{
for (int y = 0; y < HEIGHT; ++y)
{
if (knownGrid[x][y] == 1)
{
if (startGrid[x][y] > 0)
{
consoleBuffer[x][y].Char.AsciiChar = '0' + startGrid[x][y];
consoleBuffer[x][y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
}
else
{
consoleBuffer[x][y].Char.AsciiChar = 'o';
consoleBuffer[x][y].Attributes = (startGrid[x][y] < 0 ? FOREGROUND_RED : FOREGROUND_BLUE) | FOREGROUND_INTENSITY;
}
}
else
{
consoleBuffer[x][y].Char.AsciiChar = 00;
consoleBuffer[x][y].Attributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
}
if(arrowPos[0] == x && arrowPos[1] == y)
{
consoleBuffer[x][y].Attributes = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN;
}
}
}
WriteConsoleOutputA(wHnd, consoleBuffer, characterBufferSize, characterPosition, &consoleWriteArea);
switch(getch())
{
case KEY_UP:
arrowPos[0]--;
break;
case KEY_DOWN:
arrowPos[0]++;
break;
case KEY_LEFT:
arrowPos[1]--;
break;
case KEY_RIGHT:
arrowPos[1]++;
break;
case '\r':
ExpandGrid(startGrid, knownGrid, arrowPos);
break;
}
}
}
Reducing from 58K to 3K is a pretty big ask.
A few ideas:
Examine the libraries that are linked in during your build process, and reduce those to the bare minimum; Eliminate calls to library functions that you may be able to write yourself.
Possibly use shorter types. Do you need int? Can you use a byte (char) or a short instead?
Consider what parts of windows.h and stdlib.h you really need.

ASCII characters in visual studio look weird

I was coding c in code blocks and wanted to move over to visual studio since it has a tendency to compile programs better, yet for some reason the ascii values that were displaying numbers fine in code blocks eem to be giving me weird results in vs. here's a snippet of what they look like:
Since the code is exactly the same on each platform, this leads me to believe there is some setting I overlooked int vs pertaining to ascii which is already set in code blocks. For review, here is my code:
#include <windows.h>
#define WIDTH 18
#define HEIGHT 18
#define BOMBS 50
struct xorshift_state {
int a;
};
int xorshift(struct xorshift_state *state)
{
int x = state->a;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return state->a = x;
}
void ExpandGrid(int fullGrid[WIDTH][HEIGHT], int knownGrid[WIDTH][HEIGHT], int blankPos[2])
{
int neighbors[8][2] = { { 0,1 },{ 1,0 },{ 1,1 },
{ 0,-1 },{ -1,0 },
{ -1,-1 },{ -1,1 },{ 1,-1 } };
int curTile[2];
knownGrid[blankPos[0]][blankPos[1]] = 1;
if (fullGrid[blankPos[0]][blankPos[1]] != 0) return;
for (int blck = 0; blck < 8; ++blck)
{
curTile[0] = blankPos[0] + neighbors[blck][0];
curTile[1] = blankPos[1] + neighbors[blck][1];
if (curTile[0] > WIDTH - 1 || curTile[1] > HEIGHT - 1 || curTile[0] < 0 || curTile[1] < 0) continue;
if (fullGrid[curTile[0]][curTile[1]] == 0 && knownGrid[curTile[0]][curTile[1]] == 0)
{
knownGrid[curTile[0]][curTile[1]] = 1;
ExpandGrid(fullGrid, knownGrid, curTile);
}
else if (fullGrid[curTile[0]][curTile[1]] > 0) knownGrid[curTile[0]][curTile[1]] = 1;
}
}
int main(int argc, char *argv[])
{
COORD characterBufferSize = { WIDTH, HEIGHT };
COORD characterPosition = { 0, 0 };
SMALL_RECT consoleWriteArea = { 0, 0, WIDTH - 1, HEIGHT - 1 };
CHAR_INFO consoleBuffer[WIDTH][HEIGHT];
HANDLE wHnd = GetStdHandle(-11);
HANDLE rHnd = GetStdHandle(-10);
DWORD numEventsRead = 0;
DWORD numEvents = 0;
INPUT_RECORD *eventBuffer = { 0 };
int wait = 1000;
int startGrid[WIDTH][HEIGHT] = { 0 };
int knownGrid[WIDTH][HEIGHT] = { 0 };
int arrowPos[2] = { 0, 0 };
int bomb[2] = { 0 };
struct xorshift_state seed = { argc == 2 ? (int)argv[1] : 1 };
for (int i = 0; i < BOMBS; i++)
{
while (startGrid[bomb[0]][bomb[1]] < -1 || bomb[0] <= 0 || bomb[1] <= 0 || bomb[0] >= WIDTH - 1 || bomb[1] >= HEIGHT - 1)
{
bomb[0] = (xorshift(&seed) % WIDTH - 1) + 1;
bomb[1] = (xorshift(&seed) % HEIGHT - 1) + 1;
}
startGrid[bomb[0]][bomb[1]] = -9;
startGrid[bomb[0] + 1][bomb[1] + 1]++;
startGrid[bomb[0] + 1][bomb[1]]++;
startGrid[bomb[0]][bomb[1] + 1]++;
startGrid[bomb[0] - 1][bomb[1] + 1]++;
startGrid[bomb[0]][bomb[1] - 1]++;
startGrid[bomb[0] + 1][bomb[1] - 1]++;
startGrid[bomb[0] - 1][bomb[1] - 1]++;
startGrid[bomb[0] - 1][bomb[1]]++;
}
while (1)
{
if (arrowPos[0] > WIDTH - 1) arrowPos[0] = WIDTH - 1;
if (arrowPos[0] < 0) arrowPos[0] = 0;
if (arrowPos[1] > HEIGHT - 1) arrowPos[1] = HEIGHT - 1;
if (arrowPos[1] < 0) arrowPos[1] = 0;
for (int x = 0; x < WIDTH; ++x)
{
for (int y = 0; y < HEIGHT; ++y)
{
if (knownGrid[x][y] == 1)
{
if (startGrid[x][y] > 0)
{
consoleBuffer[x][y].Char.AsciiChar = '0' + startGrid[x][y];
consoleBuffer[x][y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
}
else
{
consoleBuffer[x][y].Char.AsciiChar = 'o';
consoleBuffer[x][y].Attributes = (startGrid[x][y] < 0 ? FOREGROUND_RED : FOREGROUND_BLUE) | FOREGROUND_INTENSITY;
}
}
else
{
consoleBuffer[x][y].Char.AsciiChar = '00';
consoleBuffer[x][y].Attributes = 0;
}
if (arrowPos[0] == x && arrowPos[1] == y)
{
consoleBuffer[x][y].Attributes = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN;
}
}
}
WriteConsoleOutput(wHnd, *consoleBuffer, characterBufferSize, characterPosition, &consoleWriteArea);
numEvents = 0;
numEventsRead = 0;
GetNumberOfConsoleInputEvents(rHnd, &numEvents);
if (numEvents)
{
eventBuffer = malloc(sizeof(INPUT_RECORD) * numEvents);
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
}
if (numEventsRead && wait <= 0)
{
wait = 1000;
switch (eventBuffer[0].Event.KeyEvent.wVirtualKeyCode)
{
case 38:
arrowPos[0]--;
break;
case 40:
arrowPos[0]++;
break;
case 37:
arrowPos[1]--;
break;
case 39:
arrowPos[1]++;
break;
case 13:
ExpandGrid(startGrid, knownGrid, arrowPos);
break;
}
}
wait--;
}
}
This does not do what you think it does :
bomb[0] = (xorshift(&seed) % WIDTH - 1) + 1;
From the use of this calculated value right after, you appear to expect this to return a value in the range [1,WIDTH-2].
But :
operator precedence means that (foo % WIDTH - 1) + 1 is the same as ((foo % WIDTH) - 1) + 1. Ie. subtracting 1 is cancelled out by adding 1 again.
left-shifting negative values (as you potentially do in the xorshift function when the seed is/becomes negative) has undefined behavior : ie. anything at all could happen.
right-shifting negative values (as you potentially do in the xorshift function when the seed is/becomes negative) has implementation defined behavior : ie. it could behave differently, depending on the compiler, etc.
modulo operations on negative values are well-defined, and will result in another negative value.
Given all those issues on that one line, I'm not surprised you see strange things happen. Not only have you not properly limited the range of the calculated value, you're also a victim of implementation defined and undefined behavior.
There might well be other issues in the code, but I'd start with these.

How can i solve this issue for showing the grade

**Write the program to calculate the grade
Average of marks
1-59: F
60-69: D
70-79: C
80-89: B
90-100: A
**
```
let marks = [100,100,100];
console.log(calculateGrade(marks));
function calculateGrade(marks) {
var total = 0;
var sum = 0;
for (let total of marks)
sum +=total;
sum /= marks.length;
return (( sum <=59 || sum <=69 ? 'F' : 'D') && ( sum <=79 || sum <=89 ? 'C' : 'B'));
(sum >= 90)console.log('A');
}
console.log(calculateGrade(marks));
function calculateGrade(marks) {
var total = 0;
var sum = 0;
for (let total of marks)
sum +=total;
average = sum / marks.length;
console.log(average);
// return ((((average = 0 && average <=59) && (average = 60 && average <=69) ? 'F' : 'D')) && (((average = 70 && average <=79) && (average = 80 && average <=89) ? 'C' : 'B')));
// else console.log('A');
if ( average < 60) return "F";
if ( average < 70) return "D";
if ( average < 80) return "C";
if ( average < 90) return "B";
return "A";
}

Is my code correctly portraying the diagram?

What my function does, is it takes the values of the profit (function declared as fieldProfit) and the field score (function declared as fieldScore); and if both are above 10, then you earn a badge, hence, innerbadge = 1. BUT, there's also another condition that must be met, the field or (x, y) coordinates, have to fall in the area depicted as the shaded in box that has a hole in the middle. I've written the code for it, and I just wanted to make sure that my logic/syntax is correct! Any help is appreciated!
Here's my code:
int badgeInnerCircle(int x, int y) {
double fprofit, fscore;
int innerbadge;
if ((x >= 1 && x <= 20) && (y >= 1 && y <= 20)) {
if (((x == 7 || x == 8) && (y >= 7 && y <= 14)) || ((x == 13 || x == 14)
&& (y >= 7 && y <= 14)) || ((x >= 7 && x <= 14) && (y == 7 || y == 8))
|| ((x >= 7 && x <= 14) && (y == 13 || y == 14))) {
fprofit = fieldProfit(x, y);
fscore = fieldScore(x, y);
if (fprofit >= 10 && fscore >= 10) {
innerbadge = 1;
}
else {
innerbadge = 0;
}
}
}
else {
innerbadge = -1;
}
return innerbadge;
}
no, your code is not correct.
int innerbadge;
if (condition) {
if (condition) {
if (condition) {
innerbadge = 1;
}
else {
innerbadge = 0;
}
}
//else unidentified!
}
else {
innerbadge = -1;
}
you should change the initialisazion to "int innerbadge = 0;" or something approriate

datagridview and NumericUpDown?

I have a NumericUpDown box and depending on its value, I want to insert the letter into a DataGridView. Here is my code, but it does not insert into the column I want.
if (MarkNumericUpDown.Value < 50)
{
//dataGridView1.Rows.Add("F");
}
else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64)
{
//dataGridView1.Rows.Add("D");
}
else if (MarkNumericUpDown.Value > 64 && MarkNumericUpDown.Value <= 68)
{
//dataGridView1.Rows.Add("D+");
}
else if (MarkNumericUpDown.Value > 68 && MarkNumericUpDown.Value <= 72)
{
//dataGridView1.Rows.Add("C-");
}
else if (MarkNumericUpDown.Value > 72 && MarkNumericUpDown.Value <= 76)
{
//dataGridView1.Rows.Add("C");
}
else if (MarkNumericUpDown.Value > 76 && MarkNumericUpDown.Value <= 80)
{
//dataGridView1.Rows.Add("C+");
}
else if (MarkNumericUpDown.Value > 80 && MarkNumericUpDown.Value <= 84)
{
//dataGridView1.Rows.Add("B-");
}
else if (MarkNumericUpDown.Value > 88 && MarkNumericUpDown.Value <= 92)
{
//dataGridView1.Rows.Add("B");
}
else if (MarkNumericUpDown.Value > 92 && MarkNumericUpDown.Value <= 96)
{
//dataGridView1.Rows.Add("B+");
}
else if (MarkNumericUpDown.Value > 96 && MarkNumericUpDown.Value <= 100)
{
//dataGridView1.Rows.Add("A-");
}
I suspect you are more interested in the Cells values rather than the Rows values. Try something like this:
if (MarkNumericUpDown.Value < 50)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[1].Value = "F";
}
else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[2].Value = "D";
}
Update:
Judging by your picture, it looks like you are only concerned about EDITING a row and not ADDING a row. If this is the case, you need to keep track of which row you are concerned with and which column you are concerned with (please change the variable names to something that makes more sense for your application):
int indexOfRowICareAbout = 0;
int indexOfColumnIStoreLettersIn = 4; //Judging by your picture
if (MarkNumericUpDown.Value < 50)
{
dataGridView1.Rows[indexOfRowICareAbout].Cells[indexOfColumnIStoreLettersIn].Value = "F";
}
else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64)
{
dataGridView1.Rows[indexOfRowICareAbout].Cells[indexOfColumnIStoreLettersIn].Value = "D";
}

Resources