Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Enter an odd value greater than zero -- I understand how to do that.
Print out a triangle that looks like the following if 5 is entered:
54321
432
3
If 11 is entered:
10987654321
098765432
9876543
and so on
I see that we must divide the input by 10 and print the remainder but I'm having trouble printing the countdown.
for (i = n; i >= 1; i--)
i = i - 1;
use a for loop (start at 0), and on each iteration print a substring from i to string.length()-1 an use the set width to increase the indent, following code below:
string num = "10987654321";
for (int i = 0; i < num.length(); ++i){
cout << setw(i) << right << num.substr(i, num.length()-i) << endl;
}
This should give you your desired output. (if "<< right <<" doesn't work, swap "right" with "left")
ALSO
you need to include iomanip to use setw()
int num = 10987654321;
int numSpaces = 1;
cout << num << endl;
for (int i = 0; i >= 1; i--)
{
for (int j = 0; j < numSpaces; j++)
{
cout << " ";
}
cout << num % 10 << endl;
num = num / 10;
numSpaces++;
}
Voila a beautiful triangle :)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
int n;
cin >> n; cin.ignore();
for(int i = 1; i < 11; i++){
cout << i * n << " ";// stop printing space at the end number
}
Desired output-> "1 2 3 4 5 6 7 8 9 10"
The standard approach is to use a boolean variable, which is one time either true or false, then print a space or not, depending on the boolean, and set the boolen to a new value.
Often the output of the space in the loop is done first and then then value.
Example:
bool printSpace = false;
for(int i = 1; i < 11; ++i) {
if (printSpace) std::cout << ' ';
printSpace = true;
std::cout << i * n;
}
You could also use std::exchange for flipping the bool. Please read here.
Then you could do something like the below:
#include <iostream>
#include <vector>
#include <utility>
int main() {
std::vector data{1,2,3,4};
bool showComma{};
for (const int i: data)
std::cout << (std::exchange(showComma, true) ? "," : "") << i;
}
And the super high sophisticated stuff will be done with the std::ostream_joiner.
Reade here for an example. But, unfortunately this is not available for many installations.
A simple approach is the following
for( int i = 0; i < 10; i++){
if ( i ) cout << ' ';
cout << ( i + 1 ) * n;
}
or
for( int i = 1; i < 11; i++){
if ( i != 1 ) cout << ' ';
cout << i * n;
}
One-liner:
cout << (i > 1 ? " " : "") << i * n;
Note: I prefer the "prefix" form because the starting value is easier to know than the ending one (>1 vs. >9).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Every time I run this code in Visual Studio 2015 it shows the error identifier "treasureLocation" is undefined... what am I doing wrong?
int main()
{
int gridSize [2];
int gridX = 0;
int gridY = 0;
int treasureLoaction[2];
int end;
std :: cout << "what size grid would you like to play on?" << std :: endl;
std :: cin >> gridSize [1];
std :: cin >> gridSize [2];
treasureLocation[1] = rand() % gridSize[1] + 1;
treasureLocation[2] = rand() % gridSize[2] + 1;
while (gridY < gridSize[2]) {
gridY++;
while (gridX < gridSize[1]) {
std::cout << "* ";
gridX++;
}
std::cout << "" << std::endl;
gridX = 0;
};
std::cout << treasureLoaction;
std::cin >> end;
return 0;
}
There is a typo
int treasureLoaction[2];
^^^^^^^^
Take into account that if an array has n elements then the valid range of indices is [0, n-1]
And this statement
std::cout << treasureLoaction;
does not outputs the elements of the array as you might think.
You could do it in a loop as for example
for ( int x : treasureLocation ) std::cout << x << ' ';
std::cout << std::endl;
Try iterating through the array to print its contents like this
for (int i = 0; i < your array length; i++) cout << array[i];
unless you are trying to print the address, and it also looks like you have a spelling error in treasureLoaction.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include
using namespace std;
int main()
{
int n, i;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: " << endl;
for(i = 1; i <= n; ++i)
{
if(n % i == 0)
cout << i << endl;
}
return 0;
}
I understand the below problem of finding factors of numbers. But i want to do a c++ program which only show the numbers which have 5 or more factors. suppose i give a range of numbers 15 to 20.then it will print only those numbers those have 5 or more factors. such as example if i give a range 15 to 20 then it will print out only 16,18,20.because these 3 integers have 5 or more factors in 15 to 20 range. i couldnt understand how to do that code so i am asking.
As I understood you are searching the tech finding number prime factors of an natural number. Firstly the code you published is for getting all the divisor's of given positive number. But Finding its prime factors a little bit different but the idea same as you used (modular arithmetic)
this is a very simple version of achieving your task (but needs optimization)
#include <iostream>
//This function does not handle the repeating factors count
int numberOfPrimeFactors(int number) {
int count = 0;
for ( int i = 2; i <= number; ++i ) {
while ( number % i == 0 ) {
number /= i;
count++;
}
}
return count;
}
int main() {
int Rbegin = 1;
int Rend = 100;
for(int i = Rbegin; i<Rend; ++i) {
if(numberOfPrimeFactors(i) >= 5)
std::cout << i << " has 5 or more prime factor"<< std::endl;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am working in c++ and I am trying to find the 1001th prime number. When I run this code it throws error on line 14 that the function is overloaded. Can you tell me why it is throwing me the error? Thanks.
#include <iostream>
using namespace std;
int main() {
cout << "Hi in this program I will tell you the 1001th prime number" << endl;
int number;
for (int i = 2; i < 1000000; i++) {
for (int prime = 2; prime < 1000000; prime ++) {
if (prime % i != 0) {
for (int count = 1; count < 100000; count ++) {}
cout << count << prime << " is a prime number" << endl;// this line has the problem
}
}
}
}
The "count" variable has it's scope only within the "for" loop. That loop terminates with the '}' character at the end of line.
The count you are referencing on your cout line is from somewhere else. I assume that somewhere in global scope you're pulling in definitions of two "count" functions and the compiler thinks you want to refernce one of those, but doesn't know which one to take.
Take a look at your curly braces after the for loop:
for (int count = 1; count < 100000; count ++) {}
You're closing the for loop block before you get to the next line, where you reference 'count'. So, the count variable is not in scope when you try to print it. Try changing it to this:
if (prime % i != 0) {
for (int count = 1; count < 100000; count ++) {
cout << count << prime << " is a prime number" << endl;
} // closing curly brace is here now :)
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to make a part of the game hangman, where the user inputs a letter and then a loop check for that letter in a random array. If the letter is found, it then couts a changed array including now that letter and offers the user to again, input another letter. It seems for loops are not working since the program doesnt scan the whole array for ever letter inputted. How can I fix this?
int main(){
string guess[25];
string password[5];
srand((unsigned)time(0));
string letters[5] = {"_ ","_ ","_ ","_ ","_ "};
char array[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','z'};
for(int r = 0; r < 5; r++){
int g = rand() % 24;
password[r] = array[g];
}
cout << endl;
for(int z = 0; z < 25; z++){
cout << "Enter Letter: " << endl;
cin >> guess[z];
for(int b = 0; b < 5; b++){
if(uguess[z] == password[b]){
letters[b] = guess[b];
cout << letters[b];
}else{
cout << letters[b];
}
}
cout << endl;
}
can someone point me in the right direction. Thanks
It always says that the word being guessed if asdfg, but it messes it up very badly, as in doesn't always show the letter, even if it has been guessed, it shows it later.
crke[b] = ugib[b];
This line should be:
crke[b] = ugib[z];
You might want to consider investing some time in learning how to use a debugger, which would've helped you figure it out.