there. I'm self learning C++ out of "C++ without fear". There is an exercise dealing with the GCD of 2 numbers that asks to print "GCD(a,b) =>" at each step in the proceedure. I was able to get this working:
int gcd (int a, int b);
int main() {
int i,j;
cout << "Enter the first integer" << endl;
cin >> i;
cout << "Enter the second integer" << endl;
cin >> j;
int k = gcd(i,j);
cout << "The GCD is " << k << endl;
system("PAUSE");
return 0;
}
int gcd (int a, int b){
if(b==0){
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " <<endl;
return a;
}
else {
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " << endl;
return gcd(b,a%b);
}
}
I was just wondering if there is a nicer way to go about printing each step of finding the GCD. That is, is there a "nicer" way to write this part of the code:
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " << endl;
? Thanks in advance.
You can do something like:
cout << "GCF(" << a << ',' << b << ") =>" << endl;
It is not C++ but you could use the C way of printing it which in my opinion looks better in this situation because there are far fewer stream operators, << in the way.
#include <cstdio>
printf("GCF(%d, %d) =>\n", a, b);
But this is a C way of doing things... You could use something like boost::format as is mentioned in this SO answer.
try this one
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
cout << 6+2 <<"\n" << 6-2;
}
Related
I have this function to solve the Tower of Hanoi problem and fortunately It's working good but can anybody explain to me if the function is calling it self before the cout statement in case m!=0 then how does it ever reach the cout statement or even the other call of itself ??
#include <iostream>
using namespace std;
void Hanoi(int m, char a, char b, char c){
if(m == 1){
cout << "Move disc " << m << " from " << a << " to " << c << endl;
}else{
Hanoi(m-1, a,c,b);
cout << "Move disc " << m << " from " << a << " to " << c << endl;
Hanoi(m-1,b,a,c);
}
}
int main(){
int discs;
cout << "Enter the number of discs: " << endl;
cin >> discs;
Hanoi(discs, 'A', 'B', 'C');
return 0;
}
Calling Hanoi(m), where m > 1: First it executes Hanoi(m-1) and all resulting calls. Then it executes cout. Then it executes Hanoi(m-1) and all resulting calls a second time.
Consider m == 3:
Hanoi(3)
Hanoi(2)
Hanoi(1)
cout
cout
Hanoi(1)
cout
cout
Hanoi(2)
Hanoi(1)
cout
cout
Hanoi(1)
cout
i am trying to code a family feud type game and I want to know how do I stop repeated answers? like in this code i could keep writing "bugs" and the loop would just carry on.
void f()
{
int y=0;
string q[17];
string ans1[4];
ans1[0]= "bears";
ans1[1]="bugs";
ans1[2]= "snakes";
ans1[3]="skunks";
string ans;
int sum=0;
q[0]="Name something you try to avoid when camping in the woods.";
cout << q[0] << endl;
for (int a=0; a<7; a++)
{
int b;
getline(cin,ans);
if (ans==ans1[0]||ans==ans1[1]||ans==ans1[2]||ans==ans1[3])
{
if (ans==ans1[0])
{
b=42;
cout << "SURVEY SAYS " << b << "! Good Job! " << endl; sum=sum+b;
}
else if (ans==ans1[1])
{
b=33;
cout << "SURVEY SAYS " << b << "! Nice one man! " << endl; sum=sum+b;
}
else if (ans==ans1[2])
{
b=20;
cout <<"SURVEY SAYS " << b << "! Fantastic man!"<< endl; sum=sum+b;
}
else if (ans==ans1[3])
{
b=5;
cout << "SURVEY SAYS " << b << "! You Got it!" << endl; sum=sum+b;
}
}
else if (ans!=ans1[0]&&ans!=ans1[1]&&ans!=ans1[2]&&ans!=ans1[3])
{
cout << "YOU GOT THIS ONE WRONG! "<< endl; y++;
if (y==3) { cout << "LOOOOSER" << endl; break;}
}
}
cout << " your total score for
this round is " << sum << endl;
}
You'd need to keep a list (I'd recommend std::vector) of all the accepted answers. Then, when given a new answer, you'd need to check it isn't in the list.
Alternatively, for each answer, have an wasUsed variable which you check when the answer is given and set after an answer is accepted.
I want from this program to give me the answer of a+(a+1)+(a+2)...+b but it
gives me a + b! HELP!
#include <iostream>
using namespace std;
int main(){
int a, b, c;
cout << "Enter the value of the first number" << endl;
cin >> a; cout << endl;
cout << "Enter the value of the second number" << endl;
cin >> b; cout << endl;
for(a; c <= b; c++){
c = c + 1;
}
cout << "The sum of the numbers between " << a << " and " << b << " is " << c << endl;
return 0;
}
the code you want is here:
#include <iostream>
using namespace std;
int main(){
int a, b, c=0;
cout << "Enter the value of the first number" << endl;
cin >> a; cout << endl;
cout << "Enter the value of the second number" << endl;
cin >> b; cout << endl;
for(int i=a;i<=b;i++) c+=i;//notice here !!!
cout << "The sum of the numbers between " << a << " and " << b << " is " << c << endl;
return 0;
}
The code that I posted below is supposed to work in recursion (the Sort() function) even up to 1kk times. The problem is: when the Sort() function gets into loop number 43385 the console stops working and alerts: "The program has stopped working". Is it a problem with memory? If yes, where is the bad part of the code? Greetings.
#include <iostream>
#include <string>
using namespace std;
string a, b;
int n=0,i=0,counter=0;
int Sort(int i)
{
int x=0,y=0,tmp0=0;
char tmp1;
for(x=i;x<n;x++) {
if(a[x]==b[i]){
tmp0=x;
tmp1=a[x];
break;
}
else
continue;
}
for(y=tmp0;y>=i;y--)
y==i ? a[i]=tmp1 : a[y]=a[y-1];
counter+=tmp0-i;
if(i==n-1)
return counter;
else
Sort(i+1);
}
int main()
{
cin >> n >> a >> b;
Sort(0);
return 0;
}
Perhaps a call stack overflow because of too deep recursion?
To add to iltal's comment, you may want to print out information on strings a, b: a.size(), a.length(), a.capacity(), a.max_size()
I'm not sure what this code is trying to do. Here's a revision, with some print statements added, along with a random string generator.
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
string a, b;
int n=0,i=0,counter=0;
int Sort(int i)
{
int x=0,y=0,tmp0=0;
char tmp1;
for(x=i;x<n;x++) {
if(a[x]==b[i]){
tmp0=x;
tmp1=a[x];
cout << "x = " << x << " set tmp0 to " << tmp0 << " and tmp1 to " << tmp1 << endl;
break;
}
else
continue;
}
for(y=tmp0;y>=i;y--)
y==i ? a[i]=tmp1 : a[y]=a[y-1];
counter+=tmp0-i;
cout << " endof sort: a is " << a << endl;
cout << " b is " << b << endl;
if(i==n-1) {
cout << "Returning counter " << counter << endl;
return counter;
} else {
cout << "Running sort(" << i << " + 1)" << endl;
Sort(i+1);
}
}
string randomStrGen(int length) {
static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string result;
result.resize(length);
for (int i = 0; i < length; i++)
result[i] = charset[rand() % charset.length()];
return result;
}
int main()
{
n = 50;
srand(time(NULL));
string a0, b0;
a0 = randomStrGen(n);
a = a0;
b0 = randomStrGen(n);
b = b0;
// cin >> n >> a >> b;
cout << "Max string size is " << a.max_size() << endl;
cout << "Calling sort" << endl
<< " n is " << n << endl
<< " a is " << a << endl
<< " b is " << b << endl;
Sort(0);
cout << " endof program: a inital: " << a0 << endl;
cout << " a final: " << a << endl;
cout << " b inital: " << b0 << endl;
cout << " b final: " << b << endl;
return 0;
}
counter is of type int but it has a lot of values summed in it which may be in all larger than int. maybe try int64?
You could hard code some test cases, like n = 20, a = "xyz...", b = "abc...", and add print statements to your sort function to track what is going on. Also, it may be helpful to add some comments to clarify what the purpose of the different loops are.
I'm trying to run a simple C++ program (as I'm JUST starting to learn C++) and tried this example code off a website as a program that adds two numbers together. When I execute the program, I never get errors, but c always returns as 0. Help??
#include <iostream.h>
int Add (int x, int y)
{
std::cout << "In Add(), received " << x << " and " << y << "\n";
return 0;
}
int main()
{
std::cout << "I'm in main()!\n";
int a, b, c;
std::cout << "Enter two numbers here: ";
cin >> a;
cin >> b;
std::cout << "\nCalling Add()\n";
c=Add(a,b);
std::cout << "\nBack in main().\n";
std::cout << "c was set to " << c;
std::cout << "\nExiting...\n\n";
return 0;
}
Thanks in advance.
Since Add returns 0, c will always be 0. You need to, you know, actually add the numbers you pass into Add and return that from the function.
Give this a whirl. NOTE: Add returns (x+y)
#include <iostream>
using namespace std;
int Add(int x, int y)
{
std::cout << "In Add(), received " << x << " and " << y << std::endl;
return (x+y);
}
int main()
{
std::cout << "I'm in main()!\n";
int a, b, c;
std::cout << "Enter two numbers: ";
std::cin >> a;
std::cin >> b;
std::cout << "\nCalling Add()\n";
c=Add(a,b);
std::cout << "\nBack in main().\n";
std::cout << "c was set to " << c;
std::cout << "\nExiting...\n\n";
return 0;
}
Your Add function always returns 0, so it's a mystery why c is always 0? :) It should be return x + y.