计算并显示二维数组中的出现次数
我的代码可以很好地显示数组。如何显示给定整数重复的次数并显示重复的下标位置?
#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");
}
My code is dispalying the array fine. How to display the number of times the given integer is repeated and display the subscript locations where it is repeated?
#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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有看到你的计数代码在矩阵上迭代。 “for”循环中的任何地方都没有提到“x”。
I don't see your counting code iterating over the matrix. 'x' is not mentioned anywhere in 'for' loop.
您应该将代码: 替换
为:
You should to replace your code:
to this:
有两种方法可以做到这一点:
您可以在 for 循环中显示下标位置:
或者您可以创建一个遇到的下标的特殊数组:
最后一种方法不是最佳方法,但它适合学习 C ;)祝您编码顺利!
There are two ways to do this:
You can display subscript locations in the for loop:
Or you can create a special array of encountered subscripts:
The last method isn't optimal, but it's suitable for learning C ;) Have a good coding!