Struct C++ array in function parameters not working at all - c++

hello i have to do a program using an array of structures.. and i have to initialize it in a function. below i am trying, but my prototype keeps getting error "Expected primary expression".. i have followed tutorials but cant figure out what im doing wrong please help. i cant use pointers or vectors.. just basic stuff thank you for your time
struct gameCases{
bool flag = false;
int casenum;
double value;
};
int initialize(gameCases cases); //prototype
--- main()
gameCases cases[26];
initialize(cases); //call
int initialize(gameCases cases) //definition
{
double values[26] = {.01, 1, 5, 10, 25, 50,
75, 100, 200, 300, 400, 500, 750, 1000,
5000, 10000 , 25000, 50000, 75000, 100000,
200000 , 300000, 400000, 500000,
1000000, 2000000};
for (int i = 0; i < 26; i++)
{
array[i].value = values[i];
}
}

Declare the function like
int initialize( gameCases *array, size_t n );
and call it like
initialize( cases, 26 );
Or you could pass the array by reference. For example
int initialize( gameCases ( &cases )[26] );
Take into account that the function is declared as having return type int but it acrually returns nothing.

int initialize(gameCases cases[26]); //prototype
int initialize(gameCases cases[26]) //definition
{
double values[26] = {.01, 1, 5, 10, 25, 50,
75, 100, 200, 300, 400, 500, 750, 1000,
5000, 10000 , 25000, 50000, 75000, 100000,
200000 , 300000, 400000, 500000,
1000000, 2000000};
for (int i = 0; i < 26; i++)
{
cases[i].value = values[i];
}
}
and to call:
initialize(cases);

Related

Using a Vector to fill up array of Class object - DMX Fixture Profile

Im writing a program to control different lights with DMX protocol. Each light has different channels to control different aspects (like Intensity, Color Temperature, colors etc.)
For that i want to create an easy way to put together a profile for each light.
This is what I came up so far. I'm working with an Arduino Due.
When I print out values after initialization, it just prints out 0 . Could somebody help me explain, what im doing wrong here? Or is there a better way to do this?
#include <vector>
struct Channel {
String name;
String unit;
int minValue;
int maxValue;
int color;
int dmxChannels;
int sliderChannels;
}; Channel AladdinBiColor[2], AladdinRGB[6];
class Profile {
public:
std::vector<Channel> channels;
int dmxChannels;
int sliderChannels;
String name[];
String unit[];
int minValue[];
int maxValue[];
int color[];
Profile(Channel* input, int count){
for (int i=0; i<count; i++){
channels.push_back(*input);
input++;
}
}
};
Profile AladdinBiColor_fixture(AladdinBiColor, 2);
Profile AladdinRGB_fixture(AladdinRGB, 6);
in Setup I call this function:
void setup(){
void initializeProfiles();
Serial.println(AladdinBiColor_fixture.channels[0].maxValue);
}
this prints out 0
which looks like this. It initializes the array.
void initializeProfiles(){
AladdinBiColor[0] = {"INT","%", 0, 100, WHITE,2,2};
AladdinBiColor[1] = {"INT","%", 0, 100, WHITE,2,2};
AladdinRGB[0] = {"INT","%", 0, 100, WHITE,2,2};
AladdinRGB[1] = {"INT","%", 0, 100, WHITE,2,2};
AladdinRGB[2] = {"CF","%", 0, 100, WHITE,2,2};
AladdinRGB[3] = {"RED","%", 0, 100, RED,2,2};
AladdinRGB[4] = {"GREEN","%", 0, 100, GREEN,2,2};
AladdinRGB[5] = {"BLUE","%", 0, 100, BLUE,2,2};
}
So the Problem was that the initialization took place after the Profile objects have been created. I rearranged the order and created an array which points to the objects of Profile so one can access it easily.
#include <vector>
struct Channel {
String name;
String unit;
int minValue;
int maxValue;
int color;
};
Channel AladdinBiColor[2] = {
{"INT","%", 0, 100, WHITE},
{"CCT","K", 0, 100, WHITE}
};
Channel AladdinRGB[6] = {
{"INT","%", 0, 100, WHITE},
{"CCT","K", 0, 100, WHITE},
{"CF","%", 0, 100, WHITE},
{"RED","%", 0, 100, RED},
{"GREEN","%", 0, 100, GREEN},
{"BLUE","%", 0, 100, BLUE}
};
class Profile {
public:
std::vector<Channel> channels;
int dmxChannels;
int sliderChannels;
Profile(Channel* input, int count, int dmxChannelsA){
for (int i=0; i<count; i++){
channels.push_back(*input);
input++;
}
dmxChannels = dmxChannelsA;
sliderChannels = count;
}
};
Profile AladdinBiColor_fixture(AladdinBiColor, 2,2);
Profile AladdinRGB_fixture(AladdinRGB, 6, 5);
Profile *lightProfiles[1][3] = { //index 1 = brand, index 2 = light
{&AladdinRGB_fixture, &AladdinBiColor_fixture, &AladdinRGB_fixture}
};
This way - in my main file - i can access the Profile objects with index numbers:
void setup(){
// first parameter of lightProfiles is the brand, second the light of it
Serial.println(lightProfiles[0][1]->channel[0].maxValue);
}

