编写一个程序,填充从1到100的100个整数的数组
Writing a program that fills an array of 100 integers from 1 to 100
所以我有一个小问题。我只是不知道我做得对不对。这个问题很含糊。(对我来说)我想知道我是否能得到一些帮助,因为我在书中处理这个简单的问题已经两个小时了,它让我很失望!提前感谢:)
编写一个程序,用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<<"nn The Sum is " << sum << "n";
cout <<"n The Average is "<< avg << "n";
cout<<"nnn";
system ("pause");
return 0;
}
代码的核心只需要创建数组,例如使用
int arr[] = new int[100];
然后将其填充到for循环中,例如使用
for (i = 0; i<100; i++) arr[i] = i+1;
请注意,数组索引从零开始计数,但您希望从一开始填充。
您确定代码与您的问题有关吗?
一个可以做你想要的事情的示例程序是:
#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;
}
#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;
}
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 为什么我的 Char* 在填充整数后为空?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 错误:以增量方式填充向量时,表达式必须具有整数或无作用域枚举类型
- 如何计算未完全填充的数组中的整数
- 填充整数列表比填充整数向量快 100 倍
- 构造时,用整数序列填充向量
- 如果我有一个固定大小的填充ascii数字的char数组,我知道它指的是一个无符号整数,那么将其转换为无符号整数的最快方法
- 练习:用 C/C++ 中的整数填充向量
- 用整数填充自定义整数数组
- 如何在保持其顺序的同时填充整数数组
- 如何在没有多余整数的情况下填充整数之间的分隔符
- 用std::cout正确填充带零的负整数
- 更快的方式,然后我填充一个唯一的整数除了两个值的向量?c++
- 为什么堆栈上对齐的整数之间有 8 个字节的"0xcc"填充?C++ 32位视窗7
- 如何编写一个程序,从键盘上填充一个二维整数表a[3][2]
- 编写一个程序,填充从1到100的100个整数的数组