Seriously, I don’t remember..
Would an integer work??
But I don’t know why its not working...
I'm trying to add integers, like this.
int main() {
i = 1;
b = 3;
}
Signed int addition() {
i + b
}
You can't use functions and variables, including local variables, parameters, etc before they have been declared first. Though, you can initialize variables at the same time you declare them. For example:
#include <iostream>
int addition(int a, int b);
int main() {
int i = 1;
int b = 3;
int sum = addition(i, b);
std::cout << sum;
}
int addition(int a, int b) {
return a + b;
}
You have a lot of syntax errors in that code. Until you are more comfortable with programming, just start with a simple working "Hello world" example, and add one line of code at a time, compiling after each line you add. Look carefully at the error messages the compiler gives you and fix them before adding another line. You might arrive at something like this:
int main() {
int i = 1;
int b = 3;
return i + b;
}
When you run this and check the process return code (e.g. using$? in Bash) then you would get 4.
It should not be hard.
// [...]
int Add(int x, int y) { return x + y; }
using namespace std;
int main(void) {
int a = 1, b = 2;
printf("%d\n", Add(a + b));
return 0;
}
Related
#include <stdio.h>
#define swapOut(a,b) a+b-a, a+b-b
int main()
{
int a = 5;
int b = 6;
printf("%d,%d", swapOut(a+b,b-a));
return 0;
}
When executing this program, I am expecting the output to be "1,11", but the actual output is "13,1". Can someone explain what is happening here ?
To understand what's happening, you have to realise that macros are not like functions; they perform very simple text substitution. Thus, when you define
#define swapOut(a,b) a+b-a, a+b-b
and then use it as swapOut(a+b,b-a), it is expanded as a+b+b-a-a+b, a+b+b-a-b-a. With the values a=5 and b=6 this results in 13,1.
So here's a valuable lesson: always surround the arguments in macros with parentheses. The corrected version of your program is
#include <stdio.h>
#define swapOut(a,b) ((a)+(b)-(a)), ((a)+(b)-(b))
int main()
{
int a = 5;
int b = 6;
printf("%d,%d", swapOut(a+b,b-a));
return 0;
}
This indeed outputs 1,11 as you expected.
To achieve this, you have to localize the value first.
#include <stdio.h>
#define swapOut(a,b) a+b-a, a+b-b
int main()
{
int a = 5;
int b = 6;
int c = a + b;
int d = b - a;
printf("%d,%d", swapOut(c,d));
return 0;
}
I've written some code to calculate LCM for 3 numbers. My question is:
why with line:
std::cout << "";
code does work, and without this line code doesn't work?
How it is possible that printing some text on screen, can affect on how program works? It is a matter of some buffering or smth. like this?
#include <iostream>
int NWD(int a, int b){
int r;
while (r != 0){
if (a>b)
r = a-b;
else
r = b-a;
a = b;
b = r;
}
return a;
}
int NWW(int a, int b){
std::cout << ""; // without this code doesn't work
return a*b/ (NWD(a,b));
}
int NWW3(int a, int b, int c){
return NWW(c, NWW(a,b));
}
int main()
{
cout << NWW3(1,7,9);
}
In your NWD() function:
int NWD(int a, int b){
int r; // <-- notice this
while (r != 0){
if (a>b)
r = a-b;
else
r = b-a;
a = b;
b = r;
}
return a;
}
You declare r, but you didn't assign any value to r, so r is now uninitialized.
Then, you compare r with 0 in r != 0. Because r is uninitialized, this cause undefined behaviour. Undefined behaviour can do anything, from still running normally to crashing the program.
A quick fix to this problem is to initialize r with a non-zero value:
int r {-1};
Another issue is in main():
int main()
{
cout << NWW3(1,7,9);
}
It should be:
int main()
{
std::cout << NWW3(1,7,9);
}
In your main function, you have cout declared, but have not included the standard namespace along with it, like you have done on in your NWW function.
So it should look like
int main()
{
std::cout << NWW3(1,7,9);
}
You could also include " using namespace std; " under the header files.
This fills in the std:: for you as it pulls cout from its list of predefined features.
I am self-studying C++ and the book "Programming-Principles and Practices Using C++" by Bjarne Stroustrup. One of the "Try This" asks this:
Implement square() without using the multiplication operator; that is, do the x*x by repeated addition (start a
variable result at 0 and add x to it x times). Then run some version of “the first program” using that square().
Basically, I need to make a square(int x) function that will return the square of it without using the multiplication operator. I so far have this:
int square(int x)
{
int i = 0;
for(int counter = 0; counter < x; ++counter)
{
i = i + x;
}
return i;
}
But I was wondering if there was a better way to do this. The above function works, but I am highly sure it is not the best way to do it. Any help?
Mats Petersson stole the idea out of my head even before I thought to think it.
#include <iostream>
template <typename T>
T square(T x) {
if(x < 0) x = T(0)-x;
T sum{0}, s{x};
while(s) {
if(s & 1) sum += x;
x <<= 1;
s >>= 1;
}
return sum;
}
int main() {
auto sq = square(80);
std::cout << sq << "\n";
}
int square(int x) {
int result = { 0 };
int *ptr = &result;
for (int i = 0; i < x; i++) {
*ptr = *ptr + x;
}
return *ptr;
}
I am reading that book atm. Here is my solution.
int square(int x)
{
int result = 0;
for (int counter = 0; counter < x; ++counter) result += x;
return result;
}
int square(int n)
{
// handle negative input
if (n<0) n = -n;
// Initialize result
int res = n;
// Add n to res n-1 times
for (int i=1; i<n; i++)
res += n;
return res;
}
//Josef.L
//Without using multiplication operators.
int square (int a){
int b = 0; int c =0;
//I don't need to input value for a, because as a function it already did it for me.
/*while(b != a){
b ++;
c = c + a;}*/
for(int b = 0; b != a; b++){ //reduce the workload.
c = c +a;
//Interesting, for every time b is not equal to a, it will add one to its value:
//In the same time, when it add one new c = old c + input value will repeat again.
//Hence when be is equal to a, c which intially is 0 already add to a for a time.
//Therefore, it is same thing as saying a * a.
}
return c;
}
int main(void){
int a;
cin >>a;
cout <<"Square of: "<<a<< " is "<<square(a)<<endl;
return 0;
}
//intricate.
In term of the running time complexity,your implementation is clear and simply enough,its running time is T(n)=Θ(n) for input n elements.Of course you also can use Divide-and-Conquer method,assuming split n elements to n/2:n/2,and finally recursive compute it then sum up two parts,that running time will be like
T(n)=2T(n/2)+Θ(n)=Θ(nlgn),we can find its running time complexity become worse than your implementation.
You can include <math.h> or <cmath> and use its sqrt() function:
#include <iostream>
#include <math.h>
int square(int);
int main()
{
int no;
std::cin >> no;
std::cout << square(no);
return 0;
}
int square(int no)
{
return pow(no, 2);
}
The following is how I was taught to use constructors, and it seems to work for one variable, but when I use a few it seems to act odd.
I'm not too sure what to do to fix this, but I would like some direction. Thanks in advance!
#include <iostream>
#include <string>
using namespace std;
class Numbers
{
public:
Numbers (int a, int b, int c)
{
setNum (a);
setNum (b);
setNum (c);
}
void setNum (int x, int y, int z)
{
numbers = x;
digits = y;
numerals = z;
}
int getNum ()
{
return numbers;
return digits;
return numerals;
}
int add (int x, int y, int z)
{
int answer = x + y + z;
return answer;
}
private:
int numbers;
int digits;
int numerals;
};
int main ()
{
Numbers numbersobject (12,13,14);
cout << numbersobject.getNum () << endl;
return 0;
}
Odd meaning compile error?!
In the constructor, look at your setNum, it must take 3 parameters. You probably need
setNum(a,b,c);
And you CANNOT return 3 elements in getNum, using 3 return statements. If you need to return more than 2 elements, use std::tuple, or std::pair for 2 elements.
You could change your constructor to use a member initialization list
Numbers (int a, int b, int c)
: numbers {a}, digits {b}, numerals {c} {}
Or the older version
Numbers (int a, int b, int c)
{
numbers = a;
digits = b;
numerals = c;
}
I suggest you simplify your constructor
class Numbers
{
public:
Numbers(int a, int b, int c)
: numbers(a), digits(b), numerals(c) // Initialization list
{ ; }
//...
};
In your simple class, there is no need for the constructor to call a setter function.
using namespace std;
int addition (int a, int b)
{
return (a+b);
}
int subtraction (int a, int b)
{
return (a-b);
}
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return(g);
}
int main()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7,5,addition);
n = operation (20,m,minus);
cout << n;
return 0;
}
Can anybody explain this line for me
int (*minus)(int,int) = subtraction;
Thanks a lot!
int (*minus)(int,int) = subtraction; is creating a variable called minus and assigning it a pointer to the function called subtraction. if the code is valid then the function subtraction would be declared int subtraction(int a, int b);.
the best way to deal with function pointers is to make them readable using typedef.
example:
typedef int (*math_op)(int,int); // new types is math_op
int subtraction (int a, int b)
{
return (a-b);
}
math_op minus = subtraction;
later on these can be called like they are normal functions.
example:
int result = minus(10, 2); // result is now set to 8
your code rewritten:
using namespace std;
typedef int (*math_op)(int,int); // new types is math_op
int addition (int a, int b)
{
return (a+b);
}
int subtraction (int a, int b)
{
return (a-b);
}
int operation (int x, int y, math_op functocall)
{
int g;
g = functocall(x,y);
return(g);
}
int main()
{
int m,n;
math_op minus = subtraction;
m = operation (7,5,addition);
n = operation (20,m,minus);
cout << n;
return 0;
}
"minus" is a name of a variable which is a pointer to a function taking two int arguments and returning another int.
The function called "operation" takes 3 arguments: 2 ints and a pointer to a function which operates on 2 ints and return another one. When invoked, the operation function applies argument 3 to the arguments 1 and 2.
int (*minus)(int,int)
says
A pointer to a function taking two ints as arguments returning an int.
The parentheses around (*minus) are there to make sure that the asterisk binds to the name of the typedef and not the return type (i.e., the function does not return an int*).