How to find the highest number of passenger of all round? - c++

im a new problem solver. recently i came across this problem on codeforces website. i managed to get the two values required for each turn depending on the number of turn given by the user, but i cant find the highest number of passenger at stop out of all.
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int turn,get_off,get_on,total_passenger,highest_total;
cin>>turn;
int* stops=new int(turn);
for(int i=0;i<turn;i++){
cin>>get_off>>get_on;
total_passenger=get_on-get_off;
stops[i]=total_passenger;
}
for(int i=0;i<turn;i++){
if(stops[i]>stops[i+1]){
highest_total=stops[i];
}
}
cout<<highest_total<<endl;
return 0;
}

you can try using https://en.cppreference.com/w/cpp/algorithm/max_element
also you set the total passengers number to the difference between get off and get on and I think you should just add that difference, and to allocate memory for int* you use [] instead of ()
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int turn,get_off,get_on,total_passenger,highest_total;
cin>>turn;
int* stops=new int[turn];
for(int i=0;i<turn;i++){
cin>>get_off>>get_on;
total_passenger+=get_on-get_off;
stops[i]=total_passenger;
}
cout<<std::max_element(stops, stops + turn)<<endl;
}

The issue seems to be with int *stops = new int(turn). It will allocate memory for 1 integer.
Use int stops[turn] and your algorithm need to be modified.
stop[i] += total_passenger need to be used with some if-else.

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem.
Alongside you will have to check corner cases as your highest_total variable may provide garbage value. So while declaring the variable assign a value to it. I prefer initializing int highest_total=-1 here.
Actually you dont need an array or pointer here. This can be solved this way-
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int turn,get_off,get_on,total_passenger=0;
cin>>turn;
int highest_total=-1;
for(int i=0;i<turn;i++){
cin>>get_off>>get_on;
total_passenger=total_passenger+get_on-get_off;
if(total_passenger>highest_total)
highest_total=total_passenger;
}
cout<<highest_total<<endl;
return 0;
}

Firstly: int* stops=new int(turn); initializes a pointer to a new int with the value turn. See int a = 0 and int a(0) differences
Instead you should either use int* stops=new int[turn];, read about when you should use new here: When should I use the new keyword in C++?. An important note is that you have to delete the variable in order to avoid a Memory leak.
Or (which I would say is the case you should use in this scenario), int stops[turn];.
Besides that I can not argue with your logic since I believe this is not a forum to help you solve code, although the syntax seems correct.
I would recommend reading Why is "using namespace std;" considered bad practice? if you are interested in learning C++ though.

Related

Creating links between arrays of pointers in C++?

how can I create links between two arrays of pointers in C++.
I (obviously) tried the method where I declared 2 pointer Arrays and started equating their indexes individually but C++ would give me an error (stating: cannot convert int* to int**).
So...any solutions?
For a better understanding of the question, look at this link:
QUESTION
EDIT:
Here's a simple breakdown code that I tried (but it didn't work at all):
#include<iostream>
using namespace std;
int main ()
{
int *A[4];
int *B[4];
A[0] = &B[1];
}
From the picture in your description, it seems that only elements of A need to point to elements of B. This means B can just be a regular array of ints:
int *A[4];
int B[4];
A[0] = &B[1];

why do you have to pass dynamically allocated arrays from main to functions in c++?

I'm just started to learn programming so please bear with me if it's a stupid question:
Why do we have to pass a dynamically allocated array from main to function? For example, in python I think there is no need right? So why do we do it in c++? why can't the function directly access the array?
In the code below, if I don't pass arr_ptr[] as an argument to the function, it doesn't work. I was wondering why that is the case. Since the array is dynamically allocated, why can't the function directly access the array through the pointer??
#include<iostream>
using namespace std;
//function sums the values in array
double sumArray(double arr_ptr[],int size)
{ double sum=0.0;
for (int i=0;i<size;i++)
{
sum=sum+arr_ptr[i];
}
return sum;
}
int main()
{
int num_of_vals=0;
cout<<"Enter number of values to be summed: ";
cin>>num_of_vals;
double* arr_ptr=NULL; //creates double ptr
arr_ptr= new double[num_of_vals]; //dyn array
for (int i=0;i<num_of_vals;i++)
{ cout<<"enter value #"<<i<<" out of " <<num_of_vals-1<<" to be
summed:";
cin>>arr_ptr[i];
}
int sum=sumArray(arr_ptr,num_of_vals);
cout<<"sum of values is:"<<sum<<endl;
delete[] arr_ptr;
return 0;
}
Short answer: you don't. You can always use globals.
That being said: globals are evil. It is often considered to be a bad practice, and a lazy one often.
The thing that guides usually development on C++ that allows it to be used for huge projects it to narrow visibility and responsability. One major goal is to create class invariants. That's hard to guarantee with Python.
Python and C++ are different tools suited for different jobs.
Anyway, you should pass your variables as arguments in Python too: that allows you to reuse code.

