【发布时间】:2011-05-07 16:57:15
【问题描述】:
我的代码很好地显示了数组。如何显示给定整数的重复次数,并显示重复的下标位置?
#include <iostream>
#include <cmath>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
int table [10][10]={{0},{0}};
int repeat=0;
int count=0;
int r=0;
int c=0;
//seeding the random function
srand(static_cast<int>(time(0)));
for(r=0; r<10; r++)//row
{
for(c=0; c<10; c++)
{
table[r][c] = 50+rand() %(100-50+1);
cout << table[r][c]<<" ";
}
cout<<endl;
}
cout<<"Enter the number to know how many times it is repeated(50 to 100): ";
cin>>repeat;
for (int x=0; x<10; x++)
{
if(repeat==table[r][c])
count+=1;
}
cout<<"the number "<<repeat<<" appeared"<<count<<" times."<<endl;
//display new line
system("pause");
}
【问题讨论】:
-
如何编写代码来显示给定整数的重复次数并显示重复的下标位置?
-
有点,我是从一本书中自学的。
-
混合制表符和空格缩进通常是个问题;使用所有工具(包括您一开始可能没有想到的工具,例如 SO)获得一致结果的最佳方法是让您的编辑器在您按 Tab 键时插入空格。 (我在这里为你重新格式化了代码。)
标签: c++ arrays multidimensional-array