Strings giving weird values - c++

I have to get a string of numbers of 2 digits separated by spaces, take the numbers out somehow and do operations with them. But the values are all going wrong, see for yourselves:
...
string = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08"; //example
for (int i = 0; i < string.size()-2; i += 3){
arr[i] = 10 * string[i] + string[i+1];
cout << arr[i] <<' '<< 10 * string[i] <<' '<< string[i] << string[i+1] <<endl;
}
output:
536 480 08 i guess 0 times 10 isn't 480 as well as that plus 8 equals 536
530 480 02
550 500 22
625 570 97
566 510 38
543 490 15
528 480 00
568 520 40
528 480 00
603 550 75
532 480 04
533 480 05
535 480 07
606 550 78
580 530 52
540 490 12
578 530 50
605 550 77
619 570 91
536 480 08

You're doing your calculation with the ASCII values of the characters, not the numeric values of the digits. E.g. '0' is 48, '1' is 49, and so on.
for (int i = 0; i < string.size()-2; i += 3){
arr[i] = 10 * (string[i]-'0') + (string[i+1]-'0');
cout << arr[i] <<' '<< 10 * string[i] <<' '<< string[i] << string[i+1] <<endl;
}

You're misunderstanding the int values you're retrieving. Just as an example, the 08 you're pulling.
'0' is not 0, it's 48
'8' is not 8, it's 56
48 * 10 + 56 = 536
instead of taking the ASCII representation of a character and using that in your calculations, use atoi.
Also, instead of going char by char you might want to look into the many ways to split a string of text in C++.

Converting a character to an integer gives you the numeric value that represents the character; for the numeric digits, that's not the same as the value of the digit. Just about every computer you'll find these days uses ASCII coding, where '0' is represented by the value 48, '1' by 49, and so on.
You want to subtract the value that represents '0' so that '0' becomes 0, '1' becomes 1, and so on:
arr[i] = 10 * (string[i] - '0') + (string[i+1] - '0');

You could use the stoi()(string to integer) to convert your string to numerical values and then perform your operations.

Related

Can't find the right max Value in my Array loop

