Loop through an array in C ++

I want to iterate over an array that I have that has a maximum value of 1000. I fill the array with values ​​from a text file. I am trying to go through this array, but in my for loop I don’t know the length of the array, so I don’t know what to add to the second part of the for loop statement. For example: I have an array called: int scores[1000]; , and I'm trying to iterate over this array and put the grades in a class. Thus, A = 90-100, B = 80-89, C = 70-79, D = 60-69, F = 0-59.

So, I don't know what my for loop will look like:

 for(int i = 0; i < ...; i++){ if(scores[i] > = 90 || scores[i] <= 100){ //Do stuff... } 

I guess I'm also embarrassed by how to get the final counts of each category at the end. But for the most part, this is how to iterate over this array. I know that sizeof (score []) does not work, because it will give me the size of int, not the length of the array itself. Thanks, though in advance!

+7
source share
4 answers

If you use std::vector ( link ), you can add elements and change the dynamic vector. This size can be easily requested using the size() method. If you use such arrays, you must track the number of elements in it yourself.

If you have a vector filler with elements, your loop might look like this:

 std::vector<int> scores; // fill vector for (unsigned int i=0; i<scores.size(); i++) { // use value } 

If you need to use arrays and actually have a scoreCount variable with the number of real values ​​entered, just use this in your loop:

 for (int i=0; i<scoreCount; i++) { // use value } 

The third option, as I mentioned in the comments, will initialize the entire array with a value that you never use (usually -1), and then use this as a marker for filled or empty positions as an array:

 for (int i=0; i<1000; i++) { scores[i] = -1; } // add real values to scores int i=0; while (scores[i] != -1 && i < 1000) { // use value i++; } 
+6
source

In fact, sizeof() should be executed as follows:

sizeof(scores) / sizeof(scores[0])

And this will give you the full array element numbers.

+7
source

When you populate the scores array, you really need to count how many items you put into it. Then you remember this number and use it to continue later. For example, you may have read your grades as follows:

 // Read some scores: Stop when -1 is entered, an error occurs, or 1000 values are read. int num_scores = 0; for( ; num_scores < 1000; num_scores++ ) { if( !(cin >> scores[num_scores]) || scores[num_scores] == -1 ) break; } // Do stuff with scores... for(int i = 0; i < num_scores; i++) { ... } 

There are other options:

  • use a gatekeeper value to represent the end of the data, for example, a value of -1. A.
  • use std::vector instead.

By the way, the logical operator inside your loop will always be true. Are you sure you did not use && instead of || ?

+2
source

If you really want to use a container with a fixed size, use std::array for modern C ++ instead of a C array:

 #include <array> std::array<std::int32_t, 1000> scores; for (std::size_t i{0}; i < scores.size(); ++i) { // Do stuff... } 

Otherwise use std::vector :

 #include <vector> std::vector<std::int32_t> scores; for (std::size_t i{0}; i < scores.size(); ++i) { // Do stuff... } 

If you can use C ++ 11, I also recommend using integer types of fixed width.

+2
source

All Articles