C ++需要这一方面的帮助 [英] C++ need help with this one

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

问题描述

我在使用这种语言时遇到麻烦,并且在您的回答中明确(例如,确切的去向以及出于什么原因)将极大地帮助我,我似乎无法理解这些时间和语句的时间和位置.放置以便使它们工作,以及应将cin放置在什么位置以使其处于正确的顺序.
感谢所有提供帮助的人,我真的需要在阅读本文时进行解释,这些材料使我感到困惑.


编写一个程序,要求用户输入多少个数字
他们想进入.

从用户那里读取那么多数字并计算
总数,平均数,最高和最低数字,
以及偶数和奇数

然后,您的程序应将所有这些信息显示回
向用户展示.您的程序还应该使用for循环来
控制用户输入的数字数量.



I am having trouble with this language and being specific in your answers (ie. exactly where things go and for what reason please)will help me a great deal I cant seem to understand when and where these if while and for statements are to be placed in order for them to work and where the cin should be placed in order for it to be in the correct order.
thanks to all who help I really need explanations on this reading the material is getting me confused.


write a program that asks the user to enter how many numbers
they would like to enter.

Read in that many numbers from the user and calculate the
total, average, highest, and lowest numbers,
and the number of even and odd numbers.

Your program should then display all of that information back
out to the user. Your program should also use a for loop to
control the number of numbers entered by the user.



#include <iostream>

using namespace std;

int main() {

	int total = 0;
	int grade = 0;

	for (int curGrade = 5; curGrade > 0; curGrade--) {
		cout << "Enter a grade: ";
		cin >> grade;
		total += grade;
	}

	cout << "Average = " << total / 5;

	return 0;
}

推荐答案

首先,我将为您提供代码,然后再进行解释.

First I will give you the code, then I wil explain.

/*
Collect numbers from user, then perform basic operations on them.
*/
#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
//first step: declare our variables
    int i=0; //holds the index for our loop.
    int input=0; //holds the numbers a user would like to collect.
    int lowest = 0; //holds the lowest number.
    int highest = 0; //holds the highest number.
    int total = 0;
    cout << "How many numbers do you want to input?";
    cin >> input;
//Now we use an if statement to make sure input isn''t less than one, or more than 100.
    if ((input < 1) || (input > 100)) {
        cout << "You must use a number between 1 and 100." << endl;
        return 1;
    }

//now we create an array.
    int *numbers = new int[input];
//now we iterate through the array and accept numbers.
    for (i=0; i < input; i++) {
        cout << "Please enter entry " << i+1 << "." << endl;
        cin >> numbers[i];
    }

//now we iterate through again and pick out the numbers.
//first, we set lowest to numbers[0], otherwise it won''t get set
    lowest = numbers[0];
    for (i=0; i < input; i++) {
//add the entry to the total.
        total += numbers[i];
//check for the highest number.
        if (numbers[i] > highest) {
            highest=numbers[i];
        }
        if (numbers [i] < lowest) {
            lowest=numbers[i];
        }
    }

//now we show the stetistics:
    cout << "Average: " << total/input << "." << endl;
    cout << "lowest: " << lowest << " highest: " << highest << "." << endl;
    delete []numbers;
    return 0;
}
</iostream>



现在:这是我的解释:



Now: here is my explaination:

//first step: declare our variables
    int i=0; //holds the index for our loop.
    int input=0; //holds the numbers a user would like to collect.
    int lowest = 0; //holds the lowest number.
    int highest = 0; //holds the highest number.
    int total = 0;


如代码所示,这只是声明变量.最好将声明放在顶部,因为它们更易于查找.


As the code says, this just declares the variables. It is good to have your declarations at the top, because they are easier to track down.

    cout << "How many numbers do you want to input?";
    cin >> input;
//Now we use an if statement to make sure input isn''t less than one, or more than 100.
    if ((input < 1) || (input > 100)) {
        cout << "You must use a number between 1 and 100." << endl;
        return 1;
    }


因此,我们从用户那里获得输入,然后进行一些错误检查.这是您要做的习惯;从用户那里获得输入时,请检查一些界限.如果没有,您将得到一些聪明的黑客说:我想知道如果我提供-1会发生什么!


So, we get the input from the user, then do some error checking. This is something you''ll want to get in the habbit of doing; when getting input from the user, check on some bounds. If not, you''ll get some clever hacker that says, "I wonder what happens if I supply -1!

