Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 793105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:05:58+00:00 2026-05-14T22:05:58+00:00

I have an array like this: int a[100]; I am filling only the first

  • 0

I have an array like this:

int a[100]; 

I am filling only the first 4 elements in this array:

a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;

When I do sizeof(a)/sizeof(a[0]) it returns 100.

Is there a way I can get number of elements to which I have assinged a value and thus filtering out the remaining 96 unassigned elements?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:05:59+00:00Added an answer on May 14, 2026 at 10:05 pm

    All elements must be assigned to something, thus the array always has 100 elements. If you can ensure all the elements are initialized to a special value which means “unassigned” to you (e.g. -1), you can work it out like this:

    
    // fill the array with a special value which means "uninitialized"
    const int special_uninitialized = -1;
    std::fill(&a[0], &a[100], special_uninitialized);
    
    // set up your values
    a[0] = 1;
    
    // count
    std::size_t uninitialized_count = std::count(&a[0], &a[100], special_uninitialized);
    std::size_t initialized_count = 100 - uninitialized_count;
    

    If you just want to know how many elements are in an array, you have these options:

    1. Don’t use an array, use std::vector, which has a size() function, and is generally a better choice than a basic array

    2. Keep track of the element count yourself, in a separate variable

    3. Use the special “unassigned” value as described above, and use std::find to find the first one, and work out how many are there by subtracting the address of the zeroth element from that. This is a pretty ugly solution.

    For a beginner, std::vector is a much better choice. You can use it like this:

    
    std::vector<int> vec;
    
    vec.push_back(17);
    vec.push_back(23);
    vec.push_back(5);
    
    int x = vec[0]; // x will be 17
    vec[0] = 40; // set element 0
    
    size_t s = vec.size(); // s will be 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you have an array like this: int foo[3][2][2]; and you make: int *bar
If I have an int array structured like this: private int[][] map = new
I have an non-square array like this: const int dim1 = 3, dim2 =
Lets say I have an array like this: string [] Filelist = ... I
I need to move backwards through an array, so I have code like this:
I have a struct with many char array like this (and it works) :
I'm creating my own JavaScript Array-like object and I have methods that call closures.
I have a 2D bitmap-like array of let's say 500*500 values. I'm trying to
I have byte array as input. I would like to convert that array to
I have a byte array in memory, read from a file. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.