C ++ Primer 4 / e练习1.19 [英] C++ Primer 4/e exercise 1.19

查看:60
本文介绍了C ++ Primer 4 / e练习1.19的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明:编写一个程序,提示用户输入两个数字,

将两个数字指定范围内的每个数字写入

标准输出。修改程序,使其每行打印的数量不会超过
10个数字。


i已经写了第一部分打印范围:


#include< iostream>


int main()

{

std: :cout<< 输入2个数字:;

int v1,v2;

std :: cin> v1> v2;


int lower,upper;

if(v1< = v2)

{

lower = v1;

upper = v2;

}

else

{

lower = v2;

upper = v1;

}


for(int i = lower; i< = upper; ++ i)

std :: cout<<我

<<"英寸; //两个数字之间的空格

std :: cout<< \ n谢谢你; - ) << std :: endl;

}

它有效,但我不能每行打印10个数字。我试图用
在原来的for循环中嵌入一个for循环我在程序中使用

但这不起作用。


任何提示?

STATEMENT: Write a program that prompts the user for two numbers and
writes each number in the range specified by the two numbers to the
standard output. Revise the program so that it never prints more than
10 numbers per line.

i have written the 1st part of it which prints the range:

#include <iostream>

int main()
{
std::cout << "Enter 2 numbers: ";
int v1, v2;
std::cin >v1 >v2;

int lower,upper;
if(v1 <= v2)
{
lower = v1;
upper = v2;
}
else
{
lower = v2;
upper = v1;
}

for(int i=lower; i <= upper; ++i)
std::cout << i
<<" "; // space between two numbers
std:: cout << "\nThank you ;-)" << std::endl;
}
it works but i am not able to print 10 numbers per line. i tried to
embed a for loop within the original for loop i used in the programme
but that does not work.

any hints ?

推荐答案

for(int i = lower; i< = upper; ++ i)
for(int i=lower; i <= upper; ++i)

std :: cout<<我

<<"英寸; //两个数字之间的空格


std :: cout<< \ n谢谢你; - ) <<的std :: ENDL;
std::cout << i
<<" "; // space between two numbers
std:: cout << "\nThank you ;-)" << std::endl;



int number_counter = 0;

for(int i = lower; i< = upper; ++ i)

{

std :: cout<< i<< " " ;;

number_counter ++;

if(number_counter> 9)

{

std :: cout< < " \ n";

}

}


警告:此代码仍不符合要求(故意)。

我允许你这样做。

AG。

int number_counter=0;
for(int i=lower;i<=upper;++i)
{
std::cout << i << " ";
number_counter++;
if(number_counter>9)
{
std::cout << "\n";
}
}

warning : this code still does not meet the requirements (on purpose).
I let you do it.
AG.


7月12日,12日:44 pm,AG < a ... @tb.frwrote:
On Jul 12, 12:44 pm, "AG" <a...@tb.frwrote:

for(int i = lower; i< = upper; ++ i)

std :: cout<<我

<<"英寸; //两个数字之间的空格
for(int i=lower; i <= upper; ++i)
std::cout << i
<<" "; // space between two numbers


std :: cout<< \ n谢谢你; - ) <<的std :: ENDL;
std:: cout << "\nThank you ;-)" << std::endl;



int number_counter = 0;

for(int i = lower; i< = upper; ++ i)

{

std :: cout<< i<< " " ;;

number_counter ++;

if(number_counter> 9)

{

std :: cout< < " \ n";

}


}


警告:此代码仍不符合要求(故意)。

我允许你这样做。

AG。


int number_counter=0;
for(int i=lower;i<=upper;++i)
{
std::cout << i << " ";
number_counter++;
if(number_counter>9)
{
std::cout << "\n";
}

}

warning : this code still does not meet the requirements (on purpose).
I let you do it.
AG.



是的,它有效:


int counter = 0;

for(int i =更低; i< =更高; ++ i)

{

if(counter 9)

{

std :: cout<< std :: endl;

counter = 0;

}

std :: cout<< i<< '''; //在2个数字之间插入空格

++ counter;

}

这是输出:

[ arnuld @ arch cpp]

yes, it works:

int counter = 0;
for(int i=lower; i <= upper; ++i)
{
if(counter 9)
{
std::cout << std::endl;
counter =0;
}
std::cout << i << '' ''; // inserts space between 2 numbers
++counter;
}
this is the output:
[arnuld@arch cpp]


g ++ -ansi -pedantic -Wall -Wextra ex-1.19.cpp

[arnuld @ arch cpp]
g++ -ansi -pedantic -Wall -Wextra ex-1.19.cpp
[arnuld@arch cpp]


这篇关于C ++ Primer 4 / e练习1.19的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