integer

C++ read integers from file and save into array

↘锁芯ラ 提交于 2020-12-26 20:20:12
问题 I'm making a program that reads only integers from text file. I want to make a function that reads integers and stores them in an array so I can use that array later to sort them with bubble sort. This is what I have so far but the output I get is some random -803234.... number : void read(int A[max], int& numbers) { ifstream file("ints.txt"); if (file.is_open()) { file >> numbers; for (int i = 0; i < numbers; i++) { cout << "numbers: " << A[i] << endl; } file.close(); } cout << "numbers in

C++ read integers from file and save into array

一世执手 提交于 2020-12-26 20:19:22
问题 I'm making a program that reads only integers from text file. I want to make a function that reads integers and stores them in an array so I can use that array later to sort them with bubble sort. This is what I have so far but the output I get is some random -803234.... number : void read(int A[max], int& numbers) { ifstream file("ints.txt"); if (file.is_open()) { file >> numbers; for (int i = 0; i < numbers; i++) { cout << "numbers: " << A[i] << endl; } file.close(); } cout << "numbers in

printing the double number wrongly

只愿长相守 提交于 2020-12-26 09:15:06
问题 I want to write code that, if I input a decimal number like 612.216, I can print it as a 612216 (actually convert it to integer). However, the program changes my number to something like 2162160000000000000000001 and I don't what to do about it. This is my code: #include <stdio.h> #include <math.h> int main() { long double x; scanf_s("%Lf", &x); while (floor(x)!=x) x = x * 10; printf("%Lf", x); return 0;} 回答1: How about this: #include <stdio.h> int main() { double number = 612.216; char

printing the double number wrongly

隐身守侯 提交于 2020-12-26 09:14:11
问题 I want to write code that, if I input a decimal number like 612.216, I can print it as a 612216 (actually convert it to integer). However, the program changes my number to something like 2162160000000000000000001 and I don't what to do about it. This is my code: #include <stdio.h> #include <math.h> int main() { long double x; scanf_s("%Lf", &x); while (floor(x)!=x) x = x * 10; printf("%Lf", x); return 0;} 回答1: How about this: #include <stdio.h> int main() { double number = 612.216; char

printing the double number wrongly

纵然是瞬间 提交于 2020-12-26 09:12:45
问题 I want to write code that, if I input a decimal number like 612.216, I can print it as a 612216 (actually convert it to integer). However, the program changes my number to something like 2162160000000000000000001 and I don't what to do about it. This is my code: #include <stdio.h> #include <math.h> int main() { long double x; scanf_s("%Lf", &x); while (floor(x)!=x) x = x * 10; printf("%Lf", x); return 0;} 回答1: How about this: #include <stdio.h> int main() { double number = 612.216; char

How to split() a string to an array of integers

怎甘沉沦 提交于 2020-12-25 00:33:46
问题 I have a string that should be executed as an array: var q = "The, 1, 2, Fox, Jumped, 3, Over, 4"; var z = q.split(','); If I use split() , it will create an array of strings: [‘The’, '1', '2', ‘Fox’, ‘Jumped’, '3', ‘Over’, '4'] and I don’t need that. I need an array like: [‘The’, 1, 2, ‘Fox’, ‘Jumped’, 3, ‘Over’, 4] indicating which is a string and which is a number. 回答1: One option is using the Number constructor which returns a number or NaN : var res = q.split(',').map(el => { let n =

How to split() a string to an array of integers

北慕城南 提交于 2020-12-25 00:32:44
问题 I have a string that should be executed as an array: var q = "The, 1, 2, Fox, Jumped, 3, Over, 4"; var z = q.split(','); If I use split() , it will create an array of strings: [‘The’, '1', '2', ‘Fox’, ‘Jumped’, '3', ‘Over’, '4'] and I don’t need that. I need an array like: [‘The’, 1, 2, ‘Fox’, ‘Jumped’, 3, ‘Over’, 4] indicating which is a string and which is a number. 回答1: One option is using the Number constructor which returns a number or NaN : var res = q.split(',').map(el => { let n =

How to split() a string to an array of integers

大憨熊 提交于 2020-12-25 00:31:38
问题 I have a string that should be executed as an array: var q = "The, 1, 2, Fox, Jumped, 3, Over, 4"; var z = q.split(','); If I use split() , it will create an array of strings: [‘The’, '1', '2', ‘Fox’, ‘Jumped’, '3', ‘Over’, '4'] and I don’t need that. I need an array like: [‘The’, 1, 2, ‘Fox’, ‘Jumped’, 3, ‘Over’, 4] indicating which is a string and which is a number. 回答1: One option is using the Number constructor which returns a number or NaN : var res = q.split(',').map(el => { let n =

Small vs. identical types of loop variables in C/C++ for performance

与世无争的帅哥 提交于 2020-12-15 07:20:24
问题 Say I have a large nested loop of the form long long i, j, k, i_end, j_end; ... for (i = 0; i < i_end; i++) { j_bgn = get_j_bgn(i); for (j = j_bgn; j < j_end; j++) { ... } } with some large i_end and j_end , say i_end = j_end = 10000000000 . If I know that j_bgn is always small, perhaps even always either 0 or 1 , is it beneficial performance-wise to use a smaller type for this, like signed char j_bgn ? Or does this come with a recurring cost due to implicit casting to long long each time we

Converting each line from a list of integers into an array of integers?

淺唱寂寞╮ 提交于 2020-12-15 06:16:34
问题 I'm new to Java, so apologies if any of this is unclear - I want to convert a list of four-digit integers into a list of integer arrays - i.e if I have an integer that is 4567, I want to convert that into an array of four separate integers [4, 5, 6, 7]. So I want to convert each line/index of the list into it's own array (rather than converting the entire list into an array). I'm currently reading a file that has four-digit integers (each on a new line) and adding them to a list, which is