计算并显示二维数组中的出现次数

发布于 2024-10-01 08:06:14 字数 962 浏览 0 评论 0原文

我的代码可以很好地显示数组。如何显示给定整数重复的次数并显示重复的下标位置?

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧竹 2024-10-08 08:06:14

我没有看到你的计数代码在矩阵上迭代。 “for”循环中的任何地方都没有提到“x”。

I don't see your counting code iterating over the matrix. 'x' is not mentioned anywhere in 'for' loop.

俯瞰星空 2024-10-08 08:06:14

您应该将代码: 替换

for (int x=0; x<10; x++)
{
    if(repeat==table[r][c])
        count+=1;
}

为:

for (r = 0; r < 10; r ++)
{
    for (c = 0; c < 10; c ++)
    {
        if(table[r][c] == repeat)    // checking
             count ++;
    }
}

You should to replace your code:

for (int x=0; x<10; x++)
{
    if(repeat==table[r][c])
        count+=1;
}

to this:

for (r = 0; r < 10; r ++)
{
    for (c = 0; c < 10; c ++)
    {
        if(table[r][c] == repeat)    // checking
             count ++;
    }
}
烂柯人 2024-10-08 08:06:14

有两种方法可以做到这一点:

您可以在 for 循环中显示下标位置:

puts ("Locations:");
for (r = 0; r < 10; r ++)
{
    for (c = 0; c < 10; c ++)
    {
        if(table [r][c] == repeat)            // checking
        {
            printf ("[%i, %i]\n", r, c);      // display where it is
            count ++;
        }
    }
}

或者您可以创建一个遇到的下标的特殊数组:

int rs [100];    // rows and columns indexes of repeated subscripts
int cs [100];    //

for (r = 0; r < 10; r ++)
{
    for (c = 0; c < 10; c ++)
    {
        if(table [r][c] == repeat)            // checking
        {
            // no printf code here
            rs [count] = r;
            cs [count] = c;
            count ++;
        }
    }
}

// subscripts can be displayed or used in math algorithm now:

puts ("Locations:");
for (int i = 0; i < count; i ++)
    printf ("[%i, %i]", rs [i], cs [i]);

最后一种方法不是最佳方法,但它适合学习 C ;)祝您编码顺利!

There are two ways to do this:

You can display subscript locations in the for loop:

puts ("Locations:");
for (r = 0; r < 10; r ++)
{
    for (c = 0; c < 10; c ++)
    {
        if(table [r][c] == repeat)            // checking
        {
            printf ("[%i, %i]\n", r, c);      // display where it is
            count ++;
        }
    }
}

Or you can create a special array of encountered subscripts:

int rs [100];    // rows and columns indexes of repeated subscripts
int cs [100];    //

for (r = 0; r < 10; r ++)
{
    for (c = 0; c < 10; c ++)
    {
        if(table [r][c] == repeat)            // checking
        {
            // no printf code here
            rs [count] = r;
            cs [count] = c;
            count ++;
        }
    }
}

// subscripts can be displayed or used in math algorithm now:

puts ("Locations:");
for (int i = 0; i < count; i ++)
    printf ("[%i, %i]", rs [i], cs [i]);

The last method isn't optimal, but it's suitable for learning C ;) Have a good coding!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文