• Register
1 vote


Hello All,

I am a new student in C++ programming. I started learning programming out of my own curiosity. One of my friends told me that C++ is easy to pick up and I stick on his words. I am helping my friends’ and different websites help to learn new things every day. Today, I have got a new problem to solve. The problem is asking me to find all the number of elements in an array. Let say, I have an array of

int array [] = {1,2,3,4,5,6,7,8,9,10};

The program should count all the elements and print the result 10 as there are ten elements in my array.

How can I solve this problem? I am looking forward to having a solution from you. Thanks for your help, guys.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
Best answer


Hello Gavin,

You’ve asked a nice but entry question. Basically, you want to find out the array length/size. We can solve the problem in a couple of ways. I am trying to keep it as simple as possible. I hope this might help you more. Please, find the code snippet below and reply to this thread if you still got any confusion.

#include <iostream>
using namespace std;
int main(){
    int array[] = {1,2,3,4,5,6,7,8,9,10};
    int array_count=0;
    for(int i=0;i<10;i++){
    cout<<"Total elements in this array are: "<<array_count;

And the program will produce an output

Total elements in this array are: 10


13 9 6
94,240 points

Related questions

0 votes
1 answer 53 views
Problem: Hello Programmers! I am a novice C programmer striving hard to learn as much programming as possible in minimum time. I came across an assignment in which the programmer has to write a program that prints the second largest number in an integer array. I tried writing such a program and I ... +) { if(arr[i] > largest) { largest = arr[i]; } } printf("%d", largest); return 0; } Output: 44
asked Jan 2 Code Learner 6.5k points
1 vote
1 answer 37 views
Problem: I am trying to finding the largest number in an array c++ Every time I am trying my code is printing all the values of the array instead of the largest one. I might be doing something wrong inside my iteration. Do you have any idea to do it more easily and efficiently? I&rsquo;d love to hear from you guys. Please shar your ideas about this. Thanks.
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 15 views
Well, this is a vector that tries to capture the letters, that is, each element of the vector is a letter that is being typed, however, in the end I don't know how to print the letters before typed. This is just one function. char enterChar () {      char character = '';      int i = 0;      if ...        cout << "element" << i << "is:" << phrase [i] << endl;      }      else          return 0; }
asked Aug 15, 2020 game 4.6k points
0 votes
1 answer 7 views
7 views asked Jan 27 Afi khan 1.4k points
0 votes
1 answer 47 views
1 vote
1 answer 278 views
Problem : I am new to C programming. I have recently started working on exercise. Please find below my code for your reference: int mydistinct(int a[], int n) { int i, k, j, count=0; int max = a[0]; for (i = 1; i < n; i++) { if (max < a[i]) { ... . In this case my code will only try to count 8,7,6,5 and it will return only four unique numbers. So I am looking for proper guidance in C programming.
asked May 19, 2020 Martin K 6.6k points
0 votes
1 answer 19 views
Problem: Array of Objects in C++
asked Feb 13 jasimchaudary 640 points
0 votes
1 answer 14 views
14 views asked Oct 27, 2020 sjbnazkj 370 points
0 votes
1 answer 33 views
I am trobuleing to find the length of array in c without using sizeof.
asked Oct 3, 2020 Daniel Anderson 4k points
0 votes
1 answer 15 views
I have an array of string, in which I want to store sentences. These sentences contain variables, how do I tell C ++ that within the sentences there are variables? I don't want to print them on screen, so options like cout or printf don't work for me. I just want them to be ... ; record [0] = "Withdrawn" << withdraw << "dollars from the ATM." << balance << "dollars left; cout << record [0]; }
asked Aug 14, 2020 game 4.6k points