【发布时间】:2015-08-17 03:43:10
【问题描述】:
我正在尝试编写一个程序来计算程序遇到的每个数字。通过将 M 作为数组元素数量的输入,Max 是最大数量的数字,例如在 M[i] 中写入输入时不应超过此数字。出于某种原因,当我输入像
这样的小输入时,程序运行良好数据输入:
10 3
1 2 3 2 3 1 1 1 1 3
答案:
5 2 3
但是当我为数组元素输入一个大输入(如 364)和最大输入为 15 时。输出没有按预期工作,我找不到原因!
#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include<conio.h>
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int checker[1000];
int element_cntr = 0;
int cntr = 0;
int n = 0;
void main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue>> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
checker[i]= M[i] ;
element_cntr++;
if (M[i] > Max)
{
cout << "the element number " << element_cntr << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (M[n] == checker[j])
{
cntr+=1;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
n++;
}
}
【问题讨论】:
标签: c++ arrays loops if-statement