我的代码中涉及while循环和数组的问题

时间:2016-06-04 11:29:22

标签: c++ arrays

我被要求就这些问题做一个程序部分。

  1. 显示Int A [20] [50],Use While循环中包含的每一行的最后一个元素。
  2. 分配一个数组D,存储从1到N的整数平方。
  3. 一个while循环,用于确定从A到B的所有奇数整数的乘积。
  4. 1

    While(a<20){
    a++
    cout<< A[a][50]<<"\n";
    }
    

    编辑1:

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    int a,n,m;
    int  A [20][50]={};
    a=1;
    cout<<"enter your number:"<<endl;
    cin>>n;
    cin>>m;
    cout <<A [n][m] <<"\n";
        while (a> 20){
             cout << A [a][49]<<"\n";
    }
        system ("pause");
        return 0;
    

    编辑2:

     #include <iostream>
        #include <string>
        using namespace std;
        int main()
        {
        int a,n,m;
        int  A [20][50]={};
        a=1;
        cout<<"enter your number:"<<endl;
        cin>>n;
        cin>>m;
        cout <<A [n][m] <<"\n";
            while (a < 20){
    
                 cout << A [a][49]<<"\n";
                }
                 a++;
           }
            system ("pause");
            return 0;
          }
    

    2

    float num; float d[num];
     cout<<"How many number do you want to enter to find there squure?"<<endl;
     cin>>num;
     for(int i; i<num; i++){
     cin>>d[i];}
    for(int i; i<num; i++) {
    result[i]=d[i]*d[i];}
     for(int i=0; i<num i++){
     cout<<result[i];
    

    编辑1

    #include <iostream>
    #include <string>
    #include<iomanip>
    using namespace std;
    int main()
    {
    int b;
    int x;
    int A[]={};
    int N;
    int n;
       cout <<"enter value for N:"<<endl;
       cin>>N;
    x=1;
    n=N;
    while (x<=N){
       b =x*x;
    A[n]=b;
      x++ ;
    }
    cout <<A[n]<<"\t";
        system ("pause");
        return 0;
    }
    

    编辑2

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
      int x=1,b;
      int N=5;
      while (x<=N){
        b=x*x;
    cout<<b<<"\n";
    x++;
    }
    system("pause");
    return 0;
    }
    

    3

    我不知道如何做到这一点:(

    这些代码是否正确?除了3号因为我真的不知道怎么做。 谁能纠正这些?非常感谢你:))

    编辑:我对1号做了一个新的答案,并且可编辑:) 编辑:我现在编辑了我的号码2。但似乎无法将其打印成阵列。 编辑:能够使#2工作,但我确定它是否是被问到的输出:s

3 个答案:

答案 0 :(得分:0)

我尝试了第3个号码而且我得到了这个。

#include <iostream>

using namespace std;

int main(void) {
    cout << "Enter the starting value: ";
    int a;
    cin >> a;

    cout << "Enter the number you wish to stop at: ";
    int b;
    cin >> b;

    // Check if b is less than a.
    if (b < a) {
        cerr << "The stopping value must be greater than the starting value"
             << endl;

        // No need to continue.
        return 1;
    }

    int c = a;  // we start at a.
                // I didn't use a directly because
                // I want to use it to display the result.
    int result;
    while (c <= b) {
        // Check if c is odd.
        if (c%2 == 1)
            result *= c;

        ++c;
    }

    // Display the result.
    cout << "The product of all odd integers from "
         << a
         << " to "
         << b
         << " is "
         << result
         << endl;

    // Keep window open.
    cin.get();
}

答案 1 :(得分:0)

这是我对数字2的尝试。
附:您可以通过抽象函数中的某些部分来使这些代码更加优雅。

#include <iostream>

using namespace std;

int main(void) {
    cout << "Please enter the number of integers whose squares you wish to "
         << "compute: ";

    // Read the number of integers.
    int n;
    cin >> n;

    // Declare an array to hold the squares.
    // You can call it a variable length array if you want.
    int squares[n];

    // Invariant: we have squared n integers.
    for (int i = 0; i != n; ++i) {

        // square (i+1) and add it to the array.
        squares[i] = (i+1)*(i+1);
    }

    // Display the result.
    cout << "Here are the squares of integers from "
         << 1 << " to " << n << endl;

    // Invariant: we have printed n squares to std::cout.
    for (int i = 0; i != n; ++i)
         cout << i+1 << ": " << squares[i] << endl;

    return 0;
}

答案 2 :(得分:0)

以下是我对第一个问题的看法。

#include <iostream>

using namespace std;

int main(void) {
    // Decalare an array of 20 rows,
    // each containing an array of 50 integers.
    int A[20][50];

    // Initialize all the elements of the array to 0.
    // Invariant: we have initialized `row` rows.
    for (int row = 0; row != 20; ++row) {

        // Invariant: we have initialized `col` columns.
        for (int col = 0; col != 50; ++col)
             A[row][col] = 0;
    }

    // We can now fill in the values we want into the array.
    // For this example, we are filling the 1st row with 0,
    // 2nd row with 1, 3rd row with 2 and so on.
    // You can fill it with any values you like.
    for (int row = 0; row != 20; ++row) {
        for (int col = 0; col != 50; ++col)
            A[row][col] = row+1;
    }

    // Display the last element of each row.
    // Invariant: we have printed `row` rows.
    for (int row = 0; row != 20; ++row) {

        // The index -1 gives us the last element in an array.
        cout << "A[" << row << "][50]: " << A[row][-1] << endl;
    }

    return 0;
}