• Register
1 vote
19 views

Problem:

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

Solution:

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++){
        if(array[i]!='\0')
            array_count++;
    }
    cout<<"Total elements in this array are: "<<array_count;
}

And the program will produce an output

Total elements in this array are: 10

Thanks

13 9 6
94,260 points

Related questions

0 votes
1 answer 8 views
8 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 3 views
3 views
Problem: I'm having trouble finding a solution; could you please assist me &ldquo;Find repeated number in array in c&rdquo;?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 13 views
13 views
Problem How can i find largent number in array in c++
asked Mar 27 Subhendu 4.5k points
0 votes
1 answer 152 views
152 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 9.8k points
1 vote
1 answer 92 views
92 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 7 views
7 views
Problem: I was recently faced with a line of code and four options: char fullName[30] = {NULL}; A) First element is assigned a NULL character. B) Every element of the array is assigned 0 ( Zeroes ) C) Every element of the array is assigned NULL D) The ... ; there was no other information provided. I'm curious to which one is correct, and if someone could explain why said answer would be correct.
asked Apr 24 sumaiya simi 43.9k points
0 votes
1 answer 17 views
17 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
Problem: 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 exactly.
asked Apr 20 ummesalma 29.2k points
0 votes
1 answer 6 views
6 views
Problem: who know this answer please help? number of elements in array c++
asked Mar 28 Rohit kr 17.5k points
0 votes
2 answers 16 views
16 views
Problem Any idea how i get number of elements in array in c++
asked Mar 25 Subhendu 4.5k points