I am trying to solve this problem : http://codeforces.com/contest/664/problem/B
Here is my code : http://ideone.com/fWgQEn
I am getting Runtime Error on Test Case 5, even though my answer is correct and I am printing it properly.
Can anyone tell me what could be the reason of this?
#include<bits/stdc++.h>
using namespace std;
int main(){
int i = 0, pos = 1, neg = 0, n;
string str;
char x;
while(1){
cin >> x;
if(x == '=') break;
else if (x == '?') continue;
else if (x == '+') pos++;
else if (x == '-') neg++;
str[i++] = x;
}
str[i] = '\0';
// cout << str[0] << str[1] << str.size() << endl;
cin >> n;
if (!(pos * n - neg >= n && pos - neg * n <= n))
cout << "Impossible" << endl;
else{
cout << "Possible\n";
int neg_sum, pos_sum;
for (int i = neg; i <= neg * n; i++){
pos_sum = n + i;
if(pos_sum <= pos * n && pos_sum >= pos) {
neg_sum = i; pos_sum = n + i;
break;
}
}
// cout << str.size() << endl;
// cout << pos_sum << " " << neg_sum << endl;
int pos_count = 1, neg_count = 0;
for(int i = -1 ; i < pos + neg - 1; i++){
// cout << "i " << i << " " << str[i] <<endl;
if(!(i + 1)){
if(pos == 1) cout << pos_sum << " ";
else cout << pos_sum / (pos - 1) << " ";
}
else{
if(str[i] == '+'){
if(pos_count++ != pos -1) cout << "+ "<< pos_sum / (pos - 1) << " ";
else cout << "+ "<< pos_sum % (pos - 1) << " ";
}
else{
if(neg == 1) cout << "- " << neg_sum << " ";
else if(neg_count++ != neg -1) cout << "- "<< neg_sum / (neg - 1) << " ";
else cout << "+ "<< neg_sum % (neg - 1) << " ";
}
}
}
cout << "= " << n;
}
return 0;
}
TIA!!
string str;
char x;
while(1){
cin >> x;
if(x == '=') break;
else if (x == '?') continue;
else if (x == '+') pos++;
else if (x == '-') neg++;
str[i++] = x;
}
I think at least str[i++] = x; will run into un-allocated spaces and cause undefined behavior. See
http://www.cplusplus.com/reference/string/string/operator[]/
Try
str += x; i++;
instead.
Related
I'm having some trouble on my code if yourcurrency is larger than convert the convert will choose the 2 instead of 1
can't figure it out what's wrong with my code i've tried on different compiler I think the issue is the code itself can you help me guys?
#include <iostream>
#include <string>
using namespace std;
int picked[5] = {0,0,0,0,0};
int main(){
string currentvalue[5][5] = {{"1","0.01818484","0.015423716","2.4462308","0.019060361"},{"55.012111","1","0.84815483","134.47933","1.0482778"},{"64.90818","1.1789799","1","158.58285","1.2359668"},{"0.40901121","0.0074303636","0.0063022858","1","0.0077907482"},{"52.511804","0.95391572","0.80907737","128.35738","1"}};
string currency[5] = {"Philippine Peso","euro","pounds","yen","usd"};
int yourcurrency;
int convert;
int timesToRun = 5;
int number = 1;
system("COLOR 0a");
cout << "Choose your currency \n" << endl;
for (int counter = 0 ; counter < timesToRun; counter++)
{
cout << number;
cout << "." + currency[counter] << endl;
number++;
}
cout << "\nOption: ";
cin >> yourcurrency;
system("CLS");
yourcurrency = yourcurrency - 1;
picked[yourcurrency] = 1;
cout << "Select your currency you want to convert into \n" << endl;
number = 1;
for (int counter = 0; counter < timesToRun; counter++)
{
if (picked[counter] != 1){
cout << number;
cout << "." + currency[counter] << endl;
number++;
}
}
cout << "\nOption: ";
cin >> convert;
system("CLS");
cout << currency[yourcurrency]+ " - " + currency[convert];
cout << " [" + currentvalue[yourcurrency][convert] + "] " << endl;
cout << "Amount: ";
int cash;
cin >> cash;
double value = stof(currentvalue[yourcurrency][convert]);
double total = cash * value;
cout << currency[convert]<< ": " << total;
}
I have a point cloud data with wide range in all 3 dimensions. I have filtered it using pcl::ConditionalRemoval::filter() in range -20 >= x >= 20, -20 >= y >= 20 and -2 >= z >= 2.
Now I want to have min and max values in each dimension, so I searched online for nay PCL function. I got pcl::getMinMax3D. I used it and to verify I also manually searched in the cloud data. The code is as below:
float xmin = 10000, xmax=-10000;
cout << "Manual Search Result:"<<endl;
for(unsigned int i = 0; i < filtered_data->size(); i++)
{
if(filtered_data->at(i)._PointXYZ::x > xmax)
xmax = filtered_data->at(i)._PointXYZ::x;
if(filtered_data->at(i)._PointXYZ::x < xmin)
xmin = filtered_data->at(i)._PointXYZ::x;
}
cout << "Xmin: " << xmin << "\t\tXmax: " << xmax << endl;
float ymin = 10000, ymax=-10000;
for(unsigned int i = 0; i < filtered_data->size(); i++)
{
if(filtered_data->at(i)._PointXYZ::y > ymax)
ymax = filtered_data->at(i)._PointXYZ::y;
if(filtered_data->at(i)._PointXYZ::y < ymin)
ymin = filtered_data->at(i)._PointXYZ::y;
}
cout << "Ymin: " << ymin << "\t\tYmax: " << ymax << endl;
float zmin = 10000, zmax=-10000;
for(unsigned int i = 0; i < filtered_data->size(); i++)
{
if(filtered_data->at(i)._PointXYZ::z > zmax)
zmax = filtered_data->at(i)._PointXYZ::z;
if(filtered_data->at(i)._PointXYZ::z < zmin)
zmin = filtered_data->at(i)._PointXYZ::z;
}
cout << "Zmin: " << zmin << "\t\tZmax: " << zmax << endl;
pcl::PointXYZ minPt, maxPt;
pcl::getMinMax3D (*filtered_data, minPt, maxPt);
cout << "getMinMax3D Search Result:"<<endl;
std::cout << "Min x: " << minPt.x << "\t\tMax x: " << maxPt.x << std::endl;
std::cout << "Min y: " << minPt.y << "\t\tMax y: " << maxPt.y << std::endl;
std::cout << "Min z: " << minPt.z << "\t\tMax z: " << maxPt.z << std::endl;
The output I m getting is:
Manual Search Result:
Xmin: -19.992 Xmax: 19.915
Ymin: -19.75 Ymax: 19.982
Zmin: -1.999 Zmax: 1.059
getMinMax3D Search Result:
Min x: -3.895 Max x: 3.967
Min y: -4.238 Max y: 4.291
Min z: -1.887 Max z: 0
Is my understanding wrong about the usage of getMinMax3D()?
If the input cloud has NAN point, make sure you set cloud.is_dense as false, otherwise the function pcl::getMinMax3D would not check for NaN.
Here is original implementation from https://pointclouds.org/documentation/common_2include_2pcl_2common_2impl_2common_8hpp_source.html
template <typename PointT> inline void
pcl::getPointsInBox (const pcl::PointCloud<PointT> &cloud,
Eigen::Vector4f &min_pt, Eigen::Vector4f &max_pt,
Indices &indices)
{
indices.resize (cloud.size ());
int l = 0;
// If the data is dense, we don't need to check for NaN
if (cloud.is_dense)
{
for (std::size_t i = 0; i < cloud.size (); ++i)
{
// Check if the point is inside bounds
if (cloud[i].x < min_pt[0] || cloud[i].y < min_pt[1] || cloud[i].z < min_pt[2])
continue;
if (cloud[i].x > max_pt[0] || cloud[i].y > max_pt[1] || cloud[i].z > max_pt[2])
continue;
indices[l++] = int (i);
}
}
// NaN or Inf values could exist => check for them
else
{
for (std::size_t i = 0; i < cloud.size (); ++i)
{
// Check if the point is invalid
if (!std::isfinite (cloud[i].x) ||
!std::isfinite (cloud[i].y) ||
!std::isfinite (cloud[i].z))
continue;
// Check if the point is inside bounds
if (cloud[i].x < min_pt[0] || cloud[i].y < min_pt[1] || cloud[i].z < min_pt[2])
continue;
if (cloud[i].x > max_pt[0] || cloud[i].y > max_pt[1] || cloud[i].z > max_pt[2])
continue;
indices[l++] = int (i);
}
}
indices.resize (l);
}
I have to calculateand then display in OOP of C++
Minimum of the even elements of the matrix and the maximum of the prime elements
As i made up my code
My problem is that i have no idea how to make so that the max value from a matrix to be a prime number and I have no clue if I should work with a for( ) and then use a if (value%i==0) or if there can be a better method.
#include <iostream>
using namespace std;
class matrix3
{
int a[10][10], b[10][10], c[10][10], d[10][10], e[10][10], f[10][10], x, y, i, j, l1,l2,max, max2 = 0, min = 0, save, c1, c2;
public:
void values();
void transpose();
void sum();
void diff();
void linie();
void coloana();
void minimul_elementelor_pare();
void maximul_elementelor_prime();
};
void matrix3::values()
{
cout << "Enter the rows "; cin >> x;
cout << "Enter the columns "; cin >> y;
cout << "Enter elements of first matrix\n\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
cin >> a[i][j];
}
}
cout << "Enter elements of second matrix\n\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
cin >> c[i][j];
}
}
}
void matrix3::sum()
{
cout << "Sum of Matrices 1 and 2 is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
e[i][j] = a[i][j] + c[i][j];
cout << e[i][j] << "";
}
cout << endl;
}
}
void matrix3::diff()
{
cout << "Difference of Matrices 1 and 2 (1-2) is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
f[i][j] = a[i][j] - c[i][j];
cout << f[i][j] << "";
}
cout << endl;
}
}
void matrix3::transpose()
{
cout << "transpose of the matrix is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
b[i][j] = a[j][i];
cout << b[i][j] << "";
}
cout << endl;
}
cout << "Transpose of the second matrix is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
d[i][j] = c[j][i];
cout << b[i][j] << "";
}
cout << endl;
}
}
void matrix3::coloana()
{
cout << "test is\n";
cout << "\n Numerele coloanelor care doriti sa le interschimbati";
cout << "\n c1: ";cin >> c1;
cout << "\n c2: ";cin >> c2;
for (i = 1; i <= x; i++)
{
save = a[i][c1];
a[i][c1] = a[i][c2];
a[i][c2] = save;
}
for (i = 1;i <= x;i++) {
for (j = 1;j <= y;j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
void matrix3::linie()
{
cout << "test is\n";
cout << "\n Numerele coloanelor care doriti sa le interschimbati";
cout << "\n l1: ";cin >> l1;
cout << "\n l2: ";cin >> l2;
for (i = 1; i <= x; i++)
{
save = a[l1][j];
a[l1][j] = a[l2][j];
a[l2][j] = save;
}
for (i = 1;i <= x;i++) {
for (j = 1;j <= y;j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
void matrix3::minimul_elementelor_pare()
{
cout << "Minimul Elementelor Pare";
for (i = 1;i <= x;i++)
{
if (x % 2 == 0)
{
if (a[i][j] < min)
min = a[i][j];
}
min++;
}
cout << " " << min;
}
void matrix3::maximul_elementelor_prime()
{
cout << "max matrice" << endl;
max=a[1][1];
for (i = 1;i <= x/2;i++)
{
for (j = 1;j <= y/2;j++)
if (x%i != 0 && max < a[i][j])
max = a[i][j];
}
cout << " maximul din matrice este " << max;
}
int main()
{
int input;
char ch;
matrix3 m;
m.values();
do
{
cout << "Enter your choice\n";
cout << " 1. Sum of 1 and 2\n" << " 2. Difference of 1 and 2\n" << " 3. Transpose of both 1 amd 2\n" << "4. Linii\n" << "5. Minim\n" << "6.Linie\n"<<"7. Maxim\n";
cin >> input;
switch (input)
{
case 1:
m.sum();
break;
case 2:
m.diff();
break;
case 3:
m.transpose();
break;
case 4:
m.coloana();
break;
case 5:
m.coloana();
break;
case 6:
m.minimul_elementelor_pare();
break;
case 7:
m.maximul_elementelor_prime();
}
cout << "\nDo another y/n?\n";
cin >> ch;
} while (ch != 'n');
cout << "\n";
system("pause");
return 0;
}```
My program can't read all of the data from my MarvelIn.txt file.
It reads about 29 spaces in, and MarvelIn.txt contains only 9 entries, then I get a runtime error.
I think I have all the syntax right, however this is the only error I have. It will not output to the output file "MarvelOut.txt".
Here is the code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;
struct sstruct
{
string first, last;
string department;
int salary;
};
void init2(sstruct s[50])
{
int maxarray = 50;
sstruct init = { "Darth", "Vader", "None", 0 };
for (int i = 0; i < maxarray; i++) {
s[i] = init;
cout << "init: " <<s[i].first<<endl;
}
}
void read(sstruct s[50], int &nums)
{
int maxarray = 50;
ifstream inf("MarvelIn.txt");
int i = 0;
while (!inf.eof())
{
inf >> s[i].first >> s[i].last >> s[i].department >> s[i].salary;
cout << "read: "<<s[i].first<<s[i].last << s[i].department <<
s[i].salary << endl;
i++;
}
nums = i;
}
void avg(sstruct s[50], int &nums, double &average2)
{
int maxarray = 50;
int i;
for (i = 0; i < nums; i++)
average2 += s[i].salary;
average2 /= nums;
}
void print(sstruct s[50], int nums, double &average2)
{
int maxarray = 50;
ofstream outf("MarvelOut.txt");
int i = 0;
string temp;
outf << "the number of professors is: " << nums << endl;
cout << "the number of professors is: " << nums << endl;
outf << endl << "The average salary of the professors is: " << average2 << endl;
outf << "Advisor " << "Major " << " Department " << "Salary " << endl;
for (i = 0; i < nums; i++)
{
temp= s[i].last + "," + s[i].first;
cout << "last, first " << temp << endl;
outf << left << setw(20) << temp << right << setw(5)<< s[i].department << setw(5) << s[i].salary << setw(8) << endl;
}
outf << endl << endl;
}
void swap(sstruct &a, sstruct &b)
{
sstruct temp;
temp=a;
a=b;
b=temp;
}
void bubbleSort(sstruct s[50], int &nums)
{
int maxarray = 50;
int i, j;
bool swapped;
for (i = 0; i < nums - 1; i++)
{
swapped = false;
for (j = 0; j < nums - i - 1; j++)
{
if (s[j].department > s[j + 1].department)
{
swap(s[j], s[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}
int main() {
int nums=0;
double average3=0.0;
const int maxarray = 50;
sstruct s[maxarray];
init2(s);
print(s, nums, average3);
read(s, nums);
cout << "numsfirst: " << nums << endl;
avg(s, nums, average3);
cout << "nums" << nums << endl;
bubbleSort(s,nums);
print(s, nums, average3);
system("pause");
return 0;
}
I'm trying to write a script which could inline replace a printf statement with the cout statement. It could be either in sed or awk.
For example, if printf statement is
printf("int=%d, str=%s", i, s) ;
Then it should be replaced with :
cout << "int=" << i << ", str=" << s;
printf statement could have any no. of arguments. Accordingly, cout statement should be generated and replaced inline.
Other such examples could be:
printf("Statement2: %d %d %s %f", i, j, s ,f);
printf("statement3: %d %f",
i,
f);
CString failedMsg;
failedMsg.Format(_T("FAILED Triggered=%d expected=%d in %s"),
(int)i,
(int)j,
static_cast<LPCTSTR>(__TFUNCTION__));
Output:
cout << "Statement2: " << i << " " << j << " " << s << " " << f;
cout << "statement3: " << i << " " << f;
std::stringstream failedMsg;
failedMsg << "FAILED Triggered=" << (int)i
<< " expected=" << (int)i
<< " in " << __TFUNCTION__;
Yes, I'm new to StackOverflow. I'll learn the way how it works and how to ask for help. But for right now I'll just request for your gracious support to solve this.
Thanks.
───▄██▄─██▄───▄
─▄██████████▄███▄
─▌████████████▌
▐▐█░█▌░▀████▀░░
░▐▄▐▄░░░▐▄▐▄░░░░
using Perl if your printf function is on a single line not separate line; otherwise I think you cannot
for something like:
printf("int=%d, str=%s", i, s) ;
std::cout << "one" << '\n'; // just for sure
printf("one=%d, two=%d, three=%d, four=%d, five=%d", a, b , c, d , e);
for( int i = 0; i < 10; ++i ){
std::cout << i << '\n';
}
you can:
perl -ne '$T=0;/printf/ && s/printf|[ ",;()]+|%[a-z]/ /g && (#A=split) && ($T=1);if($T){$s=($#A+1)/2;$n=0;print "std::cout << ";print "\"$A[$n] \" << $A[$n+++$s] << " for 1..$s;print q("\n":),"\n";}else{print}' file
the output:
std::cout << "int= " << i << "str= " << s << "\n":
std::cout << "one" << '\n'; // just for sure
std::cout << "one= " << a << "two= " << b << "three= " << c << "four= " << d << "five= " << e << "\n":
for( int i = 0; i < 10; ++i ){
std::cout << i << '\n';
}
For something like:
printf("Statement2: %d %d %s %f", i, j, s ,f);
std::cout << "one" << '\n'; // just for sure
printf("Statement2: %d %d %s %f", i, j, s ,f);
for( int i = 0; i < 10; ++i ){
std::cout << i << '\n';
}
You can use: ( as script.sh )
perl -ne '$T=0;/printf/ && s/printf|[ ",;()]+|%[a-z]/ /g && (#A=split) && ($T=1);if($T){$n=0;print "std::cout << \"$A[0] \" << ";print " $A[++$n] <<" for 1..$#A-1;print q( "\n"),"\n"; }else{print}' file
the output:
std::cout << "Statement2: " << i << j << s << "\n"
std::cout << "one" << '\n'; // just for sure
std::cout << "Statement2: " << i << j << s << "\n"
for( int i = 0; i < 10; ++i ){
std::cout << i << '\n';
}