Solving a C2039 error and a C3861 error using std::minmax_element

I'm newer to C++.
I've written the following line in a test function inside a standard VS2019 test project:
auto minAndMaxYards = std::minmax_element(simResults.begin(), simResults.end());
It yields both C2039 and C3861 errors for the minmax_element function even though intellisense recognizes it as a member of std, and I can peek its definition. I can't figure out what I'm missing. I've included the algorithm file as well at the top of the test project.
Is there a project setting that I don't have right?
Full error text:
C2039 'minmax_element': is not a member of 'std'
C3861 'minmax_element': identifier not found
Edit, including code in case it helps
#include <algorithm>
#include "pch.h"
#include "CppUnitTest.h"
#include "Playbook.h"
#include "PlaySim.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
std::string output;
using std::vector;
namespace FootballDynastyV20UnitTest
{
TEST_CLASS(PlaybookIO)
{
public:
TEST_METHOD(setAndGetPlayblookName)
{
Playbook testPlays;
string testName = "testPlays";
testPlays.setName(testName);
string name = testPlays.getName();
Assert::IsTrue(name == testName);
}
TEST_METHOD(addPlayIncrementsPlayNum)
{
Playbook testPlays;
string playName = "Play1";
int numDLine = 4;
int numLB = 3;
vector<int> playerPos = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 19 };
vector<int> playerStance = { 2, 1, 0, 0, 1, 2, 2, 3, 2, 3, 3 };
vector<int> playerBlitzGaps = { 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0 };
testPlays.setName("testPlays");
testPlays.addPlay(playName, numDLine, numLB, playerPos, playerStance, playerBlitzGaps);
Assert::IsTrue(testPlays.getNumPlays() == 1);
}
TEST_METHOD(saveAndLoadPlayblook)
{
Playbook testPlays;
Playbook testPlaysLoad;
string playName = "Play1";
int numDLine = 4;
int numLB = 3;
vector<int> playerPos = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 19 };
vector<int> playerStance = { 2, 1, 0, 0, 1, 2, 2, 3, 2, 3, 3 };
vector<int> playerBlitzGaps = { 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0 };
testPlays.setName("testPlays");
testPlays.addPlay(playName, numDLine, numLB, playerPos, playerStance, playerBlitzGaps);
testPlays.save();
testPlaysLoad.load(testPlays.getName());
Assert::IsTrue(testPlays == testPlaysLoad);
}
};
TEST_CLASS(PlaySimTesting)
{
public:
TEST_METHOD(playSimReturnsYdsGainedBetweenNegative10And40)
{
PlaySim newPlay;
int numSims = 2000;
int lwrBound = -10;
int uprBound = 40;
vector<int> simResults;
for (int i = 0; i < numSims; i++)
{
newPlay.Run();
simResults.push_back(newPlay.GetYds());
}
auto minAndMaxYards = std::minmax_element(simResults.begin(), simResults.end());
int actualMin = *minAndMaxYards.first;
int actualMax = *minAndMaxYards.second;
int yds = newPlay.GetYds();
Assert::IsTrue((actualMin >= lwrBound) && (actualMax <= uprBound));
}
};
}
Move #include "pch.h" to the top of the file. When using precompiled headers, the compiler ignores everything above this line. In your example, that would be #include <algorithm>, that's why std::minmax_element is not found.

