想要学习......但是很困惑。 ! [英] Want To Learn .. But Confused. !!!

查看:64
本文介绍了想要学习......但是很困惑。 !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..希望你们的健康状况良好。 !!

我是计算机科学专业的学生[BS(CS)]在本学期,我参加了一门名为编程入门的课程。我是关于它的平均值..

但是每当教师通过使用循环(在测试中)以不同的方式提出关于打印星号的问题时,我很困惑......我不是知道该怎么做..

一些阅读的东西,文章,建议将不胜感激..



高级软件工程师请建议一些事情。 。!?

Hi .. Hope You Guys Are At Good State Of Health . !!
I''m Student Of Computer Sciences[BS(CS)] In current Semester I''m enrolled a course called Introduction To Programming. I''m about average in it ..
But whenever teacher gives a question regarding printing asterisks in different manner by using loops (in the test) I''m puzzled .. I don''t know what to do ..
Some reading stuff, articles, suggestions would be appreciated..

Senior Software Engineers Please Suggest Some Thing .. !?

#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()
{
	int M, row = 0, column = 0, space = 0;
	cout << "Please Enter The Number Of Rows to Print Stars in Descending Order: "; cin >> M;
	while (row < M)
	{
		while (column <= row)
		{
			cout << "*";
			while (space <= M - 1)
			{
				cout << " ";
				space++;
			}
			column++;
		}
		cout << '\n';
		row++;
	}
	return 0;
}

推荐答案

所以问题是你可以使用什么样的循环等?



如果是这样,对于初学者来说,看看这些:

- 控制结构 [ ^ ] < br $> b $ b - 第3课:循环 [ ^ ]
So the problem is what kind of loops you can use etc?

If so, for starters, have a look at these:
- Control Structures[^]
- Lesson 3: Loops[^]


不要先考虑代码,试着用你自己的话先描述问题。

想象一下,你必须向你的祖母解释这个问题。你会怎么解释?



也许,你从基础开始:

Don''t think in "code" first, try to describe the problem in your own words first.
Imagine, you had to explain the problem to your grand mother. How would you explain?

Maybe, you start at the basics:


  1. 我必须编写一个计算机程序,在计算机显示器上打印一些N行和列,显示一些三角形形式


  1. I have to write a computer program that prints on a computer display some N rows and columns that show some triangle of the form