What is the difference between assigning a pointer to variable address and explicitly to memory address?

See the following pieces of code:
#include<iostream>
using namespace std;
int main()
{
int a=10;
int *p=&a;
*(p+1)=6;
cout<<*(p+1);
}
The code above does not work. However, the following piece of code, which assigns to an explicit memory address, does work:
#include<iostream>
using namespace std;
int main()
{
int *p=(int *)0x28fefc;
*(p+1)=6;
cout<<*(p+1);
}
Why would one of these methods work and not the other?
I have used a memory address of my computer in the second one.this address may not work in some other machine.
Those are both undefined behavior. You are trying to modify memory that you did not allocate. The second is even less safe, because you are assuming a is going to be allocated to that address every time, which is absolutely not a safe assumption.

Reading list of names into an array

I've just realized that I've never learned to read strings from file, so I did a little messing around to figure it out, but I'm having a problem with my compiler.
For my programming class I use visual c++ 2010 because it is required and it hasn't given me much problem so I haven't switched to any other.
Anyways heres my code and my problem.
It is basically supposed to read in full names from a file and store them in an array.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
const int maxsize = 100;
string friendArray[maxsize];
ifstream friends;
friends.open("myFriends.dat");
int sub = 0;
while (friendArray[sub] <= 100)
{
getline(friends, friendArray[sub]);
sub++;
}
}
in my while loop, I am recieving: error: no operator "<=" matches these operands.
I'm getting the same thing with any other operators I use also.
Any help?
You want this instead:
while (sub < 100)
Originally, you were comparing a string to an integer literal. You obviously can't do that.
Note that I also changed the <= to < otherwise, you'll be overrunning the array.

Is vector::push_back() making a shallow copy & how to solve this

In the program I am writing, I have something similar to the code here:
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
struct people
{
string name;
int st;
int sn[50];
};
int main()
{
unsigned int n,ST[10]={25,18,15,12,10,8,6,4,2,1};
vector<people> master;
cin>>n;
for (int i=0;i<n;i++)
{
unsigned int m;
cin>>m;
for (int j=0;j<m;j++)
{
people youngling; //I am declaring it here, but it doesn't solve the issue
string s;
cin>>s;
for (int l=0;l<master.size();l++)
{
if (master[l].name.compare(s)==0)
{
if (j<10) master[l].st+=ST[j];
master[l].sn[j]++;
goto loop;
}
}
youngling.name=s;
if (j<10) youngling.st=ST[j];
for (int l=0;l<50;l++) youngling.sn[l]=0;
youngling.sn[j]++;
master.push_back(youngling);
loop:;
}
}
}
As you can see, I am pushing back a struct (people youngling) into a vector (vector<people> master). However, this code is giving me the wrong results, and I think it might be caused by the struct and the shallow copy issue. This is proved somewhat since if I use a full array of people to store the input then the answer is correct. But I am puzzled by this:
Is struct just a pointer to the compiler, or why does this shallow copy issue exist?
I am declaring the people youngling inside the inner loop, hoping to solve this issue, but no use. Is there any easy way to correct the code snippet above?
When I am testing on small cases using GCC 4.4, the answer seem to be right. However, when I test it use gnu C++0X, the answer is wrong. Is this a compiler-specific issue?
Note: I cannot provide the wrong test case since I am testing it online using a judge system.
push_back is making the copy of object being inserted using its copy constructor. If there is no custom one (like in case of your struct), the default is just to copy all fields, using their own copy constructors, etc.
For your struct - containing a string and a fixed-size array of fundamental type - result should be equivalent to deep copy.