I'm having trouble compiling code with a struct vector. The compiler keeps sending errors, but I cannot locate any. The code is right beneath.
//2018 USACO Bronze Task 2
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
struct Period{
int end,starting;
};
int main(){
ifstream fin("lifeguards.in");
ofstream fout("lifeguards.out");
int N;
fin>>N;
vector <Period> periods[N];
for(int i=0;i<N;i++){
fin>>periods[i].starting>>periods[i].end;
}
int record=0,temp=0;
for(int i=0;i<N-1;i++){
temp+=periods[i].end-periods[i].starting;
for(int j=0;j<N;j++){
if(j==i)continue;
temp+=periods[j].end-periods[j].starting;
temp-=max(periods[i].end-periods[j].starting,0);
}
if(temp>record)record=temp;
temp=0;
}
fout<<record<<endl;
}
The error message is in the link
Errmsg
I have checked for any possible grammatical errors that I know of, but it keeps popping out compilation errors. Is there any fix to this?
lets look at this line:
vector <Period> periods[N];
in this line you defined array of vector so when you call periods[i] this will return one of array item which is vector <Period> so this is returned type not an Period struct it is vector type so it is incorrect. in order to achieve this you should call periods[i][j] or if you mean you want vector with N size you can call reserve method.
Related
i am writing a code for a multidimensional array with two functions.
First function(read()) gets the value of each array, and the second one shows each of them.
My problem is return the gotten array from the read function.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include<cassert>
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
using namespace std;
typedef int Sec[2][2];
int read(Sec sec){
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
cin>>sec[i][j];
}
}
return sec;
}
void sho(){
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
cout<<sec[i][j];
}
}
}
int main() {
read(Sec sec);
sho(sec);
}
Here are your mistakes:
You don't need to return anything from read function because argument passed to this function is passed as pointer. Therefore, content on these addresses will be updated based on user input. This is perfectly fine function signature void read(Sec sec);
In your main function, you need first to initialize your local variable Sec sec; and then pass it to read function like this read(sec);
Hope this will help you!
try it this way:
#include <iostream>
//you dont need ctime here
#include <ctime>
//you dont need csdtlib here
#include <cstdlib>
//you dont need cmath here
#include <cmath>
//you dont need time.h here
#include <time.h>
//you dont need cassert here
#include<cassert>
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
using namespace std;
typedef int Sec[2][2];
//by adding the "&" symbol you give the function read a refrenze to a variable of
//type sec, which allows to change the values, it is like a derefrenzed pointer
void read(Sec& sec){
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
cin>>sec[i][j];
}
}
}
//you dont need a refrenze here, because u just want to read from the Sec object, if
//you want to run a bit faster you couldnt use:
//void show(const Sec& sec),
//this would give the function show a refrenze you cant edit, so perfectly for
//reading values
void show(Sec sec){
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
cout<<sec[i][j];
}
}
}
int main() {
Sec sec;
read(sec);
show(sec)
}
What are the right methods?
How to avoid the 3 errors?
I tried the followings:
#include <vector>
#include <array>
#include <iostream>
using namespace std;
struct s_4{double x,z,k,wsize;};
typedef vec4 vector <array<double,4>>; //ERROR #1
void main()
{
vector <s_4> s1;
vector <array<double,4>> d1;
s1.push_back(*new (s_4){10.0,11,1,0.25e-3}); //OK
d1.push_back(*new (array<double,4>){10.0,11,1,0.25e-3}); //OK
d1.push_back(*new (double[4]){10.0,11,1,0.25e-3}); //ERROR #2
vector <array<double,4>> d2{11,12,13,14.1}; //ERROR #3
getchar();
}
It is like it is very difficult to use large arrays in vectors
The correct code is:
#include <vector>
#include <array>
#include <iostream>
using namespace std;
struct s_4{double x,z,k,wsize;};
typedef vector <array<double,4>> vec4;
int main()
{
vector <s_4> s1;
vector <array<double,4>> d1;
s1.push_back({10.0,11,1,0.25e-3});
d1.push_back({10.0,11,1,0.25e-3});
d1.push_back({10.0,11,1,0.25e-3});
vector <array<double,4>> d2{{11,12,13,14.1}};
return 0;
}
Your first error in the typedef is that the name of the typedef comes last.
Your first three push_backs were leaking memory, you don't need to name the type when initialising.
The second error is because a c array can't be converted directly to a std::array.
The last needs two sets of braces, one to initialise the vector and one to initialise each array.
In addition to Alan's answer:
Why are you trying to allocate your arrays on the heap? You could place your arrays on the stack and use initializer lists:
#include <vector>
#include <array>
#include <iostream>
int main()
{
std::vector <std::array<double,4>> data = {
{10.0,11,1,0.25e-3},
{10.0,11,1,0.25e-3},
{10.0,11,1,0.25e-3},
{11,12,13,14.1}
};
}
However, initializer lists are a C++11 feature so you may compile with -std=c++11:
g++ -g -Wall -O2 -std=c++11 test.cpp -o test
Furthermore you should avoid using namespace std, as this may cause problems if you use additional libraries that implement for example vectors for mathematical calculations.
#include <iostream>
#include <queue>
using namespace std;
int main () {
struct process {
int burst;
int ar;
};
int x=4;
process a[x];
queue <string> names; /* Declare a queue */
names.push(a[1]);
return 0;
}
I'm trying to pushing struct variable in queue but its not taking it and gives errors
no matching function for #include queue and invalid argument
how can I do that?
C++ is a strongly typed language. In the line names.push(a[1]); you are trying to push a struct (from your process a[x]; array) into a queue<string>. Your struct is not a string, so the compiler will emit an error. You at least need a queue<process>.
Other issues: variable length arrays are not standard C++ (process a[x];). Use a std::vector<process> instead. Here is some simple example that works:
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main () {
struct process // move this outside of main() if you don't compile with C++11 support
{
int burst;
int ar;
};
vector<process> a;
// insert two processes
a.push_back({21, 42});
a.push_back({10, 20});
queue <process> names; /* Declare a queue */
names.push(a[1]); // now we can push the second element, same type
return 0; // no need for this, really
}
EDIT
Locally defined classes/structs used to instantiate templates are valid only in C++11 and later, see e.g. Why can I define structures and classes within a function in C++? and the answers within. If you don't have access to a C++11 compliant compiler, then move your struct definition outside of main().
for some reason I get the "Process terminated with status -1073741819" error whenever I run my program, I've read that some people get this error because of something wrong with code-blocks/the compiler, i just wanted to know if there is anything wrong with my code before i go reinstalling compilers and such. I'm using code::blocks and the GNU GCC compiler.
my code creates a vector which stores 40 working hours in a week, and a vector inside that vector which stores letters representing the 5 people available in those hours.
Schedule.cpp:
#include <iostream>
#include "Schedule.h"
#include <vector>
#include <string>
using namespace std;
/// Creates a Vector which holds 40 items (each hour in the week)
/// each item has 5 values ( J A P M K or X, will intialize as J A P M K)
vector< vector<string> > week(40, vector<string> (5));
Schedule::Schedule(){
for (int i = 0; i<40; i++){
week[i][0] = 'J';
week[i][1] = 'A';
week[i][2] = 'P';
week[i][3] = 'M';
week[i][4] = 'K';
}
// test
cout << week[1][3] << endl;
}
header file:
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include <vector>
#include <string>
using namespace std;
class Schedule
{
public:
Schedule();
protected:
private:
vector< vector<string> > week;
};
#endif // SCHEDULE_H
main.cpp:
#include <iostream>
#include "Schedule.h"
#include <vector>
#include <string>
using namespace std;
int main()
{
Schedule theWeek;
}
This is not a copiler bug.
You are getting a memory fault in your constructor.
There are several things wrong with your code, for example in your cpp you declare a global vector week which then is hiden in the constructor since the constructor will access Schedule::week .
Your cpp should be something like :
// comment out the global declaration of a vector week ...
// you want a vector for each object instantiation, not a shared vector between all Schedule objects
// vector< vector<string> > week(40, vector<string> (5));
Schedule::Schedule()
{
for (int i=0;i<40;i++)
{
vector<string> stringValues;
stringValues.push_back("J");
stringValues.push_back("A");
stringValues.push_back("P");
stringValues.push_back("M");
stringValues.push_back("K");
week.push_back(stringValues);
}
}
You get the memory fault in your code when you try to access your week vector for the first time :
week[i][0] = 'J' ;
At the moment you call that line of code, your Schedule::week vector has 0 elements inside it (so week[i] is already a fault).
I am trying to create a lottery program within c++ and the issue i'm having is attempting to output all values of the Numbers array into a file, however when i run the code, the only thing that gets outputted is the first set of values i put in, however the program allows me to type in more than one set. (The program allows for up to 6 sets of data), however it only outputs one.
Here is all my code
LotteryData.cpp
LotteryData::LotteryData()
{
}
LotteryData::~LotteryData()
{
}
void LotteryData::PassInfo(int (&Numbers)[6][6], int &NumberofGames)
{
ofstream Numfile;
while(NumberofGames>0)
{
Numfile.open("Numbers.txt");
for (int j=0; j<6; j++)
{
Numfile << Numbers[NumberofGames][j];
}
NumberofGames = NumberofGames - 1;
Numfile.close();
}
}
Player.h
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class Player
{
private:
public:
Player();
~Player();
void Input();
int Numbers[6][6];
int NumberofGames;
};
main.cpp
#include <iostream>
#include "Lottery.h"
#include "Player.h"
#include "LotteryData.h"
using namespace std;
int main()
{
Player player;
Lottery random;
LotteryData data;
player.Input();
random.setRandomNumber();
data.PassInfo(player.Numbers, player.NumberofGames);
}
Im not exactly sure where the problem is coming from but i think it may be from one of the pointers although not entirely sure.
Any help on this problem would be much appreciated.
Cheers
Edit: I've Changed the code within the PassInfo function within LotteryData.cpp file as #ali suggested
Edit2: I've cut down on the code as to where I think the problem occurs but as all of the code compiles, Visual Studio 2012 doesnt point to any actual errors in the program
In the while loop in the PassInfo function you are not changing the value of NumberOfGames. Therefore in the for loop you are using the same row of your Numbers array. You need another for loop to change the index for the first index of the Numbers[][] array.