....*
...**
..***
.****
*****

  • 行数和列数(N)可能因不同的方块而异(在示例中,N设置为5)
  • 因为计算机显示是行方式的填充(如在类型编写器上写文本),程序逐行写入图像,从顶部开始
  • 我使用点(。)向前移动一行中的一个字符和一个星号(*) )绘制三角形的一部分
  • 这些行打印如下:

  • the number of rows and columns (N) may vary for different squares (in the example, N is set to 5)
  • since computer displays are row-wise filled (like writing text on type writer), the program writes the image row by row, starting at the top
  • I use a dot (.) to move forward one character in the line and an asteriks (*) to draw a fraction of the triangle
  • the lines are printed as follows:



    1. 第一行打印4个点和1个星号
    2. 第二行打印3个点和2个星号
    3. ...
    4. 第二行打印1点和4个星号
    5. 最后一行打印0点和5个asteriks

  • 试图将数字5替换为N,这可以描述为

  • trying to replace the number 5 by N, this can be described as



    1. 第1行:N-1点,1个星号
    2. 第2行:N-2点,2个星号
    3. ...
    4. 第N行-1:1点,N-1 asteriks
    5. 线N:0点,N星号





  • 现在您已准备好转换为代码,称为伪代码(因为它不是像C ++这样的特定语言,您可以自由地在中编写本地语言):


    Now you are ready to translate into something like code, called pseudo code (since it is not a particular language like C++, you have the freedom to write in your native language):

    1. get size of the image which is the number of rows to print and store the value in N
    2. print N lines, one line after the other, in the following way (line number 1...N):
       1. print so many dots: N minus linenumber 
          (e.g. line 1: N-1, which results for N = 5: 5-1 = 4)
       2. print so many asteriks: linenumber (e.g. line 1: 1)



    从这个伪代码中,您现在必须找到与您的计算机语言匹配的内容,例如C ++。



    • 获取图片大小:
      cout<<提示给用户.. 。; int n; cin>> n;
    • 打印N行:
      for(int row = 1; row< = n; ++ row ){PrintLineOneBased(n,row); }
      int row = 1; while(row< = n){PrintLineOneBased(n,row);行++; }
      或者使用更常用的从零开始的计数
      for(int row = 0; row< n; ++ row){PrintLineZeroBased( n,行); }
      或等效的while循环
      int row = 0; while(row< n){PrintLineZeroBased(n,row);行++; }
    • PrintLineZeroBased(int n,int line):
      PrintSpacesZeroBased(n,line); PrintAsteriksZeroBased(n,line);
    • PrintSpacesZeroBased(int n,int line):
      / * print n-(line + 1)dots * /
      for(int col = 0; col< n-(line + 1); col ++)cout<<''。'';
    • PrintAsteriksZeroBased(int n,int line):
      / * print(line + 1)asteriks * / < br> for(int col = 0; col<(line + 1); col ++)cout<<''*'';

    • From this pseudo code, you now have to find a match to your computer language, e.g. C++.


      • get size of image:
        cout <<"prompt to the user..."; int n; cin >>n;
      • print N lines:
        for(int row = 1; row <= n; ++row) { PrintLineOneBased(n, row); }
        or int row = 1; while(row <= n) { PrintLineOneBased(n, row); row++; }
        or be using the more commonly used zero-based counting
        for(int row = 0; row < n; ++row) { PrintLineZeroBased(n, row); }
        or the equivalent while loop
        int row = 0; while(row < n) { PrintLineZeroBased(n, row); row++; }
      • PrintLineZeroBased(int n, int line):
        PrintSpacesZeroBased(n, line); PrintAsteriksZeroBased(n, line);
      • PrintSpacesZeroBased(int n, int line):
        /* print n-(line+1) dots */
        for(int col = 0; col < n-(line+1); col++) cout <<''.'';
      • PrintAsteriksZeroBased(int n, int line):
        /* print (line+1) asteriks */
        for(int col = 0; col < (line+1); col++) cout <<''*'';
      • 1. get size of the image which is the number of rows to print and store the value in N
        2. print N lines, one line after the other, in the following way (line number 0...(N-1)):
           1. print so many dots: N minus (linenumber plus 1)
              (e.g. line 0: N-1, which results for N = 5: 5-(0+1) = 4)
           2. print so many asteriks: linenumber plus 1 (e.g. line 0: (0+1))





        设计和实施的关键项目:

        1)用你的话说出问题

        2)写伪代码

        3)分成函数

        4)仔细检查循环条件(初始化,执行条件,增量)



        干杯
        Andi



        PS:这是一种通用的方法:对于像这样的小问题以及更复杂的问题。这被称为:自上而下的方法 - 你从最顶层的问题开始并将其划分为较小的问题。



        PPS:如果你必须明白什么你的初始代码做了,做一个演练:列出一张纸上的所有变量,然后逐步完成并手动写下所做的事情(变量值,输出)。在代码中添加注释以告知行的内容,例如: / *遍历所有行0 ..(N-1)* / 等。您将看到该代码已损坏,您必须决定是否要修复破碎的代码或者如果你从头开始实现自己的代码。 这是BTW每个软件开发人员的日常情况:修复或转储它。是什么推动了这一决定?商业和质量方面的考虑(以及如果我理解代码和域足以修复或重写):我能否负担得起重新编写代码,并且我可以测试代码以提供更好的置信度我所做的?你对这项工作的结果负责(在学习期间和专业期间),所以这也取决于你的声誉。



        Key items for designing and implementing:
        1) state the problem in your words
        2) write pseudo code
        3) divide into functions
        4) carefully check the loop conditions (init, execute-while condition, increment)

        Cheers
        Andi

        PS: This is a general approach: for tiny problems like this as well as for far more complex ones. This is called: "top-down approach" - you start at the top-most problem and divide it into smaller ones.

        PPS: If you have to understand what your initial code does, do a walk-through: list all variables on a piece of paper and go through step-by-step and write manually down what is does (variable values, output). Add comments to the code to tell what the lines do, e.g. /* loop over all lines 0..(N-1) */, etc. You will see that that code is broken and you have to decide if you want to fix the broken code or if you implement your own from scratch. This is BTW a daily situation of every software developer: "fix it or dump it". What drives that decision? Commercial and quality considerations (as well as if I understand the code and the domain well enough to either fix or re-write): can I afford to re-write the code, and can I test the code to give a better confidence level of what I do? You are responsible for the outcome of that work (during the studies as well as professional), so it''s also your reputation that depends on that.


        我不想发布一个解决方案,但可能会推动你朝着正确的方向发展。



        1.创建一个函数 print_stars()使用最大星号数的参数。

        2.将其他行视为从1到(最大)

        3.对于每行(n)在1和之间(最大)。

        4.写空格(最多 - n)

        5.开始写(n)



        使用for循环进行计数。

        通常,对于从0开始而不是1开始,通常你会从i = 0迭代,而(i


        从那以后,你应该可以继续使用一个好的C参考。
        I don''t want to post a solution, but maybe push you in the right direction.

        1. Create a function print_stars() taking a parameter with the max number of asterisks.
        2. Think of other lines as from 1 to (max)
        3. For each line (n) between 1 and (max).
        4. Write spaces (max - n)
        5. Write starts (n)

        Use a for loop to do the counting.
        Typically, for starts at 0 rather than 1, so normally you''d iterate from i = 0, while (i < n).

        From that, you should be able to proceed with a good C reference.


        这篇关于想要学习......但是很困惑。 !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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