//now we create an array.
    int *numbers = new int[input];


我知道您还没有介绍过,但这是执行此操作的较好方法之一.另一种方法是将所有内容加起来,然后在得到输入时进行计算.我还将为此提供代码.
基本上,数组只是一组变量,您最终将要使用它们,但是它是保存相同类型的多个变量的一种方法,因此您可以执行以下操作:


I know you haven''t covered this, but this is one of the better ways to do it. Another way would be to add everything up, then do your calculations as your getting input; Iwill provide the code for this, as well.
Basically, an array is just a set of variables, which you will get to eventually, but it''s a way to hold multiple variables of the same type, so you could do something like:

int tttBoard [3][3];


您必须习惯的一件事是从0索引开始,而不是一个索引.


One thing you''ll have to get used to that we use is starting at a 0 index, rather than one.

//now we iterate through the array and accept numbers.
    for (i=0; i < input; i++) {
        cout << "Please enter entry " << i+1 << "." << endl;
        cin >> numbers[i];
    }


这是您要询问的循环之一,所以让我解释一下:
for循环将按顺序执行某些操作,直到满足条件为止,因此将其分开.


This is one of the loops you were asking about, so let me explain:
A for loop will do something in order until the condition is met, so lets split it apart.

for (i=0...


这基本上将您的计数器(i)设置为0.


This basically sets your counter (i) to 0.

i < input


在此条件为真时,循环将继续进行.换句话说,虽然我小于输入.当我等于输入时,循环将结束.


The loop will keep going while this condition is true. In otherwords, while i is less than input. When I is equal to input, the loop will finish.

i++


每次循环运行(也称为迭代)时,I都会递增.
因此,总而言之:循环从零开始,直到n小于输入(这是用户请求的最大数量).因此,这之所以有效,是因为正如我们前面所述,数组从零索引开始运行.


Fore each time the loop is ran, (otherwise known as an iteration), I is incremented.
So, to sum up: The loop starts I at zero, the n runs until I is less than input (which is the max amount of numbers the user requested). So, this works because as we stated earlier, arrays run off of a zero index.

//now we iterate through again and pick out the numbers.
//first, we set lowest to numbers[0], otherwise it won''t get set
    lowest = numbers[0];
    for (i=0; i < input; i++) {
//add the entry to the total.
        total += numbers[i];
//check for the highest number.
        if (numbers[i] &gt; highest) {
            highest=numbers[i];
        }
        if (numbers [i] < lowest) {
            lowest=numbers[i];
        }
    }


同样,我们只是使用if语句运行循环.
一个if语句看起来像这样:


Again, we just run the loop over, with an if statement.
An if statement looks something like this:

if (condition)
{
do something
}


因此,如果满足条件,例如:


So, if the condition is met, for example:

if (numbers[i] < lowest)


它基本上检查数字是否小于数字[i],如果等于,则为true,我们在花括号内执行代码.
其余代码只是从堆中删除数组,打印结果,然后返回0.
希望对您有所帮助.这些概念不容易在论坛上解释,而需要一本好书.我个人建议Bruce eckel用C ++进行思考.它是免费下载的,只需用Google标题即可.跳过第一章(基本上只是讨论OOP,直到稍后才会使您感到困惑),然后继续进行研究.


which basically checks to see if the number is lower than numbers[i], and if so evaluates to true, we execute the code within the braces.
The rest of the code just deletes the array from the heap, prints the results, and returns 0.
I hope this helped. These are concepts that are not easily explained over a forum, but rather with a good book. I personally recommend thinking in C++, by Bruce eckel. It''s a free download, just google the title. Skip chapter one (which just basically talks about OOP and will confuse you until later), and dig in.


#include <iostream.h>
#include <conio.h>
int main()
{
    clrscr();
    top:
    int size = 0;
    int sum,avearge,odd,even;
    cout<<"How many nos. do you want to enter";
    cin>>size;
    int *array = new int[size];
    count<<"Enter the numbers";
    for (int i = 0; i<size; i++)
    {
       cin>>array[i];
    }
    cout<<"Press 1 for sum\n"
        <<"Press 2 for average\n"
        <<"Press 3 for finding out highest and lowest\n"
        <<"Press 4 for finding out odd and even numbers\n";
   int opt;
   cin>>opt;
   switch(opt)
   {
      case 1:
      {
         for (int j=0 ; j<size; j++)         
         {
            sum+=array[j]; 
         }
         cout>>"The sum of the nos:\t"<<sum;
         break;
      }

      case 2:
      {
          cout>>"The average of the nos:\t"<<sum/size;
          break;
      }
      case 3:
      {
         for (int k=0; k<size;k++)         
         {
           if(array[k]%2==0)
           {
               odd++;
           }
           else
           {
               even++;
           }
         }
         cout>>"The total no. of odd nos:\t">>odd;
         cout>>"The total no. of even nos:\t">>even;
         break;
      }
      default:
      {
         cout>>"Sorry! No options available\n";
      }
   }
   delete[] array;
   cout>>"Do you want to continue(Y/N)?";
   char ans[1];
   cin<<ans;
   if(ans=="Y" || ans=="y")
   {
       goto top;
   }
   getch();
   return 0;
}


我认为不需要在那里进行切换.总计是指所读取的所有数字的总和?如果是这样,我的代码就是这样
我在for循环中重复了部分代码,因为它也需要在其中进行比较.我会尝试解释一下.
需要比较或尝试条件时使用如果",例如如果我的成绩大于60,则我通过".
如果"if"不正确并且您想要另一个比较(如果在它之后使用另一个)或替代方法,则使用"else",例如,如果1的分数大于60,则我通过,否则,我将失败). br/> 在需要重复已知次数的次数时使用"for".抱歉,如果我无法更好地解释它.
希望我能有所帮助.

