How can I find the number of elements in an array?

前端 未结 14 1250
广开言路
广开言路 2020-12-02 15:01

I have an int array and I need to find the number of elements in it. I know it has something to do with sizeof but I\'m not sure how to use it exac

14条回答
  •  感情败类
    2020-12-02 15:45

    If we don't know the number of elements in the array and when the input is given by the user at the run time. Then we can write the code as

    C CODE:

    while(scanf("%d",&array[count])==1) { 
      count++;
    }
    

    C++ CODE:

    while(cin>>a[count]) {
      count++;
    }
    

    Now the count will be having the count of number of array elements which are entered.

提交回复
热议问题