如何找到数组中的元素present的数量?

编程入门 行业动态 更新时间:2024-10-25 06:31:49
本文介绍了如何找到数组中的元素present的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数组变量,说了一个例子: -

int类型的[10];

和假设我有加4元说 1,2,3,4 从 A [0] 启动直到 A [3] 。现在我该怎样找到多少元素是present数组变量 A ,其最大尺寸为 10 ?

我的方式来获得元件present阵列假设用户的数量将会给输入大于或等于零: - 的

#包括LT&;&iostream的GT;使用命名空间std;的#define SIZE 10诠释主(){    为int * A =(INT *)malloc的(的sizeof(int)的* SIZE);    INT ICOUNT = 0;    INT iNumberOfElements,iElements;    COUT&所述;&下,\\ n输入要输入的元素数:;    CIN>> iNumberOfElements;    COUT&所述;&下,\\ n输入的元件(请输入元素大于或等于零):\\ N的;    对(INT jIndex = 0; jIndex&下; iNumberOfElements; jIndex ++){        CIN>> iElements;            而(iElements℃,){               COUT<<\\ n请输入元素大于或等于零\\ NRE输入:\\ n;               CIN>> iElements;        }        一个[jIndex] = iElements;    }    对于(INT iIndex = 0; iIndex<大小; iIndex ++){        如果(A [iIndex]> = 0){//我以这种方式检查假设用户将给予输入> = 0            ICOUNT ++;        }    }    COUT&所述;&下,\\ n该阵列中的元件present的总数为:&所述;&所述; ICOUNT&所述;&下; ENDL;    返回0;}

解决方案

您必须建立在该公约自己。

例如,你可以在整个数组初始化为一些神奇的常量,预示着未使用,或者你可以通过一个单独的可变跟踪长度。

无论哪种方式,框架不会帮你可言,也没有办法。对于所有意图和目的,数组有10个元素,无论你设置它们。

I have an array variable, say for an example:-

int a[10];

And suppose i have added 4 elements say 1,2,3,4 starting from a[0] till a[3]. Now how do i find that how much element is present in the array variable a whose max size is 10?

My way to obtain the number of elements present in the array assuming that user will give input greater than or equal to Zero:-

#include<iostream> using namespace std; #define SIZE 10 int main(){ int *a = (int*)malloc(sizeof(int)*SIZE); int iCount=0; int iNumberOfElements,iElements; cout<<"\nEnter the number of elements to be entered:"; cin>>iNumberOfElements; cout<<"\nEnter the elements(Please enter elements greater than or equal to zero):\n"; for (int jIndex = 0; jIndex < iNumberOfElements; jIndex++){ cin>>iElements; while(iElements<0){ cout<<"\nPlease enter the element greater than or equal to Zero.\nRe-enter:\n"; cin>>iElements; } a[jIndex] = iElements; } for(int iIndex=0;iIndex<SIZE;iIndex++){ if(a[iIndex] >= 0){ //I am checking in this way assuming that user will give input >= 0 iCount++; } } cout<<"\nThe total number of element present in the array is:"<<iCount<<endl; return 0; }

解决方案

You have to build in that convention yourself.

For instance, you could initialize the whole array to some magic constant that signals "not in use", or you could track the length through a separate variable.

Either way, the framework will not help you at all, there is no way. For all intents and purposes, the array has 10 elements, whether you set them or not.

更多推荐

如何找到数组中的元素present的数量?

本文发布于:2023-11-29 17:34:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   元素   数量   present

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!