[英]How to increment each printed row to the power 2 to the power of n and n-1 in C++?
Trying to write a program in C++ that prints rows of "*" based on the result of 2^n then after a certain number is entered decrease 2^n-1.尝试在 C++ 中编写一个程序,根据 2^n 的结果打印“*”行,然后在输入某个数字后减少 2^n-1。 For example any where between 0 and 6 (both of these being n) a row of stars is printed from *, **, **** and so forth.
例如,在 0 到 6(这两个都是 n)之间的任何位置,从 *、**、**** 等开始打印一行星号。 Then anything between 7 and 12 the number of stars decreases 2^n-1.
然后在 7 到 12 之间的任何东西,星星的数量都会减少 2^n-1。
So far I have this but it only prints one singe line of * based on the result of the exponent.到目前为止,我有这个,但它只根据指数的结果打印一行 *。
int base = 2, exponent, result;
char star = '*';
cout << "Enter a number between 0 and 12: ";
cin >> exponent;
result = pow(base, exponent);
string pattern = string(result, star);
cout << pattern << endl;
If user enters 4
Output; ********
Goal for it to look like this让它看起来像这样的目标
2^0 = *
2^1 = **
2^2 = ****
2^3 = ********
2^4 = ****************
2^5 = ********
2^6 = ****
2^7 = **
2^8 = *
Hope that makes sense.
Right now you are taking the input and storing it in your exponent variable, and then only printing once.现在,您正在获取输入并将其存储在指数变量中,然后只打印一次。
What you want to do is create a loop that varies your exponent up to maximum value based on the input you got.您想要做的是创建一个循环,根据您获得的输入将您的指数变为最大值。
ie for the increasing part即对于增加的部分
int max_num;
cin >> max_num
for (int i; i < max_num; i++){
// code to print 2^n stars, with n=i
}
You can write a similar loop for the decreasing part by counting down.您可以通过倒计时为递减部分编写类似的循环。
int max_num;
cin >> max_num
for (int i; i >= 0; i--){
// code to print 2^n stars, with n=i
}
Sorry i needed to edit your code.抱歉,我需要编辑您的代码。 you have to use iteration for you to achieve that output.
您必须使用迭代来实现 output。
int rvalue = 1;
int i;
string outputDisplay = "";
int base_ = 2;
int exponent = 0;
cout << "Enter a number between 0 and 12: ";
cin >> exponent;
for (i = 1; i <= exponent; i++)
rvalue = rvalue * base_;
while (rvalue != 0)
{
outputDisplay += "*";
rvalue--;
}
cout << outputDisplay << endl;
On this part im getting the X^n output without using any library (Math.Pow for example).在这部分我得到 X^n output 而不使用任何库(例如 Math.Pow)。
for (i = 1; i <= exponent; i++)
rvalue = rvalue * base_;
For the output you just need to add "*" on a string depending on the rvalue or the X^n value.对于 output,您只需根据右值或 X^n 值在字符串上添加“*”。
while (rvalue != 0)
{
outputDisplay += "*";
rvalue--;
}
On this case i inputted 2 and 4 initially on the variable, output will be like;在这种情况下,我最初在变量上输入了 2 和 4,output 会像这样;
Ok so the problem is that you need a loop, i tried to not edit your variables this is the best i could do:好的,问题是你需要一个循环,我试图不编辑你的变量这是我能做的最好的:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int base = 2, exponent, result;
char star = '*';
int aux=1;
cout << "Enter a number between 0 and 12: ";
cin >> exponent;
for(int i=0;i<=exponent;i++)
{
cout <<" 2 ^ "<<i<<" = ";
result = pow(base, i);
for(int j=1;j<=result;j++)
{
cout<<star;
}
cout<<endl;
}
aux=exponent;
for(int i=exponent-1;i>=0;i--)
{
cout <<" 2 ^ "<<aux<<" = ";
aux++;
result = pow(base, i);
for(int j=1;j<=result;j++)
{
cout<<star;
}
cout<<endl;
}
return 0;
}
the first for loop is when the numbers are under the exponent and the other are for when the numbers are above the exponent.第一个 for 循环是当数字低于指数时,另一个是当数字高于指数时。 Hope that helps you, i will provide further help if needed!
希望对你有帮助,如果需要我会提供进一步的帮助!
This is the photo of the run window click to see image这是运行 window 的照片点击查看图片
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.