illegal, left operand has type 'DWORD [29]'

I'm pretty new to C++ and I'm really stuck here.
if (bAk == 1)
{
int fireRate = 134;
if (shotTiming < 30)
{
int valueX = (AssaultRifle::recoilTableX[shotTiming] * 0.48) + shakerNum;
int smoothingX = valueX / 5;
int valueY = (AssaultRifle::recoilTableY[shotTiming] * 0.48) + shakerNum;
int smoothingY = valueY / 5;
Sleep(3);
for (int i = 0; i < 5; i++)
{
mouse_move(valueX, valueY);
Sleep(fireRate / 5);
}
shotTiming++;
cout << valueX;
}
}
The only build error I am getting at this point is illegal, left operand has type 'DWORD [29]' Both int values of recoilTable are saying that the AssaultRifle namespace must have arithmetic or unscoped enum type. I just need to be put in the right direction of finishing it.
#pragma once
#include <Windows.h>
namespace AssaultRifle
{
const size_t MAX_INDEX_WEAPON = 1;
const size_t MAX_INDEX_RECOIL = 29;
DWORD recoilTableY[MAX_INDEX_WEAPON][MAX_INDEX_RECOIL] = {
{ 40, 48, 48, 48, 33, 33, 28, 24, 16, 13, 18, 22, 24, 29, 33, 33, 33, 29, 22, 20, 17, 17, 17, 17, 20, 27, 27, 27, 26 }
};
DWORD recoilTableX[MAX_INDEX_WEAPON][MAX_INDEX_RECOIL] = {
{ -36, 5, -59, -49, 3, 20, 25, 45, 43, 32, 82, 8, 43, -32, -25, -40, -35, -32, -43 , -42, -42, -55, -25, 15, 20, 35, 50, 62, 40 }
};
}
Your recoilTableX and recoilTableY arrays are both 2-dimensional 1:
DWORD recoilTableY[<# elements in first dimension>][<# elements in second dimension>] = {...};
DWORD recoilTableX[<# elements in first dimension>][<# elements in second dimension>] = {...};
But when reading individual values from the arrays, your code is indexing into only the first dimension. That is why you are getting the error, as you can't access an entire array as a single integer like you are attempting to do. You have to specify indexes for ALL of the available dimensions.
Change this:
AssaultRifle::recoilTableX[shotTiming]
AssaultRifle::recoilTableY[shotTiming]
To this instead:
AssaultRifle::recoilTableX[0][shotTiming]
AssaultRifle::recoilTableY[0][shotTiming]
MAX_INDEX_WEAPON is 1, so there is only 1 slot in the first dimension of the arrays, so the ONLY valid index in the first dimension is 0, which makes the first dimension pretty useless and should be removed, unless you are planning on adding values for additional weapons in the future.
MAX_INDEX_RECOIL is 29, so there are 29 slots in the second dimension of the arrays, so the ONLY valid indexes in the second dimension are 0..28 inclusive, but your code allows index 29 to be accessed.
The NAMES of your MAX_INDEX_WEAPON and MAX_INDEX_RECOIL constants are misleading, as they are not actually being used as indexes at all.
1: also, your arrays should be declared as const.
Try this instead:
#pragma once
#include <Windows.h>
namespace AssaultRifle
{
const size_t MAX_WEAPONS = 1;
const size_t MAX_RECOILS = 29;
const int recoilTableY[MAX_WEAPONS][MAX_RECOILS] = {
{ { 40, 48, 48, 48, 33, 33, 28, 24, 16, 13, 18, 22, 24, 29, 33, 33, 33, 29, 22, 20, 17, 17, 17, 17, 20, 27, 27, 27, 26 } }
};
const int recoilTableX[MAX_WEAPONS][MAX_RECOILS] = {
{ { -36, 5, -59, -49, 3, 20, 25, 45, 43, 32, 82, 8, 43, -32, -25, -40, -35, -32, -43 , -42, -42, -55, -25, 15, 20, 35, 50, 62, 40 } }
};
}
if (bAk == 1)
{
int fireRate = 134;
if (shotTiming < AssaultRifle::MAX_RECOILS)
{
int valueX = (AssaultRifle::recoilTableX[0][shotTiming] * 0.48) + shakerNum;
int smoothingX = valueX / 5;
int valueY = (AssaultRifle::recoilTableY[0][shotTiming] * 0.48) + shakerNum;
int smoothingY = valueY / 5;
Sleep(3);
for (int i = 0; i < 5; i++)
{
mouse_move(valueX, valueY);
Sleep(fireRate / 5);
}
shotTiming++;
cout << valueX;
}
}

Multidimensional arrays from MATLAB to C++

So I'm trying to do one of my project I did in MATLAB in C++ but I got stuck along the way.
Here's the portion of code here in MATLAB I want to convert to C++. It does work on MATLAB but not working in C++
RelRough = [0, 1E-6, 5E-6, 1E-5, 5E-5, 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001];
ReT = [4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 200000, 300000, 400000, 500000];
for i = 1:length(ReT)
for j = 1:length(RelRough)
FCT_guess = 1;
tolerance = 1;
while tolerance > 1e-14
FCT_cal = 1/(-2*log10((RelRough(j)/3.7) + (2.51/(ReT(i)*sqrt(FCT_guess)))))^2;
tolerance = abs(FCT_cal-FCT_guess);
FCT_guess = FCT_cal;
FCT(i,j) = FCT_cal*1000;
end
end
end
Here's my C++ version and I kept getting error like "expression must have integral or unscoped enum type" for variable g
double RelRough[] = { 0, 1E-6, 5E-6, 1E-5, 5E-5, 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001 };
const int lengthRelRough = sizeof(RelRough) / sizeof(RelRough[0]);
double ReT[] = { 4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 200000, 300000, 400000, 500000 };
const int lengthReT = sizeof(ReT) / sizeof(ReT[0]);
double fct[lengthReT][lengthRelRough] = { 0 };
double fct_guess = 1;
double tolerance = 1;
double fct_cal = 0;
for (int ii = 0; ii < lengthReT; ++ii) {
for (int jj = 0; jj < lengthRelRough; ++jj) {
while (tolerance > 1e-14) {
double h = (RelRough[jj] / 3.7), w = (2.51 / (ReT[ii] * sqrt(fct_guess)));
double g = (-2*log10(h+w))^2;
fct_cal = 1/g;
tolerance = abs(fct_cal - fct_guess);
fct_guess = fct_cal;
fct[ii][jj] = fct_cal;
std::cout << fct[ii][jj] << "\t";
}
}
}
return 0;
}
Is there anyone that help to see what's wrong. Thanks in advance!
Change this:
double g = (-2*log10(h+w))^2;
into:
double g = pow(-2*log10(h+w),2.0);
As #Eljay pointed out in his comment, the operator ^ performs a XOR in C++, and not an exponentiation. For more information:
Exponentiation (pow function)
Boolean Operations (including XOR)

Extra zeros in OpenCV Mat

Tried building 'cv::Mat' from 2D array but I find that extra zeros are added to the Mat which I am not able to understand. The code I tried is :
int a2D [7][7];
for(loop condition)
{
a2D[x][y] = value;
cout << "Value :"<< value << endl;
}
Mat outmat = Mat(7, 7, CV_8UC1, &a2D);
cout << "Mat2D : "<< outmat << endl;
Output is :
Value : 22
Value : 179
Value : 145
Value : 170
Value : 251
Value : 250
Value : 171
Value : 134
Value : 218
Value : 178
Value : 6
....Upto 49 values.
Mat2D : [ 22, 0, 0, 0, 179, 0, 0;
0, 145, 0, 0, 0, 170, 0;
0, 0, 251, 0, 0, 0, 250;
0, 0, 0, 171, 0, 0, 0;
134, 0, 0, 0, 218, 0, 0;
0, 178, 0, 0, 0, 6, 0;
0, 0, 72, 0, 0, 0, 25]
As in Mat2D output after every value 3 zeros are added.Why and how?
You are using int buffer to initialize cv::Mat with unsigned char elements, that explains why values are written at each fourth element (int seems to be 4 times larger than unsigned char on your machine).
Changing type of a2D to unsigned char should fix the issue.
The assignment a2D[x][y] = value is wrong if the type of a2D is int[49], you are writing outside the array. The 0's you see are uninitialized garbage.
You have to access a2D with a single index. For example: a2D[i] = value.