I want to know if:
val1 = val2 = val3 ... = val6
I try this:
if (val1 == val2 == val3 == val4 == val5 == val6)
{
}
but it don't work, why?
The == operator only works between pairs of values. When you do this:
val1 == val2 == val3
What's really happening is this:
(val1 == val2) == val3
So if val1 and val2 are equal, the expression in the parenthesis evaluates to true:
true == val3
And then it checks whether true == val3, not whether val1 or val2 == val3. You have to do this instead:
val1 == val2 && val1 == val3
This is getting pretty unwieldy for six variables though. Do you really have six local variables that you have to compare? Perhaps you should store them in an array of some sort. Then you can do:
bool all_equal(int *vals, int length) {
if (length == 0) {
return true;
}
int first = vals[0];
for (int i=1; i < length; i++) {
if (vals[i] != first) {
return false;
}
}
return true;
}
So instead of:
int val1 = ..., val2 = ..., val3 = ..., val4 = ..., val5 = ..., val6 = ...;
if (val1 == val2 && val2 == val3 && val3 == val4 && val4 == val5 && val5 == val6) {
...
}
You would to:
int vals[6] = {..., ..., ..., ..., ..., ...};
if (all_equal(vals, 6)) {
...
}
You can't chain the == operator. Do this:
if (val1 == val2 && val2 == val3 && val3 == val4 && val4 == val5 && val5 == val6) {
// they are all equal
}
Error Issue is already ably explained by other users, I just wanted to share an another approach.
I written a C code using variable number of arguments: 9.9. Variable numbers of arguments I hope you find it interesting
#include<stdio.h>
#include<stdarg.h>
typedef enum {FALSE, TRUE} boolean;
boolean isAllEqual(int count, ...){
va_list ap; // vlist variable
int num = 0;
boolean flag = TRUE;
//Step2: To initialize `ap` using right-most argument that is `c`
va_start(ap, count);
//Step3: Now access vlist `ap` elements using va_arg()
num = va_arg(ap, int);
while(--count){
if (num != va_arg(ap, int)){
flag = FALSE;
break;
}
}
//Step4: Now work done, we should reset pointer to NULL
va_end(ap);
return flag? TRUE: FALSE;
}
int main(){
if(isAllEqual(5, 2, 2, 2, 2, 2))
printf(" Yes, All are equal\n");
else
printf(" No, All are NOT equal\n");
if(isAllEqual(4, 2, 4, 2, 5))
printf(" Yes, All are equal\n");
else
printf(" No, All are NOT equal\n");
return 0;
}
Output:
Yes, All are equal
No, All are NOT equal
Check codepade.
My boolean isAllEqual(int count, ...) function can check any number of integers, count = number of values you wants to compare. e.g. In isAllEqual(4, 2, 4, 2, 5) first 4 means you wants to compare next four values.
A short theory in four points will help you to understand my code.
I would suggest writing a macro or a function(if it is array with big size or if it is allocated at runtime) in this case.
Related
Currently using Dart with gsheets_api, which don't seem to have a function to convert column letters to numbers (column index)
As an example , this is what I use with AppScript (input: column letter, output: column index number):
function Column_Nu_to_Letter(column_nu)
{
var temp, letter = '';
while (column_nu > 0)
{
temp = (column_nu - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column_nu = (column_nu - temp - 1) / 26;
}
return letter;
};
This is the code I came up for Dart, it works, but I am sure there is a more elegant or correct way to do it.
String colLetter = 'L'; //Column 'L' as example
int c = "A".codeUnitAt(0);
int end = "Z".codeUnitAt(0);
int counter = 1;
while (c <= end) {
//print(String.fromCharCode(c));
if(colLetter == String.fromCharCode(c)){
print('Conversion $colLetter = $counter');
}
counter++;
c++;
}
// this output L = 12
Do you have any suggestions on how to improve this code?
First we need to agree on the meaning of the letters.
I believe the traditional approach is "A" is 1, "Z" is 26, "AA" is 27, "AZ" is 52, "BA" is 53, etc.
Then I'd probably go with something like these functions for converting:
int lettersToIndex(String letters) {
var result = 0;
for (var i = 0; i < letters.length; i++) {
result = result * 26 + (letters.codeUnitAt(i) & 0x1f);
}
return result;
}
String indexToLetters(int index) {
if (index <= 0) throw RangeError.range(index, 1, null, "index");
const _letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (index < 27) return _letters[index - 1];
var letters = <String>[];
do {
index -= 1;
letters.add(_letters[index.remainder(26)]);
index ~/= 26;
} while (index > 0);
return letters.reversed.join("");
}
The former function doesn't validate that the input only contains letters, but it works correctly for strings containing only letters (and it ignores case as a bonus).
The latter does check that the index is greater than zero.
A simplified version base on Irn's answer
int lettersToIndex(String letters) =>
letters.codeUnits.fold(0, (v, e) => v * 26 + (e & 0x1f));
String indexToLetters(int index) {
var letters = '';
do {
final r = index % 26;
letters = '${String.fromCharCode(64 + r)}$letters';
index = (index - r) ~/ 26;
} while (index > 0);
return letters;
}
How can I create function that convert large number into shorten number with character in Dart?
like
1000 => 1K
10000 => 10K
1000000 => 1M
10000000 => 10M
1000000000 => 1B
There is a built-in function in Dart that can be used and it's simple:
var f = NumberFormat.compact(locale: "en_IN");
print(f.format(12345));
to make it a method:
getShortForm(var number) {
var f = NumberFormat.compact(locale: "en_US");
return f.format(number);
}
for this to work import
import 'package:intl/intl.dart';
Refer to this doc for more https://pub.dev/documentation/intl/latest/intl/NumberFormat-class.html
If you are looking for a hard way:
getShortForm(int number) {
var shortForm = "";
if (number != null) {
if (number < 1000) {
shortForm = number.toString();
} else if (number >= 1000 && number < 1000000) {
shortForm = (number / 1000).toStringAsFixed(1) + "K";
} else if (number >= 1000000 && number < 1000000000) {
shortForm = (number / 1000000).toStringAsFixed(1) + "M";
} else if (number >= 1000000000 && number < 1000000000000) {
shortForm = (number / 1000000000).toStringAsFixed(1) + "B";
}
}
return shortForm;
}
String toString(int value) {
const units = <int, String>{
1000000000: 'B',
1000000: 'M',
1000: 'K',
};
return units.entries
.map((e) => '${value ~/ e.key}${e.value}')
.firstWhere((e) => !e.startsWith('0'), orElse: () => '$value');
}
A simpler approach, if you only need the suffix. It may not be compiling, but this is the idea.
String getSuffix (int t)
{
int i = -1;
for ( ; (t /= 1000) > 0 ; i++ );
return ['K','M','B'][i];
}
Edit
This is the mathematical way to do it, and it compiles. The point is you are searching for the amount of "groups of 3 decimal" places:
x 000 - 1
x 000 000 - 2
and so on. Which is log1000 number.
String getSuffix (int num)
{
int i = ( log(num) / log(1000) ).truncate();
return (num / pow(1000,i)).truncate().toString() + [' ','K','M','B'][i];
}
The Intl package does this as "compact" numbers, but it has a fixed format and it will also change with different locales, which might or might not be what you want.
Make a class and used its static method every where.
class NumberFormatter{
static String formatter(String currentBalance) {
try{
// suffix = {' ', 'k', 'M', 'B', 'T', 'P', 'E'};
double value = double.parse(currentBalance);
if(value < 1000){ // less than a thousand
return value.toStringAsFixed(2);
}else if(value >= 1000 && value < (1000*100*10)){ // less than a million
double result = value/1000;
return result.toStringAsFixed(2)+"k";
}else if(value >= 1000000 && value < (1000000*10*100)){ // less than 100 million
double result = value/1000000;
return result.toStringAsFixed(2)+"M";
}else if(value >= (1000000*10*100) && value < (1000000*10*100*100)){ // less than 100 billion
double result = value/(1000000*10*100);
return result.toStringAsFixed(2)+"B";
}else if(value >= (1000000*10*100*100) && value < (1000000*10*100*100*100)){ // less than 100 trillion
double result = value/(1000000*10*100*100);
return result.toStringAsFixed(2)+"T";
}
}catch(e){
print(e);
}
}
}
I made a Tic-Tac-Toe game in Ruby. The method below checks for a winner in the vertical columns.
How do I make it so that this method can be applied to boards of different sizes, like 4x4, 6x6?
def vertical_check(array)
result = nil
if (array[0][0] == "X" && array[1][0] == "X" && array[2][0] == "X") ||
(array[0][1] == "X" && array[1][1] == "X" && array[2][1] == "X") ||
(array[0][2] == "X" && array[1][2] == "X" && array[2][2] == "X")
result = "X"
elsif (array[0][0] == "O" && array[1][0] == "O" && array[2][0] == "O") ||
(array[0][1] == "O" && array[1][1] == "O" && array[2][1] == "O") ||
(array[0][2] == "O" && array[1][2] == "O" && array[2][2] == "O")
result = "O"
else
result = nil
end
return result
end
The following is a failed attempt:
def vertical_check_x(array)
result = nil
index = 0
index2 = 0
until result != nil || index == array.length
while array[index][index2] == "X"
index += 1
end
if index == array.length
result = "X"
else
result = nil
index = array.length
end
index2 += 1
end
return result
end
def vertical_check_o(array)
result = nil
index = 0
index2 = 0
until result != nil || index == array.length
while array[index][index2] == "O"
index += 1
end
if index -1 == array.length
result = "O"
else
result = nil
index = array.length
end
index2 += 1
end
return result
end
def vertical_check(array)
result = vertical_check_x(array)
if result == nil
result = vertical_check_o(array)
end
return result
end
To quickly find a winner in given array, count the number of unique elements, confirm that there is only one unique element and if it is only X or O:
def winner arr
return arr[0] if arr.uniq.length == 1 && ['X', 'O'].include?(arr[0])
nil
end
The next problem is selecting the rows, columns and diagonals for an nxn array.
Rows are easy:
rows = arr.map {|row| row}
Columns are as follows - you select elements with the same index for each row:
cols = n.times.collect {|i| arr.map {|row| row[i]}}
Next are diagonals. There are two diagonals, one starts from leftmost corner and the other from the rightmost.
The leftmost diagonal has the sequence as:
(0, 0) -> (1, 1) -> (2, 2) ....
See the pattern?
diag = n.times.collect {|i| arr[i][i]}
The rightmost diagonal has pattern that goes like this (for a 3x3):
(0, 2) -> (1, 1) -> (2, 0)
For a 4x4, it's like this:
(0, 3) -> (1, 2) -> (2, 1) -> (3, 0)
So, the pattern for an nxn is:
(0, n-1-0) -> (1, n-1-1) -> (2, n-1-2) -> ... (i, n-1-i) ... -> (n-1, 0)
So:
diag = n.times.collect {|i| arr[i][n - 1 - i]}
Now, you can just do something like:
w = rows.map {|r| winner r}.compact[0]
for each array to get the winner.
For example I want to declare
let len, (*mutable*) i =
if s.Length >= 2 && s.[0] = '0' && (s.[1] = 'x' || s.[1] = 'X') then
(s.Length - 2, 2)
else (s.Length, 0)
constant binding len and mutable i, is it possible ?
Added :
I will use references then
let len, i =
if s.Length >= 2 && s.[0] = '0' && (s.[1] = 'x' || s.[1] = 'X') then
(s.Length - 2, ref 2)
else (s.Length, ref 0)
No. mutable applies to the entire let binding. You'll have to do:
let len, i = ...
let mutable i = i
im having a problem with scanf and gets. and I kno that its bound to errors but I couldn't find any other way. This way, the name is printing out but It doesn't print out the first letter of it.
Here's my code:
#include <stdio.h>
float calculations(int age, float highBP, float lowBP);
char option;
int counter, age;
char temp_name[50];
float highBP, lowBP, riskF, optimalH = 120.0, optimalL = 80.0;
typedef struct {
char name[50]; /*which represents the patient’s name*/
int age; /*which represents the patient’s age*/
float highBP; /*highBP, which represents the patient’s high (systolic) blood pressure*/
float lowBP; /*lowBP, which represents the patient’s low (diastolic) blood pressure*/
float riskF; /*riskFactor, which represents the patient’s risk factor for stroke due to hypertension.*/
}patient;/*end structure patient*/
patient *pRecords[30];
void printMenu()
{
printf("\n---------------------------------------------------------\n");
printf("|\t(N)ew record\t(D)isplay db\t(U)pdate record\t|\n");
printf("|\t(L)oad disk\t(W)rite disk\t(E)mpty disk\t|\n");
printf("|\t(S)ort db\t(C)lear db\t(Q)uit \t\t|\n");
printf("---------------------------------------------------------\n");
printf("choose one:");
}/*end print menu*/
void enter()
{
if(counter == 30)
printf("database full.");
else{
printf("name: ");
while(getchar()=='\n');
gets(temp_name);
strcpy(pRecords[counter]->name , temp_name);
printf("age: "); scanf("%d", &age);
pRecords[counter]->age = age;
printf("highBP: "); scanf("%f", &highBP);
pRecords[counter]->highBP = highBP;
printf("lowBP: "); scanf("%f", &lowBP);
pRecords[counter]->lowBP = lowBP;
float temp = calculations(age, highBP,lowBP);
pRecords[counter]->riskF = temp;
/*printf("name: %s, age: %d, highbp:%.1f, lowBP:%.1f\n", pRecords[counter]->name,pRecords[counter]->age,pRecords[counter]->highBP,pRecords[counter]->lowBP);
printf("risk factor: %.1f\n", pRecords[counter]->riskF);*/
counter ++;
}
}/*end of void enter function*/
memallocate(int counter){
pRecords[counter] = (patient *)malloc (sizeof(patient));
}/*end memallocate function*/
void display()
{
printf("===============================\n");
int i;
for(i=0; i<counter; i++)
{
printf("name: %s\n", pRecords[i]->name);
printf("age: %d\n", pRecords[i]->age);
printf("bp: %.2f %.2f\n", pRecords[i]->highBP, pRecords[i]->lowBP);
printf("risk: %.2f\n\n", pRecords[i]->riskF);
}/*end of for loop*/
printf("========== %d records ==========", counter);
}/*end of display method*/
float calculations(int age, float highBP, float lowBP)
{ float risk;
if((highBP <= optimalH) && (lowBP <= optimalL))
{ risk = 0.0;
if(age >=50)
risk = 0.5;
}
else if(highBP <= optimalH && (lowBP>optimalL && lowBP <=(optimalL+10)))
{ risk= 1.0;
if(age >=50)
risk = 1.5;
}
else if ((highBP >optimalH && highBP <= (optimalH+10))&& lowBP <=optimalL)
{ risk= 1.0;
if(age >=50)
risk= 1.5;
}
else if((highBP > optimalH && highBP <=(optimalH+10)) && (lowBP >optimalL && lowBP <= (optimalL+10)))
{ risk= 2.0;
if(age >=50)
risk = 2.5;
}
else if(highBP < optimalH && (lowBP >(optimalL+11) && lowBP<(optimalL+20)))
{ risk = 3.0;
if(age >=50)
risk = 3.5;
}
else if((lowBP < optimalL) && (highBP >(optimalH+11) && highBP<(optimalH+20)))
{ risk = 3.0;
if(age >=50)
risk = 3.5;
}
else if((highBP>=(optimalH+11) && highBP <= (optimalH+20))&& (lowBP>=(optimalL+11) && lowBP<=(optimalL+20)))
{ risk = 4.0;
if(age >=50)
risk = 4.5;
}
else
{ risk = 5.0;
if(age >=50)
risk = 5.5;
}
return risk;
}/*end of calculation function*/
main()
{
printMenu();
char option=getchar();
while(option != 'q' || option != 'Q'){
if(option == 'N' || option == 'n')
{
memallocate(counter);
enter();
printMenu();
}
if (option == 'L' || option == 'l')
{
printMenu();
}
if(option == 'S' || option == 's')
{
printMenu();
}
if(option == 'D' || option == 'd')
{
display();
printMenu();
}
if(option == 'W' || option == 'w')
{
printMenu();
}
if(option == 'C' || option == 'c')
{
printMenu();
}
if(option == 'U' || option == 'u')
{
printMenu();
}
if(option == 'E' || option == 'e')
{
printMenu();
}
if(option == 'Q' || option == 'q')
{
exit(0);
}
option = getchar();
}/*end while*/
system("pause");
}/*end main*/
sample output:
---------------------------------------------------------
| (N)ew record (D)isplay db (U)pdate record |
| (L)oad disk (W)rite disk (E)mpty disk |
| (S)ort db (C)lear db (Q)uit |
---------------------------------------------------------
choose one: n
name: judy
age: 30
high bp: 110
low bp: 88
3
---------------------------------------------------------
| (N)ew record (D)isplay db (U)pdate record |
| (L)oad disk (W)rite disk (E)mpty disk |
| (S)ort db (C)lear db (Q)uit |
---------------------------------------------------------
choose one: n
name: cindy white
age: 52
high bp: 100.7
low bp: 89.4
---------------------------------------------------------
| (N)ew record (D)isplay db (U)pdate record |
| (L)oad disk (W)rite disk (E)mpty disk |
| (S)ort db (C)lear db (Q)uit |
---------------------------------------------------------
choose one: d
===============================
name: udy
age: 30
bp: 110.00 88.00
risk: 1.0
name: indy white
age: 52
bp: 100.70 89.40
risk: 1.5
========== 2 records ==========
Your while loop and use of gets() is generally not good practice.
Try something like:
fflush(stdin);
fgets(pRecords[counter]->name, sizeof(pRecords[counter]->name), stdin);
Try
if (strlen(pRecords[counter]->name) > 0)
{
pRecords[counter]->name[strlen(pRecords[counter]->name) - 1] = '\0';
}
You lose the first character to while(getchar()=='\n');. I don't know why that statement is necessary, but it loops until it gets a character that is not '\n' (which is 'j' and 'c' in your case).
while (getchar() == '\n');
This line eats the newlines plus one character. When getchar() does not return a newline, it has already consumed the first character.
Look at ungetc() to write that character back onto the stream.
This:
while(getchar()=='\n');
loops until it gets a non-newline, which will be the first character of the name.
Try this instead:
do
c = getchar();
while(c == '\n');
ungetc(c, stdin);