I think there is no need for a switch over there. Total do you mean the sum of all the numbers read? if it is, my code would be like this
I repeat part of the code inside the for loop because it needs to compare it inside it too. I''ll try to explain a bit.
"if" you use when you need to compare something or try a condition, like in "if i get grade greater than 60, i pass".
"else" is used in case when the "if" is not correct and you want another comparison(using another if after it) or an alternative,for ie if 1 get a grade greater tha 60 i pass, else i fail).
"for" you use when you need to repeat something for a known number of times. Sorry if i couldn''t explain it better.
hope i helped a bit.

#include <iostream>;
#include <conio.h>//is not recommended to use it because is not considered standard
using namespace std;
int main()
{
  int qtyNumbers,odd=0,even=0,number,highest=0,lowest=0,total=0,i;
  float average=0;

  cout << "Please Informe the quantity of numbers you wish to enter: ";
  cin >> qtyNumbers;
  cout << "Enter a number: ";
  cin >> number;
  if(number % 2 ==0) //if the rest of number divide by 2 is zero
  {
    even++; //adds 1 to even
  }
  else odd++; //else the number is odd
  highest = number; // i placed this one outside the for because you will compare the other values against this one and the lowest one below, and also because the first value read will be both the lowest and the highest ones.
  lowest = number;
  total=total + number;
  for(i=1;i<qtyNumbers;i++) /*Like i explained above, for is used to repeat something for a known number of times, so, here you will repeat it for about the same number as the person asked inputs, if she inputs 7 numbers in the first question, then it will repeat here 6 times because you are already reading one number before the "for" loop. You use it whenever you need to repeat the same thing several times(but a limited number)*/
  {
     clearscr();
     cout << "Enter a number: ";
     cin >> number;
     total=total+number;
     if(number % 2 ==0) //if the rest of number divide by 2 is zero//this if goes here because every time a number is read, you need to compare if it is even or odd, that''s when you use "if"
     {
       even++; //adds 1 to even
     }
     else odd++; //"else" is always used(when needed) after an "if"

     if(number > highest)//this is another condition you''re trying,if the number is lower than the first number read.
     {
       highest = number; //if the above condition is true, than it will enter here and modify the highest number.
     }
     else
     if(number < lowest)
     {
       lowest = number; //else if the number is lower than the lowest one before, it will modify it. a good thing to explain is that it "enters" the scope only if the condition is true in here, if both cases are not true, > highest or < lowest, nothing will happen to those values(lowest and highest).
     }
  }
  average = (float)total/qtyNumbers; //need to cast(force it to be float) it to get division correcly
  cout << "the total is: "<<total <<endl;
  cout << "the average value is: " <<average <<endl;
  cout << "the highest number is: " <<highest <<endl;
  cout << "the lowest number is: " <<lowest <<endl;
  cout << "The total of odd numbers is: "<<odd<<endl;
  cout << "The total of even numbers is: "<<even<<endl;

}


这篇关于C ++需要这一方面的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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