【问题标题】:Writing a program that fills an array of 100 integers from 1 to 100 [closed]编写一个程序,从 1 到 100 填充 100 个整数的数组 [关闭]
【发布时间】:2014-04-14 23:50:14
【问题描述】:

所以我对此有一个小问题。我只是不知道我这样做是否正确。问题很模糊。 (对我)想知道我是否可以得到一些帮助,因为我已经在我的书中解决了这个简单的问题 2 个小时了,这让我很生气!在此先感谢:)

“编写一个程序,用 1 到 100 的数字“填充”一个由 100 个整数元素组成的数组,然后输出数组中的数字。”

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const int size = 301;
int N, I, k;
int score[size];
srand(time(0));


cout   << setprecision(2)
       << setiosflags(ios::fixed)
       << setiosflags(ios::showpoint);

//1)Get # of bowlers ..............................................................
      cout << "Enter number of bowlers? (Must be between 1 and 301) ";
      cin >> N;
      while (N<1||N>size)
{
      cout << "\nError!! Must be between 1 and 301!! ";
      cin >> N;
}
//2) and 5)  Get scores ............................................................
for(I = 0; I<N; I++)
{
//cout << "\nEnter score for bowler # " << I + 1 << " ";
//cin >> score[I];
score[I]=rand()%301;

for(k=0; k<I; k++)
{
    if(score[k]==score[I])
    {
        I--;
        break;
    }

}
}

//3)Get Sum/Avg .....................................................................
int sum = 0;
float avg;
for(I = 0; I<N; I++)
{
sum += score [I];

}

avg = float(sum)/N;




//4) Output scores, sum, and avg ....................................................

for(I = 0; I<N; I++)
{
cout << "\nScore for bowler # " << I + 1 << " is  "  << score[I];

}
cout<<"\n\n The Sum is " << sum << "\n";
cout <<"\n The Average is "<< avg << "\n";


cout<<"\n\n\n";
system ("pause");
return 0;
}

【问题讨论】:

  • 代码与问题描述完全不符。
  • 问题要求像这样的数组 {1, 2, 3,.... 100}
  • 天哪。我忘了提到我发布的这段代码是一个较早的项目,假设与它“相似”。但我认为这是一个比下面的答案更难的过程。感谢您的小费。我喜欢一个月。所以我为糟糕的解释道歉。
  • 真的吗?还没有人提到std::iota?这就是它的用途。

标签: c++ arrays integer


【解决方案1】:

代码的核心只需要创建数组,例如与

int arr[] = new int[100];

然后将其填充到 for 循环中,例如与

for (i = 0; i<100; i++) arr[i] = i+1;

请注意,数组索引从零开始计数,但您希望从一开始填充。

【讨论】:

  • 谢谢。我使用数组的时间很短,这让我很困惑哈哈。我买了 John Molluzzo 的《商业程序员的 C++》一书,并且只在第 8 章,下面的一些回复正在使用我还没有使用过的函数。你的似乎是最合法的。至少在我目前的状态下。
【解决方案2】:

您确定代码与您的问题有关吗?

执行您想要的操作的示例程序是这样的:

#include <stdlib.h>
#include <stdio.h>

#define N 100

int main(void)
{
        int arr[N], i;

        for (i = 0; i < N; i++)
                arr[i] = i + 1;

        for (i = 0; i < N; i++)
                printf("%d ", arr[i]);

        return 0;
}  

【讨论】:

    【解决方案3】:
    #include <iostream>
    #define NUM_VALUES 100
    
    int main()
    {
        int i = 1;
        int array[NUM_VALUES];
    
        while (i <= NUM_VALUES)
        {
            array[i-1] = i;
            i++;
        }
    
        i = 1;
    
        while (i <= NUM_VALUES)
        {
            std::cout << array[i-1] << " ";
            i++;
        }
    
        std::cout << std::endl;
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      相关资源
      最近更新 更多