I need to convert Java alghoritm into C++. I'm having a hard time doing this, because C++ is definitely not my language, it's much harder and illogical sometimes compared to Java. (At least for me, full-time Java developer).
Anyway, I failed. Code is almost identical to Java in terms of logic, and still doesn't work :( The worst part was converting traditional Java arrays to C++'s vector<int>
. And I think I messed up here.
This is my C++ code:
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
double calculate(int n, int k, vector<int>& values);
int main()
{
printf("%f\n", calculate(7, 4, vector<int>{ 2, 2, 2, 1, 2, 2, 2 }));
system("pause");
return 0;
}
double calculate(int n, int k, vector<int>& values) {
int amount = k;
int offset = 0;
int predictions = (n - k) + 1;
vector<vector<int>> arrays;
int index = 0;
while (predictions > 0) {
for (int i = 0; i < predictions; i++) {
vector<int> arr;
for (int y = 0; y < amount; y++) {
arr[y] = values[offset + y]; // HERE IS THE ERROR!!!
}
arrays[index] = arr;
offset++;
index++;
}
amount++;
offset = 0;
predictions--;
}
vector<double> averages;
for (size_t a = 0; a < arrays.size(); a++) {
vector<int> arr = arrays[a];
int sum = 0;
for (int value : arr)
sum += value;
averages[a] = (double)sum / (double)arr.size();
}
double biggestAverage = 0;
for (double average : averages) {
if (average > biggestAverage)
biggestAverage = average;
}
return biggestAverage;
}
And Java code here: (works flawlessly)
import java.text.DecimalFormat;
import java.util.Arrays;
public class Main {
private static final DecimalFormat df = new DecimalFormat("#.000");
public static void main(String[] args) {
System.out.println(calc(7, 4, 2, 2, 2, 1, 2, 2, 2));
}
public static double calc(int n, int k, int... values) {
int amount = k;
int offset = 0;
int predictions = (n - k) + 1;
int[][] arrays = new int[n * n][n];
int index = 0;
while (predictions > 0) {
for (int x = 0; x < predictions; x++) {
int[] arr = new int[amount];
System.arraycopy(values, offset, arr, 0, amount);
//CLEAR CODE!!! WORKS TOO!!!
//for (int y = 0; y < amount; y++) {
// arr[y] = values[offset + y];
//}
arrays[index] = arr;
offset++;
index++;
}
amount++;
offset = 0;
predictions--;
}
double[] averages = new double[arrays.length];
for (int a = 0; a < arrays.length; a++) {
int[] arr = arrays[a];
int sum = 0;
for (int anArr : arr)
sum += anArr;
averages[a] = (double) sum / (double) arr.length;
}
double biggestAverage = 0;
for (double average : averages) {
if (average > biggestAverage)
biggestAverage = average;
}
return Double.valueOf(
df.format(biggestAverage)
.replace(",", "."));
}
}
Full error stacktrace (C++):
Exception thrown at 0x010C10B5 in Application.exe: 0xC0000005: Access violation writing location 0x00000000.
You need to allocate the vector elements before assigning values to them, when you do
vector<int> arr; // defines an empty vector.
arr[y] = ... // access element at position y.
arr
here is empty, pointing to a zero array, you are trying to write at memory 0x0000.
Checkout vector documentation, to see the different ways to initialize it.
In C++ you need to handle your memory, you cannot just port code from different higher-level languages.
There are different ways to do it, for instance
vector<int> arr;
arr.reserve(amount);
for (int y = 0; y < amount; ++y) {
arr.push_back(values[offset + y]);
}
User contributions licensed under CC BY-SA 3.0