Answer to Question #277718 in C++ for maks

Question #277718

Can someone pls explain how this code works.. Thank youu..


#include <iostream>

#include <string>

using namespace std;

int main() {

  string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};

  for(int i = 0; i < 4; i++) {

      cout << i << ": " << cars[i] << "\n";

  }

  return 0;

}


1
Expert's answer
2021-12-09T17:37:29-0500
#include <iostream> // including basic c++ lib for cout 


#include <string> // required to use string in code


using namespace std; // without this line we will have to write std::cout, std::string. 


int main() {


    string cars[4] = { "Volvo", "BMW", "Ford", "Mazda" }; // here we declare and initialize an array of string with 4 elements. { "Volvo", "BMW", "Ford", "Mazda" } - this is the value of array, Each car is one element of a string array


    for (int i = 0; i < 4; i++) { // this statement is required for loop, where we will iterate over each element, untill we will face with the end of array


        cout << i << ": " << cars[i] << "\n"; // and we display counter here and element of an array.


    }


    return 0; //main function returns a value 0.


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog