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;
}
#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.
}
Comments
Leave a comment