So my professor gave us this project to read a text file and find the max value, min value, sum. But for some reason when I write the for loop to find the max value it returns a number thats not even in the text file... and I don't know what i did wrong. I'll attach my code and also the output. Thank you
int main () {
ifstream myFile;
char myArray[210];
int i;
int maxVal;
int j;
int minValue;
double myAverage;
myFile.open("Lab #5A Data File.Txt", ios::in | ios::out);
if (myFile.is_open()) {
cout << "The file is open." << endl;
myFile >> noskipws;
while (!myFile.eof()){
for (i=0; i<210; ++i) {
myFile >> myArray[i];
cout << myArray[i];
}
myFile >>myArray[i];
}
maxVal=myArray[0];
for (j=0; j< 210; j++)
if (myArray[j] > maxVal){
maxVal=myArray[j];
}
What i get when I run the code :
The file is open.
346 130 982 90 656 117 595
415 948 126 4 558 571 87
42 360 412 721 463 47 119
441 190 985 214 509 2 571
77 81 681 651 995 93 74
310 9 995 561 92 14 288
466 664 892 8 766 34 639
151 64 98 813 67 834 369
The max value is: 51 <--- I have no idea where this number came from...
The 51 is coming from the line:
maxVal=myArray[0];
In your loop to try to find the biggest element you have:
for (j=0; j< 210; j++)
if (myArray[j] > maxVal){
myArray[i]=maxVal;
}
}
However this will assign maxVal to myArray[i] which is not what you want. First of all you need to be assigning myArray[j], not myArray[i], and secondly you need to assign maxVal to the bigger value. As it is maxVal=myArray[0]; is the only time you assign anything to maxVal, which is why it is 51 (The ASCII value of the character 3, which is the first character you read). You need to do something along the lines of:
if (myArray[j] > maxVal){
maxVal = myArray[j];
}
I believe you wanted myArray to be an int[]. Also a better way of doing this is instead of having two for loops and looping until EOF, loop while myFile >> myArray[i]:
int myArray[210];
int i = 0;
//...
while (myFile >> myArray[i]) {
cout << myArray[i] << " ";
if (myArray[i] > maxVal) {
maxVal = myArray[i];
}
i++;
}
Which for the input file:
346 130 982 90 656 117 595
415 948 126 4 558 571 87 42
360 412 721 463 47 119 441
190 985 214 509 2 571 77 81
681 651 995 93 74 310 9 995
561 92 14 288 466 664 892 8
766 34 639 151 64 98 813 67 834 369
Returns:
995
To achieve what you want, you cannot do a comparison like this
if (myArray[j] > maxVal){
because myArray[j] is a char (definitely not holding the integers you are interested in) and maxVal is an int. This is also the reason you see a 51 - when you try to store an integer into your char, you essentially only read 8 bits from the stream (which results in some value between 0 and 254 which is basically just some 8-bit block from your input stream).
You definitely want something like
char myArray[32][210];
to be able to read your full integers from the stream into one of these 210 char* slots.
Then, when comparing (and assigning to maxValue), you need to convert the textual int value to a numeric value, e.g., using atoi().

OpenMP integral image slower then sequential

I have implemented Summed Area Table (or Integral image) in C++ using OpenMP.
The problem is that the Sequential code is always faster then the Parallel code even changing the number of threads and image sizes.
For example I tried images from (100x100) to (10000x10000) and threads from 1 to 64, but none of the combination is ever faster.
I also tried this code in different machines like:
Mac OSX 1,4 GHz Intel Core i5 dual core
Mac OSX 2,3 GHz Intel Core i7 quad core
Ubuntu 16.04 Intel Xeon E5-2620 2,4 GHz 12 cores
The time has been measured with OpenMP function: omp_get_wtime().
For compiling I use: g++ -fopenmp -Wall main.cpp.
Here is the parallel code:
void transpose(unsigned long *src, unsigned long *dst, const int N, const int M) {
#pragma omp parallel for
for(int n = 0; n<N*M; n++) {
int i = n/N;
int j = n%N;
dst[n] = src[M*j + i];
}
}
unsigned long * integralImageMP(uint8_t*x, int n, int m){
unsigned long * out = new unsigned long[n*m];
unsigned long * rows = new unsigned long[n*m];
#pragma omp parallel for
for (int i = 0; i < n; ++i)
{
rows[i*m] = x[i*m];
for (int j = 1; j < m; ++j)
{
rows[i*m + j] = x[i*m + j] + rows[i*m + j - 1];
}
}
transpose(rows, out, n, m);
#pragma omp parallel for
for (int i = 0; i < n; ++i)
{
rows[i*m] = out[i*m];
for (int j = 1; j < m; ++j)
{
rows[i*m + j] = out[i*m + j] + rows[i*m + j - 1];
}
}
transpose(rows, out, m, n);
delete [] rows;
return out;
}
Here is the sequential code:
unsigned long * integralImage(uint8_t*x, int n, int m){
unsigned long * out = new unsigned long[n*m];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
unsigned long val = x[i*m + j];
if (i>=1)
{
val += out[(i-1)*m + j];
if (j>=1)
{
val += out[i*m + j - 1] - out[(i-1)*m + j - 1];
}
} else {
if (j>=1)
{
val += out[i*m + j -1];
}
}
out[i*m + j] = val;
}
}
return out;
}
I also tried without the transpose but it was even slower probably because the cache accesses.
An example of calling code:
int main(int argc, char **argv){
uint8_t* image = //read image from file (gray scale)
int height = //height of the image
int width = //width of the image
double start_omp = omp_get_wtime();
unsigned long* integral_image_parallel = integralImageMP(image, height, width); //parallel
double end_omp = omp_get_wtime();
double time_tot = end_omp - start_omp;
std::cout << time_tot << std::endl;
start_omp = omp_get_wtime();
unsigned long* integral_image_serial = integralImage(image, height, width); //sequential
end_omp = omp_get_wtime();
time_tot = end_omp - start_omp;
std::cout << time_tot << std::endl;
return 0;
}
Each thread is working on a block of rows (maybe an illustration of what each thread is doing can be useful):
Where ColumnSum is done transposing the matrix and repeating RowSum.
Let me first say, that the results are a bit surprising to me and I would guesstimate the problem being in the non local memory access required by the transpose algorithm.
You can anyway mitigate it by turning your sequential algorithm into parallel by a two pass approach. The first pass has to calculate the 2D integral in T threads N rows apart and the second pass must compensate the fact that each block didn't start from the accumulated result of the previous row but from zero.
An example with Matlab shows the principle in 2D.
f=fix(rand(12,8)*8) % A random matrix with 12 rows, 8 columns
5 6 1 4 7 5 4 4
4 6 0 7 1 3 2 0
7 0 2 3 0 1 6 3
5 3 1 7 4 3 7 2
6 4 3 2 7 3 5 1
3 3 2 5 5 0 2 1
3 5 7 5 1 4 4 3
6 5 7 4 2 1 0 0
0 2 0 5 3 3 7 4
1 3 5 5 7 4 7 3
1 0 2 1 1 2 6 5
3 7 3 1 6 2 2 5
ff=cumsum(cumsum(f')') % The Summed Area Table
5 11 12 16 23 28 32 36
9 21 22 33 41 49 55 59
16 28 31 45 53 62 74 81
21 36 40 61 73 85 104 113
27 46 53 76 95 110 134 144
30 52 61 89 113 128 154 165
33 60 76 109 134 153 183 197
39 71 94 131 158 178 208 222
39 73 96 138 168 191 228 246
40 77 105 152 189 216 260 281
41 78 108 156 194 223 273 299
44 88 121 170 214 245 297 328
fx=[cumsum(cumsum(f(1:4,:)')'); % The original table summed in
cumsum(cumsum(f(5:8,:)')'); % three parts -- 4 rows per each
cumsum(cumsum(f(9:12,:)')')] % "thread"
5 11 12 16 23 28 32 36
9 21 22 33 41 49 55 59
16 28 31 45 53 62 74 81
21 36 40 61 73 85 104 113 %% Notice this row #4
6 10 13 15 22 25 30 31
9 16 21 28 40 43 50 52
12 24 36 48 61 68 79 84
18 35 54 70 85 93 104 109 %% Notice this row #8
0 2 2 7 10 13 20 24
1 6 11 21 31 38 52 59
2 7 14 25 36 45 65 77
5 17 27 39 56 67 89 106
fx(4,:) + fx(8,:) %% this is the SUM of row #4 and row #8
39 71 94 131 158 178 208 222
%% and finally -- what is the difference of the piecewise
%% calculated result and the real result?
ff-fx
0 0 0 0 0 0 0 0 %% look !! the first block
0 0 0 0 0 0 0 0 %% is already correct
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
21 36 40 61 73 85 104 113 %% All these rows in this
21 36 40 61 73 85 104 113 %% block are short by
21 36 40 61 73 85 104 113 %% the row #4 above
21 36 40 61 73 85 104 113 %%
39 71 94 131 158 178 208 222 %% and all these rows
39 71 94 131 158 178 208 222 %% in this block are short
39 71 94 131 158 178 208 222 %% by the SUM of the rows
39 71 94 131 158 178 208 222 %% #4 and #8 above
Fortunately one can start integrating the block 2, i.e. rows 2N..3N-1 before the block #1 has been compensated -- one just has to calculate the offset, which is a relatively small sequential task.
acc_for_block_2 = row[2*N-1] + row[N-1];
acc_for_block_3 = acc_for_block_2 + row[3*N-1];
..
acc_for_block_T-1 = acc_for_block_(T-2) + row[N*(T-1)-1];

Next higher number with one zero bit

Today I've run into this problem, but I couldn't solve it after a period of time. I need some help
I have number N. The problem is to find next higher number ( > N ) with only one zero bit in binary.
Example:
Number 1 can be represented in binary as 1.
Next higher number with only one zero bit is 2 - Binary 10
A few other examples:
N = 2 (10), next higher number with one zero bit is 5 (101)
N = 5 (101), next higher number is 6 (110)
N = 7 (111), next higher number is 11 (1011)
List of 200 number:
1 1
2 10 - 1
3 11
4 100
5 101 - 1
6 110 - 1
7 111
8 1000
9 1001
10 1010
11 1011 - 1
12 1100
13 1101 - 1
14 1110 - 1
15 1111
16 10000
17 10001
18 10010
19 10011
20 10100
21 10101
22 10110
23 10111 - 1
24 11000
25 11001
26 11010
27 11011 - 1
28 11100
29 11101 - 1
30 11110 - 1
31 11111
32 100000
33 100001
34 100010
35 100011
36 100100
37 100101
38 100110
39 100111
40 101000
41 101001
42 101010
43 101011
44 101100
45 101101
46 101110
47 101111 - 1
48 110000
49 110001
50 110010
51 110011
52 110100
53 110101
54 110110
55 110111 - 1
56 111000
57 111001
58 111010
59 111011 - 1
60 111100
61 111101 - 1
62 111110 - 1
63 111111
64 1000000
65 1000001
66 1000010
67 1000011
68 1000100
69 1000101
70 1000110
71 1000111
72 1001000
73 1001001
74 1001010
75 1001011
76 1001100
77 1001101
78 1001110
79 1001111
80 1010000
81 1010001
82 1010010
83 1010011
84 1010100
85 1010101
86 1010110
87 1010111
88 1011000
89 1011001
90 1011010
91 1011011
92 1011100
93 1011101
94 1011110
95 1011111 - 1
96 1100000
97 1100001
98 1100010
99 1100011
100 1100100
101 1100101
102 1100110
103 1100111
104 1101000
105 1101001
106 1101010
107 1101011
108 1101100
109 1101101
110 1101110
111 1101111 - 1
112 1110000
113 1110001
114 1110010
115 1110011
116 1110100
117 1110101
118 1110110
119 1110111 - 1
120 1111000
121 1111001
122 1111010
123 1111011 - 1
124 1111100
125 1111101 - 1
126 1111110 - 1
127 1111111
128 10000000
129 10000001
130 10000010
131 10000011
132 10000100
133 10000101
134 10000110
135 10000111
136 10001000
137 10001001
138 10001010
139 10001011
140 10001100
141 10001101
142 10001110
143 10001111
144 10010000
145 10010001
146 10010010
147 10010011
148 10010100
149 10010101
150 10010110
151 10010111
152 10011000
153 10011001
154 10011010
155 10011011
156 10011100
157 10011101
158 10011110
159 10011111
160 10100000
161 10100001
162 10100010
163 10100011
164 10100100
165 10100101
166 10100110
167 10100111
168 10101000
169 10101001
170 10101010
171 10101011
172 10101100
173 10101101
174 10101110
175 10101111
176 10110000
177 10110001
178 10110010
179 10110011
180 10110100
181 10110101
182 10110110
183 10110111
184 10111000
185 10111001
186 10111010
187 10111011
188 10111100
189 10111101
190 10111110
191 10111111 - 1
192 11000000
193 11000001
194 11000010
195 11000011
196 11000100
197 11000101
198 11000110
199 11000111
200 11001000
There are three cases.
The number x has more than one zero bit in its binary representation. All but one of these zero bits must be "filled in" with 1 to obtain the required result. Notice that all numbers obtained by taking x and filling in one or more of its low-order zero bits are numerically closer to x compared to the number obtained by filling just the top-most zero bit. Therefore the answer is the number x with all-but-one of its zero bits filled: only its topmost zero bit remains unfilled. For example if x=110101001 then the answer is 110111111. To get the answer, find the index i of the topmost zero bit of x, and then calculate the bitwise OR of x and 2^i - 1.
C code for this case:
// warning: this assumes x is known to have *some* (>1) zeros!
unsigned next(unsigned x)
{
unsigned topmostzero = 0;
unsigned bit = 1;
while (bit && bit <= x) {
if (!(x & bit)) topmostzero = bit;
bit <<= 1;
}
return x | (topmostzero - 1);
}
The number x has no zero bits in binary. It means that x=2^n - 1 for some number n. By the same reasoning as above, the answer is then 2^n + 2^(n-1) - 1. For example, if x=111, then the answer is 1011.
The number x has exactly one zero bit in its binary representation. We know that the result must be strictly larger than x, so x itself is not allowed to be the answer. If x has the only zero in its least-significant bit, then this case reduces to case #2. Otherwise, the zero should be moved one position to the right. Assuming x has zero in its i-th bit, the answer should have its zero in i-1-th bit. For example, if x=11011, then the result is 11101.
You could also use another approach:
Every number with exactly one zero bit can be represented as
2^n - 1 - 2^m
Now the task is easy:
1. Find an n, great enough for at least 2^n-1-2^0>x, that's equivalent to 2^n>x+2
2. Find the greatest m for which 2^n-1-2^m is still greater than x.
as Code:
#include <iostream>
#include <math.h>
using namespace std;
//binary representation
void bin(unsigned n)
{
for (int i = floor(log2(n));i >= 0;--i)
(n & (1<<i))? printf("1"): printf("0");
}
//outputs the next greater int to x with exactly one 0 in binary representation
int nextHigherOneZero(int x)
{
unsigned int n=0;
while((1<<n)<= x+2 ) ++n;
unsigned int m=0;
while((1<<n)-1-(1<<(m+1)) > x && m<n-2)
++m;
return (1<<n)-1-(1<<m);
}
int main()
{
int r=0;
for(int i = 1; i<100;++i){
r=nextHigherOneZero(i);
printf("\nX: %i=",i);
bin(i);
printf(";\tnextHigherOneZero(x):%i=",r);
bin(r);
printf("\n");
}
return 0;
}
You can try it here (with some additional Debug-Output):
http://ideone.com/6w3fAN
As a note: its probably possible to get m and n faster with some good binary logic, feel free to contribute...
Pro of this approach:
No assumptions needs to be made
Cons:
Ugly while loops
couldn't miss the opportunity to remember binary logic :), here's my solution:
here's main
main(int argc, char** argv)
{
int i = 139261;
i++;
while (!oneZero(i))
{
i++;
}
std::cout << i;
}
and here's all logic to find if number has 1 zero
bool oneZero(int i)
{
int count = 0;
while (i != 0)
{
// check last bit if it is zero
if ((1 & i) == 0) {
count++;
if (count > 1) return false;
}
// make the number shorter :)
i = i >> 1;
}
return (count == 1);
}

looking for the most accurate to sort double numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to sort small float number.
When I use std::sort() //algorithm library,
I found that it's inaccurate in case of very very small numbers.
How can I sort this array in most accurate way?
edit : my friend suggested to me this lines of code which i don't understand them and they seemed don't work properly for the second items in pair
bool is_smaller(pair<double,int> a, pair <double , int> b)
{
return (b.first - a.first) > 1e9;
}
sort(a.begin(), a.end(), is_smaller);
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin >> s;
vector <pair<double,int> > a;
double x = 0, y = 1, k, d;
for(int i = 0;i < s.size();i++)
{
k = (x + y)/2;
d = abs(k - y);
//printf("[%.3lf %0.3lf] %.3lf %.3lf \n",x, y, k, d);
a.push_back({k,i+1});
if(s[i] == 'l')
y = k, x = k - d;
else
y = k + d, x = k;
}
sort(a.begin(), a.end());
for (int i =0;i < a.size();i++)
printf("%d\n",a[i].second);
return 0;
}
input : rrlllrrrlrrlrrrlllrlrlrrrlllrllrrllrllrrlrlrrllllrlrrrrlrlllrlrrrlrlrllrlrlrrlrrllrrrlrlrlllrrllllrl
code's output :
1
2
6
7
8
10
11
13
14
15
19
21
23
24
25
29
32
33
36
39
40
42
44
45
50
52
53
51
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
49
48
47
46
43
41
38
37
35
34
31
30
28
27
26
22
20
18
17
16
12
9
5
4
3
expected output :
1
2
6
7
8
10
11
13
14
15
19
21
23
24
25
29
32
33
36
39
40
42
44
45
50
52
53
54
55
57
61
63
64
65
67
69
72
74
76
77
79
80
83
84
85
87
89
93
94
99
100
98
97
96
95
92
91
90
88
86
82
81
78
75
73
71
70
68
66
62
60
59
58
56
51
49
48
47
46
43
41
38
37
35
34
31
30
28
27
26
22
20
18
17
16
12
9
5
4
3
comment :wrong answer 28th numbers differ - expected: '54', found: '51'
Floating point arithmetic has limited precision, although this precision is high with doubles, but it is still limited.
You algorithm generates a sequence of numbers, K(i), where
|K(i+1) - k(i)| = 2^(-i).
The |difference| above is a geometric sequence, so it decreases exponentially. Therefore, at some value of ì, the difference will become so small that it cannot be reported into the floating-point representation.
I ran your code with exactly the same input, but I also printed the numbers deside the indices, and I did not apply the sorting. I printed the numbers up to 50 decimal digits (%.50f, just to see!). What did I observe?
The numbers for positions i > 53 are all equal (within the precision that the double could achieve). Therefore, the numbers indexed above 53 will be sorted somehow randomly, because they are equal.
If you print the floats with enough precision:
printf("%03d %.18f\n",a[i].second,a[i].first);
then you'll see that the computations lead to the same floating point value for the rank 51 to 100...

Project Euler 11 c++ wrong answer

What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
My c++ code :
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fd ("file.txt"); // this file contains the grid
int A[463],max=-1;
for (int i=1;i<=400;i++){
fd >> A[i];
}
for (int i=463;i>400;i--){ // zero out array numbers that exceed 400
A[i]=0;
}
for (int i=1;i<=400;i++){
if ((A[i]*A[i+20]*A[i+40]*A[i+60])>max){ //vertical maximum
max=A[i]*A[i+20]*A[i+40]*A[i+60];
}
if ((i%20!=0)||((i-18)%20!=0)||((i-19)%20!=0)){ //diagnol \ maximum (first if checks if out of bounds)
if ((A[i]*A[i+21]*A[i+42]*A[i+63])>max){
max=A[i]*A[i+21]*A[i+42]*A[i+63];
}
}
if (((i-1)%20!=0)||((i-2)%20!=0)||((i-3)%20!=0)){// diagnol / maximum (first if checks if out of bounds)
if ((A[i]*A[i+19]*A[i+18]*A[i+17])>max){
max=A[i]*A[i+19]*A[i+18]*A[i+17];
}
}
if ((i+2)%20!=0){ // horizontal maximum (first if checks if out of bounds)
if ((A[i]*A[i+1]*A[i+2]*A[i+3])>max){
max=A[i]*A[i+1]*A[i+2]*A[i+3];
}
}
}
cout <<max;
fd.close();
return 0;
}
Whit this code I get the answer 51267216 which is the vertical answer, but this answer is not correct, I'm thinking there is a problem with my diagnol checking.
If I output the diagnol maximum I get something 4xxxxxxx which is close to the maximum vertical but not correct.
P.S I check both / and \ diagnols.
First thing you should do is using 2D Arrays. They make your problem look a lot easier. Second thing - use constants. Don't use meaningless numbers which could seem to a second person like black magic. Third thing - use a right, left and down padding of three rows and three columns. Last thing to remember - write your code like someone else would have to read it. Even if only you and a computer will see the code - it's a good habit and gives you an advance when you apply for a job.
const int parameters = 20;
const int padding = 3;
int tab[parameters + padding][parameters + padding];
See? It's a lot easier to read and tells you what you're doing. The extra columns and rows will spare us a lot of if if statements. You can pack the program in functions - it'll be easier to read and easier to use. Normally I would do it by defining a new class, but for algorithm purposes I'll use global variables and functions.
#include <fstream>
using namespace std;
const int parameters = 20;
const int padding = 3;
int tab[parameters + padding][parameters + 2*padding];
int diagonalLeft(int i, int j) {
int answer = 1;
for (int u = 0; u <= padding; u++)
answer *= tab[i - u][j - u];
return answer;
}
int diagonalRight(int i, int j) {
int answer = 1;
for (int u = 0; u <= padding; u++)
answer *= tab[i - u][j + u];
return answer;
}
int vertical(int i, int j) {
int answer = 1;
for (int u = 0; u <= padding; u++)
answer *= tab[i + u][j];
return answer;
}
int horizontal(int i, int j){
int answer = 1;
for (int u = 0; u <= padding; u++)
answer *= tab[i][j + u];
return answer;
}
int max(int a, int b) {
if (a > b)
return a;
return b;
}
int main() {
ifstream input("file.txt");
for (int i = 0; i < parameters; i++)
for (int j = padding; j < parameters + padding; j++)
input >> tab[i][j];
int maximum = 0;
int temp;
for (int i = 0; i < parameters; i++) {
for (int j = padding; j < parameters + padding; j++) {
temp = diagonalLeft(i, j);
temp = max(temp, diagonalRight(i, j));
temp = max(temp, vertical(i, j));
temp = max(temp, horizontal(i, j));
maximum = max(maximum, temp);
}
}
cout << maximum << endl;
input.close();
return 0;
}
If you the code is not only easier to read, it's easier to find bugs in a clean code. Don't even try to find them in your code, because it'll take you even several hours.
There is a reason why two dimensional arrays were invented. Your could have more bugs than you think you have, because of this one dimensional array.
Look at the size of your if statements. Mine are shorter and easier to debug. The padding around the table leaves me without exceptions like the ones you have.
Indeed, the tests for diagonals look wrong.
For instance, the \ diagonal test should be:
// not starting from the 3 rightmost columns,
// not starting from the 3 bottom rows
if ((i%20 <= 16) && i < 20*17)
The other diagonal should be:
// idem above:
if ((i%20 >= 3) && i < 20*17)
Edit :
The indices for the top-right to bottom-left diagonal are wrong: they should be {0, 19, 2*19, 3*19}, ie {0, 19, 38, 57}:
Here is the whole for loop, outputting the correct result: (70600674)
for (int i=0; i < 400; ++i) // <- I used a regular array indexed from 0
{
if (i < 17 * 20)
{
if ((A[i] * A[i + 20] * A[i + 40] * A[i + 60]) > max)
{
max = A[i] * A[i + 20] * A[i + 40] * A[i + 60];
}
if (i % 20 <= 16)
{
if ((A[i] * A[i + 21] * A[i + 42] * A[i + 63]) > max)
{
max = A[i] * A[i + 21] * A[i + 42] * A[i + 63];
}
}
if (i % 20 >= 3)
{
if ((A[i] * A[i + 19] * A[i + 38] * A[i + 57]) > max)
{
max = A[i] * A[i + 19] * A[i + 38] * A[i + 57];
}
}
}
if (i % 20 <= 16)
{
if ((A[i]*A[i+1]*A[i+2]*A[i+3])>max)
{
max=A[i]*A[i+1]*A[i+2]*A[i+3];
}
}
}
On a side note, a 2 dimensional array would have been much more clear (and you probably wouldn't have made such a mistake)