I wrote this, but when running it, the console just sits at "Running..." and won't really do anything, at least that I can see. I am kind of at a loss here as I can't think of anything else to do.
#include <iostream>
#include <cmath>
#include <cstdlib>
int main(void) {
int count = 0;
do {
int a = 1;
int b = 2;
int c = 3;
int total;
for (a=1;a<b;a++) {
for (b=2;b<c;b++) {
for (c=3;c<=1000;c++) {
total = a+b+c;
if (total == 1000 && a*a + b*b == c*c) {
std::cout << a << ", " << b << ", " << c;
}
}
}
}
count++;
} while(count < 1000);
return 0;
std::cin.get();
}
You might not see any output because your terminal is line-buffered and you never write a line break or flush the stream. To fix this you could add std::endl to your output line:
std::cout << a << ", " << b << ", " << c << std::endl;
This way you should see all triples as soon as they are found, but the program will still take a long time to complete. It might even take a long time till any results are found. You could speed the program up by avoiding some of the nested loops.
Related
This question already has answers here:
"std::endl" vs "\n"
(10 answers)
Closed 1 year ago.
I am trying to understand buffer, endl vs \n and why the latter is more efficient than endl.
Specifically, I am reading this
https://www.geeksforgeeks.org/buffer-flush-means-c/
The following code is supposedly output 1,2,3,4,5 at once
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << i << " " ;
Sleep(300);
}
cout << endl;
return 0;
}
whereas the following will output each integer one at a time:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << i << " " << flush;
Sleep(300);
}
return 0;
}
However, both looks exactly the same during the time of execution, they appear one a time. Is my understanding wrong?
I'm also confused why endl is less efficient compared to \n.
From what I read, endl adds a \n and then flushes.
Isn't this more efficient, since it waits for the buffer to be full then output everything at once, compared to \n?
Please correct my flawed understanding, thank you.
You will clearly see the difference if you run the following code:
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << i << " " << flush;
usleep(300000);
}
cout<<"\n";
for (int i = 1; i <= 5; ++i)
{
cout << i << " " ;
usleep(300000);
}
cout << endl;
return 0;
}
std::endl is equivalent to using "\n" and flush together.
If your program is stable then simply use "\n" else go for std::endl. "\n" is more efficient in terms that it does not force the buffer to be flushed right then.
Also in your code, you might now be able to see the difference because you are using Sleep() which takes input in milisec, so try with greater value.
On both windows and linux I am using Geany IDE and writing a c++ program. For some reason right now it is not catching an error on either linux/windows. I am using EXIT_SUCCESS and need to have the header file , right? also using copy function and it requires ? It only stops when I exclude and I discovered this because in my code I left it out by accident but it compiled, built and ran just fine.
It catches it when I use just G++, I'm not sure what is going on, could it be some setting I have in Geany?
Here is the code I'm working with
#include <iostream>
#include <algorithm>
#include <cassert>
//~ #include <cstdlib>
int main()
{
int xSize = 10;
int ySize = 50;
int xData[xSize];
int yData[ySize];
for(int i = 0; i < xSize; ++i) {
xData[i] = i;
std::cout << " First X Array value: "<< xData[i] << " " << std::endl;
}
std::cout << std::endl;
for(int i = ySize; i >= 0; --i) {
yData[i] = i;
std::cout << " First Y Array value: "<< yData[i] << " " << std::endl;
}
std::cout << std::endl;
std::copy(xData, xData+6,yData+42);
std::cout << "The copy function added it's first 6 values to the yData array starting at Y's 42nd array position" << std::endl;
for (int i = 42; i < ySize; ++i) {
std::cout << "First array value of Y is now: " << yData[i] << std::endl;
}
assert(yData[42]==0);
return EXIT_SUCCESS;
}
Thanks!
I am trying to create something that fills in an array with 100 random numbers between 1 and 100. This was working fine when it was in the main function, but when I put it in an int function nothing outputs; I must be missing something simple as I'm just beginning. What can I do to fix this?
#include "stdafx.h"
#include <time.h>
#include <math.h>
#include <iostream>
int arrayer(int ar[101], int i);
int main()
{
srand(time(0));
int ar[101];
for (int i = 1; i < 101; ++i)
{
int arrayer(int ar[101], int i);
}
return 0;
}
int arrayer(int ar[101], int i) {
ar[i] = rand() % 100 + 1;
if (ar[i] < 10) {
std::cout << i << ": " << "0" << ar[i] << std::endl;
}
else {
std::cout << i << ": " << ar[i] << std::endl;
}
return ar[i];
}
You're calling and declaring the function incorrectly. This is what it should look like:
#include "stdafx.h"
#include <time.h>
#include <math.h>
#include <iostream>
int arrayer(int ar[101], int i);
int main() {
srand(time(0));
int ar[101];
for (int i = 1; i < 101; ++i)
{
arrayer(ar, i);
}
return 0;
}
int arrayer(int* ar, int i) {
ar[i] = rand() % 100 + 1;
if (ar[i] < 10) {
std::cout << i << ": " << "0" << ar[i] << std::endl;
}
else {
std::cout << i << ": " << ar[i] << std::endl;
}
return ar[i];
}
Notice also that you're not using the return value, so you can omit that if it's not going to ever be used.
EDIT: You can actually replace the if-else for printing the value with this:
std::cout << i << ": " << setw(2) << setfill('0') << ar[i] << std::endl;
You will need to include <iomanip> to do that.
You are passing the arguments in a very wrong manner, the way of calling a function is way more different than declaring or defining it. You need to call the function as :
arrayer(ar, i);
Just pass the address of the array, ar as it is and the variable i.
Also, its better to have the function arrayer return void rather than int, since the array gets modified when passed to the function and the value gets printed in the function itself, so you don't need to return anything.
Well there is nothing in output because you never call the arrayer function!
In your loop, you just declare again the function. As the new declaration is compatible with the previous one, the compiler accepts it without error.
In order to actually call the function, you need an function call expression:
for (int i = 1; i < 101; ++i)
{
arrayer(ar, i);
}
I was trying to run the fib.exe by using this command "fib 12" so it can directly print out the result in this format:
http://pastebin.com/ytR92i8f
But with my code compiled, for instance, if I use 12 to test my program, it won' t read any number and show the result in this command "fib xx" but it will run the program and let you type the number you want in the next line and then print out the result...
I have attached the link to my main.cpp below as reference.
main.cpp : http://pastebin.com/fhUAkNQR
Because main.c can not be modify so I can only use one function to get it works.
Right now I already have the correct result.
void fibonacci(int max)
{
do
{
std::cin >> max;
}
while(max < 2 && max > 46);
std::cout << " Fibonacci Fibonacci" << std::endl;
std::cout << " N number quotient" << std::endl;
std::cout << "-------------------------------------" << std::endl;
std::cout << " 0 0 N/A" << std::endl;
std::cout << " 1 1 N/A" << std::endl;
int count = max;
int fib1 = 0, fib2 = 1;
for (int i = 2; i <= count; i++)
{
int next = fib1 + fib2;
// Add statements to print here...
std::cout << std::setw(2) << i;
std::cout << std::setw(14) << next;
std::cout << std::setw(21) << std::setprecision(17) << std::showpoint;
std::cout << static_cast<double>(next) / fib2 << std::endl;
std::cout.unsetf(std::ios_base::showpoint);
fib1 = fib2;
fib2 = next;
}
return;
}
Here's and example. Problems like the Fibonacci are better dealt with recursion. I see that with your do while loops you are trying to use some sort of recursion but it's not really working
int fibonacci(int x)
{
if (x == 0)
return 0;
if (x == 1)
return 1;
return fibonacci(x-1)+fib(x-2);
}
This basically does all you've typed in your main, just supply x and enjoy. Recursion is a difficult concept but once you've got the idea it can preform very effectively but it's pretty limited in my opinion.
I want to make a simple game of 3 players, each player moves in a block depending of the random function from 1 to 6 blocks each time, when first player has been moved the second player start and then then the third player. To do that I increase the index of an array rach time a player finish its move.
My problem is that the indexer seems no to been increased, and it stacks in the player 1 even if I increase it. I have exactly the same code in C# and it works well!
Here is the code in C++.
int main ()
{
string namesofplayers[] = {"one","two","three"};
int movementofplayers[] = {0,0,0}; // start position of players is
int gamesize = 32; //32 blocks-steps of game
int random;
int y = 0;
a:
y++;
if (y >= 3)
{
y = 0;
}
cout << "it's" << namesofplayers[y] << "turn to play";
int R = (rand() % 6 + 1);
cout << "player " << namesofplayers[y] << " moves to block" << R << endl;
movementofplayers[y] += random;
cout << movementofplayers[y];
if (movementofplayers[y] < gamesize)
{
goto a;
}
else
{
cout << "Player " << namesofplayers[y] << " wins the game" << endl;
}
}
On the off chance of doing your work, I took the liberty to write up an alternative implementation which fixes some of the problems your former code had and also produces more readable output. I also threw out the one-liners because they drive me crazy, but that's personal preference. Also, I tend to explicitly qualify symbols from the standard library using the appropriate scope.
Get rid of goto. You can browse SO and the web for multiple reasons why not to use an explicit jump like that. Just use a loop
Fix the missing initial seed for the pseudo-random number generator. If you set a varying seed, i.e. by invoking it with some variable value (e.g. time(nullptr) ), you'll always get the same succession of "random" values - with each program invocation.
Fix the use of the variable random. You tried to add some garbage-initialized value random to movementofplayers[y]. Interestingly, g++-4.7 seems to ensure that the variable is set to 1 before being used in the arithmetic op. However, the correct variable you need is R.
Return a well defined value from main().
I hope the code still does what you intended it to do:
#include <string>
#include <iostream>
int main ()
{
srand(time(NULL));
std::string namesofplayers[] = {"one","two","three"};
int movementofplayers[] = {0,0,0}; // start position of players is
int gamesize = 32; //32 blocks-steps of game
int y = 1;
while(movementofplayers[y] < gamesize)
{
if (y >= 3)
{
y = 0;
}
std::cout << "it's " << namesofplayers[y] << " turn to play" << std::endl;
int R = (rand() % 6 + 1);
std::cout << "player " << namesofplayers[y] << " moves to block " << R << std::endl;
movementofplayers[y] += R;
std::cout << "movements of player " << namesofplayers[y] <<": " << movementofplayers[y] << std::endl;
y++;
}
std::cout << "Player " << namesofplayers[y] << " wins the game" << std::endl;
return 0;
}
Here is how I would do it.
Added seeding the random number generator so you don't get the same game every time.
Added a constant for number of players to get rid of the magic number and also make it easier to expand the number of players if desired.
Got rid of the goto. Although it is possible to use goto in a reasonable way it is prone to accidental misuse, makes the code harder to follow, and makes people angry. :)
I tweaked the output and names a bit just to make it a little easier for me to test. In doing so I corrected an issue where it said the player moved to block R which was their roll for that turn, not their actual position in the game.
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
std::srand(static_cast<unsigned int>(std::time(0)));
const int gamesize = 32;
const int num_players = 3;
const std::string namesofplayers[num_players] = {"1", "2", "3"};
int movementofplayers[num_players] = {0, 0, 0};
int current_player = 0;
for(;;) //Loop forever, the game logic will exit the loop when a winner is found
{
const int roll = rand() % 6 + 1;
movementofplayers[current_player] += roll;
std::cout << "Player " << namesofplayers[current_player] << " rolls a " << roll << " and moves to block " << movementofplayers[current_player] << std::endl;
//Check if they won and if so, end the game
if(movementofplayers[current_player] >= gamesize)
{
std::cout << "Player " << namesofplayers[current_player] << " wins the game!" << std::endl;
break;
}
current_player = (current_player + 1) % num_players;
}
return 0;
}