查找数组C ++的重复元素 [英] Find repeating elements of an array C++
本文介绍了查找数组C ++的重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道为什么程序在运行时崩溃
#include < iostream > ;
void 输入( int a [],< span class =code-keyword> int size);
void search( int a [], int size);
void print( int a [],int& count,int& i);
使用 命名空间标准;
int main(){
int count,i;
int size = 5 , array 跨度> [大小];
cout<< 输入5个数字:<< ENDL;
输入(数组,大小);
search( array ,size);
for ( int j = 0 ; j< size; j ++){
print( array ,count,i);
}
return 0 ;
}
void 输入( int a [], int size){
for ( int i = 0 ; i< size; i ++){
cin>> A [1];
}
}
void search( int a [], int size){
int count = 0 跨度>;
for ( int i = 0 ; i< size; i ++){
for ( int j = 0 ; j< i; j ++){
if (a [i] == a [j] ){
count = 1 ;
count ++;
}
}
}}
void print(< span class =code-keyword> int a [],int& count,int& i){
cout<< 元素<< a [i]<< 重复<<计数<< times<< ENDL;
}
我的尝试:
程序一直在崩溃
解决方案
在以下循环中:
< pre lang =c ++> for ( int j = 0 ; j< size; j ++){
print( array ,count,i);
}您使用
j
作为循环计数器和未初始化的i
作为
也许你意思是:
#include < iostream >
void 输入( int a [], int size);
int 重复( int a [], int size, int value );
void print( int a [], int size, int i);
使用 命名空间标准;
int main()
{
int size = 5 , array [size];
cout<< 输入5个数字:<< ENDL;
输入(数组,大小);
for ( int j = 0 ; j< size; j ++)
{
print( array ,size,j);
}
return 0 ;
}
void 输入( int a [], int size)
{
for ( int i = 0 ; i< size; i ++)
{
cin>> A [1];
}
}
int 重复( int a [], int 大小, int value )
{
int count = 0 ;
for ( int n = 0 ; n< size; ++ n)
{
if (a [n] == 值)count ++;
}
返回计数;
}
void print( int a [],< span class =code-keyword> int size, int i)
{
cout<< 元素<< a [i]<< 重复<<重复(a,size,a [i])<< times<< ENDL;
}
或
#include < iostream >
#include < array < span class =code-keyword>>
#include < span class =code-keyword>< 算法 >
使用 命名空间标准;
int main()
{
const int SIZE = 5 ;
array< int,SIZE> AR;
cout<< 请输入5个数字<< ENDL;
for ( auto & a:ar)
cin >>一个;
for ( auto a:ar)
cout< < item<< a<< 重复<< count(ar.begin(),ar.end(),a)<< times<< ENDL;
}
i dont know why the program is crashing when ever i run it
#include <iostream>
void input(int a[],int size);
void search(int a[],int size);
void print(int a[],int&count,int&i);
using namespace std;
int main(){
int count,i;
int size=5,array[size];
cout << "Enter 5 numbers :" << endl;
input(array,size);
search(array,size);
for (int j=0;j<size ;j++){
print(array,count,i);
}
return 0;
}
void input(int a[],int size){
for (int i=0;i<size;i++){
cin >> a[i];
}
}
void search(int a[],int size){
int count=0;
for (int i=0;i<size;i++){
for (int j=0;j<i;j++){
if ( a[i]==a[j] ){
count=1;
count++;
}
}
}}
void print(int a[],int&count,int&i){
cout << "The element " << a[i] <<" is repeated for " << count << " times " << endl;
}
What I have tried:
the program is keeps on crashing
解决方案
In the following loop:
for (int j=0;j<size ;j++){ print(array,count,i); }You use
j
as the loop counter and the uninitializedi
as the index for the
Probably you meant something like:
#include <iostream> void input(int a[],int size); int repetitions(int a[], int size, int value); void print(int a[], int size, int i); using namespace std; int main() { int size=5, array[size]; cout << "Enter 5 numbers :" << endl; input(array,size); for (int j=0;j<size ;j++) { print(array, size,j); } return 0; } void input(int a[],int size) { for (int i=0;i<size;i++) { cin >> a[i]; } } int repetitions(int a[], int size, int value) { int count = 0; for (int n=0; n<size; ++n) { if ( a[n] == value) count++; } return count; } void print(int a[], int size, int i) { cout << "The element " << a[i] <<" is repeated for " << repetitions(a,size,a[i]) << " times " << endl; }
or
#include <iostream> #include <array> #include <algorithm> using namespace std; int main() { const int SIZE = 5; array<int,SIZE> ar; cout << "please enter 5 numbers " << endl; for (auto & a : ar) cin >> a; for (auto a : ar) cout << "item " << a << " is repeated for " << count( ar.begin(), ar.end(), a) << " times" << endl; }
这篇关于查找数组C ++的重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文