I want to be able to comment only the first occurrence of cin >> n; after int main.
Doing this through bash, I've tried a combination of using grep and sed, but I'm new to bash and I'm not sure how to accomplish this.
int main(int argc, char** argv)
{
int n;
cin >> n;
cout << factorial(n) << endl;
if (n > 10)
{
cin >> n; // don't want it to change this one!
double d = log(n);
cout << d;
}
return 0;
}
You can do this with sed and a range-expression 0,/regex/s/regex/replacement/ which has the effect of putting sed at the end of it range when the replacement is made limiting the replacement to the first occurrence, e.g.
sed '0,/cin >> n/s/cin >> n/\/\/ cin >> n/' file
Result:
int main(int argc, char** argv)
{
int n;
// cin >> n;
cout << factorial(n) << endl;
if (n > 10)
{
cin >> n; // don't want it to change this one!
double d = log(n);
cout << d;
}
return 0;
}
If you have code before main that can include cin >> n, then you can use int main as the start of the range, e.g.
sed '/int main/,/cin >> n/s/cin >> n/\/\/ cin >> n/' file
Thanks to #BenjaminW
If you don't like the \/\/ look, you can choose an alternative delimiter that will cut down on the picket-fence look, e.g.
sed '/int main/,/cin >> n/s|cin >> n|// cin >> n|' file
$ awk '/int main/{f=1} f && sub(/cin >> n/,"// &"){f=0} 1' file
int main(int argc, char** argv)
{
int n;
// cin >> n;
cout << factorial(n) << endl;
if (n > 10)
{
cin >> n; // don't want it to change this one!
double d = log(n);
cout << d;
}
return 0;
}
Related
I was doing a question given vectors of itemName, price, and quantity in which I had to find out how many duplicates are there in them when combined. So I did solve the problem by using the set of tuples but then I thought of unpacking them and print them. Here's my code:
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<string> name{"ball", "bat", "glove", "glove","glove"};
vector<int> price{2,3,1,2,1};
vector<int> w{2,5,1,1,1};
set<tuple<string,int,int>> s;
for(int i = 0; i < name.size(); i++){
s.insert({name[i],price[i],w[i]});
}
for(int i = 0; i < s.size(); i++){
string st;
int p;
int we;
tie(st,p,we) = s[i]; **statement**
cout<<st<<" "<<p<<" "<<we<<'\n';
}
cout<<name.size() - s.size();
return 0;
}
But there's an error in the statement line. Can't unpack. Need help. Thanks.
Unfortunately you cant unpack a tuple using tie.
Although, if you are using c++17, you could use structural bindings like this (pardon the indentation):
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<string> name{"ball", "bat", "glove", "glove","glove"};
vector<int> price{2,3,1,2,1};
vector<int> w{2,5,1,1,1};
set<tuple<string,int,int>> s;
for(int i = 0; i < name.size(); i++){
s.insert({name[i],price[i],w[i]});
}
for(auto tup : s){
auto [a, b, c] = tup;
cout<<a<<" "<<b<<" "<<c<<'\n';
}
cout<<name.size() - s.size();
return 0;
}
In C++11/14, you might consider printing it directly:
cout << get<0>(tup) << " " << get<1>(tup) << " " << get<2>(tup) << endl;
I am not able to find the solution for the error no match for 'operator <<'
below is my code where the error is producing,
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
std::cin >> t;
while(t--) {
long int i,n,count=0,idx,min=LONG_MAX;
cin >> n;
long int s[n];
for(i = 0; i < n; i++) {
cin >> s[i];
if(s[i] <= min) {
min = s[i];
idx = i;
}
}
cout << "count= " << count << " max= " << max << " idx= " << idx << "\n";
}
}
I am getting an error like,
prog.cpp: In function ‘int main()’: prog.cpp:19:43: error: no match
for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and
‘<unresolved overloaded function type>’)
cout<< "count= "<< count << " max= " << max << " idx= " << idx <<"\n";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
can anyone pls help me to resolve this.
first of all max(line 19) is undeclared.
it's giving this error because it is trying to call std::max which is inbuilt(algorithm header file).
i guess you have typo it's min(on line 19) instead of max :).
This is my assignment, i finished writing my code and it does compile but it is not giving the right answer. So i know there is some logical error but i cannot find out what.
Plz check and tell me, simple code to calculate permutations and combinations
#include <iostream>
using namespace std;
int factorial(int n)
{
if (n == 0 or n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
int permutation(int a, int b)
{
//factorial(a);
int perm = factorial(a) / factorial(a - b);
return perm;
}
int combination(int a, int b)
{
int permutation(int a, int b);
int factorial(int n);
return permutation(a, b) / factorial(b);;
}
int main()
{
int n;
int r;
cout << "Enter n: " << endl;
cin >> n;
cout << "Enter r: " << endl;
cin >> r;
int factorial(int n);
int permutation(int n, int r);
int combination(int n, int r);
if (n >= r)
{
cout << "Permutuation: " << permutation << endl;
cout << "Combination: " << combination << endl;
}
else
{
cout << "Invalid" << endl;
}
return 0;
}
The answer given by the console
Enter n:
6
Enter r:
5
Permutuation: 00AC1375
Combination: 00AC133E
There are a number of errors in your code. First, you should not re-declare your 'permutation' and 'combination' functions inside other functions.
EDIT: Actually, this is not an error but, in my opinion, very bad practice. You could accidentally 'hide' the actual function declaration, which is already provided as you have defined both before any of the calling functions.
Second, your cout << permutation << endl; code is printing a function! This will be taken as meaning the address of that function, which is what you are seeing (HEX addresses).
Here's a 'fixed' version that works (with comments).
#include <iostream>
#include <iso646.h> // Need this in order to use "or" in place of "||"
using namespace std;
int factorial(int n)
{
if (n == 0 or n == 1) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
int permutation(int a, int b)
{
//factorial(a);
int perm = factorial(a) / factorial(a - b);
return perm;
}
int combination(int a, int b)
{
// int permutation(int a, int b); // You don't need to redeclare function inside another one ...
// int factorial(int n);
return permutation(a, b) / factorial(b);;
}
int main()
{
int n, r;
cout << "Enter n: " << endl;
cin >> n;
cout << "Enter r: " << endl;
cin >> r;
// int factorial(int n);
int p = permutation(n, r); // This is how to call your functions ...
int c = combination(n, r); // and assign their returns to values.
if (n >= r) {
cout << "Permutuation: " << p << endl; // Output the values ...
cout << "Combination: " << c << endl; // ...from the functions
}
else {
cout << "Invalid" << endl;
}
return 0;
}
Feel free to ask for further explanations if there's anything I've done that you don't understand.
I was trying to solve
In which you have given a array of int and you have to return its sum.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main(){
int n;
cin >> n;
int k;
vector<int> arr(n);
for(int arr_i = 0;arr_i < n;arr_i++){
cin >> arr[arr_i];
k = k + arr[arr_i];
//cout << "arr = " << arr[arr_i] << " k " << k << endl; // [0]
if (arr_i == (n-1))
{
cout << k;
}
}
return 0;
}
This return a afkard no. instead of sum.
But when uncomment out [0] line. code starts working as it should.
P.S. i found out the solution by changing cout to cerr.
but wanted to know why it doesn't work.
as the other answer, initialize k, move if outside of loop
vector<int> arr(n);
int k = 0;
for(int arr_i = 0;arr_i < n;arr_i++){
cin >> arr[arr_i];
k = k + arr[arr_i];
//cout << "arr = " << arr[arr_i] << " k " << k << endl; // [0]
}
cout << k;
as your question, there is no need std::vector anymore
int sum = 0, num;
for(int arr_i = 0;arr_i < n;arr_i++){
cin >> num;
sum = sum + num;
}
std::cout << sum << std::endl;
Initialize k before you use, else it will contain junk value int k = 0;
As the other answers (+1) suggested, you need to initialize k. However, I think it is slightly better to assign it the first value initially and reduce the number of iterations therefore:
vector<int> arr(n);
if (n > 0) {
int k = arr[0];
for(int arr_i = 1;arr_i < n;arr_i++){
//Your cycle
}
}
I have to build a program as bellow:
User enters 3 Integers: a, b, c
User enters one integer n, and then n integers (ascending, and different numbers)
Program checks all possible pairs (x,y) (with x!=y) of the numbers entered if verifies the equation ax^2 + by^2 = c and prints all pairs which verifies the equation.
I done the program as bellow:
#include<iostream>
using namespace std;
int main() {
int a,b,c,n,i,j;
cin >> a;
cin >> b;
cin >> c;
cin >> n;
int num[n];
for(i=0;i<n;i++) {
cin >> num[i];
}
for (i=0;i<n;i++)
for (j=i+1;j<n;j++) {
if(a*num[i]*num[i]+b*num[j]*num[j] == c) {
cout << "(" << num[i] << "," << num[j] << ")";
}
if(a*num[j]*num[j]+b*num[i]*num[i] == c) {
cout << "(" << num[j] << "," << num[i] << ")";
}
}
return 0;
}
I made it by O(nlogn) with two 'for' statements but i know it could be done by O(n).
NOTE THAT MY PROGRAM WORKS AND I DON'T NEED TO ADD EXPECTED OUTPUT AND MY CURRENT OUTPUT AS YOU SAID IN THE COMMENTS. I ONLY WANT IT TO BE O(N) not O(nlogn) -> I WANT AN OPTIMIZED VERSION OF THE CODE!
How can I do this?
Example of running program: a=1, b=1, c=20
Then n = 5
Then n numbers: 2 3 4 9 18
Program will show all pairs (x,y) which verifies the equation x^2 + y^2 = 20. In this case it shows (2,4)
and (4,2).
Thank you!
Assuming 0 based index...
Set i=0
Set j=n-1
While i<n or j>=0
Set sum=a(num[i]^2)+b(num[j^2)
If sum==c then found pair, and increase i
If sum<c increase i
If sum>c decrease j
I found exactly this problem solved here: http://lonews.ro/educatie-cultura/22899-rezolvare-admitere-universitatea-bucuresti-2015-pregatire-informatica.html and changed a bit to show the pairs (it originally shows the number of pairs).
#include <iostream>
using namespace std;
int main()
{
int a, b, c, n, i=0;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "n = ";
cin >> n;
int s[n];
for(i=0; i<n; i++) {
cout << "s[" << i+1 << "] = ";
cin >> s[i];
}
int j=n-1;
i = 0;
while(j>=0 || i<n) {
if(a*s[i]*s[i] + b*s[j]*s[j] == c) {
cout << "(" << s[i] << "," << s[j] << ") ";
i++;
}
if(a*s[i]*s[i] + b*s[j]*s[j] < c) {
i++;
}
if(a*s[i]*s[i] + b*s[j]*s[j] > c) {
j--;
}
}
return 0;
}